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.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.IBinder;
23 import android.util.Log;
24 
25 import com.android.internal.util.HexDump;
26 
27 /**
28  * To verify that receiver application receives correct body data.
29  */
30 public class DataVerify extends Service {
31     private static final String LOG_TAG = "WAP PUSH";
32     private static final int TIME_WAIT = 100;
33     private static final int WAIT_COUNT = 100;
34     private static byte[] mLastReceivedPdu = null;
35     private static boolean sDataSet = false;
36 
37     private class IDataVerifyStub extends IDataVerify.Stub {
38         public Context mContext;
39 
IDataVerifyStub()40         public IDataVerifyStub() {
41         }
42 
arrayCompare(byte[] arr1, byte[] arr2)43         boolean arrayCompare(byte[] arr1, byte[] arr2) {
44             int i;
45 
46             if (arr1 == null || arr2 == null) {
47                 if (arr1 == null) {
48                     Log.w(LOG_TAG, "arr1 is null");
49                 } else {
50                     Log.w(LOG_TAG, "arr2 is null");
51                 }
52                 return false;
53             }
54 
55             if (arr1.length != arr2.length) {
56                 return false;
57             }
58 
59             for (i = 0; i < arr1.length; i++) {
60                 if (arr1[i] != arr2[i]) return false;
61             }
62             return true;
63         }
64 
65         /**
66          * Compare pdu and received pdu
67          */
verifyData(byte[] pdu)68         public synchronized boolean verifyData(byte[] pdu) {
69             int cnt = 0;
70 
71             while (!sDataSet) {
72                 // wait for the activity receive data.
73                 try {
74                     Thread.sleep(TIME_WAIT);
75                     if (cnt++ > WAIT_COUNT) {
76                         // don't wait more than 10 sec.
77                         return false;
78                     }
79                 } catch (InterruptedException e) {}
80             }
81 
82             Log.v(LOG_TAG, "verify pdu");
83             boolean ret = arrayCompare(pdu, mLastReceivedPdu);
84             return ret;
85         }
86 
87         /**
88          * Clear the old data. This method must be called before starting the test
89          */
resetData()90         public void resetData() {
91             mLastReceivedPdu = null;
92             sDataSet = false;
93         }
94     }
95 
96     private final IDataVerifyStub binder = new IDataVerifyStub();
97 
98     /**
99      * Constructor
100      */
DataVerify()101     public DataVerify() {
102     }
103 
104     /**
105      * Receiver application must call this method when it receives the wap push message
106      */
SetLastReceivedPdu(byte[] pdu)107     public static void SetLastReceivedPdu(byte[] pdu) {
108         mLastReceivedPdu = pdu;
109         sDataSet = true;
110     }
111 
112     @Override
onBind(Intent arg0)113     public IBinder onBind(Intent arg0) {
114         return binder;
115     }
116 
117 }
118 
119 
120