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