Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define LOG_TAG "nfc_hidl_hal_test" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <android/hardware/nfc/1.0/types.h> |
| 21 | #include <android/hardware/nfc/1.1/INfc.h> |
| 22 | #include <android/hardware/nfc/1.1/INfcClientCallback.h> |
| 23 | #include <android/hardware/nfc/1.1/types.h> |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 25 | #include <hardware/nfc.h> |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 26 | #include <hidl/GtestPrinter.h> |
| 27 | #include <hidl/ServiceManagement.h> |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 28 | |
| 29 | #include <VtsHalHidlTargetCallbackBase.h> |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 30 | |
| 31 | using ::android::hardware::nfc::V1_1::INfc; |
| 32 | using ::android::hardware::nfc::V1_1::INfcClientCallback; |
| 33 | using ::android::hardware::nfc::V1_1::NfcEvent; |
Ruchi Kandoi | 4cd374a | 2018-03-01 16:31:55 -0800 | [diff] [blame] | 34 | using ::android::hardware::nfc::V1_1::NfcConfig; |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 35 | using ::android::hardware::nfc::V1_0::NfcStatus; |
| 36 | using ::android::hardware::nfc::V1_0::NfcData; |
| 37 | using ::android::hardware::Return; |
| 38 | using ::android::hardware::Void; |
| 39 | using ::android::hardware::hidl_vec; |
| 40 | using ::android::sp; |
| 41 | |
Ruchi Kandoi | 4cd374a | 2018-03-01 16:31:55 -0800 | [diff] [blame] | 42 | // 261 bytes is the default and minimum transceive length |
| 43 | constexpr unsigned int MIN_ISO_DEP_TRANSCEIVE_LENGTH = 261; |
| 44 | |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 45 | constexpr char kCallbackNameSendEvent[] = "sendEvent"; |
| 46 | constexpr char kCallbackNameSendData[] = "sendData"; |
| 47 | |
| 48 | class NfcClientCallbackArgs { |
| 49 | public: |
| 50 | NfcEvent last_event_; |
| 51 | NfcStatus last_status_; |
| 52 | NfcData last_data_; |
| 53 | }; |
| 54 | |
| 55 | /* Callback class for data & Event. */ |
| 56 | class NfcClientCallback : public ::testing::VtsHalHidlTargetCallbackBase<NfcClientCallbackArgs>, |
| 57 | public INfcClientCallback { |
| 58 | public: |
| 59 | virtual ~NfcClientCallback() = default; |
| 60 | |
| 61 | /* sendEvent callback function - Records the Event & Status |
| 62 | * and notifies the TEST |
| 63 | **/ |
| 64 | Return<void> sendEvent_1_1(NfcEvent event, NfcStatus event_status) override { |
| 65 | NfcClientCallbackArgs args; |
| 66 | args.last_event_ = event; |
| 67 | args.last_status_ = event_status; |
| 68 | NotifyFromCallback(kCallbackNameSendEvent, args); |
| 69 | return Void(); |
| 70 | }; |
| 71 | |
| 72 | /** NFC 1.1 HAL shouldn't send 1.0 callbacks */ |
| 73 | Return<void> sendEvent(__attribute__((unused))::android::hardware::nfc::V1_0::NfcEvent event, |
| 74 | __attribute__((unused)) NfcStatus event_status) override { |
| 75 | return Void(); |
| 76 | } |
| 77 | |
| 78 | /* sendData callback function. Records the data and notifies the TEST*/ |
| 79 | Return<void> sendData(const NfcData& data) override { |
| 80 | NfcClientCallbackArgs args; |
| 81 | args.last_data_ = data; |
| 82 | NotifyFromCallback(kCallbackNameSendData, args); |
| 83 | return Void(); |
| 84 | }; |
| 85 | }; |
| 86 | |
| 87 | // The main test class for NFC HIDL HAL. |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 88 | class NfcHidlTest : public ::testing::TestWithParam<std::string> { |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 89 | public: |
| 90 | virtual void SetUp() override { |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 91 | nfc_ = INfc::getService(GetParam()); |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 92 | ASSERT_NE(nfc_, nullptr); |
| 93 | |
| 94 | nfc_cb_ = new NfcClientCallback(); |
| 95 | ASSERT_NE(nfc_cb_, nullptr); |
| 96 | |
| 97 | EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); |
| 98 | // Wait for OPEN_CPLT event |
| 99 | auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 100 | EXPECT_TRUE(res.no_timeout); |
| 101 | EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); |
| 102 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 103 | |
| 104 | /* |
| 105 | * Close the hal and then re-open to make sure we are in a predictable |
| 106 | * state for all the tests. |
| 107 | */ |
| 108 | EXPECT_EQ(NfcStatus::OK, nfc_->close()); |
| 109 | // Wait for CLOSE_CPLT event |
| 110 | res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 111 | EXPECT_TRUE(res.no_timeout); |
| 112 | EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); |
| 113 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 114 | |
| 115 | EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); |
| 116 | // Wait for OPEN_CPLT event |
| 117 | res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 118 | EXPECT_TRUE(res.no_timeout); |
| 119 | EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); |
| 120 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 121 | } |
| 122 | |
| 123 | virtual void TearDown() override { |
| 124 | EXPECT_EQ(NfcStatus::OK, nfc_->close()); |
| 125 | // Wait for CLOSE_CPLT event |
| 126 | auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 127 | EXPECT_TRUE(res.no_timeout); |
| 128 | EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); |
| 129 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 130 | } |
| 131 | |
| 132 | sp<INfc> nfc_; |
| 133 | sp<NfcClientCallback> nfc_cb_; |
| 134 | }; |
| 135 | |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 136 | /* |
| 137 | * factoryReset |
| 138 | * calls factoryReset() |
| 139 | * checks status |
| 140 | */ |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 141 | TEST_P(NfcHidlTest, FactoryReset) { |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 142 | nfc_->factoryReset(); |
| 143 | |
| 144 | EXPECT_EQ(NfcStatus::OK, nfc_->close()); |
| 145 | // Wait for CLOSE_CPLT event |
| 146 | auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 147 | EXPECT_TRUE(res.no_timeout); |
| 148 | EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); |
| 149 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 150 | |
| 151 | EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); |
| 152 | // Wait for OPEN_CPLT event |
| 153 | res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 154 | EXPECT_TRUE(res.no_timeout); |
| 155 | EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); |
| 156 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * OpenAndClose: |
| 161 | * Makes an open call, waits for NfcEvent.OPEN_CPLT |
| 162 | * Immediately calls closeforPowerOffCase() and waits for NfcEvent.CLOSE_CPLT |
| 163 | */ |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 164 | TEST_P(NfcHidlTest, OpenAndCloseForPowerOff) { |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 165 | EXPECT_EQ(NfcStatus::OK, nfc_->closeForPowerOffCase()); |
| 166 | // Wait for CLOSE_CPLT event |
| 167 | auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 168 | EXPECT_TRUE(res.no_timeout); |
| 169 | EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); |
| 170 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 171 | |
| 172 | EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); |
| 173 | // Wait for OPEN_CPLT event |
| 174 | res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 175 | EXPECT_TRUE(res.no_timeout); |
| 176 | EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); |
| 177 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * CloseForPowerOffCaseAfterClose: |
| 182 | * Calls closeForPowerOffCase() |
| 183 | * Calls close() - checks failed status |
| 184 | */ |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 185 | TEST_P(NfcHidlTest, CloseForPowerCaseOffAfterClose) { |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 186 | EXPECT_EQ(NfcStatus::OK, nfc_->closeForPowerOffCase()); |
| 187 | // Wait for CLOSE_CPLT event |
| 188 | auto res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 189 | EXPECT_TRUE(res.no_timeout); |
| 190 | EXPECT_EQ(NfcEvent::CLOSE_CPLT, res.args->last_event_); |
| 191 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 192 | |
| 193 | EXPECT_EQ(NfcStatus::FAILED, nfc_->close()); |
| 194 | |
| 195 | EXPECT_EQ(NfcStatus::OK, nfc_->open_1_1(nfc_cb_)); |
| 196 | // Wait for OPEN_CPLT event |
| 197 | res = nfc_cb_->WaitForCallback(kCallbackNameSendEvent); |
| 198 | EXPECT_TRUE(res.no_timeout); |
| 199 | EXPECT_EQ(NfcEvent::OPEN_CPLT, res.args->last_event_); |
| 200 | EXPECT_EQ(NfcStatus::OK, res.args->last_status_); |
| 201 | } |
| 202 | |
Ruchi Kandoi | 4cd374a | 2018-03-01 16:31:55 -0800 | [diff] [blame] | 203 | /* |
| 204 | * getConfig: |
| 205 | * Calls getConfig() |
| 206 | * checks if fields in NfcConfig are populated correctly |
| 207 | */ |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 208 | TEST_P(NfcHidlTest, GetConfig) { |
Ruchi Kandoi | 4cd374a | 2018-03-01 16:31:55 -0800 | [diff] [blame] | 209 | nfc_->getConfig([](NfcConfig config) { |
| 210 | EXPECT_GE(config.maxIsoDepTransceiveLength, MIN_ISO_DEP_TRANSCEIVE_LENGTH); |
| 211 | }); |
| 212 | } |
| 213 | |
Dan Shi | e64262f | 2019-10-18 10:47:43 -0700 | [diff] [blame] | 214 | INSTANTIATE_TEST_SUITE_P( |
| 215 | PerInstance, NfcHidlTest, |
| 216 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(INfc::descriptor)), |
| 217 | android::hardware::PrintInstanceNameToString); |
| 218 | |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 219 | int main(int argc, char** argv) { |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 220 | ::testing::InitGoogleTest(&argc, argv); |
Ruchi Kandoi | 912ce33 | 2018-01-31 13:47:17 -0800 | [diff] [blame] | 221 | |
| 222 | std::system("svc nfc disable"); /* Turn off NFC */ |
| 223 | sleep(5); |
| 224 | |
| 225 | int status = RUN_ALL_TESTS(); |
| 226 | LOG(INFO) << "Test result = " << status; |
| 227 | |
| 228 | std::system("svc nfc enable"); /* Turn on NFC */ |
| 229 | sleep(5); |
| 230 | |
| 231 | return status; |
| 232 | } |