1/*
2 * Copyright (C) 2011 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.gsm;
18
19import android.os.HandlerThread;
20import android.test.AndroidTestCase;
21import android.telephony.Rlog;
22
23import java.nio.charset.Charset;
24
25/**
26 * Test SMS-PP data download to UICC.
27 * Uses test messages from 3GPP TS 31.124 section 27.22.5.
28 */
29public class UsimDataDownloadTest extends AndroidTestCase {
30    private static final String TAG = "UsimDataDownloadTest";
31
32    class TestHandlerThread extends HandlerThread {
33        private UsimDataDownloadHandler mHandler;
34
35        TestHandlerThread() {
36            super("TestHandlerThread");
37        }
38
39        @Override
40        protected void onLooperPrepared() {
41            synchronized (this) {
42                mHandler = new UsimDataDownloadHandler(mCi);
43                notifyAll();
44            }
45        }
46
47        UsimDataDownloadHandler getHandler() {
48            synchronized (this) {
49                while (mHandler == null) {
50                    try {
51                        wait();
52                    } catch (InterruptedException ignored) {}
53                }
54                return mHandler;
55            }
56        }
57    }
58
59    private UsimDataDownloadCommands mCi;
60    private TestHandlerThread mHandlerThread;
61    UsimDataDownloadHandler mHandler;
62
63    @Override
64    protected void setUp() throws Exception {
65        super.setUp();
66        mCi = new UsimDataDownloadCommands(mContext);
67        mHandlerThread = new TestHandlerThread();
68        mHandlerThread.start();
69        mHandler = mHandlerThread.getHandler();
70        Rlog.d(TAG, "mHandler is constructed");
71    }
72
73    @Override
74    protected void tearDown() throws Exception {
75        mHandlerThread.quit();
76        super.tearDown();
77    }
78
79    // SMS-PP Message 3.1.1
80    private static final byte[] SMS_PP_MESSAGE_3_1_1 = {
81            // Service center address
82            0x09, (byte) 0x91, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0xf8,
83
84            0x04, 0x04, (byte) 0x91, 0x21, 0x43, 0x7f, 0x16, (byte) 0x89, 0x10, 0x10, 0x00, 0x00,
85            0x00, 0x00, 0x0d, 0x54, 0x65, 0x73, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61,
86            0x67, 0x65, 0x20, 0x31
87    };
88
89    // SMS-PP Download Envelope 3.1.1
90    private static final String SMS_PP_ENVELOPE_3_1_1 = "d12d8202838106099111223344556677f88b1c04"
91            + "049121437f16891010000000000d546573744d6573736167652031";
92
93    // SMS-PP Message 3.1.5
94    private static final byte[] SMS_PP_MESSAGE_3_1_5 = {
95            // Service center address
96            0x09, (byte) 0x91, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0xf8,
97
98            0x44, 0x04, (byte) 0x91, 0x21, 0x43, 0x7f, (byte) 0xf6, (byte) 0x89, 0x10, 0x10, 0x00,
99            0x00, 0x00, 0x00, 0x1e, 0x02, 0x70, 0x00, 0x00, 0x19, 0x00, 0x0d, 0x00, 0x00,
100            0x00, 0x00, (byte) 0xbf, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
101            (byte) 0xdc, (byte) 0xdc, (byte) 0xdc, (byte) 0xdc, (byte) 0xdc, (byte) 0xdc,
102            (byte) 0xdc, (byte) 0xdc, (byte) 0xdc, (byte) 0xdc
103    };
104
105    // SMS-PP Download Envelope 3.1.5
106    private static final String SMS_PP_ENVELOPE_3_1_5 = "d13e8202838106099111223344556677f88b2d44"
107            + "049121437ff6891010000000001e0270000019000d00000000bfff00000000000100"
108            + "dcdcdcdcdcdcdcdcdcdc";
109
110    public void testDataDownloadMessage1() {
111        SmsMessage message = SmsMessage.createFromPdu(SMS_PP_MESSAGE_3_1_1);
112        assertTrue("message is SMS-PP data download", message.isUsimDataDownload());
113
114        mCi.expectSendEnvelope(SMS_PP_ENVELOPE_3_1_1, 0x90, 0x00, "");
115        mCi.expectAcknowledgeGsmSms(true, 0);
116        mHandler.startDataDownload(message);
117        mCi.assertExpectedMethodsCalled();
118
119        mCi.expectSendEnvelope(SMS_PP_ENVELOPE_3_1_1, 0x90, 0x00, "0123456789");
120        mCi.expectAcknowledgeGsmSmsWithPdu(true, "00077f16050123456789");
121        mHandler.startDataDownload(message);
122        mCi.assertExpectedMethodsCalled();
123
124        mCi.expectSendEnvelope(SMS_PP_ENVELOPE_3_1_1, 0x62, 0xff, "0123456789abcdef");
125        mCi.expectAcknowledgeGsmSmsWithPdu(false, "00d5077f16080123456789abcdef");
126        mHandler.startDataDownload(message);
127        mCi.assertExpectedMethodsCalled();
128    }
129
130    public void testDataDownloadMessage5() {
131        SmsMessage message = SmsMessage.createFromPdu(SMS_PP_MESSAGE_3_1_5);
132        assertTrue("message is SMS-PP data download", message.isUsimDataDownload());
133
134        mCi.expectSendEnvelope(SMS_PP_ENVELOPE_3_1_5, 0x90, 0x00, "9876543210");
135        mCi.expectAcknowledgeGsmSmsWithPdu(true, "00077ff6059876543210");
136        mHandler.startDataDownload(message);
137        mCi.assertExpectedMethodsCalled();
138
139        mCi.expectSendEnvelope(SMS_PP_ENVELOPE_3_1_5, 0x93, 0x00, "");
140        mCi.expectAcknowledgeGsmSms(false, 0xd4);   // SIM toolkit busy
141        mHandler.startDataDownload(message);
142        mCi.assertExpectedMethodsCalled();
143    }
144}
145