blob: c70219ff947063cbf5665ce70f06583e85ad8158 [file] [log] [blame]
Sarah Chinfc5603b2021-12-21 11:34:00 -08001/*
2 * Copyright (C) 2021 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#include <aidl/android/hardware/radio/config/IRadioConfig.h>
18#include <android-base/logging.h>
19#include <android/binder_manager.h>
20
21#include "radio_sim_utils.h"
22
23#define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk())
24
25void RadioSimTest::SetUp() {
26 std::string serviceName = GetParam();
27
28 if (!isServiceValidForDeviceConfiguration(serviceName)) {
29 ALOGI("Skipped the test due to device configuration.");
30 GTEST_SKIP();
31 }
32
33 radio_sim = IRadioSim::fromBinder(
34 ndk::SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
35 ASSERT_NE(nullptr, radio_sim.get());
36
37 radioRsp_sim = ndk::SharedRefBase::make<RadioSimResponse>(*this);
38 ASSERT_NE(nullptr, radioRsp_sim.get());
39
40 count_ = 0;
41
42 radioInd_sim = ndk::SharedRefBase::make<RadioSimIndication>(*this);
43 ASSERT_NE(nullptr, radioInd_sim.get());
44
45 radio_sim->setResponseFunctions(radioRsp_sim, radioInd_sim);
46
47 // Assert IRadioConfig exists before testing
48 std::shared_ptr<aidl::android::hardware::radio::config::IRadioConfig> radioConfig =
49 aidl::android::hardware::radio::config::IRadioConfig::fromBinder(
50 ndk::SpAIBinder(AServiceManager_waitForService(
51 "android.hardware.radio.config.IRadioConfig/default")));
52 ASSERT_NE(nullptr, radioConfig.get());
53}
54
55ndk::ScopedAStatus RadioSimTest::updateSimCardStatus() {
56 serial = GetRandomSerialNumber();
57 radio_sim->getIccCardStatus(serial);
58 EXPECT_EQ(std::cv_status::no_timeout, wait());
59 return ndk::ScopedAStatus::ok();
60}
61
62/*
63 * Test IRadioSim.setSimCardPower() for the response returned.
64 */
65TEST_P(RadioSimTest, setSimCardPower) {
66 /* Test setSimCardPower power down */
67 serial = GetRandomSerialNumber();
68 radio_sim->setSimCardPower(serial, CardPowerState::POWER_DOWN);
69 EXPECT_EQ(std::cv_status::no_timeout, wait());
70 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
71 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
72 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
73 {RadioError::NONE, RadioError::INVALID_ARGUMENTS,
74 RadioError::RADIO_NOT_AVAILABLE, RadioError::SIM_ERR}));
75
76 // setSimCardPower does not return until the request is handled, and should not trigger
77 // CardStatus::STATE_ABSENT when turning off power
78 if (radioRsp_sim->rspInfo.error == RadioError::NONE) {
79 /* Wait some time for setting sim power down and then verify it */
80 updateSimCardStatus();
81 // We cannot assert the consistency of CardState here due to b/203031664
82 // EXPECT_EQ(CardStatus::STATE_PRESENT, cardStatus.cardState);
83 // applications should be an empty vector of AppStatus
84 EXPECT_EQ(0, cardStatus.applications.size());
85 }
86
87 // Give some time for modem to fully power down the SIM card
88 sleep(MODEM_SET_SIM_POWER_DELAY_IN_SECONDS);
89
90 /* Test setSimCardPower power up */
91 serial = GetRandomSerialNumber();
92 radio_sim->setSimCardPower(serial, CardPowerState::POWER_UP);
93 EXPECT_EQ(std::cv_status::no_timeout, wait());
94 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
95 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
96 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
97 {RadioError::NONE, RadioError::INVALID_ARGUMENTS,
98 RadioError::RADIO_NOT_AVAILABLE, RadioError::SIM_ERR}));
99
100 // Give some time for modem to fully power up the SIM card
101 sleep(MODEM_SET_SIM_POWER_DELAY_IN_SECONDS);
102
103 // setSimCardPower does not return until the request is handled. Just verify that we still
104 // have CardStatus::STATE_PRESENT after turning the power back on
105 if (radioRsp_sim->rspInfo.error == RadioError::NONE) {
106 updateSimCardStatus();
107 EXPECT_EQ(CardStatus::STATE_PRESENT, cardStatus.cardState);
108 }
109}
110
111/*
112 * Test IRadioSim.setCarrierInfoForImsiEncryption() for the response returned.
113 */
114TEST_P(RadioSimTest, setCarrierInfoForImsiEncryption) {
115 serial = GetRandomSerialNumber();
116 ImsiEncryptionInfo imsiInfo;
117 imsiInfo.mcc = "310";
118 imsiInfo.mnc = "004";
119 imsiInfo.carrierKey = (std::vector<uint8_t>){1, 2, 3, 4, 5, 6};
120 imsiInfo.keyIdentifier = "Test";
121 imsiInfo.expirationTime = 20180101;
122 imsiInfo.keyType = ImsiEncryptionInfo::PUBLIC_KEY_TYPE_EPDG;
123
124 radio_sim->setCarrierInfoForImsiEncryption(serial, imsiInfo);
125 EXPECT_EQ(std::cv_status::no_timeout, wait());
126 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
127 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
128
129 if (cardStatus.cardState == CardStatus::STATE_ABSENT) {
130 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
131 {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED}));
132 }
133}
134
135/*
136 * Test IRadioSim.getSimPhonebookRecords() for the response returned.
137 */
138TEST_P(RadioSimTest, getSimPhonebookRecords) {
139 serial = GetRandomSerialNumber();
140 radio_sim->getSimPhonebookRecords(serial);
141 EXPECT_EQ(std::cv_status::no_timeout, wait());
142 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
143 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
144 if (cardStatus.cardState == CardStatus::STATE_ABSENT) {
145 ASSERT_TRUE(
146 CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
147 {RadioError::INVALID_SIM_STATE, RadioError::RADIO_NOT_AVAILABLE,
148 RadioError::MODEM_ERR, RadioError::INVALID_ARGUMENTS,
149 RadioError::REQUEST_NOT_SUPPORTED},
150 CHECK_GENERAL_ERROR));
151 } else if (cardStatus.cardState == CardStatus::STATE_PRESENT) {
152 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
153 {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED},
154 CHECK_GENERAL_ERROR));
155 }
156}
157
158/*
159 * Test IRadioSim.getSimPhonebookCapacity for the response returned.
160 */
161TEST_P(RadioSimTest, getSimPhonebookCapacity) {
162 serial = GetRandomSerialNumber();
163 radio_sim->getSimPhonebookCapacity(serial);
164 EXPECT_EQ(std::cv_status::no_timeout, wait());
165 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
166 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
167 if (cardStatus.cardState == CardStatus::STATE_ABSENT) {
168 ASSERT_TRUE(
169 CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
170 {RadioError::INVALID_SIM_STATE, RadioError::RADIO_NOT_AVAILABLE,
171 RadioError::MODEM_ERR, RadioError::INVALID_ARGUMENTS,
172 RadioError::REQUEST_NOT_SUPPORTED},
173 CHECK_GENERAL_ERROR));
174 } else if (cardStatus.cardState == CardStatus::STATE_PRESENT) {
175 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
176 {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED},
177 CHECK_GENERAL_ERROR));
178
179 PhonebookCapacity pbCapacity = radioRsp_sim->capacity;
180 if (pbCapacity.maxAdnRecords > 0) {
181 EXPECT_TRUE(pbCapacity.maxNameLen > 0 && pbCapacity.maxNumberLen > 0);
182 EXPECT_TRUE(pbCapacity.usedAdnRecords <= pbCapacity.maxAdnRecords);
183 }
184
185 if (pbCapacity.maxEmailRecords > 0) {
186 EXPECT_TRUE(pbCapacity.maxEmailLen > 0);
187 EXPECT_TRUE(pbCapacity.usedEmailRecords <= pbCapacity.maxEmailRecords);
188 }
189
190 if (pbCapacity.maxAdditionalNumberRecords > 0) {
191 EXPECT_TRUE(pbCapacity.maxAdditionalNumberLen > 0);
192 EXPECT_TRUE(pbCapacity.usedAdditionalNumberRecords <=
193 pbCapacity.maxAdditionalNumberRecords);
194 }
195 }
196}
197
198/*
199 * Test IRadioSim.updateSimPhonebookRecords() for the response returned.
200 */
201TEST_P(RadioSimTest, updateSimPhonebookRecords) {
202 serial = GetRandomSerialNumber();
203 radio_sim->getSimPhonebookCapacity(serial);
204 EXPECT_EQ(std::cv_status::no_timeout, wait());
205 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
206 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
207 if (cardStatus.cardState == CardStatus::STATE_ABSENT) {
208 ASSERT_TRUE(
209 CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
210 {RadioError::INVALID_SIM_STATE, RadioError::RADIO_NOT_AVAILABLE,
211 RadioError::MODEM_ERR, RadioError::INVALID_ARGUMENTS,
212 RadioError::REQUEST_NOT_SUPPORTED},
213 CHECK_GENERAL_ERROR));
214 } else if (cardStatus.cardState == CardStatus::STATE_PRESENT) {
215 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
216 {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED},
217 CHECK_GENERAL_ERROR));
218 PhonebookCapacity pbCapacity = radioRsp_sim->capacity;
219
220 serial = GetRandomSerialNumber();
221 radio_sim->getSimPhonebookRecords(serial);
222
223 EXPECT_EQ(std::cv_status::no_timeout, wait());
224 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
225 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
226 ASSERT_TRUE(CheckAnyOfErrors(radioRsp_sim->rspInfo.error,
227 {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED},
228 CHECK_GENERAL_ERROR));
229
230 if (pbCapacity.maxAdnRecords > 0 && pbCapacity.usedAdnRecords < pbCapacity.maxAdnRecords) {
231 // Add a phonebook record
232 PhonebookRecordInfo recordInfo;
233 recordInfo.recordId = 0;
234 recordInfo.name = "ABC";
235 recordInfo.number = "1234567890";
236 serial = GetRandomSerialNumber();
237 radio_sim->updateSimPhonebookRecords(serial, recordInfo);
238
239 EXPECT_EQ(std::cv_status::no_timeout, wait());
240 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
241 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
242 EXPECT_EQ(RadioError::NONE, radioRsp_sim->rspInfo.error);
243 int index = radioRsp_sim->updatedRecordIndex;
244 EXPECT_TRUE(index > 0);
245
246 // Deleted a phonebook record
247 recordInfo.recordId = index;
248 recordInfo.name = "";
249 recordInfo.number = "";
250 serial = GetRandomSerialNumber();
251 radio_sim->updateSimPhonebookRecords(serial, recordInfo);
252
253 EXPECT_EQ(std::cv_status::no_timeout, wait());
254 EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_sim->rspInfo.type);
255 EXPECT_EQ(serial, radioRsp_sim->rspInfo.serial);
256 EXPECT_EQ(RadioError::NONE, radioRsp_sim->rspInfo.error);
257 }
258 }
259}