TestFromHeader.java :  » 6.0-JDK-Modules » j2me » javax » microedition » sip » Java Open Source

Java Open Source » 6.0 JDK Modules » j2me 
j2me » javax » microedition » sip » TestFromHeader.java
/*
 *   
 *
 * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */

package javax.microedition.sip;

import com.sun.midp.i3test.TestCase;

import javax.microedition.io.Connector;
import java.io.IOException;

/**
 * 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" <sips:bob@biloxi.com> ;tag=a48s
 *    From: sip:+12125551212@phone2net.com;tag=887s
 *    From: Anonymous <sip:c8oqz84zk7z@privacy.org>;tag=hyh8
 *
 */

public class TestFromHeader extends SipHeaderBaseTest {


    /** A name of the header that will be tested */
    private final String headerName = "From";

    /**
     * Body of the test 1.
     *
     * Test for From header field: setName()/getName().
     */
    void Test1() {
        // 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);
        }
    }

    /**
     * Body of the test 2.
     *
     * Test for From header field: getValue()/getHeaderValue().
     */
    void Test2() {
        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
    }

    /**
     * Body of the test 4.
     *
     * Test for From header field: getParameterNames()/getParameter().
     */
    void Test4() {
        // 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"));
    }

    /**
     * Body of the test 5.
     *
     * Test for From header field: setParameter()/removeParameter().
     */
    void Test5() {
        // 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"));
    }

    /**
     * Run the tests
     */
    public void runTests() {
        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);
    }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.