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