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. 179; BNF: p. 232
036: *
037: * The Via header field indicates the path taken by the request so far
038: * and indicates the path that should be followed in routing responses.
039: * The branch ID parameter in the Via header field values serves as a
040: * transaction identifier, and is used by proxies to detect loops.
041: *
042: * A Via header field value contains the transport protocol used to send
043: * the message, the client's host name or network address, and possibly
044: * the port number at which it wishes to receive responses. A Via
045: * header field value can also contain parameters such as "maddr",
046: * "ttl", "received", and "branch", whose meaning and use are described
047: * in other sections. For implementations compliant to this
048: * specification, the value of the branch parameter MUST start with the
049: * magic cookie "z9hG4bK", as discussed in Section 8.1.1.7.
050: *
051: * Transport protocols defined here are "UDP", "TCP", "TLS", and "SCTP".
052: * "TLS" means TLS over TCP. When a request is sent to a SIPS URI, the
053: * protocol still indicates "SIP", and the transport protocol is TLS.
054: *
055: * Via: SIP/2.0/UDP erlang.bell-telephone.com:5060;branch=z9hG4bK87asdks7
056: * Via: SIP/2.0/UDP 192.0.2.1:5060 ;received=192.0.2.207 ;branch=z9hG4bK77asjd
057: *
058: * The compact form of the Via header field is v.
059: *
060: * In this example, the message originated from a multi-homed host with
061: * two addresses, 192.0.2.1 and 192.0.2.207. The sender guessed wrong
062: * as to which network interface would be used. Erlang.bell-
063: * telephone.com noticed the mismatch and added a parameter to the
064: * previous hop's Via header field value, containing the address that
065: * the packet actually came from.
066: *
067: * The host or network address and port number are not required to
068: * follow the SIP URI syntax. Specifically, LWS on either side of the
069: * ":" or "/" is allowed, as shown here:
070: *
071: * Via: SIP / 2.0 / UDP first.example.com: 4000;ttl=16
072: * ;maddr=224.2.0.1 ;branch=z9hG4bKa7c6a8dlze.1
073: *
074: */
075:
076: public class TestViaHeader extends SipHeaderBaseTest {
077:
078: /** A name of the header that will be tested */
079: private final String headerName = "Via";
080:
081: /**
082: * Body of the test 1.
083: *
084: * Test for Via header field: setName()/getName().
085: */
086: void Test1() {
087: // DEBUG: System.out.println("");
088: // DEBUG: System.out.println("*** Test1 started ***");
089:
090: SipHeader sh = createSipHeader(headerName,
091: "SIP/2.0/UDP first.example.com: 4000;ttl=16; "
092: + "maddr=224.2.0.1 ;branch=z9hG4bKa7c6a8dlze.1");
093:
094: if (sh == null) {
095: return;
096: }
097:
098: // Testing getName()...
099: String ret_name = sh.getName();
100: assertTrue("Invalid header value: " + ret_name, ret_name
101: .equals(headerName));
102:
103: // Testing setName()...
104: try {
105: sh.setName(headerName);
106: } catch (java.lang.IllegalArgumentException e) {
107: fail("setName(" + headerName + ") failed (IAE): " + e);
108: } catch (Throwable e) {
109: fail("setName(" + headerName + ") failed: " + e);
110: }
111: }
112:
113: /**
114: * Body of the test 2.
115: *
116: * Test for Via header field: getValue()/getHeaderValue().
117: */
118: void Test2() {
119: SipHeader sh;
120: String val;
121: String headerValue1 = "SIP/2.0/UDP first.example.com:4000";
122: String headerValue2 = "SIP/2.0/UDP 192.0.2.1:5060";
123: String headerParam1 = headerValue1;
124: String headerParam2 = headerValue2;
125: String[] paramList = { "ttl=16", "maddr=224.2.0.1",
126: "branch=z9hG4bKa7c6a8dlze.1" };
127:
128: // DEBUG: System.out.println("");
129: // DEBUG: System.out.println("*** Test2 started ***");
130:
131: for (int i = 0; i < paramList.length + 1; i++) {
132: sh = createSipHeader(headerName, headerParam1);
133:
134: if (sh != null) {
135: val = sh.getValue();
136: assertTrue(
137: "getValue() returned invalid parameter value: '"
138: + val + "'", val.equals(headerValue1));
139:
140: val = sh.getHeaderValue();
141: assertTrue("(1) getHeaderValue() returned invalid "
142: + "parameter value: '" + val + "'", val
143: .equals(headerParam1));
144:
145: // Test if the value can be changed.
146: sh.setValue(headerValue2);
147:
148: val = sh.getHeaderValue();
149: assertTrue("(2) getHeaderValue() returned invalid "
150: + "parameter value: '" + val + "'", val
151: .equals(headerParam2));
152: }
153:
154: headerParam1 += ";";
155: headerParam2 += ";";
156:
157: if (i < paramList.length) {
158: headerParam1 += paramList[i];
159: headerParam2 += paramList[i];
160: }
161: } // end for
162: }
163:
164: /**
165: * Body of the test 4.
166: *
167: * Test for Via header field: getParameterNames()/getParameter().
168: */
169: void Test4() {
170: // DEBUG: System.out.println("");
171: // DEBUG: System.out.println("*** Test4 started ***");
172:
173: SipHeader sh = createSipHeader(headerName,
174: "SIP/2.0/UDP 192.0.2.1:5060 ;received=192.0.2.207;"
175: + "branch=z9hG4bK77asjd");
176:
177: if (sh == null) {
178: return;
179: }
180:
181: // Testing getParameterNames()...
182: String[] paramList = sh.getParameterNames();
183:
184: if (paramList == null) {
185: fail("getParameterNames() returned null!");
186: } else {
187: assertTrue("getParameterNames() returned "
188: + paramList.length + " parameters instead of 2.",
189: paramList.length == 2);
190:
191: boolean isValid0 = paramList[0].equals("received")
192: || paramList[0].equals("branch");
193:
194: assertTrue("Invalid parameter name: " + paramList[0],
195: isValid0);
196:
197: boolean isValid1 = paramList[1].equals("received")
198: || paramList[1].equals("branch");
199:
200: assertTrue("Invalid parameter name: " + paramList[1],
201: isValid1);
202: }
203:
204: // Testing getParameter()...
205: String paramVal = sh.getParameter("ttl");
206: assertTrue("getParameter() returned '" + paramVal
207: + "' for the parameter 'ttl' that doesn't exist.",
208: paramVal == null);
209:
210: paramVal = sh.getParameter("received");
211: assertTrue("getParameter() returned '" + paramVal
212: + "' for 'received'" + " instead of '192.0.2.207'.",
213: paramVal.equals("192.0.2.207"));
214: }
215:
216: /**
217: * Body of the test 5.
218: *
219: * Test for Via header field: setParameter()/removeParameter().
220: */
221: void Test5() {
222: // DEBUG: System.out.println("");
223: // DEBUG: System.out.println("*** Test5 started ***");
224:
225: SipHeader sh = createSipHeader(headerName,
226: "SIP/2.0/UDP first.example.com: 4000;"
227: + "maddr=224.2.0.1 ;branch=z9hG4bKa7c6a8dlze.1");
228:
229: if (sh == null) {
230: return;
231: }
232:
233: // Testing setParameter()...
234: sh.setParameter("maddr", "192.0.0.1");
235:
236: String paramVal = sh.getParameter("maddr");
237: assertTrue("getParameter() returned '" + paramVal
238: + "' instead of '192.0.0.1'.", paramVal
239: .equals("192.0.0.1"));
240:
241: sh.setParameter("ttl", "10"); // parameter doesn't exist
242:
243: paramVal = sh.getParameter("ttl");
244: assertTrue("getParameter() returned '" + paramVal
245: + "' instead of 10.", paramVal.equals("10"));
246:
247: // RFC 3261, p. 232: ttl = 1*3DIGIT ; 0 to 255
248: /*
249: These checks are disabled according to the clarification
250: for SipHeader received from the Expert Group.
251:
252: try {
253: sh.setParameter("ttl", "300");
254: fail("Setting 'ttl=300': IAE was not thrown.");
255: } catch (IllegalArgumentException iae) {
256: } catch (Exception e) {
257: fail(e + " was thrown instead of IAE.");
258: }
259:
260: try {
261: sh.setParameter("ttl", "invalid");
262: fail("Setting 'ttl=invalid': IAE was not thrown.");
263: } catch (IllegalArgumentException iae) {
264: } catch (Exception e) {
265: fail(e + " was thrown instead of IAE.");
266: }
267:
268: // RFC 3261, p. 232:
269: // via-received = "received" EQUAL (IPv4address / IPv6address)
270: try {
271: sh.setParameter("received", "invalid");
272: fail("Setting 'received=invalid': IAE was not thrown.");
273: } catch (IllegalArgumentException iae) {
274: } catch (Exception e) {
275: fail(e + " was thrown instead of IAE.");
276: }
277: */
278: }
279:
280: /**
281: * Run the tests
282: */
283: public void runTests() {
284: String headerParam = "SIP/2.0/UDP first.example.com:4000;ttl=16; "
285: + "maddr=224.2.0.1 ;branch=z9hG4bKa7c6a8dlze.1";
286:
287: declare("setName()/getName()");
288: Test1();
289:
290: declare("getValue()/getHeaderValue()");
291: Test2();
292:
293: declare("setValue()");
294: testSetValue(headerName, headerParam);
295:
296: declare("getParameterNames()/getParameter()");
297: Test4();
298:
299: declare("setParameter()/removeParameter()");
300: Test5();
301:
302: declare("toString()");
303: testToString(headerName, headerParam);
304: }
305: }
|