FileDocCategorySizeDatePackage
TestFromHeader.javaAPI DocphoneME MR2 API (J2ME)8901Wed May 02 18:00:40 BST 2007javax.microedition.sip

TestFromHeader

public class TestFromHeader extends SipHeaderBaseTest
RFC3261, p. 37; BNF: p. 230 From = ( "From" / "f" ) HCOLON from-spec from-spec = ( name-addr / addr-spec ) *( SEMI from-param ) from-param = tag-param / generic-param tag-param = "tag" EQUAL token The From header field indicates the logical identity of the initiator of the request, possibly the user's address-of-record. Like the To header field, it contains a URI and optionally a display name. It is used by SIP elements to determine which processing rules to apply to a request (for example, automatic call rejection). As such, it is very important that the From URI not contain IP addresses or the FQDN of the host on which the UA is running, since these are not logical names. The From header field allows for a display name. A UAC SHOULD use the display name "Anonymous", along with a syntactically correct, but otherwise meaningless URI (like sip:thisis@anonymous.invalid), if the identity of the client is to remain hidden. Usually, the value that populates the From header field in requests generated by a particular UA is pre-provisioned by the user or by the administrators of the user's local domain. If a particular UA is used by multiple users, it might have switchable profiles that include a URI corresponding to the identity of the profiled user. Recipients of requests can authenticate the originator of a request in order to ascertain that they are who their From header field claims they are (see Section 22 for more on authentication). The From field MUST contain a new "tag" parameter, chosen by the UAC. See Section 19.3 for details on choosing a tag. For further information on the From header field, see Section 20.20. Examples: From: "Bob" ;tag=a48s From: sip:+12125551212@phone2net.com;tag=887s From: Anonymous ;tag=hyh8

Fields Summary
private final String
headerName
A name of the header that will be tested
Constructors Summary
Methods Summary
voidTest1()
Body of the test 1. Test for From header field: setName()/getName().


                    
      
        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test1 started ***");

        // Testing the constructor...
        // testConstructorNegative(headerName, "Invalid Value");

        SipHeader sh = createSipHeader(headerName,
            "\"Bob\" <sips:bob@biloxi.com> ;tag=a48s");

        if (sh == null) {
            return;
        }

        // Testing getName()...
        String ret_name = sh.getName();
        assertTrue("Invalid header value: " + ret_name,
            ret_name.equals(headerName));

        // Testing setName()...
        try {
           sh.setName(headerName);
        } catch (java.lang.IllegalArgumentException e) {
            fail("setName(" + headerName + ") failed (IAE): " + e);
        } catch (Throwable e) {
            fail("setName(" + headerName + ") failed: " + e);
        }
    
voidTest2()
Body of the test 2. Test for From header field: getValue()/getHeaderValue().

        SipHeader sh;
        String val;
        String headerValue1 = "<sip:+12125551212@phone2net.com>";
        String headerValue2 = "\"Bob\" <sips:bob@biloxi.com>";
        String headerParam1 = headerValue1;
        String headerParam2 = headerValue2;
        String[] paramList = {
            "tag=887s",
            "generic=sample_value"
        };

        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test2 started ***");

        for (int i = 0; i < paramList.length + 1; i++) {
            sh = createSipHeader(headerName, headerParam1);

            if (sh != null) {
                val = sh.getValue();
                assertTrue("getValue() returned invalid parameter value: '" +
                           val + "'", val.equals(headerValue1));

                val = sh.getHeaderValue();
                assertTrue("(1) getHeaderValue() returned invalid " +
                    "parameter value: '" + val + "'", val.equals(headerParam1));

                // Test if the value can be changed.
                sh.setValue(headerValue2);

                val = sh.getHeaderValue();
                assertTrue("(2) getHeaderValue() returned invalid " +
                    "parameter value: '" + val + "'", val.equals(headerParam2));
            }

            headerParam1 += ";";
            headerParam2 += ";";

            if (i < paramList.length) {
                headerParam1 += paramList[i];
                headerParam2 += paramList[i];
            }
        } // end for
    
voidTest4()
Body of the test 4. Test for From header field: getParameterNames()/getParameter().

        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test4 started ***");

        SipHeader sh = createSipHeader(headerName,
            "Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8");

        if (sh == null) {
            return;
        }

        // Testing getParameterNames()...
        String[] paramList = sh.getParameterNames();

        if (paramList == null) {
            fail("getParameterNames() returned null!");
        } else {
            assertTrue("getParameterNames() returned " + paramList.length +
                       " parameters instead of 1.", paramList.length == 1);

            boolean isValid = paramList[0].equals("tag");

            assertTrue("Invalid parameter name: " + paramList[0],
                       isValid);
        }

        // Testing getParameter()...
        String paramVal = sh.getParameter("ttl");
        assertTrue("getParameter() returned '" + paramVal +
            "' for the parameter 'ttl' that doesn't exist.", paramVal == null);

        paramVal = sh.getParameter("tag");
        assertTrue("getParameter() returned '" + paramVal + "' for 'received'" +
            " instead of 'hyh8'.", paramVal.equals("hyh8"));
    
voidTest5()
Body of the test 5. Test for From header field: setParameter()/removeParameter().

        // DEBUG:        System.out.println("");
        // DEBUG:        System.out.println("*** Test5 started ***");

        SipHeader sh = createSipHeader(headerName,
            "\"Bob\" <sips:bob@biloxi.com> ;tag=a48s");

        if (sh == null) {
            return;
        }

        // Testing setParameter()...
        sh.setParameter("tag", "123456");

        String paramVal = sh.getParameter("tag");
        assertTrue("getParameter() returned '" + paramVal +
                   "' instead of '123456'.", paramVal.equals("123456"));

        sh.setParameter("generic", "10"); // parameter doesn't exist

        paramVal = sh.getParameter("generic");
        assertTrue("getParameter() returned '" + paramVal +
                   "' instead of 10.", paramVal.equals("10"));
    
public voidrunTests()
Run the tests

        String headerParam = "Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8";

        declare("setName()/getName()");
        Test1();

        declare("getValue()/getHeaderValue()");
        Test2();

        declare("setValue()");
        testSetValue(headerName, headerParam);

        declare("getParameterNames()/getParameter()");
        Test4();

        declare("setParameter()/removeParameter()");
        Test5();

        declare("toString()");
        testToString(headerName, headerParam);