blob: 372d78715bd04e86ab07e645b265b02c87d52943 [file] [log] [blame]
Andrew Scull2f6aae52017-06-12 16:38:46 +01001/*
2 * Copyright (C) 2017 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/hardware/weaver/1.0/IWeaver.h>
18
19#include <limits>
20
21#include <VtsHalHidlTargetTestBase.h>
22
23using ::android::hardware::weaver::V1_0::IWeaver;
24using ::android::hardware::weaver::V1_0::WeaverConfig;
25using ::android::hardware::weaver::V1_0::WeaverReadStatus;
26using ::android::hardware::weaver::V1_0::WeaverReadResponse;
27using ::android::hardware::weaver::V1_0::WeaverStatus;
28using ::android::hardware::Return;
29using ::android::sp;
30
31const std::vector<uint8_t> KEY{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
32const std::vector<uint8_t> WRONG_KEY{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
33const std::vector<uint8_t> VALUE{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
34const std::vector<uint8_t> OTHER_VALUE{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 255, 255};
35
36struct WeaverHidlTest : public ::testing::VtsHalHidlTargetTestBase {
37 virtual void SetUp() override {
38 weaver = ::testing::VtsHalHidlTargetTestBase::getService<IWeaver>();
39 ASSERT_NE(weaver, nullptr);
40 }
41
42 virtual void TearDown() override {}
43
44 sp<IWeaver> weaver;
45};
46
47/*
48 * Checks config values are suitably large
49 */
50TEST_F(WeaverHidlTest, GetConfig) {
51 WeaverStatus status;
52 WeaverConfig config;
53
54 bool callbackCalled = false;
55 auto ret = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
56 callbackCalled = true;
57 status = s;
58 config = c;
59 });
60 ASSERT_TRUE(ret.isOk());
61 ASSERT_TRUE(callbackCalled);
62 ASSERT_EQ(status, WeaverStatus::OK);
63
64 EXPECT_GE(config.slots, 16u);
65 EXPECT_GE(config.keySize, 16u);
66 EXPECT_GE(config.valueSize, 16u);
67}
68
69/*
70 * Gets the config twice and checks they are the same
71 */
72TEST_F(WeaverHidlTest, GettingConfigMultipleTimesGivesSameResult) {
73 WeaverConfig config1;
74 WeaverConfig config2;
75
76 WeaverStatus status;
77 bool callbackCalled = false;
78 auto ret = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
79 callbackCalled = true;
80 status = s;
81 config1 = c;
82 });
83 ASSERT_TRUE(ret.isOk());
84 ASSERT_TRUE(callbackCalled);
85 ASSERT_EQ(status, WeaverStatus::OK);
86
87 callbackCalled = false;
88 ret = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
89 callbackCalled = true;
90 status = s;
91 config2 = c;
92 });
93 ASSERT_TRUE(ret.isOk());
94 ASSERT_TRUE(callbackCalled);
95 ASSERT_EQ(status, WeaverStatus::OK);
96
97 EXPECT_EQ(config1, config2);
98}
99
100/*
101 * Gets the number of slots from the config and writes a key and value to the last one
102 */
103TEST_F(WeaverHidlTest, WriteToLastSlot) {
104 WeaverStatus status;
105 WeaverConfig config;
106 const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
107 status = s;
108 config = c;
109 });
110 ASSERT_TRUE(configRet.isOk());
111 ASSERT_EQ(status, WeaverStatus::OK);
112
113 const uint32_t lastSlot = config.slots - 1;
114 const auto writeRet = weaver->write(lastSlot, KEY, VALUE);
115 ASSERT_TRUE(writeRet.isOk());
116 ASSERT_EQ(writeRet, WeaverStatus::OK);
117}
118
119/*
120 * Writes a key and value to a slot
121 * Reads the slot with the same key and receives the value that was previously written
122 */
123TEST_F(WeaverHidlTest, WriteFollowedByReadGivesTheSameValue) {
124 constexpr uint32_t slotId = 0;
125 const auto ret = weaver->write(slotId, KEY, VALUE);
126 ASSERT_TRUE(ret.isOk());
127 ASSERT_EQ(ret, WeaverStatus::OK);
128
129 bool callbackCalled = false;
130 WeaverReadStatus status;
131 std::vector<uint8_t> readValue;
132 uint32_t timeout;
133 const auto readRet = weaver->read(slotId, KEY, [&](WeaverReadStatus s, WeaverReadResponse r) {
134 callbackCalled = true;
135 status = s;
136 readValue = r.value;
137 timeout = r.timeout;
138 });
139 ASSERT_TRUE(readRet.isOk());
140 ASSERT_TRUE(callbackCalled);
141 ASSERT_EQ(status, WeaverReadStatus::OK);
142 EXPECT_EQ(readValue, VALUE);
143 EXPECT_EQ(timeout, 0u);
144}
145
146/*
147 * Writes a key and value to a slot
148 * Overwrites the slot with a new key and value
149 * Reads the slot with the new key and receives the new value
150 */
151TEST_F(WeaverHidlTest, OverwritingSlotUpdatesTheValue) {
152 constexpr uint32_t slotId = 0;
153 const auto initialWriteRet = weaver->write(slotId, WRONG_KEY, VALUE);
154 ASSERT_TRUE(initialWriteRet.isOk());
155 ASSERT_EQ(initialWriteRet, WeaverStatus::OK);
156
157 const auto overwriteRet = weaver->write(slotId, KEY, OTHER_VALUE);
158 ASSERT_TRUE(overwriteRet.isOk());
159 ASSERT_EQ(overwriteRet, WeaverStatus::OK);
160
161 bool callbackCalled = false;
162 WeaverReadStatus status;
163 std::vector<uint8_t> readValue;
164 uint32_t timeout;
165 const auto readRet = weaver->read(slotId, KEY, [&](WeaverReadStatus s, WeaverReadResponse r) {
166 callbackCalled = true;
167 status = s;
168 readValue = r.value;
169 timeout = r.timeout;
170 });
171 ASSERT_TRUE(readRet.isOk());
172 ASSERT_TRUE(callbackCalled);
173 ASSERT_EQ(status, WeaverReadStatus::OK);
174 EXPECT_EQ(readValue, OTHER_VALUE);
175 EXPECT_EQ(timeout, 0u);
176}
177
178/*
179 * Writes a key and value to a slot
180 * Reads the slot with a different key so does not receive the value
181 */
182TEST_F(WeaverHidlTest, WriteFollowedByReadWithWrongKeyDoesNotGiveTheValue) {
183 constexpr uint32_t slotId = 0;
184 const auto ret = weaver->write(slotId, KEY, VALUE);
185 ASSERT_TRUE(ret.isOk());
186 ASSERT_EQ(ret, WeaverStatus::OK);
187
188 bool callbackCalled = false;
189 WeaverReadStatus status;
190 std::vector<uint8_t> readValue;
191 const auto readRet =
192 weaver->read(slotId, WRONG_KEY, [&](WeaverReadStatus s, WeaverReadResponse r) {
193 callbackCalled = true;
194 status = s;
195 readValue = r.value;
196 });
197 ASSERT_TRUE(callbackCalled);
198 ASSERT_TRUE(readRet.isOk());
199 ASSERT_EQ(status, WeaverReadStatus::INCORRECT_KEY);
200 EXPECT_TRUE(readValue.empty());
201}
202
203/*
204 * Writing to an invalid slot fails
205 */
206TEST_F(WeaverHidlTest, WritingToInvalidSlotFails) {
207 WeaverStatus status;
208 WeaverConfig config;
209 const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
210 status = s;
211 config = c;
212 });
213 ASSERT_TRUE(configRet.isOk());
214 ASSERT_EQ(status, WeaverStatus::OK);
215
216 if (config.slots == std::numeric_limits<uint32_t>::max()) {
217 // If there are no invalid slots then pass
218 return;
219 }
220
221 const auto writeRet = weaver->write(config.slots, KEY, VALUE);
222 ASSERT_TRUE(writeRet.isOk());
223 ASSERT_EQ(writeRet, WeaverStatus::FAILED);
224}
225
226/*
227 * Reading from an invalid slot fails rather than incorrect key
228 */
229TEST_F(WeaverHidlTest, ReadingFromInvalidSlotFails) {
230 WeaverStatus status;
231 WeaverConfig config;
232 const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
233 status = s;
234 config = c;
235 });
236 ASSERT_TRUE(configRet.isOk());
237 ASSERT_EQ(status, WeaverStatus::OK);
238
239 if (config.slots == std::numeric_limits<uint32_t>::max()) {
240 // If there are no invalid slots then pass
241 return;
242 }
243
244 bool callbackCalled = false;
245 WeaverReadStatus readStatus;
246 std::vector<uint8_t> readValue;
247 uint32_t timeout;
248 const auto readRet =
249 weaver->read(config.slots, KEY, [&](WeaverReadStatus s, WeaverReadResponse r) {
250 callbackCalled = true;
251 readStatus = s;
252 readValue = r.value;
253 timeout = r.timeout;
254 });
255 ASSERT_TRUE(callbackCalled);
256 ASSERT_TRUE(readRet.isOk());
257 ASSERT_EQ(readStatus, WeaverReadStatus::FAILED);
258 EXPECT_TRUE(readValue.empty());
259 EXPECT_EQ(timeout, 0u);
260}
261
262/*
263 * Writing a key that is too large fails
264 */
265TEST_F(WeaverHidlTest, WriteWithTooLargeKeyFails) {
266 WeaverStatus status;
267 WeaverConfig config;
268 const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
269 status = s;
270 config = c;
271 });
272 ASSERT_TRUE(configRet.isOk());
273 ASSERT_EQ(status, WeaverStatus::OK);
274
275 std::vector<uint8_t> bigKey(config.keySize + 1);
276
277 constexpr uint32_t slotId = 0;
278 const auto writeRet = weaver->write(slotId, bigKey, VALUE);
279 ASSERT_TRUE(writeRet.isOk());
280 ASSERT_EQ(writeRet, WeaverStatus::FAILED);
281}
282
283/*
284 * Writing a value that is too large fails
285 */
286TEST_F(WeaverHidlTest, WriteWithTooLargeValueFails) {
287 WeaverStatus status;
288 WeaverConfig config;
289 const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
290 status = s;
291 config = c;
292 });
293 ASSERT_TRUE(configRet.isOk());
294 ASSERT_EQ(status, WeaverStatus::OK);
295
296 std::vector<uint8_t> bigValue(config.valueSize + 1);
297
298 constexpr uint32_t slotId = 0;
299 const auto writeRet = weaver->write(slotId, KEY, bigValue);
300 ASSERT_TRUE(writeRet.isOk());
301 ASSERT_EQ(writeRet, WeaverStatus::FAILED);
302}
303
304/*
305 * Reading with a key that is loo large fails
306 */
307TEST_F(WeaverHidlTest, ReadWithTooLargeKeyFails) {
308 WeaverStatus status;
309 WeaverConfig config;
310 const auto configRet = weaver->getConfig([&](WeaverStatus s, WeaverConfig c) {
311 status = s;
312 config = c;
313 });
314 ASSERT_TRUE(configRet.isOk());
315 ASSERT_EQ(status, WeaverStatus::OK);
316
317 std::vector<uint8_t> bigKey(config.keySize + 1);
318
319 constexpr uint32_t slotId = 0;
320 bool callbackCalled = false;
321 WeaverReadStatus readStatus;
322 std::vector<uint8_t> readValue;
323 uint32_t timeout;
324 const auto readRet =
325 weaver->read(slotId, bigKey, [&](WeaverReadStatus s, WeaverReadResponse r) {
326 callbackCalled = true;
327 readStatus = s;
328 readValue = r.value;
329 timeout = r.timeout;
330 });
331 ASSERT_TRUE(callbackCalled);
332 ASSERT_TRUE(readRet.isOk());
333 ASSERT_EQ(readStatus, WeaverReadStatus::FAILED);
334 EXPECT_TRUE(readValue.empty());
335 EXPECT_EQ(timeout, 0u);
336}