001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package javax.microedition.sip;
028:
029: import com.sun.midp.i3test.TestCase;
030:
031: import javax.microedition.io.Connector;
032: import java.io.IOException;
033:
034: /**
035: * RFC3261, p. 37; BNF: p. 230
036: *
037: * From = ( "From" / "f" ) HCOLON from-spec
038: * from-spec = ( name-addr / addr-spec )
039: * *( SEMI from-param )
040: * from-param = tag-param / generic-param
041: * tag-param = "tag" EQUAL token
042: *
043: * The From header field indicates the logical identity of the initiator
044: * of the request, possibly the user's address-of-record. Like the To
045: * header field, it contains a URI and optionally a display name. It is
046: * used by SIP elements to determine which processing rules to apply to
047: * a request (for example, automatic call rejection). As such, it is
048: * very important that the From URI not contain IP addresses or the FQDN
049: * of the host on which the UA is running, since these are not logical
050: * names.
051: *
052: * The From header field allows for a display name. A UAC SHOULD use
053: * the display name "Anonymous", along with a syntactically correct, but
054: * otherwise meaningless URI (like sip:thisis@anonymous.invalid), if the
055: * identity of the client is to remain hidden.
056: *
057: * Usually, the value that populates the From header field in requests
058: * generated by a particular UA is pre-provisioned by the user or by the
059: * administrators of the user's local domain. If a particular UA is
060: * used by multiple users, it might have switchable profiles that
061: * include a URI corresponding to the identity of the profiled user.
062: * Recipients of requests can authenticate the originator of a request
063: * in order to ascertain that they are who their From header field
064: * claims they are (see Section 22 for more on authentication).
065: *
066: * The From field MUST contain a new "tag" parameter, chosen by the UAC.
067: * See Section 19.3 for details on choosing a tag.
068: *
069: * For further information on the From header field, see Section 20.20.
070: * Examples:
071: *
072: * From: "Bob" <sips:bob@biloxi.com> ;tag=a48s
073: * From: sip:+12125551212@phone2net.com;tag=887s
074: * From: Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8
075: *
076: */
077:
078: public class TestFromHeader extends SipHeaderBaseTest {
079:
080: /** A name of the header that will be tested */
081: private final String headerName = "From";
082:
083: /**
084: * Body of the test 1.
085: *
086: * Test for From header field: setName()/getName().
087: */
088: void Test1() {
089: // DEBUG: System.out.println("");
090: // DEBUG: System.out.println("*** Test1 started ***");
091:
092: // Testing the constructor...
093: // testConstructorNegative(headerName, "Invalid Value");
094:
095: SipHeader sh = createSipHeader(headerName,
096: "\"Bob\" <sips:bob@biloxi.com> ;tag=a48s");
097:
098: if (sh == null) {
099: return;
100: }
101:
102: // Testing getName()...
103: String ret_name = sh.getName();
104: assertTrue("Invalid header value: " + ret_name, ret_name
105: .equals(headerName));
106:
107: // Testing setName()...
108: try {
109: sh.setName(headerName);
110: } catch (java.lang.IllegalArgumentException e) {
111: fail("setName(" + headerName + ") failed (IAE): " + e);
112: } catch (Throwable e) {
113: fail("setName(" + headerName + ") failed: " + e);
114: }
115: }
116:
117: /**
118: * Body of the test 2.
119: *
120: * Test for From header field: getValue()/getHeaderValue().
121: */
122: void Test2() {
123: SipHeader sh;
124: String val;
125: String headerValue1 = "<sip:+12125551212@phone2net.com>";
126: String headerValue2 = "\"Bob\" <sips:bob@biloxi.com>";
127: String headerParam1 = headerValue1;
128: String headerParam2 = headerValue2;
129: String[] paramList = { "tag=887s", "generic=sample_value" };
130:
131: // DEBUG: System.out.println("");
132: // DEBUG: System.out.println("*** Test2 started ***");
133:
134: for (int i = 0; i < paramList.length + 1; i++) {
135: sh = createSipHeader(headerName, headerParam1);
136:
137: if (sh != null) {
138: val = sh.getValue();
139: assertTrue(
140: "getValue() returned invalid parameter value: '"
141: + val + "'", val.equals(headerValue1));
142:
143: val = sh.getHeaderValue();
144: assertTrue("(1) getHeaderValue() returned invalid "
145: + "parameter value: '" + val + "'", val
146: .equals(headerParam1));
147:
148: // Test if the value can be changed.
149: sh.setValue(headerValue2);
150:
151: val = sh.getHeaderValue();
152: assertTrue("(2) getHeaderValue() returned invalid "
153: + "parameter value: '" + val + "'", val
154: .equals(headerParam2));
155: }
156:
157: headerParam1 += ";";
158: headerParam2 += ";";
159:
160: if (i < paramList.length) {
161: headerParam1 += paramList[i];
162: headerParam2 += paramList[i];
163: }
164: } // end for
165: }
166:
167: /**
168: * Body of the test 4.
169: *
170: * Test for From header field: getParameterNames()/getParameter().
171: */
172: void Test4() {
173: // DEBUG: System.out.println("");
174: // DEBUG: System.out.println("*** Test4 started ***");
175:
176: SipHeader sh = createSipHeader(headerName,
177: "Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8");
178:
179: if (sh == null) {
180: return;
181: }
182:
183: // Testing getParameterNames()...
184: String[] paramList = sh.getParameterNames();
185:
186: if (paramList == null) {
187: fail("getParameterNames() returned null!");
188: } else {
189: assertTrue("getParameterNames() returned "
190: + paramList.length + " parameters instead of 1.",
191: paramList.length == 1);
192:
193: boolean isValid = paramList[0].equals("tag");
194:
195: assertTrue("Invalid parameter name: " + paramList[0],
196: isValid);
197: }
198:
199: // Testing getParameter()...
200: String paramVal = sh.getParameter("ttl");
201: assertTrue("getParameter() returned '" + paramVal
202: + "' for the parameter 'ttl' that doesn't exist.",
203: paramVal == null);
204:
205: paramVal = sh.getParameter("tag");
206: assertTrue("getParameter() returned '" + paramVal
207: + "' for 'received'" + " instead of 'hyh8'.", paramVal
208: .equals("hyh8"));
209: }
210:
211: /**
212: * Body of the test 5.
213: *
214: * Test for From header field: setParameter()/removeParameter().
215: */
216: void Test5() {
217: // DEBUG: System.out.println("");
218: // DEBUG: System.out.println("*** Test5 started ***");
219:
220: SipHeader sh = createSipHeader(headerName,
221: "\"Bob\" <sips:bob@biloxi.com> ;tag=a48s");
222:
223: if (sh == null) {
224: return;
225: }
226:
227: // Testing setParameter()...
228: sh.setParameter("tag", "123456");
229:
230: String paramVal = sh.getParameter("tag");
231: assertTrue("getParameter() returned '" + paramVal
232: + "' instead of '123456'.", paramVal.equals("123456"));
233:
234: sh.setParameter("generic", "10"); // parameter doesn't exist
235:
236: paramVal = sh.getParameter("generic");
237: assertTrue("getParameter() returned '" + paramVal
238: + "' instead of 10.", paramVal.equals("10"));
239: }
240:
241: /**
242: * Run the tests
243: */
244: public void runTests() {
245: String headerParam = "Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8";
246:
247: declare("setName()/getName()");
248: Test1();
249:
250: declare("getValue()/getHeaderValue()");
251: Test2();
252:
253: declare("setValue()");
254: testSetValue(headerName, headerParam);
255:
256: declare("getParameterNames()/getParameter()");
257: Test4();
258:
259: declare("setParameter()/removeParameter()");
260: Test5();
261:
262: declare("toString()");
263: testToString(headerName, headerParam);
264: }
265: }
|