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