1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Matchers.anyString;
21import static org.mockito.Matchers.nullable;
22import static org.mockito.Mockito.any;
23import static org.mockito.Mockito.anyInt;
24import static org.mockito.Mockito.doReturn;
25import static org.mockito.Mockito.eq;
26import static org.mockito.Mockito.isNull;
27import static org.mockito.Mockito.never;
28import static org.mockito.Mockito.verify;
29import static org.mockito.Mockito.when;
30
31import android.app.AppOpsManager;
32import android.content.BroadcastReceiver;
33import android.content.Intent;
34import android.os.Bundle;
35import android.os.UserHandle;
36import android.provider.Telephony;
37import android.test.suitebuilder.annotation.SmallTest;
38
39import org.junit.After;
40import org.junit.Before;
41import org.junit.Test;
42import org.mockito.ArgumentCaptor;
43import org.mockito.Mock;
44
45
46public class WapPushOverSmsTest extends TelephonyTest {
47    @Mock
48    protected IMms.Stub mIMmsStub;
49
50    private WapPushOverSms mWapPushOverSmsUT;
51
52    @Before
53    public void setUp() throws Exception {
54        super.setUp("WapPushOverSmsTest");
55
56        // Note that this replaces only cached services in ServiceManager. If a service is not found
57        // in the cache, a real instance is used.
58        mServiceManagerMockedServices.put("imms", mIMmsStub);
59        doReturn(mIMmsStub).when(mIMmsStub).queryLocalInterface(anyString());
60
61        mWapPushOverSmsUT = new WapPushOverSms(mContext);
62    }
63
64    @After
65    public void tearDown() throws Exception {
66        mWapPushOverSmsUT = null;
67        super.tearDown();
68    }
69
70    @Test @SmallTest
71    public void testDispatchWapPdu() {
72        doReturn(true).when(mWspTypeDecoder).decodeUintvarInteger(anyInt());
73        doReturn(true).when(mWspTypeDecoder).decodeContentType(anyInt());
74        doReturn((long)2).when(mWspTypeDecoder).getValue32();
75        doReturn(2).when(mWspTypeDecoder).getDecodedDataLength();
76        doReturn(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO).when(mWspTypeDecoder).getValueString();
77        byte[] pdu = new byte[]{
78                (byte) 0xFF,
79                (byte) 0x06,
80                (byte) 0xFF,
81                (byte) 0xFF,
82                (byte) 0xFF,
83                (byte) 0xFF,
84                (byte) 0xFF
85        };
86
87        mWapPushOverSmsUT.dispatchWapPdu(pdu, null, mInboundSmsHandler);
88
89        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
90        verify(mInboundSmsHandler).dispatchIntent(intentArgumentCaptor.capture(),
91                eq(android.Manifest.permission.RECEIVE_WAP_PUSH),
92                eq(AppOpsManager.OP_RECEIVE_WAP_PUSH),
93                nullable(Bundle.class),
94                isNull(BroadcastReceiver.class),
95                eq(UserHandle.SYSTEM));
96        Intent intent = intentArgumentCaptor.getValue();
97        assertEquals(Telephony.Sms.Intents.WAP_PUSH_DELIVER_ACTION, intent.getAction());
98        assertEquals(0xFF, intent.getIntExtra("transactionId", 0));
99        assertEquals(0x06, intent.getIntExtra("pduType", 0));
100
101        byte[] header = intent.getByteArrayExtra("header");
102        assertEquals(2, header.length);
103        for (int i = 0; i < header.length; i++) {
104            assertEquals((byte)0xFF, header[i]);
105        }
106
107        byte[] data = intent.getByteArrayExtra("data");
108        assertEquals(pdu.length, data.length);
109        for (int i = 0; i < pdu.length; i++) {
110            assertEquals(pdu[i], data[i]);
111        }
112
113        assertEquals(mWspTypeDecoder.getContentParameters(),
114                intent.getSerializableExtra("contentTypeParameters"));
115    }
116
117    @Test @SmallTest
118    public void testDispatchWapPduFromBlockedNumber_noIntentsDispatched() throws Exception {
119        when(mIMmsStub.getCarrierConfigValues(anyInt())).thenReturn(new Bundle());
120
121        mFakeBlockedNumberContentProvider.mBlockedNumbers.add("16178269168");
122
123        doReturn(true).when(mWspTypeDecoder).decodeUintvarInteger(anyInt());
124        doReturn(true).when(mWspTypeDecoder).decodeContentType(anyInt());
125        doReturn((long) 2).when(mWspTypeDecoder).getValue32();
126        doReturn(2).when(mWspTypeDecoder).getDecodedDataLength();
127        doReturn(WspTypeDecoder.CONTENT_TYPE_B_PUSH_CO).when(mWspTypeDecoder).getValueString();
128
129        byte pdu[] = {1, 6, 0, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 47,
130                118, 110, 100, 46, 119, 97, 112, 46, 109, 109, 115, 45, 109, 101, 115, 115,
131                97, 103, 101, 0, -116, -126, -104, 77, 109, 115, 84, 114, 97, 110, 115, 97,
132                99, 116, 105, 111, 110, 73, 68, 0, -115, 18, -119, 8, -128, 49, 54, 49, 55,
133                56, 50, 54, 57, 49, 54, 56, 47, 84, 89, 80, 69, 61, 80, 76, 77, 78, 0, -118,
134                -128, -114, 2, 3, -24, -120, 3, -127, 3, 3, -12, -128, -106, 84, 101, 115,
135                116, 32, 77, 109, 115, 32, 83, 117, 98, 106, 101, 99, 116, 0, -125, 104, 116,
136                116, 112, 58, 47, 47, 119, 119, 119, 46, 103, 111, 111, 103, 108, 101, 46, 99,
137                111, 109, 47, 115, 97, 100, 102, 100, 100, 0};
138
139        assertEquals(Telephony.Sms.Intents.RESULT_SMS_HANDLED,
140                mWapPushOverSmsUT.dispatchWapPdu(pdu, null, mInboundSmsHandler));
141        verify(mInboundSmsHandler, never()).dispatchIntent(
142                any(Intent.class),
143                any(String.class),
144                anyInt(),
145                any(Bundle.class),
146                any(BroadcastReceiver.class),
147                any(UserHandle.class));
148    }
149}