1 /*
2  * Copyright (C) 2017 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 <radio_hidl_hal_utils_v1_1.h>
18 #include <vector>
19 
20 /*
21  * Test IRadio.setSimCardPower() for the response returned.
22  */
TEST_P(RadioHidlTest_v1_1,setSimCardPower_1_1)23 TEST_P(RadioHidlTest_v1_1, setSimCardPower_1_1) {
24     /* Record the sim card state for the testing environment */
25     CardState cardStateForTest = cardStatus.cardState;
26 
27 #if 0
28     /* This test has to be removed for Japan Model.
29      * After "setSimCardPower power down", Japan model can not "setSimCardPower power up" */
30     /* Test setSimCardPower power down */
31     serial = GetRandomSerialNumber();
32     radio_v1_1->setSimCardPower_1_1(serial, CardPowerState::POWER_DOWN);
33     EXPECT_EQ(std::cv_status::no_timeout, wait());
34     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
35     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
36     ASSERT_TRUE(CheckAnyOfErrors(radioRsp_v1_1->rspInfo.error,
37                                  {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED,
38                                   RadioError::INVALID_ARGUMENTS, RadioError::RADIO_NOT_AVAILABLE}));
39     if (radioRsp_v1_1->rspInfo.error == RadioError::NONE) {
40         /* Wait some time for setting sim power down and then verify it */
41         updateSimCardStatus();
42         auto startTime = std::chrono::system_clock::now();
43         while (cardStatus.cardState != CardState::ABSENT &&
44            std::chrono::duration_cast<chrono::seconds>(std::chrono::system_clock::now() - startTime)
45                    .count() < 80) {
46             /* Set 2 seconds as interval to check card status */
47             sleep(2);
48             updateSimCardStatus();
49         }
50         EXPECT_EQ(CardState::ABSENT, cardStatus.cardState);
51     }
52 
53     /* Test setSimCardPower power up */
54     serial = GetRandomSerialNumber();
55     radio_v1_1->setSimCardPower_1_1(serial, CardPowerState::POWER_UP);
56     EXPECT_EQ(std::cv_status::no_timeout, wait());
57     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
58     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
59     ASSERT_TRUE(CheckAnyOfErrors(radioRsp_v1_1->rspInfo.error,
60                                  {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED,
61                                   RadioError::INVALID_ARGUMENTS, RadioError::RADIO_NOT_AVAILABLE}));
62 #endif
63 
64     /**
65      * If the sim card status for the testing environment is PRESENT,
66      * verify if sim status is reset back.
67      */
68     if (cardStateForTest == CardState::PRESENT &&
69         radioRsp_v1_1->rspInfo.error == RadioError::NONE) {
70         /* Wait some time for resetting back to sim power on and then verify it */
71         updateSimCardStatus();
72         auto startTime = std::chrono::system_clock::now();
73         while (cardStatus.cardState != CardState::PRESENT &&
74                std::chrono::duration_cast<chrono::seconds>(std::chrono::system_clock::now() -
75                                                            startTime)
76                        .count() < 80) {
77             /* Set 2 seconds as interval to check card status */
78             sleep(2);
79             updateSimCardStatus();
80         }
81         EXPECT_EQ(CardState::PRESENT, cardStatus.cardState);
82     }
83 }
84 
85 /*
86  * Test IRadio.startNetworkScan() for the response returned.
87  */
TEST_P(RadioHidlTest_v1_1,startNetworkScan)88 TEST_P(RadioHidlTest_v1_1, startNetworkScan) {
89     serial = GetRandomSerialNumber();
90 
91     NetworkScanRequest request;
92     request.type = ScanType::ONE_SHOT;
93     request.interval = 60;
94     RadioAccessSpecifier specifier;
95     specifier.radioAccessNetwork = RadioAccessNetworks::GERAN;
96     specifier.geranBands.resize(2);
97     specifier.geranBands[0] = GeranBands::BAND_450;
98     specifier.geranBands[1] = GeranBands::BAND_480;
99     specifier.channels.resize(2);
100     specifier.channels[0] = 1;
101     specifier.channels[1] = 2;
102     request.specifiers.resize(1);
103     request.specifiers[0] = specifier;
104 
105     radio_v1_1->startNetworkScan(serial, request);
106     EXPECT_EQ(std::cv_status::no_timeout, wait());
107     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
108     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
109 
110     if (cardStatus.cardState == CardState::ABSENT) {
111         ALOGI("startNetworkScan, rspInfo.error = %d\n", (int32_t)radioRsp_v1_1->rspInfo.error);
112         ASSERT_TRUE(CheckAnyOfErrors(
113             radioRsp_v1_1->rspInfo.error,
114             {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED, RadioError::INVALID_ARGUMENTS,
115              RadioError::SIM_ABSENT, RadioError::OPERATION_NOT_ALLOWED}));
116     }
117 }
118 
119 /*
120  * Test IRadio.startNetworkScan() for the response returned.
121  */
TEST_P(RadioHidlTest_v1_1,startNetworkScan_InvalidArgument)122 TEST_P(RadioHidlTest_v1_1, startNetworkScan_InvalidArgument) {
123     serial = GetRandomSerialNumber();
124 
125     NetworkScanRequest request;
126     request.type = ScanType::ONE_SHOT;
127     request.interval = 60;
128 
129     radio_v1_1->startNetworkScan(serial, request);
130     EXPECT_EQ(std::cv_status::no_timeout, wait());
131     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
132     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
133 
134     if (cardStatus.cardState == CardState::ABSENT) {
135         ALOGI("startNetworkScan_InvalidArgument, rspInfo.error = %d\n",
136               (int32_t)radioRsp_v1_1->rspInfo.error);
137         ASSERT_TRUE(CheckAnyOfErrors(radioRsp_v1_1->rspInfo.error,
138                                      {RadioError::INVALID_ARGUMENTS, RadioError::SIM_ABSENT,
139                                       RadioError::REQUEST_NOT_SUPPORTED}));
140     }
141 }
142 
143 /*
144  * Test IRadio.stopNetworkScan() for the response returned.
145  */
TEST_P(RadioHidlTest_v1_1,stopNetworkScan)146 TEST_P(RadioHidlTest_v1_1, stopNetworkScan) {
147     serial = GetRandomSerialNumber();
148 
149     radio_v1_1->stopNetworkScan(serial);
150     EXPECT_EQ(std::cv_status::no_timeout, wait());
151     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
152     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
153 
154     if (cardStatus.cardState == CardState::ABSENT) {
155         ALOGI("stopNetworkScan rspInfo.error = %d\n", (int32_t)radioRsp_v1_1->rspInfo.error);
156         ASSERT_TRUE(CheckAnyOfErrors(
157             radioRsp_v1_1->rspInfo.error,
158             {RadioError::NONE, RadioError::SIM_ABSENT, RadioError::REQUEST_NOT_SUPPORTED}));
159     }
160 }
161 
162 /*
163  * Test IRadio.setCarrierInfoForImsiEncryption() for the response returned.
164  */
TEST_P(RadioHidlTest_v1_1,setCarrierInfoForImsiEncryption)165 TEST_P(RadioHidlTest_v1_1, setCarrierInfoForImsiEncryption) {
166     serial = GetRandomSerialNumber();
167     ImsiEncryptionInfo imsiInfo;
168     imsiInfo.mcc = "310";
169     imsiInfo.mnc = "004";
170     imsiInfo.carrierKey = (std::vector<uint8_t>){1, 2, 3, 4, 5, 6};
171     imsiInfo.keyIdentifier = "Test";
172     imsiInfo.expirationTime = 20180101;
173 
174     radio_v1_1->setCarrierInfoForImsiEncryption(serial, imsiInfo);
175     EXPECT_EQ(std::cv_status::no_timeout, wait());
176     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
177     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
178 
179     if (cardStatus.cardState == CardState::ABSENT) {
180         ASSERT_TRUE(CheckAnyOfErrors(radioRsp_v1_1->rspInfo.error,
181                                      {RadioError::NONE, RadioError::REQUEST_NOT_SUPPORTED}));
182     }
183 }
184 
185 /*
186  * Test IRadio.startKeepalive() for the response returned.
187  */
TEST_P(RadioHidlTest_v1_1,startKeepalive)188 TEST_P(RadioHidlTest_v1_1, startKeepalive) {
189     std::vector<KeepaliveRequest> requests = {
190         {
191             // Invalid IPv4 source address
192             KeepaliveType::NATT_IPV4,
193             {192, 168, 0 /*, 100*/},
194             1234,
195             {8, 8, 4, 4},
196             4500,
197             20000,
198             0xBAD,
199         },
200         {
201             // Invalid IPv4 destination address
202             KeepaliveType::NATT_IPV4,
203             {192, 168, 0, 100},
204             1234,
205             {8, 8, 4, 4, 1, 2, 3, 4},
206             4500,
207             20000,
208             0xBAD,
209         },
210         {
211             // Invalid Keepalive Type
212             static_cast<KeepaliveType>(-1),
213             {192, 168, 0, 100},
214             1234,
215             {8, 8, 4, 4},
216             4500,
217             20000,
218             0xBAD,
219         },
220         {
221             // Invalid IPv6 source address
222             KeepaliveType::NATT_IPV6,
223             {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xED,
224              0xBE, 0xEF, 0xBD},
225             1234,
226             {0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227              0x88, 0x44},
228             4500,
229             20000,
230             0xBAD,
231         },
232         {
233             // Invalid IPv6 destination address
234             KeepaliveType::NATT_IPV6,
235             {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xED,
236              0xBE, 0xEF},
237             1234,
238             {0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239              0x88,
240              /*0x44*/},
241             4500,
242             20000,
243             0xBAD,
244         },
245         {
246             // Invalid Context ID (cid), this should survive the initial
247             // range checking and fail in the modem data layer
248             KeepaliveType::NATT_IPV4,
249             {192, 168, 0, 100},
250             1234,
251             {8, 8, 4, 4},
252             4500,
253             20000,
254             0xBAD,
255         },
256         {
257             // Invalid Context ID (cid), this should survive the initial
258             // range checking and fail in the modem data layer
259             KeepaliveType::NATT_IPV6,
260             {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xED,
261              0xBE, 0xEF},
262             1234,
263             {0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
264              0x88, 0x44},
265             4500,
266             20000,
267             0xBAD,
268         }};
269 
270     for (auto req = requests.begin(); req != requests.end(); req++) {
271         serial = GetRandomSerialNumber();
272         radio_v1_1->startKeepalive(serial, *req);
273         EXPECT_EQ(std::cv_status::no_timeout, wait());
274         EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
275         EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
276 
277         ASSERT_TRUE(
278             CheckAnyOfErrors(radioRsp_v1_1->rspInfo.error,
279                              {RadioError::INVALID_ARGUMENTS, RadioError::REQUEST_NOT_SUPPORTED}));
280     }
281 }
282 
283 /*
284  * Test IRadio.stopKeepalive() for the response returned.
285  */
TEST_P(RadioHidlTest_v1_1,stopKeepalive)286 TEST_P(RadioHidlTest_v1_1, stopKeepalive) {
287     serial = GetRandomSerialNumber();
288 
289     radio_v1_1->stopKeepalive(serial, 0xBAD);
290     EXPECT_EQ(std::cv_status::no_timeout, wait());
291     EXPECT_EQ(RadioResponseType::SOLICITED, radioRsp_v1_1->rspInfo.type);
292     EXPECT_EQ(serial, radioRsp_v1_1->rspInfo.serial);
293 
294     ASSERT_TRUE(
295         CheckAnyOfErrors(radioRsp_v1_1->rspInfo.error,
296                          {RadioError::INVALID_ARGUMENTS, RadioError::REQUEST_NOT_SUPPORTED}));
297 }
298