blob: f016515a6aa694459a7cb30576cf26e9746c3824 [file] [log] [blame]
ChengYou Ho10f8a482021-01-02 22:45:32 +08001/*
2 * Copyright (C) 2020 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 <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18
19#include <aidl/android/hardware/weaver/IWeaver.h>
20#include <android/binder_manager.h>
21#include <android/binder_process.h>
22
23#include <limits>
24
25using ::aidl::android::hardware::weaver::IWeaver;
26using ::aidl::android::hardware::weaver::WeaverConfig;
27using ::aidl::android::hardware::weaver::WeaverReadResponse;
ChengYou Ho20c47b42022-11-30 17:51:17 +000028using ::aidl::android::hardware::weaver::WeaverReadStatus;
ChengYou Ho10f8a482021-01-02 22:45:32 +080029
30using ::ndk::SpAIBinder;
31
32const std::vector<uint8_t> KEY{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
33const std::vector<uint8_t> WRONG_KEY{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
34const std::vector<uint8_t> VALUE{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
35const std::vector<uint8_t> OTHER_VALUE{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 255, 255};
36
37struct WeaverAidlTest : public ::testing::TestWithParam<std::string> {
38 virtual void SetUp() override {
39 weaver = IWeaver::fromBinder(
40 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
41 ASSERT_NE(weaver, nullptr);
42 }
43
44 virtual void TearDown() override {}
45
46 std::shared_ptr<IWeaver> weaver;
47};
48
49/*
50 * Checks config values are suitably large
51 */
52TEST_P(WeaverAidlTest, GetConfig) {
53 WeaverConfig config;
54
55 auto ret = weaver->getConfig(&config);
56
57 ASSERT_TRUE(ret.isOk());
58
59 EXPECT_GE(config.slots, 16u);
60 EXPECT_GE(config.keySize, 16u);
61 EXPECT_GE(config.valueSize, 16u);
62}
63
64/*
65 * Gets the config twice and checks they are the same
66 */
67TEST_P(WeaverAidlTest, GettingConfigMultipleTimesGivesSameResult) {
68 WeaverConfig config1;
69 WeaverConfig config2;
70
71 auto ret = weaver->getConfig(&config1);
72 ASSERT_TRUE(ret.isOk());
73
74 ret = weaver->getConfig(&config2);
75 ASSERT_TRUE(ret.isOk());
76
77 EXPECT_EQ(config1, config2);
78}
79
80/*
81 * Gets the number of slots from the config and writes a key and value to the last one
82 */
83TEST_P(WeaverAidlTest, WriteToLastSlot) {
84 WeaverConfig config;
85 const auto configRet = weaver->getConfig(&config);
86
87 ASSERT_TRUE(configRet.isOk());
88
89 const uint32_t lastSlot = config.slots - 1;
90 const auto writeRet = weaver->write(lastSlot, KEY, VALUE);
91 ASSERT_TRUE(writeRet.isOk());
92}
93
94/*
95 * Writes a key and value to a slot
96 * Reads the slot with the same key and receives the value that was previously written
97 */
98TEST_P(WeaverAidlTest, WriteFollowedByReadGivesTheSameValue) {
99 constexpr uint32_t slotId = 0;
100 const auto ret = weaver->write(slotId, KEY, VALUE);
101 ASSERT_TRUE(ret.isOk());
102
103 WeaverReadResponse response;
104 std::vector<uint8_t> readValue;
105 uint32_t timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000106 WeaverReadStatus status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800107 const auto readRet = weaver->read(slotId, KEY, &response);
108
109 readValue = response.value;
110 timeout = response.timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000111 status = response.status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800112
113 ASSERT_TRUE(readRet.isOk());
114 EXPECT_EQ(readValue, VALUE);
115 EXPECT_EQ(timeout, 0u);
ChengYou Ho20c47b42022-11-30 17:51:17 +0000116 EXPECT_EQ(status, WeaverReadStatus::OK);
ChengYou Ho10f8a482021-01-02 22:45:32 +0800117}
118
119/*
120 * Writes a key and value to a slot
121 * Overwrites the slot with a new key and value
122 * Reads the slot with the new key and receives the new value
123 */
124TEST_P(WeaverAidlTest, OverwritingSlotUpdatesTheValue) {
125 constexpr uint32_t slotId = 0;
126 const auto initialWriteRet = weaver->write(slotId, WRONG_KEY, VALUE);
127 ASSERT_TRUE(initialWriteRet.isOk());
128
129 const auto overwriteRet = weaver->write(slotId, KEY, OTHER_VALUE);
130 ASSERT_TRUE(overwriteRet.isOk());
131
132 WeaverReadResponse response;
133 std::vector<uint8_t> readValue;
134 uint32_t timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000135 WeaverReadStatus status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800136 const auto readRet = weaver->read(slotId, KEY, &response);
137
138 readValue = response.value;
139 timeout = response.timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000140 status = response.status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800141
142 ASSERT_TRUE(readRet.isOk());
143 EXPECT_EQ(readValue, OTHER_VALUE);
144 EXPECT_EQ(timeout, 0u);
ChengYou Ho20c47b42022-11-30 17:51:17 +0000145 EXPECT_EQ(status, WeaverReadStatus::OK);
ChengYou Ho10f8a482021-01-02 22:45:32 +0800146}
147
148/*
149 * Writes a key and value to a slot
150 * Reads the slot with a different key so does not receive the value
151 */
152TEST_P(WeaverAidlTest, WriteFollowedByReadWithWrongKeyDoesNotGiveTheValue) {
153 constexpr uint32_t slotId = 0;
154 const auto ret = weaver->write(slotId, KEY, VALUE);
155 ASSERT_TRUE(ret.isOk());
156
157 WeaverReadResponse response;
158 std::vector<uint8_t> readValue;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000159 WeaverReadStatus status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800160 const auto readRet =
161 weaver->read(slotId, WRONG_KEY, &response);
162
163 readValue = response.value;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000164 status = response.status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800165
ChengYou Ho20c47b42022-11-30 17:51:17 +0000166 ASSERT_TRUE(readRet.isOk());
ChengYou Ho10f8a482021-01-02 22:45:32 +0800167 EXPECT_TRUE(readValue.empty());
ChengYou Ho20c47b42022-11-30 17:51:17 +0000168 EXPECT_EQ(status, WeaverReadStatus::INCORRECT_KEY);
ChengYou Ho10f8a482021-01-02 22:45:32 +0800169}
170
171/*
172 * Writing to an invalid slot fails
173 */
174TEST_P(WeaverAidlTest, WritingToInvalidSlotFails) {
175 WeaverConfig config;
176 const auto configRet = weaver->getConfig(&config);
177 ASSERT_TRUE(configRet.isOk());
178
179 if (config.slots == std::numeric_limits<uint32_t>::max()) {
180 // If there are no invalid slots then pass
181 return;
182 }
183
184 const auto writeRet = weaver->write(config.slots, KEY, VALUE);
185 ASSERT_FALSE(writeRet.isOk());
186}
187
188/*
189 * Reading from an invalid slot fails rather than incorrect key
190 */
191TEST_P(WeaverAidlTest, ReadingFromInvalidSlotFails) {
192 WeaverConfig config;
193 const auto configRet = weaver->getConfig(&config);
194 ASSERT_TRUE(configRet.isOk());
195
196 if (config.slots == std::numeric_limits<uint32_t>::max()) {
197 // If there are no invalid slots then pass
198 return;
199 }
200
201 WeaverReadResponse response;
202 std::vector<uint8_t> readValue;
203 uint32_t timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000204 WeaverReadStatus status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800205 const auto readRet =
206 weaver->read(config.slots, KEY, &response);
207
208 readValue = response.value;
209 timeout = response.timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000210 status = response.status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800211
ChengYou Ho20c47b42022-11-30 17:51:17 +0000212 ASSERT_TRUE(readRet.isOk());
ChengYou Ho10f8a482021-01-02 22:45:32 +0800213 EXPECT_TRUE(readValue.empty());
214 EXPECT_EQ(timeout, 0u);
ChengYou Ho20c47b42022-11-30 17:51:17 +0000215 EXPECT_EQ(status, WeaverReadStatus::FAILED);
ChengYou Ho10f8a482021-01-02 22:45:32 +0800216}
217
218/*
219 * Writing a key that is too large fails
220 */
221TEST_P(WeaverAidlTest, WriteWithTooLargeKeyFails) {
222 WeaverConfig config;
223 const auto configRet = weaver->getConfig(&config);
224 ASSERT_TRUE(configRet.isOk());
225
226 std::vector<uint8_t> bigKey(config.keySize + 1);
227
228 constexpr uint32_t slotId = 0;
229 const auto writeRet = weaver->write(slotId, bigKey, VALUE);
230 ASSERT_FALSE(writeRet.isOk());
231}
232
233/*
234 * Writing a value that is too large fails
235 */
236TEST_P(WeaverAidlTest, WriteWithTooLargeValueFails) {
237 WeaverConfig config;
238 const auto configRet = weaver->getConfig(&config);
239 ASSERT_TRUE(configRet.isOk());
240
241 std::vector<uint8_t> bigValue(config.valueSize + 1);
242
243 constexpr uint32_t slotId = 0;
244 const auto writeRet = weaver->write(slotId, KEY, bigValue);
245 ASSERT_FALSE(writeRet.isOk());
246}
247
248/*
249 * Reading with a key that is loo large fails
250 */
251TEST_P(WeaverAidlTest, ReadWithTooLargeKeyFails) {
252 WeaverConfig config;
253 const auto configRet = weaver->getConfig(&config);
254 ASSERT_TRUE(configRet.isOk());
255
256 std::vector<uint8_t> bigKey(config.keySize + 1);
257
258 constexpr uint32_t slotId = 0;
259 WeaverReadResponse response;
260 std::vector<uint8_t> readValue;
261 uint32_t timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000262 WeaverReadStatus status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800263 const auto readRet =
264 weaver->read(slotId, bigKey, &response);
265
266 readValue = response.value;
267 timeout = response.timeout;
ChengYou Ho20c47b42022-11-30 17:51:17 +0000268 status = response.status;
ChengYou Ho10f8a482021-01-02 22:45:32 +0800269
ChengYou Ho20c47b42022-11-30 17:51:17 +0000270 ASSERT_TRUE(readRet.isOk());
ChengYou Ho10f8a482021-01-02 22:45:32 +0800271 EXPECT_TRUE(readValue.empty());
272 EXPECT_EQ(timeout, 0u);
ChengYou Ho20c47b42022-11-30 17:51:17 +0000273 EXPECT_EQ(status, WeaverReadStatus::FAILED);
ChengYou Ho10f8a482021-01-02 22:45:32 +0800274}
275
276GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WeaverAidlTest);
277INSTANTIATE_TEST_SUITE_P(
278 PerInstance, WeaverAidlTest,
279 testing::ValuesIn(android::getAidlHalInstanceNames(IWeaver::descriptor)),
280 android::PrintInstanceNameToString);
281
282int main(int argc, char** argv) {
283 ::testing::InitGoogleTest(&argc, argv);
284 ABinderProcess_setThreadPoolMaxThreadCount(1);
285 ABinderProcess_startThreadPool();
286 return RUN_ALL_TESTS();
287}