WapPushTest.java revision 7640caaf912a7eefacc3e2108c5afd70f7b072a4
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.smspush.unitTests;
18
19import android.app.Activity;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
24import android.os.IBinder;
25import android.os.RemoteException;
26import android.provider.Telephony.Sms.Intents;
27import android.test.ServiceTestCase;
28import android.util.Log;
29import android.util.Config;
30
31import com.android.internal.telephony.IccUtils;
32import com.android.internal.telephony.IWapPushManager;
33import com.android.internal.telephony.WapPushManagerParams;
34import com.android.internal.telephony.WspTypeDecoder;
35import com.android.internal.util.HexDump;
36import com.android.smspush.WapPushManager;
37
38import java.util.Random;
39
40/**
41 * This is a simple framework for a test of a Service.  See {@link android.test.ServiceTestCase
42 * ServiceTestCase} for more information on how to write and extend service tests.
43 *
44 * To run this test, you can type:
45 * adb shell am instrument -w \
46 * -e class com.android.smspush.unitTests.WapPushTest \
47 * com.android.smspush.unitTests/android.test.InstrumentationTestRunner
48 */
49public class WapPushTest extends ServiceTestCase<WapPushManager> {
50    private static final String LOG_TAG = "WAP PUSH";
51    private static final boolean LOCAL_LOGV = false;
52    private static final int TIME_WAIT = 100;
53
54    protected int mAppIdValue = 0x8002;
55    protected String mAppIdName = "x-wap-application:*";
56    protected int mContentTypeValue = 0x030a;
57    protected String mContentTypeName = "application/vnd.wap.sic";
58
59    protected String mPackageName;
60    protected String mClassName;
61
62    protected byte[] mGsmHeader = {
63            (byte) 0x00, // sc address
64            (byte) 0x40, // TP-MTI
65            (byte) 0x04, // sender address length?
66            (byte) 0x81, (byte) 0x55, (byte) 0x45, // sender address?
67            (byte) 0x00, // data schema
68            (byte) 0x00, // proto ID
69            (byte) 0x01, (byte) 0x60, (byte) 0x12, (byte) 0x31,
70            (byte) 0x74, (byte) 0x34, (byte) 0x63 // time stamp
71    };
72
73    protected byte[] mUserDataHeader = {
74            (byte) 0x07, // UDH len
75            (byte) 0x06, // header len
76            (byte) 0x05, // port addressing type?
77            (byte) 0x00, // dummy
78            (byte) 0x0B, (byte) 0x84, // dest port
79            (byte) 0x23, (byte) 0xF0 // src port
80    };
81
82    protected byte[] mWspHeader;
83
84    protected byte[] mMessageBody = {
85            (byte) 0x00,
86            (byte) 0x01,
87            (byte) 0x02,
88            (byte) 0x03,
89            (byte) 0x04,
90            (byte) 0x05,
91            (byte) 0x06,
92            (byte) 0x07,
93            (byte) 0x08,
94            (byte) 0x09,
95            (byte) 0x0a,
96            (byte) 0x0b,
97            (byte) 0x0c,
98            (byte) 0x0d,
99            (byte) 0x0e,
100            (byte) 0x0f
101    };
102
103    protected int mWspHeaderStart;
104    protected int mWspHeaderLen;
105    protected int mWspContentTypeStart;
106
107    /**
108     * OMA application ID in binary form
109     * http://www.openmobilealliance.org/tech/omna/omna-push-app-id.aspx
110     */
111    final int[] OMA_APPLICATION_ID_VALUES = new int[] {
112            0x00,
113            0x01,
114            0x02,
115            0x03,
116            0x04,
117            0x05,
118            0x06,
119            0x07,
120            0x08,
121            0x09,
122            0x0A,
123            0x8000,
124            0x8001,
125            0x8002,
126            0x8003,
127            0x8004,
128            0x8005,
129            0x8006,
130            0x8007,
131            0x8008,
132            0x8009,
133            0x800B,
134            0x8010
135    };
136
137    /**
138     * OMA application ID in string form
139     * http://www.openmobilealliance.org/tech/omna/omna-push-app-id.aspx
140     */
141    final String[] OMA_APPLICATION_ID_NAMES = new String[] {
142            "x-wap-application:*",
143            "x-wap-application:push.sia",
144            "x-wap-application:wml.ua",
145            "x-wap-application:wta.ua",
146            "x-wap-application:mms.ua",
147            "x-wap-application:push.syncml",
148            "x-wap-application:loc.ua",
149            "x-wap-application:syncml.dm",
150            "x-wap-application:drm.ua",
151            "x-wap-application:emn.ua",
152            "x-wap-application:wv.ua",
153            "x-wap-microsoft:localcontent.ua",
154            "x-wap-microsoft:IMclient.ua",
155            "x-wap-docomo:imode.mail.ua",
156            "x-wap-docomo:imode.mr.ua",
157            "x-wap-docomo:imode.mf.ua",
158            "x-motorola:location.ua",
159            "x-motorola:now.ua",
160            "x-motorola:otaprov.ua",
161            "x-motorola:browser.ua",
162            "x-motorola:splash.ua",
163            "x-wap-nai:mvsw.command",
164            "x-wap-openwave:iota.ua"
165    };
166
167    /**
168     * OMA content type in binary form
169     * http://www.openmobilealliance.org/tech/omna/omna-wsp-content-type.aspx
170     */
171    final int[] OMA_CONTENT_TYPE_VALUES = new int[] {
172            0x00,
173            0x01,
174            0x02,
175            0x03,
176            0x04,
177            0x05,
178            0x06,
179            0x07,
180            0x08,
181            0x09,
182            0x0A,
183            0x0B,
184            0x0C,
185            0x0D,
186            0x0E,
187            0x0F,
188            0x10,
189            0x11,
190            0x12,
191            0x13,
192            0x14,
193            0x15,
194            0x16,
195            0x17,
196            0x18,
197            0x19,
198            0x1A,
199            0x1B,
200            0x1C,
201            0x1D,
202            0x1E,
203            0x1F,
204            0x20,
205            0x21,
206            0x22,
207            0x23,
208            0x24,
209            0x25,
210            0x26,
211            0x27,
212            0x28,
213            0x29,
214            0x2A,
215            0x2B,
216            0x2C,
217            0x2D,
218            0x2E,
219            0x2F,
220            0x30,
221            0x31,
222            0x32,
223            0x33,
224            0x34,
225            0x35,
226            0x36,
227            0x37,
228            0x38,
229            0x39,
230            0x3A,
231            0x3B,
232            0x3C,
233            0x3D,
234            0x3E,
235            0x3F,
236            0x40,
237            0x41,
238            0x42,
239            0x43,
240            0x44,
241            0x45,
242            0x46,
243            0x47,
244            0x48,
245            0x49,
246            0x4A,
247            0x4B,
248            0x4C,
249            0x4D,
250            0x4E,
251            0x4F,
252            0x50,
253            0x51,
254            0x52,
255            0x53,
256            0x54,
257//            0x55,
258//            0x56,
259//            0x57,
260//            0x58,
261            0x0201,
262            0x0202,
263            0x0203,
264            0x0204,
265            0x0205,
266            0x0206,
267            0x0207,
268            0x0208,
269            0x0209,
270            0x020A,
271            0x020B,
272            0x020C,
273            0x0300,
274            0x0301,
275            0x0302,
276            0x0303,
277            0x0304,
278            0x0305,
279            0x0306,
280            0x0307,
281            0x0308,
282            0x0309,
283            0x030A,
284            0x030B,
285            0x030C,
286            0x030D,
287            0x030E,
288            0x030F,
289            0x0310,
290            0x0311,
291            0x0312,
292            0x0313,
293            0x0314,
294            0x0315,
295            0x0316,
296            0x0317,
297            0x0318,
298            0x0319,
299            0x031A,
300            0x031B
301            /*0x031C,
302              0x031D*/
303    };
304
305    /**
306     * OMA content type in string form
307     * http://www.openmobilealliance.org/tech/omna/omna-wsp-content-type.aspx
308     */
309    final String[] OMA_CONTENT_TYPE_NAMES = new String[] {
310            "*/*",
311            "text/*",
312            "text/html",
313            "text/plain",
314            "text/x-hdml",
315            "text/x-ttml",
316            "text/x-vCalendar",
317            "text/x-vCard",
318            "text/vnd.wap.wml",
319            "text/vnd.wap.wmlscript",
320            "text/vnd.wap.wta-event",
321            "multipart/*",
322            "multipart/mixed",
323            "multipart/form-data",
324            "multipart/byterantes",
325            "multipart/alternative",
326            "application/*",
327            "application/java-vm",
328            "application/x-www-form-urlencoded",
329            "application/x-hdmlc",
330            "application/vnd.wap.wmlc",
331            "application/vnd.wap.wmlscriptc",
332            "application/vnd.wap.wta-eventc",
333            "application/vnd.wap.uaprof",
334            "application/vnd.wap.wtls-ca-certificate",
335            "application/vnd.wap.wtls-user-certificate",
336            "application/x-x509-ca-cert",
337            "application/x-x509-user-cert",
338            "image/*",
339            "image/gif",
340            "image/jpeg",
341            "image/tiff",
342            "image/png",
343            "image/vnd.wap.wbmp",
344            "application/vnd.wap.multipart.*",
345            "application/vnd.wap.multipart.mixed",
346            "application/vnd.wap.multipart.form-data",
347            "application/vnd.wap.multipart.byteranges",
348            "application/vnd.wap.multipart.alternative",
349            "application/xml",
350            "text/xml",
351            "application/vnd.wap.wbxml",
352            "application/x-x968-cross-cert",
353            "application/x-x968-ca-cert",
354            "application/x-x968-user-cert",
355            "text/vnd.wap.si",
356            "application/vnd.wap.sic",
357            "text/vnd.wap.sl",
358            "application/vnd.wap.slc",
359            "text/vnd.wap.co",
360            "application/vnd.wap.coc",
361            "application/vnd.wap.multipart.related",
362            "application/vnd.wap.sia",
363            "text/vnd.wap.connectivity-xml",
364            "application/vnd.wap.connectivity-wbxml",
365            "application/pkcs7-mime",
366            "application/vnd.wap.hashed-certificate",
367            "application/vnd.wap.signed-certificate",
368            "application/vnd.wap.cert-response",
369            "application/xhtml+xml",
370            "application/wml+xml",
371            "text/css",
372            "application/vnd.wap.mms-message",
373            "application/vnd.wap.rollover-certificate",
374            "application/vnd.wap.locc+wbxml",
375            "application/vnd.wap.loc+xml",
376            "application/vnd.syncml.dm+wbxml",
377            "application/vnd.syncml.dm+xml",
378            "application/vnd.syncml.notification",
379            "application/vnd.wap.xhtml+xml",
380            "application/vnd.wv.csp.cir",
381            "application/vnd.oma.dd+xml",
382            "application/vnd.oma.drm.message",
383            "application/vnd.oma.drm.content",
384            "application/vnd.oma.drm.rights+xml",
385            "application/vnd.oma.drm.rights+wbxml",
386            "application/vnd.wv.csp+xml",
387            "application/vnd.wv.csp+wbxml",
388            "application/vnd.syncml.ds.notification",
389            "audio/*",
390            "video/*",
391            "application/vnd.oma.dd2+xml",
392            "application/mikey",
393            "application/vnd.oma.dcd",
394            "application/vnd.oma.dcdc",
395//            "text/x-vMessage",
396//            "application/vnd.omads-email+wbxml",
397//            "text/x-vBookmark",
398//            "application/vnd.syncml.dm.notification",
399            "application/vnd.uplanet.cacheop-wbxml",
400            "application/vnd.uplanet.signal",
401            "application/vnd.uplanet.alert-wbxml",
402            "application/vnd.uplanet.list-wbxml",
403            "application/vnd.uplanet.listcmd-wbxml",
404            "application/vnd.uplanet.channel-wbxml",
405            "application/vnd.uplanet.provisioning-status-uri",
406            "x-wap.multipart/vnd.uplanet.header-set",
407            "application/vnd.uplanet.bearer-choice-wbxml",
408            "application/vnd.phonecom.mmc-wbxml",
409            "application/vnd.nokia.syncset+wbxml",
410            "image/x-up-wpng",
411            "application/iota.mmc-wbxml",
412            "application/iota.mmc-xml",
413            "application/vnd.syncml+xml",
414            "application/vnd.syncml+wbxml",
415            "text/vnd.wap.emn+xml",
416            "text/calendar",
417            "application/vnd.omads-email+xml",
418            "application/vnd.omads-file+xml",
419            "application/vnd.omads-folder+xml",
420            "text/directory;profile=vCard",
421            "application/vnd.wap.emn+wbxml",
422            "application/vnd.nokia.ipdc-purchase-response",
423            "application/vnd.motorola.screen3+xml",
424            "application/vnd.motorola.screen3+gzip",
425            "application/vnd.cmcc.setting+wbxml",
426            "application/vnd.cmcc.bombing+wbxml",
427            "application/vnd.docomo.pf",
428            "application/vnd.docomo.ub",
429            "application/vnd.omaloc-supl-init",
430            "application/vnd.oma.group-usage-list+xml",
431            "application/oma-directory+xml",
432            "application/vnd.docomo.pf2",
433            "application/vnd.oma.drm.roap-trigger+wbxml",
434            "application/vnd.sbm.mid2",
435            "application/vnd.wmf.bootstrap",
436            "application/vnc.cmcc.dcd+xml",
437            "application/vnd.sbm.cid",
438            "application/vnd.oma.bcast.provisioningtrigger",
439            /*"application/vnd.docomo.dm",
440              "application/vnd.oma.scidm.messages+xml"*/
441    };
442
443    private IDataVerify mIVerify = null;
444
445    ServiceConnection mConn = new ServiceConnection() {
446            public void onServiceConnected(ComponentName name, IBinder service) {
447                Log.v(LOG_TAG, "data verify interface connected.");
448                mIVerify = IDataVerify.Stub.asInterface(service);
449            }
450            public void onServiceDisconnected(ComponentName name) {
451            }
452        };
453
454    /**
455     * Main WapPushManager test module constructor
456     */
457    public WapPushTest() {
458        super(WapPushManager.class);
459        mClassName = this.getClass().getName();
460        mPackageName = this.getClass().getPackage().getName();
461    }
462
463    /**
464     * Initialize the verifier
465     */
466    @Override
467    public void setUp() {
468        try {
469            super.setUp();
470            // get verifier
471            getContext().bindService(new Intent(IDataVerify.class.getName()),
472                    mConn, Context.BIND_AUTO_CREATE);
473        } catch (Exception e) {
474            Log.w(LOG_TAG, "super exception");
475        }
476        // Log.d(LOG_TAG, "test setup");
477    }
478
479    private IWapPushManager mWapPush = null;
480    IWapPushManager getInterface() {
481        if (mWapPush != null) return mWapPush;
482        Intent startIntent = new Intent();
483        startIntent.setClass(getContext(), WapPushManager.class);
484        IBinder service = bindService(startIntent);
485
486        mWapPush = IWapPushManager.Stub.asInterface(service);
487        return mWapPush;
488    }
489
490    /*
491     * All methods need to start with 'test'.
492     * Use various assert methods to pass/fail the test case.
493     */
494    protected void utAddPackage(boolean need_sig, boolean more_proc) {
495        IWapPushManager iwapman = getInterface();
496
497        // insert new data
498        try {
499            assertTrue(iwapman.addPackage(
500                    Integer.toString(mAppIdValue),
501                    Integer.toString(mContentTypeValue),
502                    mPackageName, mClassName,
503                    WapPushManagerParams.APP_TYPE_SERVICE, need_sig, more_proc));
504        } catch (RemoteException e) {
505            assertTrue(false);
506        }
507
508        // verify the data
509        WapPushManager wpman = getService();
510        assertTrue(wpman.verifyData(Integer.toString(mAppIdValue),
511                Integer.toString(mContentTypeValue),
512                mPackageName, mClassName,
513                WapPushManagerParams.APP_TYPE_SERVICE, need_sig, more_proc));
514    }
515
516    /**
517     * Add package test
518     */
519    public void testAddPackage1() {
520        int originalAppIdValue = mAppIdValue;
521        int originalContentTypeValue  = mContentTypeValue;
522
523        utAddPackage(true, true);
524        mAppIdValue += 10;
525        utAddPackage(true, false);
526        mContentTypeValue += 20;
527        utAddPackage(false, true);
528        mContentTypeValue += 20;
529        utAddPackage(false, false);
530
531        mAppIdValue = originalAppIdValue;
532        mContentTypeValue = originalContentTypeValue;
533
534        // clean up data
535        try {
536            IWapPushManager iwapman = getInterface();
537            iwapman.deletePackage(Integer.toString(mAppIdValue),
538                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
539            mAppIdValue += 10;
540            iwapman.deletePackage(Integer.toString(mAppIdValue),
541                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
542            mContentTypeValue += 20;
543            iwapman.deletePackage(Integer.toString(mAppIdValue),
544                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
545            mContentTypeValue += 20;
546            iwapman.deletePackage(Integer.toString(mAppIdValue),
547                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
548        } catch (RemoteException e) {
549            assertTrue(false);
550        }
551        mAppIdValue = originalAppIdValue;
552        mContentTypeValue = originalContentTypeValue;
553    }
554
555    /**
556     * Add duprecated package test.
557     */
558    public void testAddPackage2() {
559        try {
560            IWapPushManager iwapman = getInterface();
561
562            // set up data
563            iwapman.addPackage(Integer.toString(mAppIdValue),
564                    Integer.toString(mContentTypeValue), mPackageName, mClassName, 0,
565                    false, false);
566            iwapman.addPackage(Integer.toString(mAppIdValue + 10),
567                    Integer.toString(mContentTypeValue), mPackageName, mClassName, 0,
568                    false, false);
569            iwapman.addPackage(Integer.toString(mAppIdValue),
570                    Integer.toString(mContentTypeValue + 10), mPackageName, mClassName, 0,
571                    false, false);
572
573            assertFalse(iwapman.addPackage(Integer.toString(mAppIdValue),
574                    Integer.toString(mContentTypeValue), mPackageName, mClassName, 0,
575                    false, false));
576            assertFalse(iwapman.addPackage(Integer.toString(mAppIdValue + 10),
577                    Integer.toString(mContentTypeValue), mPackageName, mClassName, 0,
578                    false, false));
579            assertFalse(iwapman.addPackage(Integer.toString(mAppIdValue),
580                    Integer.toString(mContentTypeValue + 10), mPackageName, mClassName, 0,
581                    false, false));
582
583            // clean up data
584            iwapman.deletePackage(Integer.toString(mAppIdValue),
585                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
586            iwapman.deletePackage(Integer.toString(mAppIdValue + 10),
587                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
588            iwapman.deletePackage(Integer.toString(mAppIdValue),
589                    Integer.toString(mContentTypeValue + 10), mPackageName, mClassName);
590        } catch (RemoteException e) {
591            assertTrue(false);
592        }
593    }
594
595    protected void utUpdatePackage(boolean need_sig, boolean more_proc) {
596        IWapPushManager iwapman = getInterface();
597
598        // insert new data
599        try {
600            assertTrue(iwapman.updatePackage(
601                    Integer.toString(mAppIdValue),
602                    Integer.toString(mContentTypeValue),
603                    mPackageName, mClassName,
604                    WapPushManagerParams.APP_TYPE_SERVICE, need_sig, more_proc));
605        } catch (RemoteException e) {
606            assertTrue(false);
607        }
608
609        // verify the data
610        WapPushManager wpman = getService();
611        assertTrue(wpman.verifyData(
612                Integer.toString(mAppIdValue),
613                Integer.toString(mContentTypeValue),
614                mPackageName, mClassName,
615                WapPushManagerParams.APP_TYPE_SERVICE, need_sig, more_proc));
616    }
617
618    /**
619     * Updating package test
620     */
621    public void testUpdatePackage1() {
622        int originalAppIdValue = mAppIdValue;
623        int originalContentTypeValue  = mContentTypeValue;
624
625        // set up data
626        try {
627            IWapPushManager iwapman = getInterface();
628
629            iwapman.addPackage(Integer.toString(mAppIdValue),
630                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
631                    0, false, false);
632            mAppIdValue += 10;
633            iwapman.addPackage(Integer.toString(mAppIdValue),
634                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
635                    0, false, false);
636            mContentTypeValue += 20;
637            iwapman.addPackage(Integer.toString(mAppIdValue),
638                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
639                    0, false, false);
640            mContentTypeValue += 20;
641            iwapman.addPackage(Integer.toString(mAppIdValue),
642                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
643                    0, false, false);
644        } catch (RemoteException e) {
645            assertTrue(false);
646        }
647
648        mAppIdValue = originalAppIdValue;
649        mContentTypeValue = originalContentTypeValue;
650        utUpdatePackage(false, false);
651        mAppIdValue += 10;
652        utUpdatePackage(false, true);
653        mContentTypeValue += 20;
654        utUpdatePackage(true, false);
655        mContentTypeValue += 20;
656        utUpdatePackage(true, true);
657
658        mAppIdValue = originalAppIdValue;
659        mContentTypeValue = originalContentTypeValue;
660
661        // clean up data
662        try {
663            IWapPushManager iwapman = getInterface();
664
665            iwapman.deletePackage(Integer.toString(mAppIdValue),
666                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
667            mAppIdValue += 10;
668            iwapman.deletePackage(Integer.toString(mAppIdValue),
669                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
670            mContentTypeValue += 20;
671            iwapman.deletePackage(Integer.toString(mAppIdValue),
672                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
673            mContentTypeValue += 20;
674            iwapman.deletePackage(Integer.toString(mAppIdValue),
675                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
676        } catch (RemoteException e) {
677            assertTrue(false);
678        }
679        mAppIdValue = originalAppIdValue;
680        mContentTypeValue = originalContentTypeValue;
681    }
682
683    /**
684     * Updating invalid package test
685     */
686    public void testUpdatePackage2() {
687        int originalAppIdValue = mAppIdValue;
688        int originalContentTypeValue  = mContentTypeValue;
689
690        try {
691            // set up data
692            IWapPushManager iwapman = getInterface();
693
694            iwapman.addPackage(Integer.toString(mAppIdValue),
695                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
696                    0, false, false);
697            assertFalse(iwapman.updatePackage(
698                    Integer.toString(mAppIdValue + 10),
699                    Integer.toString(mContentTypeValue),
700                    mPackageName, mClassName, 0, false, false));
701            assertFalse(iwapman.updatePackage(
702                    Integer.toString(mAppIdValue),
703                    Integer.toString(mContentTypeValue + 10),
704                    mPackageName, mClassName, 0, false, false));
705            assertTrue(iwapman.updatePackage(
706                    Integer.toString(mAppIdValue),
707                    Integer.toString(mContentTypeValue),
708                    mPackageName + "dummy_data", mClassName, 0, false, false));
709            assertTrue(iwapman.updatePackage(
710                    Integer.toString(mAppIdValue),
711                    Integer.toString(mContentTypeValue),
712                    mPackageName, mClassName + "dummy_data", 0, false, false));
713            // clean up data
714            iwapman.deletePackage(Integer.toString(mAppIdValue),
715                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
716            iwapman.deletePackage(Integer.toString(mAppIdValue),
717                    Integer.toString(mContentTypeValue), mPackageName,
718                    mClassName + "dummy_data");
719        } catch (RemoteException e) {
720            assertTrue(false);
721        }
722    }
723
724    protected void utDeletePackage() {
725        IWapPushManager iwapman = getInterface();
726
727        try {
728            assertTrue(iwapman.deletePackage(
729                    Integer.toString(mAppIdValue),
730                    Integer.toString(mContentTypeValue),
731                    mPackageName, mClassName));
732        } catch (RemoteException e) {
733            assertTrue(false);
734        }
735
736        // verify the data
737        WapPushManager wpman = getService();
738        assertTrue(!wpman.isDataExist(
739                Integer.toString(mAppIdValue),
740                Integer.toString(mContentTypeValue),
741                mPackageName, mClassName));
742    }
743
744    /**
745     * Deleting package test
746     */
747    public void testDeletePackage1() {
748        int originalAppIdValue = mAppIdValue;
749        int originalContentTypeValue  = mContentTypeValue;
750
751        // set up data
752        try {
753            IWapPushManager iwapman = getInterface();
754
755            iwapman.addPackage(Integer.toString(mAppIdValue),
756                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
757                    0, false, false);
758            mAppIdValue += 10;
759            iwapman.addPackage(Integer.toString(mAppIdValue),
760                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
761                    0, false, false);
762            mContentTypeValue += 20;
763            iwapman.addPackage(Integer.toString(mAppIdValue),
764                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
765                    0, false, false);
766            mContentTypeValue += 20;
767            iwapman.addPackage(Integer.toString(mAppIdValue),
768                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
769                    0, false, false);
770        } catch (RemoteException e) {
771            assertTrue(false);
772        }
773
774        mAppIdValue = originalAppIdValue;
775        mContentTypeValue = originalContentTypeValue;
776        utDeletePackage();
777        mAppIdValue += 10;
778        utDeletePackage();
779        mContentTypeValue += 20;
780        utDeletePackage();
781        mContentTypeValue += 20;
782        utDeletePackage();
783
784        mAppIdValue = originalAppIdValue;
785        mContentTypeValue = originalContentTypeValue;
786    }
787
788    /**
789     * Deleting invalid package test
790     */
791    public void testDeletePackage2() {
792        int originalAppIdValue = mAppIdValue;
793        int originalContentTypeValue  = mContentTypeValue;
794
795        try {
796            // set up data
797            IWapPushManager iwapman = getInterface();
798
799            iwapman.addPackage(Integer.toString(mAppIdValue),
800                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
801                    0, false, false);
802
803            assertFalse(iwapman.deletePackage(Integer.toString(mAppIdValue + 10),
804                    Integer.toString(mContentTypeValue), mPackageName, mClassName));
805            assertFalse(iwapman.deletePackage(Integer.toString(mAppIdValue),
806                    Integer.toString(mContentTypeValue + 20), mPackageName, mClassName));
807            assertFalse(iwapman.deletePackage(Integer.toString(mAppIdValue + 10),
808                    Integer.toString(mContentTypeValue + 20), mPackageName, mClassName));
809
810            iwapman.deletePackage(Integer.toString(mAppIdValue),
811                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
812
813        } catch (RemoteException e) {
814            assertTrue(false);
815        }
816    }
817
818
819    protected int encodeUint32(int uint32Val, byte[] arr, int start) {
820        int bit = 1;
821        int topbit = 0;
822        int encodeLen;
823        int tmpVal;
824
825        assertTrue(uint32Val >= 0);
826        for (int i = 0; i < 31; i++) {
827            if ((bit & uint32Val) > 0) topbit = i;
828            bit = (bit << 1);
829        }
830        encodeLen = topbit/7 + 1;
831        if (arr == null) return encodeLen;
832
833        //Log.d(LOG_TAG, "uint32Val = " + Integer.toHexString(uint32Val) + ", topbit = "
834        //      + topbit + ", encodeLen = " + encodeLen);
835
836        tmpVal = uint32Val;
837        for (int i = encodeLen - 1; i >= 0; i--) {
838            long val = 0;
839            if (i < encodeLen - 1) val = 0x80;
840            val |= tmpVal & 0x7f;
841            arr[start + i] = (byte) (val & 0xFF);
842            tmpVal = (tmpVal >> 7);
843        }
844        return encodeLen;
845    }
846
847    protected int encodeShortInt(int sintVal, byte[] arr, int start) {
848        int encodeLen = 0;
849
850        if (sintVal >= 0x80) return encodeLen;
851        encodeLen = 1;
852        arr[start] = (byte) (sintVal | 0x80);
853        return encodeLen;
854    }
855
856
857    /**
858     * Generate Random WSP header with integer application ID
859     */
860    protected void createRandomWspHeader(byte[] arr, Random rd, int headerStart,
861            boolean noAppId) {
862
863        boolean appIdAdded = false;
864
865        Log.d(LOG_TAG, "headerStart = " + headerStart + ", appId = " + mAppIdValue
866                + "(" + Integer.toHexString(mAppIdValue) + ")");
867        Log.d(LOG_TAG, "random arr length:" + arr.length);
868        String typename[] = new String[] { "short int", "long int", "string", "uint32"};
869
870        while (!appIdAdded) {
871            int type;
872            int index = headerStart;
873            int len = arr.length;
874            int i;
875            boolean addAppid = false;
876            int tmpVal = 0;
877            int tmpVal2 = 0;
878
879            while (true) {
880                int add;
881
882                /*
883                 * field name
884                 * 0: short int
885                 * 1: long int
886                 * 2: text
887                 * (no uint for param value)
888                 */
889                type = rd.nextInt(3);
890                switch (type) {
891                case 0: // header short integer
892                    if (index > 100 && !appIdAdded) addAppid = true;
893                    add = 1;
894                    break;
895                case 1: // header long int
896                    add = 1 + rd.nextInt(29);
897                    break;
898                default: // header string
899                    add = 2 + rd.nextInt(10);
900                    break;
901                }
902                if (index + add >= len) break;
903
904                // fill header name
905                switch (type) {
906                case 0: // header short integer
907                    if (!addAppid) {
908                        do {
909                            arr[index] = (byte) (0x80 | rd.nextInt(128));
910                        } while (arr[index] == (byte) 0xaf);
911                    } else {
912                        Log.d(LOG_TAG, "appId added.");
913                        arr[index] = (byte) 0xaf;
914                        // if noAppId case, appId fld must be decieved.
915                        if (noAppId) arr[index]++;
916                    }
917                    break;
918                case 1: // header long int
919                    arr[index] = (byte) (add - 1);
920                    tmpVal2 = 0;
921                    for (i = 1; i < add; i++) {
922                        tmpVal = rd.nextInt(255);
923                        tmpVal2 = (tmpVal2 << 8) | tmpVal;
924                        arr[index + i] = (byte) tmpVal;
925                    }
926                    // don't set application id
927                    if (tmpVal2 == 0x2f) arr[index + 1]++;
928                    break;
929                default: // header string
930                    for (i = 0; i < add - 1; i++) {
931                        tmpVal = rd.nextInt(127);
932                        if (tmpVal < 32) tmpVal= (32 + tmpVal);
933                        arr[index + i] = (byte) tmpVal;
934                    }
935                    arr[index + i] = (byte) 0x0;
936                    break;
937                }
938
939                if (LOCAL_LOGV) {
940                    Log.d(LOG_TAG, "field name index:" + index);
941                    Log.d(LOG_TAG, "type:" + typename[type] + ", add:" + add);
942                    if (type != 2) {
943                        for (i = index; i< index + add; i++) {
944                            System.out.print(Integer.toHexString(0xff & arr[i]));
945                            System.out.print(' ');
946                        }
947                    } else {
948                        System.out.print(Integer.toHexString(0xff & arr[index]));
949                        System.out.print(' ');
950                        String str = new String(arr, index + 1, add - 2);
951                        for (i = 0; i < str.length(); i++) {
952                            System.out.print(str.charAt(i));
953                            System.out.print(' ');
954                        }
955                    }
956                    System.out.print('\n');
957                }
958                index += add;
959
960
961                /*
962                 * field value
963                 * 0: short int
964                 * 1: long int
965                 * 2: text
966                 * 3: uint
967                 */
968                if (addAppid) {
969                    type = 1;
970                } else {
971                    type = rd.nextInt(4);
972                }
973                switch (type) {
974                case 0: // header short integer
975                    add = 1;
976                    break;
977                case 1: // header long int
978                    if (addAppid) {
979                        int bit = 1;
980                        int topBit = 0;
981
982                        for (i = 0; i < 31; i++) {
983                            if ((mAppIdValue & bit) > 0) topBit = i;
984                            bit = (bit << 1);
985                        }
986                        add = 2 + topBit/8;
987                    } else {
988                        add = 1 + rd.nextInt(29);
989                    }
990                    break;
991                case 2: // header string
992                    add = 2 + rd.nextInt(10);
993                    break;
994                default: // uint32
995                    add = 6;
996                }
997                if (index + add >= len) break;
998
999                // fill field value
1000                switch (type) {
1001                case 0: // header short int
1002                    arr[index] = (byte) (0x80 | rd.nextInt(128));
1003                    break;
1004                case 1: // header long int
1005                    if (addAppid) {
1006                        addAppid = false;
1007                        appIdAdded = true;
1008
1009                        arr[index] = (byte) (add - 1);
1010                        tmpVal = mAppIdValue;
1011                        for (i = add; i > 1; i--) {
1012                            arr[index + i - 1] = (byte) (tmpVal & 0xff);
1013                            tmpVal = (tmpVal >> 8);
1014                        }
1015                    } else {
1016                        arr[index] = (byte) (add - 1);
1017                        for (i = 1; i < add; i++) {
1018                            arr[index + i] = (byte) rd.nextInt(255);
1019                        }
1020                    }
1021                    break;
1022                case 2:// header string
1023                    for (i = 0; i < add - 1; i++) {
1024                        tmpVal = rd.nextInt(127);
1025                        if (tmpVal < 32) tmpVal= (32 + tmpVal);
1026                        arr[index + i] = (byte) tmpVal;
1027                    }
1028                    arr[index + i] = (byte) 0x0;
1029                    break;
1030                default: // header uvarint
1031                    arr[index] = (byte) 31;
1032                    tmpVal = rd.nextInt(0x0FFFFFFF);
1033                    add = 1 + encodeUint32(tmpVal, null, index + 1);
1034                    encodeUint32(tmpVal, arr, index + 1);
1035                    break;
1036
1037                }
1038
1039                if (LOCAL_LOGV) {
1040                    Log.d(LOG_TAG, "field value index:" + index);
1041                    Log.d(LOG_TAG, "type:" + typename[type] + ", add:" + add);
1042                    if (type != 2) {
1043                        for (i = index; i< index + add; i++) {
1044                            System.out.print(Integer.toHexString(0xff & arr[i]));
1045                            System.out.print(' ');
1046                        }
1047                    } else {
1048                        System.out.print(Integer.toHexString(0xff & arr[index]));
1049                        System.out.print(' ');
1050                        String str = new String(arr, index + 1, add - 2);
1051                        for (i = 0; i < str.length(); i++) {
1052                            System.out.print(str.charAt(i));
1053                            System.out.print(' ');
1054                        }
1055                    }
1056                    System.out.print('\n');
1057                }
1058                index += add;
1059            }
1060            if (noAppId) break;
1061        }
1062
1063        Log.d(LOG_TAG, HexDump.dumpHexString(arr));
1064    }
1065
1066    /**
1067     * Generate Random WSP header with string application ID
1068     */
1069    protected void createRandomWspHeaderStrAppId(byte[] arr, Random rd, int headerStart,
1070            boolean randomStr) {
1071
1072        boolean appIdAdded = false;
1073
1074        Log.d(LOG_TAG, "random arr length:" + arr.length);
1075        String typename[] = new String[] { "short int", "long int", "string", "uint32"};
1076
1077        while (!appIdAdded) {
1078            int type;
1079            int index = headerStart;
1080            int len = arr.length;
1081            int i;
1082            boolean addAppid = false;
1083            int tmpVal = 0;
1084            int tmpVal2 = 0;
1085
1086            while (true) {
1087                int add;
1088
1089                /*
1090                 * field name
1091                 * 0: short int
1092                 * 1: long int
1093                 * 2: text
1094                 * (no uint for param value)
1095                 */
1096                type = rd.nextInt(3);
1097                switch (type) {
1098                case 0: // header short integer
1099                    if (index > 100 && !appIdAdded) addAppid = true;
1100                    add = 1;
1101                    break;
1102                case 1: // header long int
1103                    add = 1 + rd.nextInt(29);
1104                    break;
1105                default: // header string
1106                    add = 2 + rd.nextInt(10);
1107                    break;
1108                }
1109                if (index + add >= len) break;
1110
1111                // fill header name
1112                switch (type) {
1113                case 0: // header short integer
1114                    if (!addAppid) {
1115                        do {
1116                            arr[index] = (byte) (0x80 | rd.nextInt(128));
1117                        } while (arr[index] == (byte) 0xaf);
1118                    } else {
1119                        Log.d(LOG_TAG, "appId added.");
1120                        arr[index] = (byte) 0xaf;
1121                    }
1122                    break;
1123                case 1: // header long int
1124                    arr[index] = (byte) (add - 1);
1125                    tmpVal2 = 0;
1126                    for (i = 1; i < add; i++) {
1127                        tmpVal = rd.nextInt(255);
1128                        tmpVal2 = (tmpVal2 << 8) | tmpVal;
1129                        arr[index + i] = (byte) tmpVal;
1130                    }
1131                    // don't set application id
1132                    if (tmpVal2 == 0x2f) arr[index + 1]++;
1133                    break;
1134                default: // header string
1135                    for (i = 0; i < add - 1; i++) {
1136                        tmpVal = rd.nextInt(127);
1137                        if (tmpVal < 32) tmpVal= (32 + tmpVal);
1138                        arr[index + i] = (byte) tmpVal;
1139                    }
1140                    arr[index + i] = (byte) 0x0;
1141                    break;
1142                }
1143
1144                if (LOCAL_LOGV) {
1145                    Log.d(LOG_TAG, "field name index:" + index);
1146                    Log.d(LOG_TAG, "type:" + typename[type] + ", add:" + add);
1147                    if (type != 2) {
1148                        for (i = index; i < index + add; i++) {
1149                            System.out.print(Integer.toHexString(0xff & arr[i]));
1150                            System.out.print(' ');
1151                        }
1152                    } else {
1153                        System.out.print(Integer.toHexString(0xff & arr[index]));
1154                        System.out.print(' ');
1155                        String str = new String(arr, index + 1, add - 2);
1156                        for (i = 0; i < str.length(); i++) {
1157                            System.out.print(str.charAt(i));
1158                            System.out.print(' ');
1159                        }
1160                    }
1161                    System.out.print('\n');
1162                }
1163                index += add;
1164
1165
1166                /*
1167                 * field value
1168                 * 0: short int
1169                 * 1: long int
1170                 * 2: text
1171                 * 3: uint
1172                 */
1173                if (addAppid) {
1174                    type = 2;
1175                } else {
1176                    type = rd.nextInt(4);
1177                }
1178                switch (type) {
1179                case 0: // header short integer
1180                    add = 1;
1181                    break;
1182                case 1: // header long int
1183                    add = 1 + rd.nextInt(29);
1184                    break;
1185                case 2: // header string
1186                    if (addAppid) {
1187                        if (randomStr) {
1188                            add = 1 + rd.nextInt(10);
1189                            byte[] randStr= new byte[add];
1190                            for (i = 0; i < add; i++) {
1191                                tmpVal = rd.nextInt(127);
1192                                if (tmpVal < 32) tmpVal= (32 + tmpVal);
1193                                randStr[i] = (byte) tmpVal;
1194                            }
1195                            mAppIdName = new String(randStr);
1196                        }
1197                        add = mAppIdName.length() + 1;
1198                    } else {
1199                        add = 2 + rd.nextInt(10);
1200                    }
1201                    break;
1202                default: // uint32
1203                    add = 6;
1204                }
1205                if (index + add >= len) break;
1206
1207                // fill field value
1208                switch (type) {
1209                case 0: // header short int
1210                    arr[index] = (byte) (0x80 | rd.nextInt(128));
1211                    break;
1212                case 1: // header long int
1213                    arr[index] = (byte) (add - 1);
1214                    for (i = 1; i < add; i++)
1215                        arr[index + i] = (byte) rd.nextInt(255);
1216                    break;
1217                case 2:// header string
1218                    if (addAppid) {
1219                        addAppid = false;
1220                        appIdAdded = true;
1221                        for (i = 0; i < add - 1; i++) {
1222                            arr[index + i] = (byte) (mAppIdName.charAt(i));
1223                        }
1224                        Log.d(LOG_TAG, "mAppIdName added [" + mAppIdName + "]");
1225                    } else {
1226                        for (i = 0; i < add - 1; i++) {
1227                            tmpVal = rd.nextInt(127);
1228                            if (tmpVal < 32) tmpVal= (32 + tmpVal);
1229                            arr[index + i] = (byte) tmpVal;
1230                        }
1231                    }
1232                    arr[index + i] = (byte) 0x0;
1233                    break;
1234                default: // header uvarint
1235                    arr[index] = (byte) 31;
1236                    tmpVal = rd.nextInt(0x0FFFFFFF);
1237                    add = 1 + encodeUint32(tmpVal, null, index + 1);
1238                    encodeUint32(tmpVal, arr, index + 1);
1239                    break;
1240
1241                }
1242
1243                if (LOCAL_LOGV) {
1244                    Log.d(LOG_TAG, "field value index:" + index);
1245                    Log.d(LOG_TAG, "type:" + typename[type] + ", add:" + add);
1246                    if (type != 2) {
1247                        for (i = index; i < index + add; i++) {
1248                            System.out.print(Integer.toHexString(0xff & arr[i]));
1249                            System.out.print(' ');
1250                        }
1251                    } else {
1252                        System.out.print(Integer.toHexString(0xff & arr[index]));
1253                        System.out.print(' ');
1254                        String str = new String(arr, index + 1, add - 2);
1255                        for (i = 0; i < str.length(); i++) {
1256                            System.out.print(str.charAt(i));
1257                            System.out.print(' ');
1258                        }
1259                    }
1260                    System.out.print('\n');
1261                }
1262                index += add;
1263            }
1264        }
1265
1266        Log.d(LOG_TAG, "headerStart = " + headerStart + ", mAppIdName = " + mAppIdName);
1267        Log.d(LOG_TAG, HexDump.dumpHexString(arr));
1268    }
1269
1270    protected byte[] createPDU(int testNum) {
1271        byte[] array = null;
1272        // byte[] wsp = null;
1273
1274        switch (testNum) {
1275            // sample pdu
1276        case 1:
1277            byte[] array1 = {
1278                    (byte) 0x00, // TID
1279                    (byte) 0x06, // Type = wap push
1280                    (byte) 0x00, // Length to be set later.
1281
1282                    // Content-Type
1283                    (byte) 0x03, (byte) 0x02,
1284                    (byte) ((mContentTypeValue >> 8) & 0xff),
1285                    (byte) (mContentTypeValue & 0xff),
1286
1287                    // Application-id
1288                    (byte) 0xaf, (byte) 0x02,
1289                    (byte) ((mAppIdValue >> 8) & 0xff),
1290                    (byte) (mAppIdValue& 0xff)
1291            };
1292            array1[2] = (byte) (array1.length - 3);
1293            mWspHeader = array1;
1294            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length + 7;
1295            mWspHeaderLen = array1.length;
1296            break;
1297
1298            // invalid wsp header
1299        case 2:
1300            byte[] array2 = {
1301                    (byte) 0x00, // invalid data
1302            };
1303            mWspHeader = array2;
1304            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length;
1305            mWspHeaderLen = array2.length;
1306            break;
1307
1308            // random wsp header
1309        case 3:
1310            Random rd = new Random();
1311            int arrSize = 150 + rd.nextInt(100);
1312            byte[] array3 = new byte[arrSize];
1313            int hdrEncodeLen;
1314
1315            array3[0] = (byte) 0x0;
1316            array3[1] = (byte) 0x6;
1317            hdrEncodeLen = encodeUint32(array3.length, null, 2);
1318            hdrEncodeLen = encodeUint32(array3.length - hdrEncodeLen - 2, array3, 2);
1319            array3[hdrEncodeLen + 2] = (byte) 0x3;
1320            array3[hdrEncodeLen + 3] = (byte) 0x2;
1321            array3[hdrEncodeLen + 4] = (byte) ((mContentTypeValue >> 8) & 0xff);
1322            array3[hdrEncodeLen + 5] = (byte) (mContentTypeValue & 0xff);
1323            createRandomWspHeader(array3, rd, hdrEncodeLen + 6, false);
1324            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length + hdrEncodeLen + 6;
1325            mWspHeaderLen = array3.length;
1326
1327            Log.d(LOG_TAG, "mContentTypeValue = " + mContentTypeValue
1328                    + "(" + Integer.toHexString(mContentTypeValue) + ")");
1329
1330            mWspHeader = array3;
1331            break;
1332
1333            // random wsp header w/o appid
1334        case 4:
1335            rd = new Random();
1336            arrSize = 150 + rd.nextInt(100);
1337            array3 = new byte[arrSize];
1338
1339            array3[0] = (byte) 0x0;
1340            array3[1] = (byte) 0x6;
1341            hdrEncodeLen = encodeUint32(array3.length, null, 2);
1342            hdrEncodeLen = encodeUint32(array3.length - hdrEncodeLen - 2, array3, 2);
1343            array3[hdrEncodeLen + 2] = (byte) 0x3;
1344            array3[hdrEncodeLen + 3] = (byte) 0x2;
1345            array3[hdrEncodeLen + 4] = (byte) ((mContentTypeValue >> 8) & 0xff);
1346            array3[hdrEncodeLen + 5] = (byte) (mContentTypeValue & 0xff);
1347            createRandomWspHeader(array3, rd, hdrEncodeLen + 6, true);
1348            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length + hdrEncodeLen + 6;
1349            mWspHeaderLen = array3.length;
1350
1351            Log.d(LOG_TAG, "mContentTypeValue = " + mContentTypeValue
1352                    + "(" + Integer.toHexString(mContentTypeValue) + ")");
1353
1354            mWspHeader = array3;
1355            break;
1356
1357            // random wsp header w/ random appid string
1358        case 5:
1359            rd = new Random();
1360            arrSize = 150 + rd.nextInt(100);
1361            array3 = new byte[arrSize];
1362
1363            array3[0] = (byte) 0x0;
1364            array3[1] = (byte) 0x6;
1365            hdrEncodeLen = encodeUint32(array3.length, null, 2);
1366            hdrEncodeLen = encodeUint32(array3.length - hdrEncodeLen - 2, array3, 2);
1367            array3[hdrEncodeLen + 2] = (byte) 0x3;
1368            array3[hdrEncodeLen + 3] = (byte) 0x2;
1369            array3[hdrEncodeLen + 4] = (byte) ((mContentTypeValue >> 8) & 0xff);
1370            array3[hdrEncodeLen + 5] = (byte) (mContentTypeValue & 0xff);
1371            createRandomWspHeaderStrAppId(array3, rd, hdrEncodeLen + 6, true);
1372            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length + hdrEncodeLen + 6;
1373            mWspHeaderLen = array3.length;
1374
1375            Log.d(LOG_TAG, "mContentTypeValue = " + mContentTypeValue
1376                    + "(" + Integer.toHexString(mContentTypeValue) + ")");
1377
1378            mWspHeader = array3;
1379            break;
1380
1381            // random wsp header w/ OMA appid string
1382        case 6:
1383            rd = new Random();
1384            arrSize = 150 + rd.nextInt(100);
1385            array3 = new byte[arrSize];
1386
1387            array3[0] = (byte) 0x0;
1388            array3[1] = (byte) 0x6;
1389            hdrEncodeLen = encodeUint32(array3.length, null, 2);
1390            hdrEncodeLen = encodeUint32(array3.length - hdrEncodeLen - 2, array3, 2);
1391            array3[hdrEncodeLen + 2] = (byte) 0x3;
1392            array3[hdrEncodeLen + 3] = (byte) 0x2;
1393            array3[hdrEncodeLen + 4] = (byte) ((mContentTypeValue >> 8) & 0xff);
1394            array3[hdrEncodeLen + 5] = (byte) (mContentTypeValue & 0xff);
1395            createRandomWspHeaderStrAppId(array3, rd, hdrEncodeLen + 6, false);
1396            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length + hdrEncodeLen + 6;
1397            mWspHeaderLen = array3.length;
1398
1399            Log.d(LOG_TAG, "mContentTypeValue = " + mContentTypeValue
1400                    + "(" + Integer.toHexString(mContentTypeValue) + ")");
1401
1402            mWspHeader = array3;
1403            break;
1404
1405            // random wsp header w/ OMA content type
1406        case 7:
1407            rd = new Random();
1408            arrSize = 150 + rd.nextInt(100);
1409            array3 = new byte[arrSize];
1410
1411            array3[0] = (byte) 0x0;
1412            array3[1] = (byte) 0x6;
1413            hdrEncodeLen = encodeUint32(array3.length, null, 2);
1414            hdrEncodeLen = encodeUint32(array3.length - hdrEncodeLen - 2, array3, 2);
1415
1416            // encode content type
1417            int contentLen = mContentTypeName.length();
1418            int next = 2 + hdrEncodeLen;
1419            mWspContentTypeStart = mGsmHeader.length + mUserDataHeader.length + next;
1420            // next += encodeUint32(contentLen, array3, next);
1421            int i;
1422            Log.d(LOG_TAG, "mContentTypeName = " + mContentTypeName
1423                    + ", contentLen = " + contentLen);
1424
1425            for (i = 0; i < contentLen; i++) {
1426                array3[next + i] = (byte) mContentTypeName.charAt(i);
1427            }
1428            array3[next + i] = (byte) 0x0;
1429
1430            createRandomWspHeader(array3, rd, next + contentLen + 1, false);
1431            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length
1432                    + next + contentLen + 1;
1433            mWspHeaderLen = array3.length;
1434
1435            mWspHeader = array3;
1436            break;
1437
1438            // random wsp header w/ OMA content type, OMA app ID
1439        case 8:
1440            rd = new Random();
1441            arrSize = 150 + rd.nextInt(100);
1442            array3 = new byte[arrSize];
1443
1444            array3[0] = (byte) 0x0;
1445            array3[1] = (byte) 0x6;
1446            hdrEncodeLen = encodeUint32(array3.length, null, 2);
1447            hdrEncodeLen = encodeUint32(array3.length - hdrEncodeLen - 2, array3, 2);
1448
1449            // encode content type
1450            contentLen = mContentTypeName.length();
1451            next = 2 + hdrEncodeLen;
1452            mWspContentTypeStart = mGsmHeader.length + mUserDataHeader.length + next;
1453            // next += encodeUint32(contentLen, array3, next);
1454            Log.d(LOG_TAG, "mContentTypeName = " + mContentTypeName
1455                    + ", contentLen = " + contentLen);
1456
1457            for (i = 0; i < contentLen; i++) {
1458                array3[next + i] = (byte) mContentTypeName.charAt(i);
1459            }
1460            array3[next + i] = (byte) 0x0;
1461
1462            createRandomWspHeaderStrAppId(array3, rd, next + contentLen + 1, false);
1463            mWspHeaderStart = mGsmHeader.length + mUserDataHeader.length
1464                    + next + contentLen + 1;
1465            mWspHeaderLen = array3.length;
1466
1467            mWspHeader = array3;
1468            break;
1469
1470        default:
1471            return null;
1472        }
1473        array = new byte[mGsmHeader.length + mUserDataHeader.length + mWspHeader.length
1474                + mMessageBody.length];
1475        System.arraycopy(mGsmHeader, 0, array, 0, mGsmHeader.length);
1476        System.arraycopy(mUserDataHeader, 0, array,
1477                mGsmHeader.length, mUserDataHeader.length);
1478        System.arraycopy(mWspHeader, 0, array,
1479                mGsmHeader.length + mUserDataHeader.length, mWspHeader.length);
1480        System.arraycopy(mMessageBody, 0, array,
1481                mGsmHeader.length + mUserDataHeader.length + mWspHeader.length,
1482                mMessageBody.length);
1483        return array;
1484
1485    }
1486
1487    Intent createIntent(int pduType, int tranId) {
1488        Intent intent = new Intent();
1489        intent.putExtra("transactionId", tranId);
1490        intent.putExtra("pduType", pduType);
1491        intent.putExtra("header", mGsmHeader);
1492        intent.putExtra("data", mMessageBody);
1493        // intent.putExtra("contentTypeParameters", null);
1494        return intent;
1495    }
1496
1497    /**
1498     * Message processing test, start activity
1499     */
1500    public void testProcessMsg1() {
1501        byte[] pdu = createPDU(1);
1502        int headerLen = pdu.length -
1503                (mGsmHeader.length + mUserDataHeader.length + mMessageBody.length);
1504        int pduType = 6;
1505        int tranId = 0;
1506        String originalPackageName = mPackageName;
1507        String originalClassName = mClassName;
1508
1509        try {
1510
1511            mClassName = "com.android.smspush.unitTests.ReceiverActivity";
1512
1513            // set up data
1514            IWapPushManager iwapman = getInterface();
1515            iwapman.addPackage(Integer.toString(mAppIdValue),
1516                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1517                    WapPushManagerParams.APP_TYPE_ACTIVITY, false, false);
1518
1519            assertTrue((iwapman.processMessage(
1520                    Integer.toString(mAppIdValue),
1521                    Integer.toString(mContentTypeValue),
1522                    createIntent(pduType, tranId))
1523                    & WapPushManagerParams.MESSAGE_HANDLED) ==
1524                    WapPushManagerParams.MESSAGE_HANDLED);
1525
1526            // clean up data
1527            iwapman.deletePackage(Integer.toString(mAppIdValue),
1528                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1529
1530        } catch (RemoteException e) {
1531            assertTrue(false);
1532        }
1533
1534        mPackageName = originalPackageName;
1535        mClassName = originalClassName;
1536    }
1537
1538    /**
1539     * Message processing test, start service
1540     */
1541    public void testProcessMsg2() {
1542        byte[] pdu = createPDU(1);
1543        int headerLen = pdu.length - (mGsmHeader.length +
1544                mUserDataHeader.length + mMessageBody.length);
1545        int pduType = 6;
1546        int tranId = 0;
1547        String originalPackageName = mPackageName;
1548        String originalClassName = mClassName;
1549
1550        try {
1551
1552            mClassName = "com.android.smspush.unitTests.ReceiverService";
1553
1554            // set up data
1555            IWapPushManager iwapman = getInterface();
1556
1557            iwapman.addPackage(Integer.toString(mAppIdValue),
1558                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1559                    WapPushManagerParams.APP_TYPE_SERVICE, false, false);
1560
1561            assertTrue((iwapman.processMessage(
1562                    Integer.toString(mAppIdValue),
1563                    Integer.toString(mContentTypeValue),
1564                    createIntent(pduType, tranId))
1565                    & WapPushManagerParams.MESSAGE_HANDLED) ==
1566                    WapPushManagerParams.MESSAGE_HANDLED);
1567
1568            // clean up data
1569            iwapman.deletePackage(Integer.toString(mAppIdValue),
1570                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1571
1572        } catch (RemoteException e) {
1573            assertTrue(false);
1574        }
1575
1576        mPackageName = originalPackageName;
1577        mClassName = originalClassName;
1578    }
1579
1580    /**
1581     * Message processing test, no signature
1582     */
1583    public void testProcessMsg3() {
1584        byte[] pdu = createPDU(1);
1585        int headerLen = pdu.length -
1586                (mGsmHeader.length + mUserDataHeader.length + mMessageBody.length);
1587        int pduType = 6;
1588        int tranId = 0;
1589        String originalPackageName = mPackageName;
1590        String originalClassName = mClassName;
1591
1592        try {
1593
1594            mPackageName = "com.android.development";
1595            mClassName = "com.android.development.Development";
1596
1597            // set up data
1598            IWapPushManager iwapman = getInterface();
1599
1600            iwapman.addPackage(Integer.toString(mAppIdValue),
1601                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1602                    WapPushManagerParams.APP_TYPE_SERVICE, true, false);
1603
1604            assertFalse((iwapman.processMessage(
1605                    Integer.toString(mAppIdValue),
1606                    Integer.toString(mContentTypeValue),
1607                    createIntent(pduType, tranId))
1608                    & WapPushManagerParams.MESSAGE_HANDLED) ==
1609                    WapPushManagerParams.MESSAGE_HANDLED);
1610
1611            // clean up data
1612            iwapman.deletePackage(Integer.toString(mAppIdValue),
1613                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1614
1615        } catch (RemoteException e) {
1616            assertTrue(false);
1617        }
1618
1619        mPackageName = originalPackageName;
1620        mClassName = originalClassName;
1621    }
1622
1623    IDataVerify getVerifyInterface() {
1624        while (mIVerify == null) {
1625            // wait for the activity receive data.
1626            try {
1627                Thread.sleep(TIME_WAIT);
1628            } catch (InterruptedException e) {}
1629        }
1630        return mIVerify;
1631    }
1632
1633
1634    /**
1635     * Message processing test, received body data verification test
1636     */
1637    public void testProcessMsg4() {
1638        byte[] originalMessageBody = mMessageBody;
1639        mMessageBody = new byte[] {
1640                (byte) 0xee,
1641                (byte) 0xff,
1642                (byte) 0xee,
1643                (byte) 0xff,
1644                (byte) 0xee,
1645                (byte) 0xff,
1646                (byte) 0xee,
1647                (byte) 0xff,
1648                (byte) 0xee,
1649                (byte) 0xff,
1650                (byte) 0xee,
1651                (byte) 0xff,
1652        };
1653
1654        byte[] pdu = createPDU(1);
1655        int headerLen = pdu.length -
1656                (mGsmHeader.length + mUserDataHeader.length + mMessageBody.length);
1657        int pduType = 6;
1658        int tranId = 0;
1659        String originalPackageName = mPackageName;
1660        String originalClassName = mClassName;
1661
1662        try {
1663            IWapPushManager iwapman = getInterface();
1664            IDataVerify dataverify = getVerifyInterface();
1665
1666            dataverify.resetData();
1667
1668            // set up data
1669            mClassName = "com.android.smspush.unitTests.ReceiverActivity";
1670            iwapman.addPackage(Integer.toString(mAppIdValue),
1671                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1672                    WapPushManagerParams.APP_TYPE_ACTIVITY, false, false);
1673
1674            iwapman.processMessage(
1675                    Integer.toString(mAppIdValue),
1676                    Integer.toString(mContentTypeValue),
1677                    createIntent(pduType, tranId));
1678
1679            // clean up data
1680            iwapman.deletePackage(Integer.toString(mAppIdValue),
1681                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1682
1683            assertTrue(dataverify.verifyData(mMessageBody));
1684
1685            // set up data
1686            dataverify.resetData();
1687            mClassName = "com.android.smspush.unitTests.ReceiverService";
1688            mMessageBody = new byte[] {
1689                    (byte) 0xaa,
1690                    (byte) 0xbb,
1691                    (byte) 0x11,
1692                    (byte) 0x22,
1693                    (byte) 0xaa,
1694                    (byte) 0xbb,
1695                    (byte) 0x11,
1696                    (byte) 0x22,
1697                    (byte) 0xaa,
1698                    (byte) 0xbb,
1699                    (byte) 0x11,
1700                    (byte) 0x22,
1701                    (byte) 0xaa,
1702                    (byte) 0xbb,
1703                    (byte) 0x11,
1704                    (byte) 0x22,
1705                    (byte) 0xaa,
1706                    (byte) 0xbb,
1707                    (byte) 0x11,
1708                    (byte) 0x22,
1709                    (byte) 0xaa,
1710                    (byte) 0xbb,
1711                    (byte) 0x11,
1712                    (byte) 0x22,
1713            };
1714            pdu = createPDU(1);
1715            iwapman.addPackage(Integer.toString(mAppIdValue),
1716                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1717                    WapPushManagerParams.APP_TYPE_SERVICE, false, false);
1718
1719            iwapman.processMessage(
1720                    Integer.toString(mAppIdValue),
1721                    Integer.toString(mContentTypeValue),
1722                    createIntent(pduType, tranId));
1723
1724            // clean up data
1725            iwapman.deletePackage(Integer.toString(mAppIdValue),
1726                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1727
1728            // Log.d(LOG_TAG, HexDump.dumpHexString(mMessageBody));
1729            assertTrue(dataverify.verifyData(mMessageBody));
1730        } catch (RemoteException e) {
1731            assertTrue(false);
1732        }
1733
1734        mPackageName = originalPackageName;
1735        mClassName = originalClassName;
1736        mMessageBody = originalMessageBody;
1737    }
1738
1739    /**
1740     * Message processing test, send invalid sms data
1741     */
1742    public void testProcessMsg5() {
1743        byte[] pdu = createPDU(2);
1744        int headerLen = pdu.length -
1745                (mGsmHeader.length + mUserDataHeader.length + mMessageBody.length);
1746        int pduType = 6;
1747        int tranId = 0;
1748        String originalPackageName = mPackageName;
1749        String originalClassName = mClassName;
1750
1751        try {
1752
1753            mClassName = "com.android.smspush.unitTests.ReceiverActivity";
1754
1755            // set up data
1756            IWapPushManager iwapman = getInterface();
1757            iwapman.addPackage(Integer.toString(mAppIdValue),
1758                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1759                    WapPushManagerParams.APP_TYPE_ACTIVITY, false, false);
1760
1761            assertTrue((iwapman.processMessage(
1762                    Integer.toString(mAppIdValue),
1763                    Integer.toString(mContentTypeValue),
1764                    createIntent(pduType, tranId))
1765                    & WapPushManagerParams.MESSAGE_HANDLED) ==
1766                    WapPushManagerParams.MESSAGE_HANDLED);
1767
1768            // clean up data
1769            iwapman.deletePackage(Integer.toString(mAppIdValue),
1770                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1771
1772        } catch (RemoteException e) {
1773            assertTrue(false);
1774        }
1775
1776        mPackageName = originalPackageName;
1777        mClassName = originalClassName;
1778    }
1779
1780    /**
1781     * Message processing test, no receiver application
1782     */
1783    public void testProcessMsg6() {
1784        byte[] pdu = createPDU(1);
1785        int headerLen = pdu.length -
1786                (mGsmHeader.length + mUserDataHeader.length + mMessageBody.length);
1787        int pduType = 6;
1788        int tranId = 0;
1789        String originalPackageName = mPackageName;
1790        String originalClassName = mClassName;
1791
1792        try {
1793
1794            mClassName = "com.android.smspush.unitTests.NoReceiver";
1795
1796            // set up data
1797            IWapPushManager iwapman = getInterface();
1798            iwapman.addPackage(Integer.toString(mAppIdValue),
1799                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1800                    WapPushManagerParams.APP_TYPE_ACTIVITY, false, false);
1801
1802            assertFalse((iwapman.processMessage(
1803                    Integer.toString(mAppIdValue),
1804                    Integer.toString(mContentTypeValue),
1805                    createIntent(pduType, tranId))
1806                    & WapPushManagerParams.MESSAGE_HANDLED) ==
1807                    WapPushManagerParams.MESSAGE_HANDLED);
1808
1809            // clean up data
1810            iwapman.deletePackage(Integer.toString(mAppIdValue),
1811                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1812
1813            // set up data
1814            iwapman.addPackage(Integer.toString(mAppIdValue),
1815                    Integer.toString(mContentTypeValue), mPackageName, mClassName,
1816                    WapPushManagerParams.APP_TYPE_SERVICE, false, false);
1817
1818            assertFalse((iwapman.processMessage(
1819                    Integer.toString(mAppIdValue),
1820                    Integer.toString(mContentTypeValue),
1821                    createIntent(pduType, tranId))
1822                    & WapPushManagerParams.MESSAGE_HANDLED) ==
1823                    WapPushManagerParams.MESSAGE_HANDLED);
1824
1825            // clean up data
1826            iwapman.deletePackage(Integer.toString(mAppIdValue),
1827                    Integer.toString(mContentTypeValue), mPackageName, mClassName);
1828
1829        } catch (RemoteException e) {
1830            assertTrue(false);
1831        }
1832
1833        mPackageName = originalPackageName;
1834        mClassName = originalClassName;
1835    }
1836
1837    /**
1838     * WspTypeDecoder test, normal pdu
1839     */
1840    public void testDecoder1() {
1841        boolean res;
1842        int originalAppIdValue = mAppIdValue;
1843        Random rd = new Random();
1844
1845        for (int i = 0; i < 10; i++) {
1846            mAppIdValue = rd.nextInt(0xFFFF);
1847            byte[] pdu = createPDU(1);
1848            WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
1849
1850            res = pduDecoder.seekXWapApplicationId(mWspHeaderStart,
1851                    mWspHeaderStart + mWspHeaderLen - 1);
1852            assertTrue(res);
1853
1854            int index = (int) pduDecoder.getValue32();
1855            res = pduDecoder.decodeXWapApplicationId(index);
1856            assertTrue(res);
1857
1858            Log.d(LOG_TAG, "mAppIdValue: " + mAppIdValue
1859                    + ", val: " + pduDecoder.getValue32());
1860            assertTrue(mAppIdValue == (int) pduDecoder.getValue32());
1861        }
1862
1863        mAppIdValue = originalAppIdValue;
1864    }
1865
1866    /**
1867     * WspTypeDecoder test, no header
1868     */
1869    public void testDecoder2() {
1870        boolean res;
1871        int originalAppIdValue = mAppIdValue;
1872        Random rd = new Random();
1873
1874        mAppIdValue = rd.nextInt(0xFFFF);
1875        byte[] pdu = createPDU(2);
1876        WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
1877
1878        res = pduDecoder.seekXWapApplicationId(mWspHeaderStart,
1879                mWspHeaderStart + mWspHeaderLen - 1);
1880        assertFalse(res);
1881
1882        mAppIdValue = originalAppIdValue;
1883    }
1884
1885    /**
1886     * WspTypeDecoder test, decode appid test
1887     */
1888    public void testDecoder3() {
1889        boolean res;
1890        int originalAppIdValue = mAppIdValue;
1891        int originalContentTypeValue  = mContentTypeValue;
1892        Random rd = new Random();
1893
1894        for (int i = 0; i < 100; i++) {
1895            mAppIdValue = rd.nextInt(0x0FFFFFFF);
1896            mContentTypeValue = rd.nextInt(0x0FFF);
1897            byte[] pdu = createPDU(3);
1898            WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
1899
1900            res = pduDecoder.seekXWapApplicationId(mWspHeaderStart,
1901                    mWspHeaderStart + mWspHeaderLen - 1);
1902            assertTrue(res);
1903
1904            int index = (int) pduDecoder.getValue32();
1905            res = pduDecoder.decodeXWapApplicationId(index);
1906            assertTrue(res);
1907
1908            Log.d(LOG_TAG, "mAppIdValue: " + mAppIdValue
1909                    + ", val: " + pduDecoder.getValue32());
1910            assertTrue(mAppIdValue == (int) pduDecoder.getValue32());
1911        }
1912
1913        mAppIdValue = originalAppIdValue;
1914        mContentTypeValue = originalContentTypeValue;
1915    }
1916
1917    /*
1918      public void testEnc() {
1919      byte[] arr = new byte[20];
1920      int index = 0;
1921      index += encodeUint32(0x87a5, arr, index);
1922      index += encodeUint32(0x1, arr, index);
1923      index += encodeUint32(0x9b, arr, index);
1924      index += encodeUint32(0x10, arr, index);
1925      index += encodeUint32(0xe0887, arr, index);
1926      index += encodeUint32(0x791a23d0, arr, index);
1927
1928      Log.d(LOG_TAG, HexDump.dumpHexString(arr));
1929      }
1930    */
1931
1932    /**
1933     * WspTypeDecoder test, no appid test
1934     */
1935    public void testDecoder4() {
1936        boolean res;
1937        int originalAppIdValue = mAppIdValue;
1938        int originalContentTypeValue  = mContentTypeValue;
1939        Random rd = new Random();
1940
1941        for (int i = 0; i < 100; i++) {
1942            mAppIdValue = rd.nextInt(0x0FFFFFFF);
1943            mContentTypeValue = rd.nextInt(0x0FFF);
1944            byte[] pdu = createPDU(4);
1945            WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
1946
1947            res = pduDecoder.seekXWapApplicationId(mWspHeaderStart,
1948                    mWspHeaderStart + mWspHeaderLen - 1);
1949            assertFalse(res);
1950
1951        }
1952
1953        mAppIdValue = originalAppIdValue;
1954        mContentTypeValue = originalContentTypeValue;
1955    }
1956
1957    /**
1958     * WspTypeDecoder test, decode string appid test
1959     */
1960    public void testDecoder5() {
1961        boolean res;
1962        String originalAppIdName = mAppIdName;
1963        int originalContentTypeValue  = mContentTypeValue;
1964        Random rd = new Random();
1965
1966        for (int i = 0; i < 10; i++) {
1967            mAppIdValue = rd.nextInt(0x0FFFFFFF);
1968            mContentTypeValue = rd.nextInt(0x0FFF);
1969            byte[] pdu = createPDU(5);
1970            WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
1971
1972            res = pduDecoder.seekXWapApplicationId(mWspHeaderStart,
1973                    mWspHeaderStart + mWspHeaderLen - 1);
1974            assertTrue(res);
1975
1976            int index = (int) pduDecoder.getValue32();
1977            res = pduDecoder.decodeXWapApplicationId(index);
1978            assertTrue(res);
1979
1980            Log.d(LOG_TAG, "mAppIdValue: [" + mAppIdName + "], val: ["
1981                    + pduDecoder.getValueString() + "]");
1982            assertTrue(mAppIdName.equals(pduDecoder.getValueString()));
1983        }
1984
1985        mAppIdName = originalAppIdName;
1986        mContentTypeValue = originalContentTypeValue;
1987    }
1988
1989    /**
1990     * WspTypeDecoder test, decode string appid test
1991     */
1992    public void testDecoder6() {
1993        boolean res;
1994        String originalAppIdName = mAppIdName;
1995        int originalContentTypeValue  = mContentTypeValue;
1996        Random rd = new Random();
1997
1998        for (int i = 0; i < OMA_APPLICATION_ID_NAMES.length; i++) {
1999            mAppIdName = OMA_APPLICATION_ID_NAMES[i];
2000            mContentTypeValue = rd.nextInt(0x0FFF);
2001            byte[] pdu = createPDU(6);
2002            WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
2003
2004            res = pduDecoder.seekXWapApplicationId(mWspHeaderStart,
2005                    mWspHeaderStart + mWspHeaderLen - 1);
2006            assertTrue(res);
2007
2008            int index = (int) pduDecoder.getValue32();
2009            res = pduDecoder.decodeXWapApplicationId(index);
2010            assertTrue(res);
2011
2012            Log.d(LOG_TAG, "mAppIdValue: [" + mAppIdName + "], val: ["
2013                    + pduDecoder.getValueString() + "]");
2014            assertTrue(mAppIdName.equals(pduDecoder.getValueString()));
2015        }
2016
2017        mAppIdName = originalAppIdName;
2018        mContentTypeValue = originalContentTypeValue;
2019    }
2020
2021    /**
2022     * WspTypeDecoder test, decode OMA content type
2023     */
2024    public void testDecoder7() {
2025        boolean res;
2026        String originalAppIdName = mAppIdName;
2027        int originalContentTypeValue  = mContentTypeValue;
2028        Random rd = new Random();
2029
2030        for (int i = 0; i < OMA_CONTENT_TYPE_NAMES.length; i++) {
2031            mContentTypeName = OMA_CONTENT_TYPE_NAMES[i];
2032            byte[] pdu = createPDU(7);
2033            WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
2034
2035            res = pduDecoder.decodeContentType(mWspContentTypeStart);
2036            assertTrue(res);
2037
2038            Log.d(LOG_TAG, "mContentTypeName: [" + mContentTypeName + "], val: ["
2039                    + pduDecoder.getValueString() + "]");
2040            assertTrue(mContentTypeName.equals(pduDecoder.getValueString()));
2041        }
2042
2043        mAppIdName = originalAppIdName;
2044        mContentTypeValue = originalContentTypeValue;
2045    }
2046
2047
2048    /**
2049     * Copied from WapPushOverSms.
2050     * The code flow is not changed from the original.
2051     */
2052    public int dispatchWapPdu(byte[] pdu, IWapPushManager wapPushMan) {
2053
2054        if (Config.DEBUG) Log.d(LOG_TAG, "Rx: " + IccUtils.bytesToHexString(pdu));
2055
2056        int index = 0;
2057        int transactionId = pdu[index++] & 0xFF;
2058        int pduType = pdu[index++] & 0xFF;
2059        int headerLength = 0;
2060
2061        if ((pduType != WspTypeDecoder.PDU_TYPE_PUSH) &&
2062                (pduType != WspTypeDecoder.PDU_TYPE_CONFIRMED_PUSH)) {
2063            if (Config.DEBUG) Log.w(LOG_TAG, "Received non-PUSH WAP PDU. Type = " + pduType);
2064            return Intents.RESULT_SMS_HANDLED;
2065        }
2066
2067        WspTypeDecoder pduDecoder = new WspTypeDecoder(pdu);
2068
2069        /**
2070         * Parse HeaderLen(unsigned integer).
2071         * From wap-230-wsp-20010705-a section 8.1.2
2072         * The maximum size of a uintvar is 32 bits.
2073         * So it will be encoded in no more than 5 octets.
2074         */
2075        if (pduDecoder.decodeUintvarInteger(index) == false) {
2076            if (Config.DEBUG) Log.w(LOG_TAG, "Received PDU. Header Length error.");
2077            return Intents.RESULT_SMS_GENERIC_ERROR;
2078        }
2079        headerLength = (int) pduDecoder.getValue32();
2080        index += pduDecoder.getDecodedDataLength();
2081
2082        int headerStartIndex = index;
2083
2084        /**
2085         * Parse Content-Type.
2086         * From wap-230-wsp-20010705-a section 8.4.2.24
2087         *
2088         * Content-type-value = Constrained-media | Content-general-form
2089         * Content-general-form = Value-length Media-type
2090         * Media-type = (Well-known-media | Extension-Media) *(Parameter)
2091         * Value-length = Short-length | (Length-quote Length)
2092         * Short-length = <Any octet 0-30>   (octet <= WAP_PDU_SHORT_LENGTH_MAX)
2093         * Length-quote = <Octet 31>         (WAP_PDU_LENGTH_QUOTE)
2094         * Length = Uintvar-integer
2095         */
2096        if (pduDecoder.decodeContentType(index) == false) {
2097            if (Config.DEBUG) Log.w(LOG_TAG, "Received PDU. Header Content-Type error.");
2098            return Intents.RESULT_SMS_GENERIC_ERROR;
2099        }
2100
2101        String mimeType = pduDecoder.getValueString();
2102        long binaryContentType = pduDecoder.getValue32();
2103        index += pduDecoder.getDecodedDataLength();
2104
2105        byte[] header = new byte[headerLength];
2106        System.arraycopy(pdu, headerStartIndex, header, 0, header.length);
2107
2108        byte[] intentData;
2109
2110        if (mimeType != null && mimeType.equals(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO)) {
2111            intentData = pdu;
2112        } else {
2113            int dataIndex = headerStartIndex + headerLength;
2114            intentData = new byte[pdu.length - dataIndex];
2115            System.arraycopy(pdu, dataIndex, intentData, 0, intentData.length);
2116        }
2117
2118        /**
2119         * Seek for application ID field in WSP header.
2120         * If application ID is found, WapPushManager substitute the message
2121         * processing. Since WapPushManager is optional module, if WapPushManager
2122         * is not found, legacy message processing will be continued.
2123         */
2124        if (pduDecoder.seekXWapApplicationId(index, index + headerLength - 1)) {
2125            index = (int) pduDecoder.getValue32();
2126            pduDecoder.decodeXWapApplicationId(index);
2127            String wapAppId = pduDecoder.getValueString();
2128            if (wapAppId == null) {
2129                wapAppId = Integer.toString((int) pduDecoder.getValue32());
2130            }
2131
2132            String contentType = ((mimeType == null) ?
2133                    Long.toString(binaryContentType) : mimeType);
2134            if (Config.DEBUG) Log.v(LOG_TAG, "appid found: " + wapAppId + ":" + contentType);
2135
2136            try {
2137                boolean processFurther = true;
2138                // IWapPushManager wapPushMan = mWapConn.getWapPushManager();
2139                if (wapPushMan == null) {
2140                    if (Config.DEBUG) Log.w(LOG_TAG, "wap push manager not found!");
2141                } else {
2142                    Intent intent = new Intent();
2143                    intent.putExtra("transactionId", transactionId);
2144                    intent.putExtra("pduType", pduType);
2145                    intent.putExtra("header", header);
2146                    intent.putExtra("data", intentData);
2147                    intent.putExtra("contentTypeParameters",
2148                            pduDecoder.getContentParameters());
2149
2150                    int procRet = wapPushMan.processMessage(wapAppId, contentType, intent);
2151                    if (Config.DEBUG) Log.v(LOG_TAG, "procRet:" + procRet);
2152                    if ((procRet & WapPushManagerParams.MESSAGE_HANDLED) > 0
2153                            && (procRet & WapPushManagerParams.FURTHER_PROCESSING) == 0) {
2154                        processFurther = false;
2155                    }
2156                }
2157                if (!processFurther) {
2158                    return Intents.RESULT_SMS_HANDLED;
2159                }
2160            } catch (RemoteException e) {
2161                if (Config.DEBUG) Log.w(LOG_TAG, "remote func failed...");
2162            }
2163        }
2164        if (Config.DEBUG) Log.v(LOG_TAG, "fall back to existing handler");
2165
2166        return Activity.RESULT_OK;
2167    }
2168
2169    protected byte[] retrieveWspBody() {
2170        byte[] array = new byte[mWspHeader.length + mMessageBody.length];
2171
2172        System.arraycopy(mWspHeader, 0, array, 0, mWspHeader.length);
2173        System.arraycopy(mMessageBody, 0, array, mWspHeader.length, mMessageBody.length);
2174        return array;
2175    }
2176
2177    protected String getContentTypeName(int ctypeVal) {
2178        int i;
2179
2180        for (i = 0; i < OMA_CONTENT_TYPE_VALUES.length; i++) {
2181            if (ctypeVal == OMA_CONTENT_TYPE_VALUES[i]) {
2182                return OMA_CONTENT_TYPE_NAMES[i];
2183            }
2184        }
2185        return null;
2186    }
2187
2188    protected boolean isContentTypeMapped(int ctypeVal) {
2189        int i;
2190
2191        for (i = 0; i < OMA_CONTENT_TYPE_VALUES.length; i++) {
2192            if (ctypeVal == OMA_CONTENT_TYPE_VALUES[i]) return true;
2193        }
2194        return false;
2195    }
2196
2197    /**
2198     * Integration test 1, simple case
2199     */
2200    public void testIntegration1() {
2201        boolean res;
2202        int originalAppIdValue = mAppIdValue;
2203        int originalContentTypeValue  = mContentTypeValue;
2204        String originalAppIdName = mAppIdName;
2205        String originalContentTypeName = mContentTypeName;
2206        String originalClassName = mClassName;
2207        byte[] originalMessageBody = mMessageBody;
2208        Random rd = new Random();
2209
2210        mMessageBody = new byte[100 + rd.nextInt(100)];
2211        rd.nextBytes(mMessageBody);
2212
2213        byte[] pdu = createPDU(1);
2214        byte[] wappushPdu = retrieveWspBody();
2215
2216
2217        mClassName = "com.android.smspush.unitTests.ReceiverActivity";
2218        // Phone dummy = new DummyPhone(getContext());
2219        // Phone gsm = PhoneFactory.getGsmPhone();
2220        // GSMPhone gsm = new GSMPhone(getContext(), new SimulatedCommands(), null, true);
2221        // WapPushOverSms dispatcher = new WapPushOverSms(dummy, null);
2222
2223        try {
2224            // set up data
2225            IWapPushManager iwapman = getInterface();
2226            IDataVerify dataverify = getVerifyInterface();
2227
2228            dataverify.resetData();
2229
2230            if (isContentTypeMapped(mContentTypeValue)) {
2231                // content type is mapped
2232                mContentTypeName = getContentTypeName(mContentTypeValue);
2233                Log.d(LOG_TAG, "mContentTypeValue mapping "
2234                        + mContentTypeName + ":" + mContentTypeValue);
2235            } else {
2236                mContentTypeName = Integer.toString(mContentTypeValue);
2237            }
2238            iwapman.addPackage(Integer.toString(mAppIdValue),
2239                    mContentTypeName, mPackageName, mClassName,
2240                    WapPushManagerParams.APP_TYPE_ACTIVITY, false, false);
2241
2242            dispatchWapPdu(wappushPdu, iwapman);
2243
2244            // clean up data
2245            iwapman.deletePackage(Integer.toString(mAppIdValue),
2246                    mContentTypeName, mPackageName, mClassName);
2247
2248            assertTrue(dataverify.verifyData(mMessageBody));
2249        } catch (RemoteException e) {
2250        }
2251
2252
2253        mClassName = originalClassName;
2254        mAppIdName = originalAppIdName;
2255        mContentTypeName = originalContentTypeName;
2256        mAppIdValue = originalAppIdValue;
2257        mContentTypeValue = originalContentTypeValue;
2258        mMessageBody = originalMessageBody;
2259    }
2260
2261    /**
2262     * Integration test 2, random mAppIdValue(int), all OMA content type
2263     */
2264    public void testIntegration2() {
2265        boolean res;
2266        int originalAppIdValue = mAppIdValue;
2267        int originalContentTypeValue  = mContentTypeValue;
2268        String originalAppIdName = mAppIdName;
2269        String originalContentTypeName = mContentTypeName;
2270        String originalClassName = mClassName;
2271        byte[] originalMessageBody = mMessageBody;
2272        Random rd = new Random();
2273
2274        IWapPushManager iwapman = getInterface();
2275        IDataVerify dataverify = getVerifyInterface();
2276        mClassName = "com.android.smspush.unitTests.ReceiverActivity";
2277
2278        for (int i = 0; i < OMA_CONTENT_TYPE_NAMES.length; i++) {
2279            mContentTypeName = OMA_CONTENT_TYPE_NAMES[i];
2280            mAppIdValue = rd.nextInt(0x0FFFFFFF);
2281
2282            mMessageBody = new byte[100 + rd.nextInt(100)];
2283            rd.nextBytes(mMessageBody);
2284
2285            byte[] pdu = createPDU(7);
2286            byte[] wappushPdu = retrieveWspBody();
2287
2288            try {
2289                dataverify.resetData();
2290                // set up data
2291                iwapman.addPackage(Integer.toString(mAppIdValue),
2292                        mContentTypeName, mPackageName, mClassName,
2293                        WapPushManagerParams.APP_TYPE_ACTIVITY, false, false);
2294
2295                dispatchWapPdu(wappushPdu, iwapman);
2296
2297                // clean up data
2298                iwapman.deletePackage(Integer.toString(mAppIdValue),
2299                        mContentTypeName, mPackageName, mClassName);
2300
2301                if (mContentTypeName.equals(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO)) {
2302                    assertTrue(dataverify.verifyData(wappushPdu));
2303                } else {
2304                    assertTrue(dataverify.verifyData(mMessageBody));
2305                }
2306            } catch (RemoteException e) {
2307            }
2308        }
2309
2310
2311        mClassName = originalClassName;
2312        mAppIdName = originalAppIdName;
2313        mContentTypeName = originalContentTypeName;
2314        mAppIdValue = originalAppIdValue;
2315        mContentTypeValue = originalContentTypeValue;
2316        mMessageBody = originalMessageBody;
2317    }
2318
2319    /**
2320     * Integration test 3, iterate OmaApplication ID, random binary content type
2321     */
2322    public void testIntegration3() {
2323        boolean res;
2324        int originalAppIdValue = mAppIdValue;
2325        int originalContentTypeValue  = mContentTypeValue;
2326        String originalAppIdName = mAppIdName;
2327        String originalContentTypeName = mContentTypeName;
2328        String originalClassName = mClassName;
2329        byte[] originalMessageBody = mMessageBody;
2330        Random rd = new Random();
2331
2332        IWapPushManager iwapman = getInterface();
2333        IDataVerify dataverify = getVerifyInterface();
2334        mClassName = "com.android.smspush.unitTests.ReceiverService";
2335
2336        for (int i = 0; i < OMA_APPLICATION_ID_NAMES.length; i++) {
2337            mAppIdName = OMA_APPLICATION_ID_NAMES[i];
2338            mContentTypeValue = rd.nextInt(0x0FFF);
2339
2340            mMessageBody = new byte[100 + rd.nextInt(100)];
2341            rd.nextBytes(mMessageBody);
2342
2343            byte[] pdu = createPDU(6);
2344            byte[] wappushPdu = retrieveWspBody();
2345
2346            try {
2347                dataverify.resetData();
2348                // set up data
2349                if (isContentTypeMapped(mContentTypeValue)) {
2350                    // content type is mapped to integer value
2351                    mContentTypeName = getContentTypeName(mContentTypeValue);
2352                    Log.d(LOG_TAG, "mContentTypeValue mapping "
2353                            + mContentTypeValue + ":" + mContentTypeName);
2354                } else {
2355                    mContentTypeName = Integer.toString(mContentTypeValue);
2356                }
2357
2358                iwapman.addPackage(mAppIdName,
2359                        mContentTypeName, mPackageName, mClassName,
2360                        WapPushManagerParams.APP_TYPE_SERVICE, false, false);
2361
2362                dispatchWapPdu(wappushPdu, iwapman);
2363
2364                // clean up data
2365                iwapman.deletePackage(mAppIdName,
2366                        mContentTypeName, mPackageName, mClassName);
2367
2368                if (mContentTypeName.equals(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO)) {
2369                    assertTrue(dataverify.verifyData(wappushPdu));
2370                } else {
2371                    assertTrue(dataverify.verifyData(mMessageBody));
2372                }
2373            } catch (RemoteException e) {
2374            }
2375        }
2376
2377        mClassName = originalClassName;
2378        mAppIdName = originalAppIdName;
2379        mContentTypeName = originalContentTypeName;
2380        mAppIdValue = originalAppIdValue;
2381        mContentTypeValue = originalContentTypeValue;
2382        mMessageBody = originalMessageBody;
2383    }
2384
2385    /**
2386     * Integration test 4, iterate OmaApplication ID, Oma content type
2387     */
2388    public void testIntegration4() {
2389        boolean res;
2390        int originalAppIdValue = mAppIdValue;
2391        int originalContentTypeValue  = mContentTypeValue;
2392        String originalAppIdName = mAppIdName;
2393        String originalContentTypeName = mContentTypeName;
2394        String originalClassName = mClassName;
2395        byte[] originalMessageBody = mMessageBody;
2396        Random rd = new Random();
2397
2398        IWapPushManager iwapman = getInterface();
2399        IDataVerify dataverify = getVerifyInterface();
2400        mClassName = "com.android.smspush.unitTests.ReceiverService";
2401
2402        for (int i = 0; i < OMA_APPLICATION_ID_NAMES.length
2403                + OMA_CONTENT_TYPE_NAMES.length; i++) {
2404            mAppIdName = OMA_APPLICATION_ID_NAMES[rd.nextInt(OMA_APPLICATION_ID_NAMES.length)];
2405            int contIndex = rd.nextInt(OMA_CONTENT_TYPE_NAMES.length);
2406            mContentTypeName = OMA_CONTENT_TYPE_NAMES[contIndex];
2407
2408            mMessageBody = new byte[100 + rd.nextInt(100)];
2409            rd.nextBytes(mMessageBody);
2410
2411            byte[] pdu = createPDU(8);
2412            byte[] wappushPdu = retrieveWspBody();
2413
2414            try {
2415                dataverify.resetData();
2416                // set up data
2417                iwapman.addPackage(mAppIdName,
2418                        mContentTypeName, mPackageName, mClassName,
2419                        WapPushManagerParams.APP_TYPE_SERVICE, false, false);
2420
2421                dispatchWapPdu(wappushPdu, iwapman);
2422
2423                // clean up data
2424                iwapman.deletePackage(mAppIdName,
2425                        mContentTypeName, mPackageName, mClassName);
2426
2427                if (mContentTypeName.equals(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO)) {
2428                    assertTrue(dataverify.verifyData(wappushPdu));
2429                } else {
2430                    assertTrue(dataverify.verifyData(mMessageBody));
2431                }
2432            } catch (RemoteException e) {
2433            }
2434        }
2435
2436        mClassName = originalClassName;
2437        mAppIdName = originalAppIdName;
2438        mContentTypeName = originalContentTypeName;
2439        mAppIdValue = originalAppIdValue;
2440        mContentTypeValue = originalContentTypeValue;
2441        mMessageBody = originalMessageBody;
2442    }
2443
2444    /**
2445     * Integration test 5, iterate binary OmaApplication ID, Oma binary content type
2446     */
2447    public void testIntegration5() {
2448        boolean res;
2449        int originalAppIdValue = mAppIdValue;
2450        int originalContentTypeValue  = mContentTypeValue;
2451        String originalAppIdName = mAppIdName;
2452        String originalContentTypeName = mContentTypeName;
2453        String originalClassName = mClassName;
2454        byte[] originalMessageBody = mMessageBody;
2455        Random rd = new Random();
2456
2457        IWapPushManager iwapman = getInterface();
2458        IDataVerify dataverify = getVerifyInterface();
2459        mClassName = "com.android.smspush.unitTests.ReceiverService";
2460
2461        for (int i = 0; i < OMA_APPLICATION_ID_VALUES.length +
2462                    OMA_CONTENT_TYPE_VALUES.length; i++) {
2463            mAppIdValue = OMA_APPLICATION_ID_VALUES[rd.nextInt(
2464                    OMA_APPLICATION_ID_VALUES.length)];
2465            mContentTypeValue =
2466                    OMA_CONTENT_TYPE_VALUES[rd.nextInt(OMA_CONTENT_TYPE_VALUES.length)];
2467
2468            mMessageBody = new byte[100 + rd.nextInt(100)];
2469            rd.nextBytes(mMessageBody);
2470
2471            byte[] pdu = createPDU(3);
2472            byte[] wappushPdu = retrieveWspBody();
2473
2474            try {
2475                dataverify.resetData();
2476                // set up data
2477                if (isContentTypeMapped(mContentTypeValue)) {
2478                    // content type is mapped to integer value
2479                    mContentTypeName = getContentTypeName(mContentTypeValue);
2480                    Log.d(LOG_TAG, "mContentTypeValue mapping "
2481                            + mContentTypeValue + ":" + mContentTypeName);
2482                } else {
2483                    mContentTypeName = Integer.toString(mContentTypeValue);
2484                }
2485
2486                iwapman.addPackage(Integer.toString(mAppIdValue),
2487                        mContentTypeName, mPackageName, mClassName,
2488                        WapPushManagerParams.APP_TYPE_SERVICE, false, false);
2489
2490                dispatchWapPdu(wappushPdu, iwapman);
2491
2492                // clean up data
2493                iwapman.deletePackage(Integer.toString(mAppIdValue),
2494                        mContentTypeName, mPackageName, mClassName);
2495
2496                if (mContentTypeName.equals(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO)) {
2497                    assertTrue(dataverify.verifyData(wappushPdu));
2498                } else {
2499                    assertTrue(dataverify.verifyData(mMessageBody));
2500                }
2501            } catch (RemoteException e) {
2502            }
2503        }
2504
2505        mClassName = originalClassName;
2506        mAppIdName = originalAppIdName;
2507        mContentTypeName = originalContentTypeName;
2508        mAppIdValue = originalAppIdValue;
2509        mContentTypeValue = originalContentTypeValue;
2510        mMessageBody = originalMessageBody;
2511    }
2512
2513}
2514