Pomai Ahlo | dc1e619 | 2022-12-12 13:58:55 -0800 | [diff] [blame^] | 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 | #include <android-base/logging.h> |
| 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 | |
| 24 | void SapTest::SetUp() { |
| 25 | std::string serviceName = GetParam(); |
| 26 | if (!isServiceValidForDeviceConfiguration(serviceName)) { |
| 27 | LOG(DEBUG) << "Skipped the test due to device configuration."; |
| 28 | GTEST_SKIP(); |
| 29 | } |
| 30 | sap = ISap::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
| 31 | ASSERT_NE(sap.get(), nullptr); |
| 32 | |
| 33 | sapCb = ndk::SharedRefBase::make<SapCallback>(*this); |
| 34 | ASSERT_NE(sapCb.get(), nullptr); |
| 35 | |
| 36 | count = 0; |
| 37 | |
| 38 | ndk::ScopedAStatus res = sap->setCallback(sapCb); |
| 39 | ASSERT_OK(res) << res; |
| 40 | } |
| 41 | |
| 42 | void SapTest::TearDown() {} |
| 43 | |
| 44 | ::testing::AssertionResult SapTest::CheckAnyOfErrors(SapResultCode err, |
| 45 | std::vector<SapResultCode> errors) { |
| 46 | for (size_t i = 0; i < errors.size(); i++) { |
| 47 | if (err == errors[i]) { |
| 48 | return testing::AssertionSuccess(); |
| 49 | } |
| 50 | } |
| 51 | return testing::AssertionFailure() << "SapError:" + toString(err) + " is returned"; |
| 52 | } |
| 53 | |
| 54 | void SapTest::notify(int receivedSerial) { |
| 55 | std::unique_lock<std::mutex> lock(mtx); |
| 56 | count++; |
| 57 | if (serial == receivedSerial) { |
| 58 | cv.notify_one(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | std::cv_status SapTest::wait() { |
| 63 | std::unique_lock<std::mutex> lock(mtx); |
| 64 | |
| 65 | std::cv_status status = std::cv_status::no_timeout; |
| 66 | auto now = std::chrono::system_clock::now(); |
| 67 | while (count == 0) { |
| 68 | status = cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD)); |
| 69 | if (status == std::cv_status::timeout) { |
| 70 | return status; |
| 71 | } |
| 72 | } |
| 73 | count--; |
| 74 | return status; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Test ISap.connectReq() for the response returned. |
| 79 | */ |
| 80 | TEST_P(SapTest, connectReq) { |
| 81 | LOG(DEBUG) << "connectReq"; |
| 82 | serial = GetRandomSerialNumber(); |
| 83 | int32_t maxMsgSize = 100; |
| 84 | |
| 85 | ndk::ScopedAStatus res = sap->connectReq(serial, maxMsgSize); |
| 86 | ASSERT_OK(res) << res; |
| 87 | |
| 88 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 89 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 90 | |
| 91 | // Modem side need time for connect to finish. Adding a waiting time to prevent |
| 92 | // disconnect being requested right after connect request. |
| 93 | sleep(1); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Test ISap.disconnectReq() for the response returned |
| 98 | */ |
| 99 | TEST_P(SapTest, disconnectReq) { |
| 100 | LOG(DEBUG) << "disconnectReq"; |
| 101 | serial = GetRandomSerialNumber(); |
| 102 | |
| 103 | ndk::ScopedAStatus res = sap->disconnectReq(serial); |
| 104 | ASSERT_OK(res) << res; |
| 105 | |
| 106 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 107 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 108 | LOG(DEBUG) << "disconnectReq finished"; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Test ISap.apduReq() for the response returned. |
| 113 | */ |
| 114 | TEST_P(SapTest, apduReq) { |
| 115 | LOG(DEBUG) << "apduReq"; |
| 116 | serial = GetRandomSerialNumber(); |
| 117 | SapApduType sapApduType = SapApduType::APDU; |
| 118 | std::vector<uint8_t> command = {}; |
| 119 | |
| 120 | ndk::ScopedAStatus res = sap->apduReq(serial, sapApduType, command); |
| 121 | ASSERT_OK(res) << res; |
| 122 | |
| 123 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 124 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 125 | |
| 126 | ASSERT_TRUE(CheckAnyOfErrors( |
| 127 | sapCb->sapResultCode, |
| 128 | {SapResultCode::GENERIC_FAILURE, SapResultCode::CARD_ALREADY_POWERED_OFF, |
| 129 | SapResultCode::CARD_NOT_ACCESSSIBLE, SapResultCode::CARD_REMOVED, |
| 130 | SapResultCode::SUCCESS})); |
| 131 | LOG(DEBUG) << "apduReq finished"; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Test ISap.transferAtrReq() for the response returned. |
| 136 | */ |
| 137 | TEST_P(SapTest, transferAtrReq) { |
| 138 | LOG(DEBUG) << "transferAtrReq"; |
| 139 | serial = GetRandomSerialNumber(); |
| 140 | |
| 141 | ndk::ScopedAStatus res = sap->transferAtrReq(serial); |
| 142 | ASSERT_OK(res) << res; |
| 143 | |
| 144 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 145 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 146 | |
| 147 | ASSERT_TRUE(CheckAnyOfErrors(sapCb->sapResultCode, |
| 148 | {SapResultCode::GENERIC_FAILURE, SapResultCode::DATA_NOT_AVAILABLE, |
| 149 | SapResultCode::CARD_ALREADY_POWERED_OFF, |
| 150 | SapResultCode::CARD_REMOVED, SapResultCode::SUCCESS})); |
| 151 | LOG(DEBUG) << "transferAtrReq finished"; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Test ISap.powerReq() for the response returned. |
| 156 | */ |
| 157 | TEST_P(SapTest, powerReq) { |
| 158 | LOG(DEBUG) << "powerReq"; |
| 159 | serial = GetRandomSerialNumber(); |
| 160 | bool state = true; |
| 161 | |
| 162 | ndk::ScopedAStatus res = sap->powerReq(serial, state); |
| 163 | ASSERT_OK(res) << res; |
| 164 | |
| 165 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 166 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 167 | |
| 168 | ASSERT_TRUE( |
| 169 | CheckAnyOfErrors(sapCb->sapResultCode, |
| 170 | {SapResultCode::GENERIC_FAILURE, SapResultCode::CARD_NOT_ACCESSSIBLE, |
| 171 | SapResultCode::CARD_ALREADY_POWERED_OFF, SapResultCode::CARD_REMOVED, |
| 172 | SapResultCode::CARD_ALREADY_POWERED_ON, SapResultCode::SUCCESS})); |
| 173 | LOG(DEBUG) << "powerReq finished"; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Test ISap.resetSimReq() for the response returned. |
| 178 | */ |
| 179 | TEST_P(SapTest, resetSimReq) { |
| 180 | LOG(DEBUG) << "resetSimReq"; |
| 181 | serial = GetRandomSerialNumber(); |
| 182 | |
| 183 | ndk::ScopedAStatus res = sap->resetSimReq(serial); |
| 184 | ASSERT_OK(res) << res; |
| 185 | |
| 186 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 187 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 188 | |
| 189 | ASSERT_TRUE( |
| 190 | CheckAnyOfErrors(sapCb->sapResultCode, |
| 191 | {SapResultCode::GENERIC_FAILURE, SapResultCode::CARD_NOT_ACCESSSIBLE, |
| 192 | SapResultCode::CARD_ALREADY_POWERED_OFF, SapResultCode::CARD_REMOVED, |
| 193 | SapResultCode::SUCCESS})); |
| 194 | LOG(DEBUG) << "resetSimReq finished"; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Test ISap.transferCardReaderStatusReq() for the response returned. |
| 199 | */ |
| 200 | TEST_P(SapTest, transferCardReaderStatusReq) { |
| 201 | LOG(DEBUG) << "transferCardReaderStatusReq"; |
| 202 | serial = GetRandomSerialNumber(); |
| 203 | |
| 204 | ndk::ScopedAStatus res = sap->transferCardReaderStatusReq(serial); |
| 205 | ASSERT_OK(res) << res; |
| 206 | |
| 207 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 208 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 209 | |
| 210 | ASSERT_TRUE(CheckAnyOfErrors(sapCb->sapResultCode, |
| 211 | {SapResultCode::GENERIC_FAILURE, SapResultCode::DATA_NOT_AVAILABLE, |
| 212 | SapResultCode::SUCCESS})); |
| 213 | LOG(DEBUG) << "transferCardReaderStatusReq finished"; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Test ISap.setTransferProtocolReq() for the response returned. |
| 218 | */ |
| 219 | TEST_P(SapTest, setTransferProtocolReq) { |
| 220 | LOG(DEBUG) << "setTransferProtocolReq"; |
| 221 | serial = GetRandomSerialNumber(); |
| 222 | SapTransferProtocol sapTransferProtocol = SapTransferProtocol::T0; |
| 223 | |
| 224 | ndk::ScopedAStatus res = sap->setTransferProtocolReq(serial, sapTransferProtocol); |
| 225 | ASSERT_OK(res) << res; |
| 226 | |
| 227 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 228 | EXPECT_EQ(sapCb->sapResponseSerial, serial); |
| 229 | |
| 230 | ASSERT_TRUE(CheckAnyOfErrors(sapCb->sapResultCode, |
| 231 | {SapResultCode::NOT_SUPPORTED, SapResultCode::SUCCESS})); |
| 232 | LOG(DEBUG) << "setTransferProtocolReq finished"; |
| 233 | } |