blob: 9a1c1455762fef19b0922a23f248c3edce7c3567 [file] [log] [blame]
Pomai Ahlodc1e6192022-12-12 13:58:55 -08001/*
2 * Copyright (C) 2022 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 */
Sarah Chinbb35a432023-05-02 21:11:41 -070016
Pomai Ahlodc1e6192022-12-12 13:58:55 -080017#include <android/binder_manager.h>
18
19#include "radio_sap_utils.h"
20
21#define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
22#define TIMEOUT_PERIOD 40
23
24void SapTest::SetUp() {
Sarah Chinbb35a432023-05-02 21:11:41 -070025 ALOGD("BEGIN %s#%s", ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name(),
26 ::testing::UnitTest::GetInstance()->current_test_info()->name());
27 count = 0;
28 serial = -1;
Pomai Ahlodc1e6192022-12-12 13:58:55 -080029 std::string serviceName = GetParam();
30 if (!isServiceValidForDeviceConfiguration(serviceName)) {
Sarah Chinbb35a432023-05-02 21:11:41 -070031 ALOGI("Skipped the test due to device configuration.");
Pomai Ahlodc1e6192022-12-12 13:58:55 -080032 GTEST_SKIP();
33 }
34 sap = ISap::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
35 ASSERT_NE(sap.get(), nullptr);
36
37 sapCb = ndk::SharedRefBase::make<SapCallback>(*this);
38 ASSERT_NE(sapCb.get(), nullptr);
39
Pomai Ahlodc1e6192022-12-12 13:58:55 -080040 ndk::ScopedAStatus res = sap->setCallback(sapCb);
41 ASSERT_OK(res) << res;
42}
43
Sarah Chinbb35a432023-05-02 21:11:41 -070044void SapTest::TearDown() {
45 count_ = 0;
46 serial = -1;
47 ALOGD("END %s#%s", ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name(),
48 ::testing::UnitTest::GetInstance()->current_test_info()->name());
49}
Pomai Ahlodc1e6192022-12-12 13:58:55 -080050
51::testing::AssertionResult SapTest::CheckAnyOfErrors(SapResultCode err,
52 std::vector<SapResultCode> errors) {
53 for (size_t i = 0; i < errors.size(); i++) {
54 if (err == errors[i]) {
55 return testing::AssertionSuccess();
56 }
57 }
58 return testing::AssertionFailure() << "SapError:" + toString(err) + " is returned";
59}
60
61void SapTest::notify(int receivedSerial) {
Sarah Chinbb35a432023-05-02 21:11:41 -070062 std::lock_guard<std::mutex> lock(mtx);
Pomai Ahlodc1e6192022-12-12 13:58:55 -080063 if (serial == receivedSerial) {
Sarah Chinbb35a432023-05-02 21:11:41 -070064 count++;
Pomai Ahlodc1e6192022-12-12 13:58:55 -080065 cv.notify_one();
66 }
67}
68
69std::cv_status SapTest::wait() {
70 std::unique_lock<std::mutex> lock(mtx);
71
72 std::cv_status status = std::cv_status::no_timeout;
73 auto now = std::chrono::system_clock::now();
74 while (count == 0) {
75 status = cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
76 if (status == std::cv_status::timeout) {
77 return status;
78 }
79 }
80 count--;
81 return status;
82}
83
84/*
85 * Test ISap.connectReq() for the response returned.
86 */
87TEST_P(SapTest, connectReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -080088 serial = GetRandomSerialNumber();
89 int32_t maxMsgSize = 100;
90
91 ndk::ScopedAStatus res = sap->connectReq(serial, maxMsgSize);
92 ASSERT_OK(res) << res;
93
94 EXPECT_EQ(std::cv_status::no_timeout, wait());
95 EXPECT_EQ(sapCb->sapResponseSerial, serial);
96
97 // Modem side need time for connect to finish. Adding a waiting time to prevent
98 // disconnect being requested right after connect request.
99 sleep(1);
100}
101
102/*
103 * Test ISap.disconnectReq() for the response returned
104 */
105TEST_P(SapTest, disconnectReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800106 serial = GetRandomSerialNumber();
107
108 ndk::ScopedAStatus res = sap->disconnectReq(serial);
109 ASSERT_OK(res) << res;
110
111 EXPECT_EQ(std::cv_status::no_timeout, wait());
112 EXPECT_EQ(sapCb->sapResponseSerial, serial);
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800113}
114
115/*
116 * Test ISap.apduReq() for the response returned.
117 */
118TEST_P(SapTest, apduReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800119 serial = GetRandomSerialNumber();
120 SapApduType sapApduType = SapApduType::APDU;
121 std::vector<uint8_t> command = {};
122
123 ndk::ScopedAStatus res = sap->apduReq(serial, sapApduType, command);
124 ASSERT_OK(res) << res;
125
126 EXPECT_EQ(std::cv_status::no_timeout, wait());
127 EXPECT_EQ(sapCb->sapResponseSerial, serial);
128
129 ASSERT_TRUE(CheckAnyOfErrors(
130 sapCb->sapResultCode,
131 {SapResultCode::GENERIC_FAILURE, SapResultCode::CARD_ALREADY_POWERED_OFF,
132 SapResultCode::CARD_NOT_ACCESSSIBLE, SapResultCode::CARD_REMOVED,
133 SapResultCode::SUCCESS}));
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800134}
135
136/*
137 * Test ISap.transferAtrReq() for the response returned.
138 */
139TEST_P(SapTest, transferAtrReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800140 serial = GetRandomSerialNumber();
141
142 ndk::ScopedAStatus res = sap->transferAtrReq(serial);
143 ASSERT_OK(res) << res;
144
145 EXPECT_EQ(std::cv_status::no_timeout, wait());
146 EXPECT_EQ(sapCb->sapResponseSerial, serial);
147
148 ASSERT_TRUE(CheckAnyOfErrors(sapCb->sapResultCode,
149 {SapResultCode::GENERIC_FAILURE, SapResultCode::DATA_NOT_AVAILABLE,
150 SapResultCode::CARD_ALREADY_POWERED_OFF,
151 SapResultCode::CARD_REMOVED, SapResultCode::SUCCESS}));
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800152}
153
154/*
155 * Test ISap.powerReq() for the response returned.
156 */
157TEST_P(SapTest, powerReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800158 serial = GetRandomSerialNumber();
159 bool state = true;
160
161 ndk::ScopedAStatus res = sap->powerReq(serial, state);
162 ASSERT_OK(res) << res;
163
164 EXPECT_EQ(std::cv_status::no_timeout, wait());
165 EXPECT_EQ(sapCb->sapResponseSerial, serial);
166
167 ASSERT_TRUE(
168 CheckAnyOfErrors(sapCb->sapResultCode,
169 {SapResultCode::GENERIC_FAILURE, SapResultCode::CARD_NOT_ACCESSSIBLE,
170 SapResultCode::CARD_ALREADY_POWERED_OFF, SapResultCode::CARD_REMOVED,
171 SapResultCode::CARD_ALREADY_POWERED_ON, SapResultCode::SUCCESS}));
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800172}
173
174/*
175 * Test ISap.resetSimReq() for the response returned.
176 */
177TEST_P(SapTest, resetSimReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800178 serial = GetRandomSerialNumber();
179
180 ndk::ScopedAStatus res = sap->resetSimReq(serial);
181 ASSERT_OK(res) << res;
182
183 EXPECT_EQ(std::cv_status::no_timeout, wait());
184 EXPECT_EQ(sapCb->sapResponseSerial, serial);
185
186 ASSERT_TRUE(
187 CheckAnyOfErrors(sapCb->sapResultCode,
188 {SapResultCode::GENERIC_FAILURE, SapResultCode::CARD_NOT_ACCESSSIBLE,
189 SapResultCode::CARD_ALREADY_POWERED_OFF, SapResultCode::CARD_REMOVED,
190 SapResultCode::SUCCESS}));
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800191}
192
193/*
194 * Test ISap.transferCardReaderStatusReq() for the response returned.
195 */
196TEST_P(SapTest, transferCardReaderStatusReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800197 serial = GetRandomSerialNumber();
198
199 ndk::ScopedAStatus res = sap->transferCardReaderStatusReq(serial);
200 ASSERT_OK(res) << res;
201
202 EXPECT_EQ(std::cv_status::no_timeout, wait());
203 EXPECT_EQ(sapCb->sapResponseSerial, serial);
204
205 ASSERT_TRUE(CheckAnyOfErrors(sapCb->sapResultCode,
206 {SapResultCode::GENERIC_FAILURE, SapResultCode::DATA_NOT_AVAILABLE,
207 SapResultCode::SUCCESS}));
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800208}
209
210/*
211 * Test ISap.setTransferProtocolReq() for the response returned.
212 */
213TEST_P(SapTest, setTransferProtocolReq) {
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800214 serial = GetRandomSerialNumber();
215 SapTransferProtocol sapTransferProtocol = SapTransferProtocol::T0;
216
217 ndk::ScopedAStatus res = sap->setTransferProtocolReq(serial, sapTransferProtocol);
218 ASSERT_OK(res) << res;
219
220 EXPECT_EQ(std::cv_status::no_timeout, wait());
221 EXPECT_EQ(sapCb->sapResponseSerial, serial);
222
223 ASSERT_TRUE(CheckAnyOfErrors(sapCb->sapResultCode,
224 {SapResultCode::NOT_SUPPORTED, SapResultCode::SUCCESS}));
Pomai Ahlodc1e6192022-12-12 13:58:55 -0800225}