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. 39; BNF: p. 230
036: *
037: * Max-Forwards = "Max-Forwards" HCOLON 1*DIGIT
038: *
039: * The Max-Forwards header field serves to limit the number of hops a
040: * request can transit on the way to its destination. It consists of an
041: * integer that is decremented by one at each hop. If the Max-Forwards
042: * value reaches 0 before the request reaches its destination, it will
043: * be rejected with a 483(Too Many Hops) error response.
044: *
045: * A UAC MUST insert a Max-Forwards header field into each request it
046: * originates with a value that SHOULD be 70. This number was chosen to
047: * be sufficiently large to guarantee that a request would not be
048: * dropped in any SIP network when there were no loops, but not so large
049: * as to consume proxy resources when a loop does occur. Lower values
050: * should be used with caution and only in networks where topologies are
051: * known by the UA.
052: */
053:
054: public class TestMaxForwardsHeader extends SipHeaderBaseTest {
055:
056: /** A name of the header that will be tested */
057: private final String headerName = "Max-Forwards";
058:
059: /**
060: * Body of the test 1.
061: *
062: * Test for Max-Forwards header field: setName()/getName().
063: */
064: void Test1() {
065: // DEBUG: System.out.println("");
066: // DEBUG: System.out.println("*** Test1 started ***");
067:
068: // Testing the constructor...
069: // testConstructorNegative(headerName, "Invalid Value");
070:
071: SipHeader sh = createSipHeader(headerName, "100");
072:
073: if (sh == null) {
074: return;
075: }
076:
077: // Testing getName()...
078: String ret_name = sh.getName();
079: assertTrue("Invalid header value: " + ret_name, ret_name
080: .equals(headerName));
081:
082: // Testing setName()...
083: try {
084: sh.setName(headerName);
085: } catch (java.lang.IllegalArgumentException e) {
086: fail("setName(" + headerName + ") failed (IAE): " + e);
087: } catch (Throwable e) {
088: fail("setName(" + headerName + ") failed: " + e);
089: }
090: }
091:
092: /**
093: * Body of the test 2.
094: *
095: * Test for Max-Forwards header field: getValue()/getHeaderValue().
096: */
097: void Test2() {
098: String val;
099: String headerValue = "10";
100:
101: // DEBUG: System.out.println("");
102: // DEBUG: System.out.println("*** Test2 started ***");
103:
104: SipHeader sh = createSipHeader(headerName, headerValue);
105:
106: if (sh != null) {
107: val = sh.getValue();
108: assertTrue("getValue() returned invalid value: '" + val
109: + "'", val.equals(headerValue));
110:
111: val = sh.getHeaderValue();
112: assertTrue("(1) getHeaderValue() returned invalid "
113: + "value: '" + val + "'", val.equals(headerValue));
114:
115: // Test if the value can be changed.
116: headerValue = "70";
117: sh.setValue(headerValue);
118:
119: val = sh.getHeaderValue();
120: assertTrue("(2) getHeaderValue() returned invalid "
121: + "value: '" + val + "'", val.equals(headerValue));
122: }
123: }
124:
125: /**
126: * Body of the test 4.
127: *
128: * Test for Max-Forwards header field: getParameterNames()/getParameter().
129: */
130: void Test4() {
131: // DEBUG: System.out.println("");
132: // DEBUG: System.out.println("*** Test4 started ***");
133:
134: SipHeader sh = createSipHeader(headerName, "70");
135:
136: if (sh == null) {
137: return;
138: }
139:
140: // Testing getParameterNames()...
141: String[] paramList = sh.getParameterNames();
142:
143: if (paramList != null) {
144: fail("getParameterNames() should return null!");
145: }
146:
147: // Testing getParameter()...
148: String paramVal = sh.getParameter("ttl");
149: assertTrue("getParameter() returned '" + paramVal
150: + "' for the parameter 'ttl' that doesn't exist.",
151: paramVal == null);
152: }
153:
154: /**
155: * Body of the test 5.
156: *
157: * Test for Max-Forwards header field: setParameter()/removeParameter().
158: */
159: void Test5() {
160: // DEBUG: System.out.println("");
161: // DEBUG: System.out.println("*** Test5 started ***");
162:
163: SipHeader sh = createSipHeader(headerName, "70");
164:
165: if (sh == null) {
166: return;
167: }
168:
169: // Testing setParameter()...
170: try {
171: sh.setParameter("test", "1");
172: } catch (Exception e) {
173: fail(e + " was thrown.");
174: }
175:
176: try {
177: sh.removeParameter("test");
178: } catch (Exception e) {
179: fail("removeParameter(): " + e + " was thrown!");
180: }
181:
182: assertTrue(true); // to avoid error message from the test framework
183: }
184:
185: /**
186: * Run the tests
187: */
188: public void runTests() {
189: declare("setName()/getName()");
190: Test1();
191:
192: declare("getValue()/getHeaderValue()");
193: Test2();
194:
195: declare("setValue()");
196: testSetValue(headerName, "70");
197:
198: declare("getParameterNames()/getParameter()");
199: Test4();
200:
201: declare("setParameter()/removeParameter()");
202: Test5();
203:
204: declare("toString()");
205: testToString(headerName, "70");
206: }
207: }
|