• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 
17 #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 
SetUp()24 void SapTest::SetUp() {
25     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;
29     std::string serviceName = GetParam();
30     if (!isServiceValidForDeviceConfiguration(serviceName)) {
31         ALOGI("Skipped the test due to device configuration.");
32         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 
40     ndk::ScopedAStatus res = sap->setCallback(sapCb);
41     ASSERT_OK(res) << res;
42 }
43 
TearDown()44 void 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 }
50 
CheckAnyOfErrors(SapResultCode err,std::vector<SapResultCode> errors)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 
notify(int receivedSerial)61 void SapTest::notify(int receivedSerial) {
62     std::lock_guard<std::mutex> lock(mtx);
63     if (serial == receivedSerial) {
64         count++;
65         cv.notify_one();
66     }
67 }
68 
wait()69 std::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  */
TEST_P(SapTest,connectReq)87 TEST_P(SapTest, connectReq) {
88     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  */
TEST_P(SapTest,disconnectReq)105 TEST_P(SapTest, disconnectReq) {
106     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);
113 }
114 
115 /*
116  * Test ISap.apduReq() for the response returned.
117  */
TEST_P(SapTest,apduReq)118 TEST_P(SapTest, apduReq) {
119     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}));
134 }
135 
136 /*
137  * Test ISap.transferAtrReq() for the response returned.
138  */
TEST_P(SapTest,transferAtrReq)139 TEST_P(SapTest, transferAtrReq) {
140     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}));
152 }
153 
154 /*
155  * Test ISap.powerReq() for the response returned.
156  */
TEST_P(SapTest,powerReq)157 TEST_P(SapTest, powerReq) {
158     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}));
172 }
173 
174 /*
175  * Test ISap.resetSimReq() for the response returned.
176  */
TEST_P(SapTest,resetSimReq)177 TEST_P(SapTest, resetSimReq) {
178     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}));
191 }
192 
193 /*
194  * Test ISap.transferCardReaderStatusReq() for the response returned.
195  */
TEST_P(SapTest,transferCardReaderStatusReq)196 TEST_P(SapTest, transferCardReaderStatusReq) {
197     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}));
208 }
209 
210 /*
211  * Test ISap.setTransferProtocolReq() for the response returned.
212  */
TEST_P(SapTest,setTransferProtocolReq)213 TEST_P(SapTest, setTransferProtocolReq) {
214     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}));
225 }
226