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: /**
032: * Simple pattern matching tests
033: * to emulate native push filter handling.
034: */
035: public class TestPushUtils extends TestCase {
036: void test1() {
037: String buf = "abc\nFrom: <abc.com> def\ndef";
038: String filter = "*";
039: assertTrue("Match(" + buf + "," + filter + ")", checksender(
040: buf, filter));
041:
042: filter = "foo";
043: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
044: buf, filter));
045:
046: filter = "*.com";
047: assertTrue("Match(" + buf + "," + filter + ")", checksender(
048: buf, filter));
049:
050: filter = "???.com";
051: assertTrue("Match(" + buf + "," + filter + ")", checksender(
052: buf, filter));
053:
054: buf = "abc\nFrom: abc.com def\ndef";
055: filter = "*";
056: assertTrue("Match(" + buf + "," + filter + ")", checksender(
057: buf, filter));
058:
059: filter = "foo";
060: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
061: buf, filter));
062:
063: filter = "*.com";
064: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
065: buf, filter));
066:
067: filter = "???.com";
068: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
069: buf, filter));
070:
071: buf = "abc\nFrom : <abc.com> def\ndef";
072: filter = "*";
073: assertTrue("Match(" + buf + "," + filter + ")", checksender(
074: buf, filter));
075:
076: filter = "foo";
077: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
078: buf, filter));
079:
080: filter = "*.com";
081: assertTrue("Match(" + buf + "," + filter + ")", checksender(
082: buf, filter));
083:
084: filter = "???.com";
085: assertTrue("Match(" + buf + "," + filter + ")", checksender(
086: buf, filter));
087:
088: buf = "abc\nF : <abc.com> def\ndef";
089: filter = "*";
090: assertTrue("Match(" + buf + "," + filter + ")", checksender(
091: buf, filter));
092:
093: filter = "foo";
094: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
095: buf, filter));
096:
097: filter = "*.com";
098: assertTrue("Match(" + buf + "," + filter + ")", checksender(
099: buf, filter));
100:
101: filter = "???.com";
102: assertTrue("Match(" + buf + "," + filter + ")", checksender(
103: buf, filter));
104:
105: buf = "abc\nf: <abc.com> def\ndef";
106: filter = "*";
107: assertTrue("Match(" + buf + "," + filter + ")", checksender(
108: buf, filter));
109:
110: filter = "foo";
111: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
112: buf, filter));
113:
114: filter = "*.com";
115: assertTrue("Match(" + buf + "," + filter + ")", checksender(
116: buf, filter));
117:
118: filter = "???.com";
119: assertTrue("Match(" + buf + "," + filter + ")", checksender(
120: buf, filter));
121:
122: buf = "INVITE sip:sippy.tester@localhost:5090 SIP/2.0\n"
123: + "Call-ID: 467269204a-2130459815@129.145.167.130\n"
124: + "CSeq: 1 INVITE\n"
125: + "To: <sip:sippy.tester@localhost:5090>\n"
126: + "Via: SIP/2.0/UDP\n"
127: + "129.145.167.130:5090;branch=z9hG4bK3131323230353731383931393232373833313238333735343136313739333937\n"
128: + "Max-Forwards: 70\n"
129: + "From: <sip:sippy.tester@localhost>;tag=1017508678\n"
130: + " Subject: Hello...\n"
131: + "Contact: <sip:user@129.145.167.130:5090>\n"
132: + "Accept-Contact: *;type=\"application/x-chess\"\n"
133: + "Content-Type: text/plain\n" + "Content-Length: 15\n"
134: + "\n" + "Hello SIP-World\n";
135: filter = "*";
136: assertTrue("Match(" + buf + "," + filter + ")", checksender(
137: buf, filter));
138:
139: filter = "foo";
140: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
141: buf, filter));
142:
143: filter = "*.com";
144: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
145: buf, filter));
146:
147: filter = "???.com";
148: assertTrue("Match(" + buf + "," + filter + ")", !checksender(
149: buf, filter));
150:
151: byte[] acceptcontact_type = new byte[512];
152:
153: getSipAcceptContactType(buf.getBytes(), acceptcontact_type, 512);
154:
155: String act = new String(acceptcontact_type, 0, 7);
156:
157: assertTrue("Media type " + act + "==x-chess", act
158: .equalsIgnoreCase("x-chess"));
159: assertTrue("Media type " + act + "!=x-chessbad", !act
160: .equalsIgnoreCase("x-chessbad"));
161: assertTrue("Media type " + act + "!=bad", !act
162: .equalsIgnoreCase("bad"));
163: }
164:
165: /**
166: * Run the tests
167: */
168: public void runTests() {
169: declare("Pattern tests");
170: test1();
171: }
172:
173: boolean checksender(String buf, String filter) {
174: byte[] sender = new byte[512];
175:
176: getSipFromHeaderURI(buf.getBytes(), sender, 512);
177: return checksipfilter(filter.getBytes(), sender);
178: }
179:
180: native int getSipFromHeaderURI(byte[] buf, byte[] sender, int len);
181:
182: native int getSipAcceptContactType(byte[] buf, byte[] sender,
183: int len);
184:
185: native boolean checksipfilter(byte[] filter, byte[] sender);
186: }
|