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 "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 
29 #include <gtest/gtest.h>
30 
31 #include <VtsHalHidlTargetCallbackBase.h>
32 
33 #include <hidl/GtestPrinter.h>
34 #include <hidl/ServiceManagement.h>
35 
36 #include <openssl/hmac.h>
37 #include <openssl/sha.h>
38 
39 #include <cn-cbor/cn-cbor.h>
40 
41 using ::android::sp;
42 
43 using ::std::string;
44 
45 namespace android {
46 namespace hardware {
47 
48 namespace confirmationui {
49 namespace V1_0 {
50 
51 namespace test {
52 namespace {
53 const support::auth_token_key_t testKey(static_cast<uint8_t>(TestKeyBits::BYTE));
54 
55 class HMacImplementation {
56    public:
hmac256(const support::auth_token_key_t & key,std::initializer_list<support::ByteBufferProxy> buffers)57     static support::NullOr<support::hmac_t> hmac256(
58         const support::auth_token_key_t& key,
59         std::initializer_list<support::ByteBufferProxy> buffers) {
60         HMAC_CTX hmacCtx;
61         HMAC_CTX_init(&hmacCtx);
62         if (!HMAC_Init_ex(&hmacCtx, key.data(), key.size(), EVP_sha256(), nullptr)) {
63             return {};
64         }
65         for (auto& buffer : buffers) {
66             if (!HMAC_Update(&hmacCtx, buffer.data(), buffer.size())) {
67                 return {};
68             }
69         }
70         support::hmac_t result;
71         if (!HMAC_Final(&hmacCtx, result.data(), nullptr)) {
72             return {};
73         }
74         return result;
75     }
76 };
77 
78 using HMacer = support::HMac<HMacImplementation>;
79 
80 template <typename... Data>
testHMAC(const Data &...data)81 hidl_vec<uint8_t> testHMAC(const Data&... data) {
82     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     }
87     hidl_vec<uint8_t> result(hmac.value().size());
88     copy(hmac.value().data(), hmac.value().data() + hmac.value().size(), result.data());
89     return result;
90 }
91 
92 using ::android::hardware::keymaster::V4_0::HardwareAuthToken;
93 using ::android::hardware::keymaster::V4_0::HardwareAuthenticatorType;
94 
95 template <typename T>
toBytes(const T & v)96 auto toBytes(const T& v) -> const uint8_t (&)[sizeof(T)] {
97     return *reinterpret_cast<const uint8_t(*)[sizeof(T)]>(&v);
98 }
99 
100 HardwareAuthToken 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
hexdump(std::ostream & out,const uint8_t * data,size_t size)129 std::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 
149 constexpr 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 
hex2str(std::string a)166 std::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 
178 class ConfirmationArgs {
179    public:
180     ResponseCode error_;
181     hidl_vec<uint8_t> formattedMessage_;
182     hidl_vec<uint8_t> confirmationToken_;
verifyConfirmationToken()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 
192 class ConfirmationTestCallback : public ::testing::VtsHalHidlTargetCallbackBase<ConfirmationArgs>,
193                                  public IConfirmationResultCallback {
194    public:
result(ResponseCode error,const hidl_vec<uint8_t> & formattedMessage,const hidl_vec<uint8_t> & confirmationToken)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 
206 class ConfirmationUIHidlTest : public ::testing::TestWithParam<std::string> {
207   public:
TearDown()208     void TearDown() override { confirmator_->abort(); }
SetUp()209     void SetUp() override {
210         confirmator_ = IConfirmationUI::getService(GetParam());
211         ASSERT_NE(nullptr, confirmator_.get());
212     }
213 
214   protected:
215     sp<IConfirmationUI> confirmator_;
216 };
217 
218 #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 
225 struct CnCborDeleter {
operator ()android::hardware::confirmationui::V1_0::test::CnCborDeleter226     void operator()(cn_cbor* ptr) { cn_cbor_free(ptr); }
227 };
228 
229 typedef std::unique_ptr<cn_cbor, CnCborDeleter> CnCborPtr;
230 
231 // Simulates the User taping Ok
TEST_P(ConfirmationUIHidlTest,UserOkTest)232 TEST_P(ConfirmationUIHidlTest, UserOkTest) {
233     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,
239                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
240 
241     ASSERT_HAL_CALL(ResponseCode::OK, confirmator_->deliverSecureInputEvent(
242                                               makeTestToken(TestModeCommands::OK_EVENT)));
243 
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
TEST_P(ConfirmationUIHidlTest,MessageTooLongTest)276 TEST_P(ConfirmationUIHidlTest, MessageTooLongTest) {
277     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,
283                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
284 }
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.
TEST_P(ConfirmationUIHidlTest,MessageWayTooLongTest)288 TEST_P(ConfirmationUIHidlTest, MessageWayTooLongTest) {
289     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,
295                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
296 }
297 
298 // Simulates the User tapping the Cancel
TEST_P(ConfirmationUIHidlTest,UserCancelTest)299 TEST_P(ConfirmationUIHidlTest, UserCancelTest) {
300     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,
306                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
307 
308     ASSERT_HAL_CALL(ResponseCode::OK, confirmator_->deliverSecureInputEvent(
309                                               makeTestToken(TestModeCommands::CANCEL_EVENT)));
310 
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 
318 // Simulates the framework cancelling an ongoing prompt
TEST_P(ConfirmationUIHidlTest,AbortTest)319 TEST_P(ConfirmationUIHidlTest, AbortTest) {
320     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,
326                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
327 
328     confirmator_->abort();
329 
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 
336 // Tests if the confirmation dialog can successfully render 100 'W' characters as required by
337 // the design guidelines.
TEST_P(ConfirmationUIHidlTest,PortableMessageTest1)338 TEST_P(ConfirmationUIHidlTest, PortableMessageTest1) {
339     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,
347                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
348 
349     confirmator_->abort();
350 
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.
TEST_P(ConfirmationUIHidlTest,PortableMessageTest1Magnified)359 TEST_P(ConfirmationUIHidlTest, PortableMessageTest1Magnified) {
360     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,
368                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en",
369                                                          {UIOption::AccessibilityMagnified}));
370 
371     confirmator_->abort();
372 
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.
TEST_P(ConfirmationUIHidlTest,PortableMessageTest2)381 TEST_P(ConfirmationUIHidlTest, PortableMessageTest2) {
382     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,
390                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
391 
392     confirmator_->abort();
393 
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.
TEST_P(ConfirmationUIHidlTest,PortableMessageTest2Magnified)402 TEST_P(ConfirmationUIHidlTest, PortableMessageTest2Magnified) {
403     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,
411                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en",
412                                                          {UIOption::AccessibilityMagnified}));
413 
414     confirmator_->abort();
415 
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 
422 // Passing malformed UTF-8 to the confirmation UI
423 // This test passes a string that ends in the middle of a multibyte character
TEST_P(ConfirmationUIHidlTest,MalformedUTF8Test1)424 TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test1) {
425     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,
431                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
432 }
433 
434 // Passing malformed UTF-8 to the confirmation UI
435 // This test passes a string with a 5-byte character.
TEST_P(ConfirmationUIHidlTest,MalformedUTF8Test2)436 TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test2) {
437     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,
444                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
445 }
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.
TEST_P(ConfirmationUIHidlTest,MalformedUTF8Test3)449 TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test3) {
450     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,
456                     confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}));
457 }
458 
459 // Test the implementation of HMAC SHA 256 against a golden blob.
TEST(ConfirmationUITestSelfTest,HMAC256SelfTest)460 TEST(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 
476 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ConfirmationUIHidlTest);
477 INSTANTIATE_TEST_SUITE_P(
478         PerInstance, ConfirmationUIHidlTest,
479         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IConfirmationUI::descriptor)),
480         android::hardware::PrintInstanceNameToString);
481 
482 }  // namespace test
483 }  // namespace V1_0
484 }  // namespace confirmationui
485 }  // namespace hardware
486 }  // namespace android
487