Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
| 30 | |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 31 | #include <VtsHalHidlTargetCallbackBase.h> |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 32 | |
| 33 | #include <hidl/GtestPrinter.h> |
| 34 | #include <hidl/ServiceManagement.h> |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 { |
Janis Danisevskis | fe584fb | 2018-04-23 08:56:16 -0700 | [diff] [blame] | 53 | const support::auth_token_key_t testKey(static_cast<uint8_t>(TestKeyBits::BYTE)); |
| 54 | |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 55 | class HMacImplementation { |
| 56 | public: |
Janis Danisevskis | fe584fb | 2018-04-23 08:56:16 -0700 | [diff] [blame] | 57 | static support::NullOr<support::hmac_t> hmac256( |
| 58 | const support::auth_token_key_t& key, |
| 59 | std::initializer_list<support::ByteBufferProxy> buffers) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 60 | HMAC_CTX hmacCtx; |
| 61 | HMAC_CTX_init(&hmacCtx); |
Janis Danisevskis | fe584fb | 2018-04-23 08:56:16 -0700 | [diff] [blame] | 62 | if (!HMAC_Init_ex(&hmacCtx, key.data(), key.size(), EVP_sha256(), nullptr)) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 63 | return {}; |
| 64 | } |
| 65 | for (auto& buffer : buffers) { |
| 66 | if (!HMAC_Update(&hmacCtx, buffer.data(), buffer.size())) { |
| 67 | return {}; |
| 68 | } |
| 69 | } |
Janis Danisevskis | fe584fb | 2018-04-23 08:56:16 -0700 | [diff] [blame] | 70 | support::hmac_t result; |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 71 | if (!HMAC_Final(&hmacCtx, result.data(), nullptr)) { |
| 72 | return {}; |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | using HMacer = support::HMac<HMacImplementation>; |
| 79 | |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 80 | template <typename... Data> |
| 81 | hidl_vec<uint8_t> testHMAC(const Data&... data) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 | } |
Janis Danisevskis | fe584fb | 2018-04-23 08:56:16 -0700 | [diff] [blame] | 87 | hidl_vec<uint8_t> result(hmac.value().size()); |
| 88 | copy(hmac.value().data(), hmac.value().data() + hmac.value().size(), result.data()); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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> |
| 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 |
| 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 | |
| 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_; |
| 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: |
| 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 Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 206 | class ConfirmationUIHidlTest : public ::testing::TestWithParam<std::string> { |
| 207 | public: |
| 208 | void TearDown() override { confirmator_->abort(); } |
| 209 | void SetUp() override { |
| 210 | confirmator_ = IConfirmationUI::getService(GetParam()); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 211 | ASSERT_NE(nullptr, confirmator_.get()); |
| 212 | } |
| 213 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 214 | protected: |
| 215 | sp<IConfirmationUI> confirmator_; |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 { |
| 226 | 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 |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 232 | TEST_P(ConfirmationUIHidlTest, UserOkTest) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 239 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 240 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 241 | ASSERT_HAL_CALL(ResponseCode::OK, confirmator_->deliverSecureInputEvent( |
| 242 | makeTestToken(TestModeCommands::OK_EVENT))); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 276 | TEST_P(ConfirmationUIHidlTest, MessageTooLongTest) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 283 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 288 | TEST_P(ConfirmationUIHidlTest, MessageWayTooLongTest) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 295 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // Simulates the User tapping the Cancel |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 299 | TEST_P(ConfirmationUIHidlTest, UserCancelTest) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 306 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 307 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 308 | ASSERT_HAL_CALL(ResponseCode::OK, confirmator_->deliverSecureInputEvent( |
| 309 | makeTestToken(TestModeCommands::CANCEL_EVENT))); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 | |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 318 | // Simulates the framework cancelling an ongoing prompt |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 319 | TEST_P(ConfirmationUIHidlTest, AbortTest) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 326 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 327 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 328 | confirmator_->abort(); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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 | |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 336 | // Tests if the confirmation dialog can successfully render 100 'W' characters as required by |
| 337 | // the design guidelines. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 338 | TEST_P(ConfirmationUIHidlTest, PortableMessageTest1) { |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 347 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 348 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 349 | confirmator_->abort(); |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 359 | TEST_P(ConfirmationUIHidlTest, PortableMessageTest1Magnified) { |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 368 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 369 | {UIOption::AccessibilityMagnified})); |
| 370 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 371 | confirmator_->abort(); |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 381 | TEST_P(ConfirmationUIHidlTest, PortableMessageTest2) { |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 390 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 391 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 392 | confirmator_->abort(); |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 402 | TEST_P(ConfirmationUIHidlTest, PortableMessageTest2Magnified) { |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 411 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 412 | {UIOption::AccessibilityMagnified})); |
| 413 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 414 | confirmator_->abort(); |
Janis Danisevskis | 8d4f06f | 2020-02-20 16:30:55 -0800 | [diff] [blame] | 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 | |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 422 | // Passing malformed UTF-8 to the confirmation UI |
| 423 | // This test passes a string that ends in the middle of a multibyte character |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 424 | TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test1) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 431 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Passing malformed UTF-8 to the confirmation UI |
| 435 | // This test passes a string with a 5-byte character. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 436 | TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test2) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 444 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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. |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 449 | TEST_P(ConfirmationUIHidlTest, MalformedUTF8Test3) { |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 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, |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 456 | confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {})); |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // Test the implementation of HMAC SHA 256 against a golden blob. |
| 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 | |
Janis Danisevskis | b78beb5 | 2020-02-27 16:31:24 -0800 | [diff] [blame] | 476 | INSTANTIATE_TEST_SUITE_P( |
| 477 | PerInstance, ConfirmationUIHidlTest, |
| 478 | testing::ValuesIn(android::hardware::getAllHalInstanceNames(IConfirmationUI::descriptor)), |
| 479 | android::hardware::PrintInstanceNameToString); |
| 480 | |
Janis Danisevskis | e0b1903 | 2017-11-09 14:57:39 -0800 | [diff] [blame] | 481 | } // namespace test |
| 482 | } // namespace V1_0 |
| 483 | } // namespace confirmationui |
| 484 | } // namespace hardware |
| 485 | } // namespace android |