blob: d953ab0fb296559ee502aca1f99434a0098ab61a [file] [log] [blame]
Janis Danisevskise0b19032017-11-09 14:57:39 -08001/*
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 "ConfirmationIOHidlHalTest"
18#include <cutils/log.h>
19
20#include <algorithm>
21#include <iostream>
22#include <memory>
23
24#include <android/hardware/confirmationui/1.0/IConfirmationResultCallback.h>
25#include <android/hardware/confirmationui/1.0/IConfirmationUI.h>
26#include <android/hardware/confirmationui/1.0/types.h>
27#include <android/hardware/confirmationui/support/confirmationui_utils.h>
28
Janis Danisevskisb78beb52020-02-27 16:31:24 -080029#include <gtest/gtest.h>
30
Janis Danisevskise0b19032017-11-09 14:57:39 -080031#include <VtsHalHidlTargetCallbackBase.h>
Janis Danisevskisb78beb52020-02-27 16:31:24 -080032
33#include <hidl/GtestPrinter.h>
34#include <hidl/ServiceManagement.h>
Janis Danisevskise0b19032017-11-09 14:57:39 -080035
36#include <openssl/hmac.h>
37#include <openssl/sha.h>
38
39#include <cn-cbor/cn-cbor.h>
40
41using ::android::sp;
42
43using ::std::string;
44
45namespace android {
46namespace hardware {
47
48namespace confirmationui {
49namespace V1_0 {
50
51namespace test {
52namespace {
Janis Danisevskisfe584fb2018-04-23 08:56:16 -070053const support::auth_token_key_t testKey(static_cast<uint8_t>(TestKeyBits::BYTE));
54
Janis Danisevskise0b19032017-11-09 14:57:39 -080055class HMacImplementation {
56 public:
Janis Danisevskisfe584fb2018-04-23 08:56:16 -070057 static support::NullOr<support::hmac_t> hmac256(
58 const support::auth_token_key_t& key,
59 std::initializer_list<support::ByteBufferProxy> buffers) {
Janis Danisevskise0b19032017-11-09 14:57:39 -080060 HMAC_CTX hmacCtx;
61 HMAC_CTX_init(&hmacCtx);
Janis Danisevskisfe584fb2018-04-23 08:56:16 -070062 if (!HMAC_Init_ex(&hmacCtx, key.data(), key.size(), EVP_sha256(), nullptr)) {
Janis Danisevskise0b19032017-11-09 14:57:39 -080063 return {};
64 }
65 for (auto& buffer : buffers) {
66 if (!HMAC_Update(&hmacCtx, buffer.data(), buffer.size())) {
67 return {};
68 }
69 }
Janis Danisevskisfe584fb2018-04-23 08:56:16 -070070 support::hmac_t result;
Janis Danisevskise0b19032017-11-09 14:57:39 -080071 if (!HMAC_Final(&hmacCtx, result.data(), nullptr)) {
72 return {};
73 }
74 return result;
75 }
76};
77
78using HMacer = support::HMac<HMacImplementation>;
79
Janis Danisevskise0b19032017-11-09 14:57:39 -080080template <typename... Data>
81hidl_vec<uint8_t> testHMAC(const Data&... data) {
Janis Danisevskise0b19032017-11-09 14:57:39 -080082 auto hmac = HMacer::hmac256(testKey, data...);
83 if (!hmac.isOk()) {
84 EXPECT_TRUE(false) << "Failed to compute test hmac. This is a self-test error.";
85 return {};
86 }
Janis Danisevskisfe584fb2018-04-23 08:56:16 -070087 hidl_vec<uint8_t> result(hmac.value().size());
88 copy(hmac.value().data(), hmac.value().data() + hmac.value().size(), result.data());
Janis Danisevskise0b19032017-11-09 14:57:39 -080089 return result;
90}
91
92using ::android::hardware::keymaster::V4_0::HardwareAuthToken;
93using ::android::hardware::keymaster::V4_0::HardwareAuthenticatorType;
94
95template <typename T>
96auto toBytes(const T& v) -> const uint8_t (&)[sizeof(T)] {
97 return *reinterpret_cast<const uint8_t(*)[sizeof(T)]>(&v);
98}
99
100HardwareAuthToken makeTestToken(const TestModeCommands command, uint64_t timestamp = 0) {
101 HardwareAuthToken auth_token;
102 auth_token.challenge = static_cast<uint64_t>(command);
103 auth_token.userId = 0;
104 auth_token.authenticatorId = 0;
105 auth_token.authenticatorType = HardwareAuthenticatorType::NONE;
106 auth_token.timestamp = timestamp;
107
108 // Canonical form of auth-token v0
109 // version (1 byte)
110 // challenge (8 bytes)
111 // user_id (8 bytes)
112 // authenticator_id (8 bytes)
113 // authenticator_type (4 bytes)
114 // timestamp (8 bytes)
115 // total 37 bytes
116 auth_token.mac = testHMAC("\0",
117 toBytes(auth_token.challenge), //
118 toBytes(auth_token.userId), //
119 toBytes(auth_token.authenticatorId), //
120 toBytes(support::hton(auth_token.authenticatorType)), //
121 toBytes(support::hton(auth_token.timestamp))); //
122
123 return auth_token;
124}
125
126#define DEBUG_CONFRIMATIONUI_UTILS_TEST
127
128#ifdef DEBUG_CONFRIMATIONUI_UTILS_TEST
129std::ostream& hexdump(std::ostream& out, const uint8_t* data, size_t size) {
130 for (size_t i = 0; i < size; ++i) {
131 uint8_t byte = data[i];
132 out << std::hex << std::setw(2) << std::setfill('0') << (unsigned)byte;
133 switch (i & 0xf) {
134 case 0xf:
135 out << "\n";
136 break;
137 case 7:
138 out << " ";
139 break;
140 default:
141 out << " ";
142 break;
143 }
144 }
145 return out;
146}
147#endif
148
149constexpr char hex_value[256] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
152 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9'
153 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F'
154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
155 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f'
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
162 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
165
166std::string hex2str(std::string a) {
167 std::string b;
168 size_t num = a.size() / 2;
169 b.resize(num);
170 for (size_t i = 0; i < num; i++) {
171 b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]);
172 }
173 return b;
174}
175
176} // namespace
177
178class ConfirmationArgs {
179 public:
180 ResponseCode error_;
181 hidl_vec<uint8_t> formattedMessage_;
182 hidl_vec<uint8_t> confirmationToken_;
183 bool verifyConfirmationToken() {
184 static constexpr char confirmationPrefix[] = "confirmation token";
185 EXPECT_EQ(32U, confirmationToken_.size());
186 return 32U == confirmationToken_.size() &&
187 !memcmp(confirmationToken_.data(),
188 testHMAC(confirmationPrefix, formattedMessage_).data(), 32);
189 }
190};
191
192class ConfirmationTestCallback : public ::testing::VtsHalHidlTargetCallbackBase<ConfirmationArgs>,
193 public IConfirmationResultCallback {
194 public:
195 Return<void> result(ResponseCode error, const hidl_vec<uint8_t>& formattedMessage,
196 const hidl_vec<uint8_t>& confirmationToken) override {
197 ConfirmationArgs args;
198 args.error_ = error;
199 args.formattedMessage_ = formattedMessage;
200 args.confirmationToken_ = confirmationToken;
201 NotifyFromCallback(args);
202 return Void();
203 }
204};
205
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800206class ConfirmationUIHidlTest : public ::testing::TestWithParam<std::string> {
207 public:
208 void TearDown() override { confirmator_->abort(); }
209 void SetUp() override {
210 confirmator_ = IConfirmationUI::getService(GetParam());
Janis Danisevskise0b19032017-11-09 14:57:39 -0800211 ASSERT_NE(nullptr, confirmator_.get());
212 }
213
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800214 protected:
215 sp<IConfirmationUI> confirmator_;
Janis Danisevskise0b19032017-11-09 14:57:39 -0800216};
217
Janis Danisevskise0b19032017-11-09 14:57:39 -0800218#define ASSERT_HAL_CALL(expected, call) \
219 { \
220 auto result = call; \
221 ASSERT_TRUE(result.isOk()); \
222 ASSERT_EQ(expected, static_cast<decltype(expected)>(result)); \
223 }
224
225struct CnCborDeleter {
226 void operator()(cn_cbor* ptr) { cn_cbor_free(ptr); }
227};
228
229typedef std::unique_ptr<cn_cbor, CnCborDeleter> CnCborPtr;
230
231// Simulates the User taping Ok
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800232TEST_P(ConfirmationUIHidlTest, UserOkTest) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800233 static constexpr char test_prompt[] = "Me first, gimme gimme!";
234 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
235 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
236 hidl_string prompt_text(test_prompt);
237 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
238 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800239 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800240
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800241 ASSERT_HAL_CALL(ResponseCode::OK, confirmator_->deliverSecureInputEvent(
242 makeTestToken(TestModeCommands::OK_EVENT)));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800243
244 auto result = conf_cb->WaitForCallback();
245 ASSERT_EQ(ResponseCode::OK, result.args->error_);
246
247 ASSERT_TRUE(result.args->verifyConfirmationToken());
248
249 cn_cbor_errback cn_cbor_error;
250 auto parsed_message =
251 CnCborPtr(cn_cbor_decode(result.args->formattedMessage_.data(),
252 result.args->formattedMessage_.size(), &cn_cbor_error));
253 // is parsable CBOR
254 ASSERT_TRUE(parsed_message.get());
255 // is a map
256 ASSERT_EQ(CN_CBOR_MAP, parsed_message->type);
257
258 // the message must have exactly 2 key value pairs.
259 // cn_cbor holds 2*<no_of_pairs> in the length field
260 ASSERT_EQ(4, parsed_message->length);
261 // map has key "prompt"
262 auto prompt = cn_cbor_mapget_string(parsed_message.get(), "prompt");
263 ASSERT_TRUE(prompt);
264 ASSERT_EQ(CN_CBOR_TEXT, prompt->type);
265 ASSERT_EQ(22, prompt->length);
266 ASSERT_EQ(0, memcmp(test_prompt, prompt->v.str, 22));
267 // map has key "extra"
268 auto extra_out = cn_cbor_mapget_string(parsed_message.get(), "extra");
269 ASSERT_TRUE(extra_out);
270 ASSERT_EQ(CN_CBOR_BYTES, extra_out->type);
271 ASSERT_EQ(3, extra_out->length);
272 ASSERT_EQ(0, memcmp(test_extra, extra_out->v.bytes, 3));
273}
274
275// Initiates a confirmation prompt with a message that is too long
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800276TEST_P(ConfirmationUIHidlTest, MessageTooLongTest) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800277 static constexpr uint8_t test_extra[static_cast<uint32_t>(MessageSize::MAX)] = {};
278 static constexpr char test_prompt[] = "D\'oh!";
279 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
280 hidl_string prompt_text(test_prompt);
281 hidl_vec<uint8_t> extra(test_extra, test_extra + sizeof(test_extra));
282 ASSERT_HAL_CALL(ResponseCode::UIErrorMessageTooLong,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800283 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800284}
285
286// If the message gets very long some HAL implementations might fail even before the message
287// reaches the trusted app implementation. But the HAL must still diagnose the correct error.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800288TEST_P(ConfirmationUIHidlTest, MessageWayTooLongTest) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800289 static constexpr uint8_t test_extra[static_cast<uint32_t>(MessageSize::MAX) * 10] = {};
290 static constexpr char test_prompt[] = "D\'oh!";
291 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
292 hidl_string prompt_text(test_prompt);
293 hidl_vec<uint8_t> extra(test_extra, test_extra + sizeof(test_extra));
294 ASSERT_HAL_CALL(ResponseCode::UIErrorMessageTooLong,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800295 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800296}
297
298// Simulates the User tapping the Cancel
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800299TEST_P(ConfirmationUIHidlTest, UserCancelTest) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800300 static constexpr char test_prompt[] = "Me first, gimme gimme!";
301 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
302 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
303 hidl_string prompt_text(test_prompt);
304 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
305 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800306 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800307
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800308 ASSERT_HAL_CALL(ResponseCode::OK, confirmator_->deliverSecureInputEvent(
309 makeTestToken(TestModeCommands::CANCEL_EVENT)));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800310
311 auto result = conf_cb->WaitForCallback();
312 ASSERT_EQ(ResponseCode::Canceled, result.args->error_);
313
314 ASSERT_EQ(0U, result.args->confirmationToken_.size());
315 ASSERT_EQ(0U, result.args->formattedMessage_.size());
316}
317
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800318// Simulates the framework cancelling an ongoing prompt
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800319TEST_P(ConfirmationUIHidlTest, AbortTest) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800320 static constexpr char test_prompt[] = "Me first, gimme gimme!";
321 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
322 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
323 hidl_string prompt_text(test_prompt);
324 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
325 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800326 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800327
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800328 confirmator_->abort();
Janis Danisevskise0b19032017-11-09 14:57:39 -0800329
330 auto result = conf_cb->WaitForCallback();
331 ASSERT_EQ(ResponseCode::Aborted, result.args->error_);
332 ASSERT_EQ(0U, result.args->confirmationToken_.size());
333 ASSERT_EQ(0U, result.args->formattedMessage_.size());
334}
335
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800336// Tests if the confirmation dialog can successfully render 100 'W' characters as required by
337// the design guidelines.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800338TEST_P(ConfirmationUIHidlTest, PortableMessageTest1) {
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800339 static constexpr char test_prompt[] =
340 "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
341 "WWWWWWWWWWWWWW";
342 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
343 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
344 hidl_string prompt_text(test_prompt);
345 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
346 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800347 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800348
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800349 confirmator_->abort();
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800350
351 auto result = conf_cb->WaitForCallback();
352 ASSERT_EQ(ResponseCode::Aborted, result.args->error_);
353 ASSERT_EQ(0U, result.args->confirmationToken_.size());
354 ASSERT_EQ(0U, result.args->formattedMessage_.size());
355}
356
357// Tests if the confirmation dialog can successfully render 100 'W' characters as required by
358// the design guidelines in magnified mode.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800359TEST_P(ConfirmationUIHidlTest, PortableMessageTest1Magnified) {
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800360 static constexpr char test_prompt[] =
361 "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
362 "WWWWWWWWWWWWWW";
363 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
364 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
365 hidl_string prompt_text(test_prompt);
366 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
367 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800368 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en",
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800369 {UIOption::AccessibilityMagnified}));
370
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800371 confirmator_->abort();
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800372
373 auto result = conf_cb->WaitForCallback();
374 ASSERT_EQ(ResponseCode::Aborted, result.args->error_);
375 ASSERT_EQ(0U, result.args->confirmationToken_.size());
376 ASSERT_EQ(0U, result.args->formattedMessage_.size());
377}
378
379// Tests if the confirmation dialog can successfully render 8 groups of 12 'W' characters as
380// required by the design guidelines.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800381TEST_P(ConfirmationUIHidlTest, PortableMessageTest2) {
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800382 static constexpr char test_prompt[] =
383 "WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW "
384 "WWWWWWWWWWWW WWWWWWWWWWWW";
385 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
386 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
387 hidl_string prompt_text(test_prompt);
388 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
389 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800390 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800391
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800392 confirmator_->abort();
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800393
394 auto result = conf_cb->WaitForCallback();
395 ASSERT_EQ(ResponseCode::Aborted, result.args->error_);
396 ASSERT_EQ(0U, result.args->confirmationToken_.size());
397 ASSERT_EQ(0U, result.args->formattedMessage_.size());
398}
399
400// Tests if the confirmation dialog can successfully render 8 groups of 12 'W' characters as
401// required by the design guidelines in magnified mode.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800402TEST_P(ConfirmationUIHidlTest, PortableMessageTest2Magnified) {
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800403 static constexpr char test_prompt[] =
404 "WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW "
405 "WWWWWWWWWWWW WWWWWWWWWWWW";
406 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
407 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
408 hidl_string prompt_text(test_prompt);
409 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
410 ASSERT_HAL_CALL(ResponseCode::OK,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800411 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en",
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800412 {UIOption::AccessibilityMagnified}));
413
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800414 confirmator_->abort();
Janis Danisevskis8d4f06f2020-02-20 16:30:55 -0800415
416 auto result = conf_cb->WaitForCallback();
417 ASSERT_EQ(ResponseCode::Aborted, result.args->error_);
418 ASSERT_EQ(0U, result.args->confirmationToken_.size());
419 ASSERT_EQ(0U, result.args->formattedMessage_.size());
420}
421
Janis Danisevskise0b19032017-11-09 14:57:39 -0800422// Passing malformed UTF-8 to the confirmation UI
423// This test passes a string that ends in the middle of a multibyte character
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800424TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test1) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800425 static constexpr char test_prompt[] = {char(0xc0), 0};
426 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
427 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
428 hidl_string prompt_text(test_prompt);
429 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
430 ASSERT_HAL_CALL(ResponseCode::UIErrorMalformedUTF8Encoding,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800431 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800432}
433
434// Passing malformed UTF-8 to the confirmation UI
435// This test passes a string with a 5-byte character.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800436TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test2) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800437 static constexpr char test_prompt[] = {char(0xf8), char(0x82), char(0x82),
438 char(0x82), char(0x82), 0};
439 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
440 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
441 hidl_string prompt_text(test_prompt);
442 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
443 ASSERT_HAL_CALL(ResponseCode::UIErrorMalformedUTF8Encoding,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800444 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800445}
446
447// Passing malformed UTF-8 to the confirmation UI
448// This test passes a string with a 2-byte character followed by a stray non UTF-8 character.
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800449TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test3) {
Janis Danisevskise0b19032017-11-09 14:57:39 -0800450 static constexpr char test_prompt[] = {char(0xc0), char(0x82), char(0x83), 0};
451 static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3};
452 sp<ConfirmationTestCallback> conf_cb = new ConfirmationTestCallback;
453 hidl_string prompt_text(test_prompt);
454 hidl_vec<uint8_t> extra(test_extra, test_extra + 3);
455 ASSERT_HAL_CALL(ResponseCode::UIErrorMalformedUTF8Encoding,
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800456 confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
Janis Danisevskise0b19032017-11-09 14:57:39 -0800457}
458
459// Test the implementation of HMAC SHA 256 against a golden blob.
460TEST(ConfirmationUITestSelfTest, HMAC256SelfTest) {
461 const char key_str[32] = "keykeykeykeykeykeykeykeykeykeyk";
462 const uint8_t(&key)[32] = *reinterpret_cast<const uint8_t(*)[32]>(key_str);
463 auto expected = hex2str("2377fbcaa7fb3f6c20cfa1d9ebc60e9922cf58c909e25e300f3cb57f7805c886");
464 auto result = HMacer::hmac256(key, "value1", "value2", "value3");
465
466#ifdef DEBUG_CONFRIMATIONUI_UTILS_TEST
467 hexdump(std::cout, reinterpret_cast<const uint8_t*>(expected.data()), 32) << std::endl;
468 hexdump(std::cout, result.value().data(), 32) << std::endl;
469#endif
470
471 support::ByteBufferProxy expected_bytes(expected);
472 ASSERT_TRUE(result.isOk());
473 ASSERT_EQ(expected, result.value());
474}
475
Janis Danisevskisb78beb52020-02-27 16:31:24 -0800476INSTANTIATE_TEST_SUITE_P(
477 PerInstance, ConfirmationUIHidlTest,
478 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IConfirmationUI::descriptor)),
479 android::hardware::PrintInstanceNameToString);
480
Janis Danisevskise0b19032017-11-09 14:57:39 -0800481} // namespace test
482} // namespace V1_0
483} // namespace confirmationui
484} // namespace hardware
485} // namespace android