Subrahmanyaman | 2e95fa2 | 2022-08-09 19:01:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "ConfirmationIOAidlHalTest" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <condition_variable> |
| 21 | #include <future> |
| 22 | #include <iostream> |
| 23 | #include <memory> |
| 24 | #include <mutex> |
| 25 | |
| 26 | #include <aidl/Gtest.h> |
| 27 | #include <aidl/Vintf.h> |
| 28 | #include <aidl/android/hardware/confirmationui/BnConfirmationResultCallback.h> |
| 29 | #include <aidl/android/hardware/confirmationui/IConfirmationUI.h> |
| 30 | #include <aidl/android/hardware/confirmationui/TestModeCommands.h> |
| 31 | #include <aidl/android/hardware/confirmationui/UIOption.h> |
| 32 | #include <android-base/thread_annotations.h> |
| 33 | #include <android/hardware/confirmationui/support/confirmationui_utils.h> |
| 34 | #include <cutils/log.h> |
| 35 | |
| 36 | #include <openssl/hmac.h> |
| 37 | #include <openssl/sha.h> |
| 38 | |
| 39 | #include <cn-cbor/cn-cbor.h> |
| 40 | |
| 41 | #include <android/binder_manager.h> |
| 42 | #include <android/binder_process.h> |
| 43 | #include <android/binder_status.h> |
| 44 | |
| 45 | static constexpr int TIMEOUT_PERIOD = 10; |
| 46 | |
| 47 | namespace aidl::android::hardware::confirmationui::test { |
| 48 | using ::aidl::android::hardware::security::keymint::HardwareAuthenticatorType; |
| 49 | using ::aidl::android::hardware::security::keymint::HardwareAuthToken; |
| 50 | using ::android::hardware::confirmationui::support::auth_token_key_t; |
| 51 | using ::android::hardware::confirmationui::support::ByteBufferProxy; |
| 52 | using ::android::hardware::confirmationui::support::HMac; |
| 53 | using ::android::hardware::confirmationui::support::hmac_t; |
| 54 | using ::android::hardware::confirmationui::support::hton; |
| 55 | using ::android::hardware::confirmationui::support::NullOr; |
| 56 | using std::shared_ptr; |
| 57 | using std::string; |
| 58 | using std::vector; |
| 59 | |
| 60 | namespace { |
| 61 | const auth_token_key_t testKey(static_cast<uint8_t>(IConfirmationUI::TEST_KEY_BYTE)); |
| 62 | |
| 63 | class HMacImplementation { |
| 64 | public: |
| 65 | static NullOr<hmac_t> hmac256(const auth_token_key_t& key, |
| 66 | std::initializer_list<ByteBufferProxy> buffers) { |
| 67 | HMAC_CTX hmacCtx; |
| 68 | HMAC_CTX_init(&hmacCtx); |
| 69 | if (!HMAC_Init_ex(&hmacCtx, key.data(), key.size(), EVP_sha256(), nullptr)) { |
| 70 | return {}; |
| 71 | } |
| 72 | for (auto& buffer : buffers) { |
| 73 | if (!HMAC_Update(&hmacCtx, buffer.data(), buffer.size())) { |
| 74 | return {}; |
| 75 | } |
| 76 | } |
| 77 | hmac_t result; |
| 78 | if (!HMAC_Final(&hmacCtx, result.data(), nullptr)) { |
| 79 | return {}; |
| 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | using HMacer = HMac<HMacImplementation>; |
| 86 | |
| 87 | template <typename... Data> |
| 88 | vector<uint8_t> testHMAC(const Data&... data) { |
| 89 | auto hmac = HMacer::hmac256(testKey, data...); |
| 90 | if (!hmac.isOk()) { |
| 91 | ADD_FAILURE() << "Failed to compute test hmac. This is a self-test error."; |
| 92 | return {}; |
| 93 | } |
| 94 | vector<uint8_t> result(hmac.value().size()); |
| 95 | std::copy(hmac.value().data(), hmac.value().data() + hmac.value().size(), result.data()); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | auto toBytes(const T& v) -> const uint8_t (&)[sizeof(T)] { |
| 101 | return *reinterpret_cast<const uint8_t(*)[sizeof(T)]>(&v); |
| 102 | } |
| 103 | |
| 104 | HardwareAuthToken makeTestToken(const TestModeCommands command, uint64_t timestamp = 0) { |
| 105 | HardwareAuthToken auth_token; |
| 106 | auth_token.challenge = static_cast<uint64_t>(command); |
| 107 | auth_token.userId = 0; |
| 108 | auth_token.authenticatorId = 0; |
| 109 | auth_token.authenticatorType = HardwareAuthenticatorType::NONE; |
| 110 | auth_token.timestamp = {static_cast<int64_t>(timestamp)}; |
| 111 | |
| 112 | // Canonical form of auth-token v0 |
| 113 | // version (1 byte) |
| 114 | // challenge (8 bytes) |
| 115 | // user_id (8 bytes) |
| 116 | // authenticator_id (8 bytes) |
| 117 | // authenticator_type (4 bytes) |
| 118 | // timestamp (8 bytes) |
| 119 | // total 37 bytes |
| 120 | auth_token.mac = testHMAC("\0", |
| 121 | toBytes(auth_token.challenge), // |
| 122 | toBytes(auth_token.userId), // |
| 123 | toBytes(auth_token.authenticatorId), // |
| 124 | toBytes(hton(auth_token.authenticatorType)), // |
| 125 | toBytes(hton(auth_token.timestamp))); // |
| 126 | |
| 127 | return auth_token; |
| 128 | } |
| 129 | |
| 130 | #define DEBUG_CONFRIMATIONUI_UTILS_TEST |
| 131 | |
| 132 | #ifdef DEBUG_CONFRIMATIONUI_UTILS_TEST |
| 133 | std::ostream& hexdump(std::ostream& out, const uint8_t* data, size_t size) { |
| 134 | for (size_t i = 0; i < size; ++i) { |
| 135 | uint8_t byte = data[i]; |
| 136 | out << std::hex << std::setw(2) << std::setfill('0') << (unsigned)byte; |
| 137 | switch (i & 0xf) { |
| 138 | case 0xf: |
| 139 | out << "\n"; |
| 140 | break; |
| 141 | case 7: |
| 142 | out << " "; |
| 143 | break; |
| 144 | default: |
| 145 | out << " "; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | return out; |
| 150 | } |
| 151 | #endif |
| 152 | |
| 153 | constexpr char hex_value[256] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 154 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 155 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 156 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, // '0'..'9' |
| 157 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'A'..'F' |
| 158 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 159 | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 'a'..'f' |
| 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 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 166 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 167 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 168 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 169 | |
| 170 | std::string hex2str(std::string a) { |
| 171 | std::string b; |
| 172 | size_t num = a.size() / 2; |
| 173 | b.resize(num); |
| 174 | for (size_t i = 0; i < num; i++) { |
| 175 | b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]); |
| 176 | } |
| 177 | return b; |
| 178 | } |
| 179 | |
| 180 | int getReturnCode(const ::ndk::ScopedAStatus& result) { |
| 181 | if (result.isOk()) return IConfirmationUI::OK; |
| 182 | |
| 183 | if (result.getExceptionCode() == EX_SERVICE_SPECIFIC) { |
| 184 | return static_cast<int>(result.getServiceSpecificError()); |
| 185 | } |
| 186 | return result.getStatus(); |
| 187 | } |
| 188 | |
| 189 | } // namespace |
| 190 | |
| 191 | class ConfirmationUIAidlTest : public ::testing::TestWithParam<std::string> { |
| 192 | public: |
| 193 | void TearDown() override { confirmator_->abort(); } |
| 194 | void SetUp() override { |
| 195 | std::string name = GetParam(); |
| 196 | ASSERT_TRUE(AServiceManager_isDeclared(name.c_str())) << name; |
| 197 | ndk::SpAIBinder binder(AServiceManager_waitForService(name.c_str())); |
| 198 | ASSERT_NE(binder, nullptr); |
| 199 | confirmator_ = IConfirmationUI::fromBinder(binder); |
| 200 | ASSERT_NE(confirmator_, nullptr); |
| 201 | } |
| 202 | |
| 203 | // Used as a mechanism to inform the test about data/event callback |
| 204 | inline void notify() { |
| 205 | std::unique_lock<std::mutex> lock(mtx_); |
| 206 | cv_.notify_one(); |
| 207 | } |
| 208 | |
| 209 | // Test code calls this function to wait for data/event callback |
| 210 | inline std::cv_status wait() { |
| 211 | std::unique_lock<std::mutex> lock(mtx_); |
| 212 | auto now = std::chrono::system_clock::now(); |
| 213 | std::cv_status status = cv_.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD)); |
| 214 | return status; |
| 215 | } |
| 216 | |
| 217 | protected: |
| 218 | shared_ptr<IConfirmationUI> confirmator_; |
| 219 | |
| 220 | private: |
| 221 | // synchronization objects |
| 222 | std::mutex mtx_; |
| 223 | std::condition_variable cv_; |
| 224 | }; |
| 225 | |
| 226 | class ConfirmationTestCallback |
| 227 | : public ::aidl::android::hardware::confirmationui::BnConfirmationResultCallback { |
| 228 | public: |
| 229 | ConfirmationTestCallback(ConfirmationUIAidlTest& parent) : parent_(parent){}; |
| 230 | virtual ~ConfirmationTestCallback() = default; |
| 231 | |
| 232 | ::ndk::ScopedAStatus result(int32_t err, const vector<uint8_t>& msg, |
| 233 | const vector<uint8_t>& confToken) override { |
| 234 | error_ = err; |
| 235 | formattedMessage_ = msg; |
| 236 | confirmationToken_ = confToken; |
| 237 | parent_.notify(); |
| 238 | return ndk::ScopedAStatus::ok(); |
| 239 | } |
| 240 | |
| 241 | bool verifyConfirmationToken() { |
| 242 | static constexpr char confirmationPrefix[] = "confirmation token"; |
| 243 | EXPECT_EQ(32U, confirmationToken_.size()); |
| 244 | return 32U == confirmationToken_.size() && |
| 245 | !memcmp(confirmationToken_.data(), |
| 246 | testHMAC(confirmationPrefix, formattedMessage_).data(), 32); |
| 247 | } |
| 248 | |
| 249 | int error_; |
| 250 | vector<uint8_t> formattedMessage_; |
| 251 | vector<uint8_t> confirmationToken_; |
| 252 | |
| 253 | private: |
| 254 | ConfirmationUIAidlTest& parent_; |
| 255 | }; |
| 256 | |
| 257 | struct CnCborDeleter { |
| 258 | void operator()(cn_cbor* ptr) { cn_cbor_free(ptr); } |
| 259 | }; |
| 260 | |
| 261 | typedef std::unique_ptr<cn_cbor, CnCborDeleter> CnCborPtr; |
| 262 | |
| 263 | // Simulates the User taping Ok |
| 264 | TEST_P(ConfirmationUIAidlTest, UserOkTest) { |
| 265 | static constexpr char test_prompt[] = "Me first, gimme gimme!"; |
| 266 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 267 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 268 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 269 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 270 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 271 | ASSERT_TRUE(confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}).isOk()); |
| 272 | // Simulate the user taping ok. |
| 273 | ASSERT_TRUE(confirmator_->deliverSecureInputEvent(makeTestToken(TestModeCommands::OK_EVENT)) |
| 274 | .isOk()); |
| 275 | // Wait for the callback. |
| 276 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 277 | ASSERT_EQ(IConfirmationUI::OK, conf_cb->error_); |
| 278 | |
| 279 | ASSERT_TRUE(conf_cb->verifyConfirmationToken()); |
| 280 | |
| 281 | cn_cbor_errback cn_cbor_error; |
| 282 | auto parsed_message = CnCborPtr(cn_cbor_decode( |
| 283 | conf_cb->formattedMessage_.data(), conf_cb->formattedMessage_.size(), &cn_cbor_error)); |
| 284 | // is parsable CBOR |
| 285 | ASSERT_TRUE(parsed_message.get()); |
| 286 | // is a map |
| 287 | ASSERT_EQ(CN_CBOR_MAP, parsed_message->type); |
| 288 | |
| 289 | // the message must have exactly 2 key value pairs. |
| 290 | // cn_cbor holds 2*<no_of_pairs> in the length field |
| 291 | ASSERT_EQ(4, parsed_message->length); |
| 292 | // map has key "prompt" |
| 293 | auto prompt = cn_cbor_mapget_string(parsed_message.get(), "prompt"); |
| 294 | ASSERT_TRUE(prompt); |
| 295 | ASSERT_EQ(CN_CBOR_TEXT, prompt->type); |
| 296 | ASSERT_EQ(22, prompt->length); |
| 297 | ASSERT_EQ(0, memcmp(test_prompt, prompt->v.str, 22)); |
| 298 | // map has key "extra" |
| 299 | auto extra_out = cn_cbor_mapget_string(parsed_message.get(), "extra"); |
| 300 | ASSERT_TRUE(extra_out); |
| 301 | ASSERT_EQ(CN_CBOR_BYTES, extra_out->type); |
| 302 | ASSERT_EQ(3, extra_out->length); |
| 303 | ASSERT_EQ(0, memcmp(test_extra, extra_out->v.bytes, 3)); |
| 304 | } |
| 305 | |
| 306 | // Initiates a confirmation prompt with a message that is too long |
| 307 | TEST_P(ConfirmationUIAidlTest, MessageTooLongTest) { |
| 308 | static constexpr uint8_t test_extra[IConfirmationUI::MAX_MESSAGE_SIZE] = {}; |
| 309 | static constexpr char test_prompt[] = "D\'oh!"; |
| 310 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 311 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 312 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 313 | vector<uint8_t> extra(test_extra, test_extra + sizeof(test_extra)); |
| 314 | auto result = confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}); |
| 315 | ASSERT_EQ(IConfirmationUI::UI_ERROR_MESSAGE_TOO_LONG, getReturnCode(result)); |
| 316 | } |
| 317 | |
| 318 | // If the message gets very long some HAL implementations might fail even before the message |
| 319 | // reaches the trusted app implementation. But the HAL must still diagnose the correct error. |
| 320 | TEST_P(ConfirmationUIAidlTest, MessageWayTooLongTest) { |
| 321 | static constexpr uint8_t test_extra[(IConfirmationUI::MAX_MESSAGE_SIZE)*10] = {}; |
| 322 | static constexpr char test_prompt[] = "D\'oh!"; |
| 323 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 324 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 325 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 326 | vector<uint8_t> extra(test_extra, test_extra + sizeof(test_extra)); |
| 327 | auto result = confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}); |
| 328 | ASSERT_EQ(IConfirmationUI::UI_ERROR_MESSAGE_TOO_LONG, getReturnCode(result)); |
| 329 | } |
| 330 | |
| 331 | // Simulates the User tapping the Cancel |
| 332 | TEST_P(ConfirmationUIAidlTest, UserCancelTest) { |
| 333 | static constexpr char test_prompt[] = "Me first, gimme gimme!"; |
| 334 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 335 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 336 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 337 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 338 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 339 | ASSERT_TRUE(confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}).isOk()); |
| 340 | |
| 341 | // Simulate the user taping ok. |
| 342 | ASSERT_TRUE(confirmator_->deliverSecureInputEvent(makeTestToken(TestModeCommands::CANCEL_EVENT)) |
| 343 | .isOk()); |
| 344 | // Wait for the callback. |
| 345 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 346 | ASSERT_EQ(IConfirmationUI::CANCELED, conf_cb->error_); |
| 347 | |
| 348 | ASSERT_EQ(0U, conf_cb->confirmationToken_.size()); |
| 349 | ASSERT_EQ(0U, conf_cb->formattedMessage_.size()); |
| 350 | } |
| 351 | |
| 352 | // Simulates the framework cancelling an ongoing prompt |
| 353 | TEST_P(ConfirmationUIAidlTest, AbortTest) { |
| 354 | static constexpr char test_prompt[] = "Me first, gimme gimme!"; |
| 355 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 356 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 357 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 358 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 359 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 360 | ASSERT_TRUE(confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}).isOk()); |
| 361 | |
| 362 | confirmator_->abort(); |
| 363 | |
| 364 | // Wait for the callback. |
| 365 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 366 | ASSERT_EQ(IConfirmationUI::ABORTED, conf_cb->error_); |
| 367 | ASSERT_EQ(0U, conf_cb->confirmationToken_.size()); |
| 368 | ASSERT_EQ(0U, conf_cb->formattedMessage_.size()); |
| 369 | } |
| 370 | |
| 371 | // Tests if the confirmation dialog can successfully render 100 'W' characters as required by |
| 372 | // the design guidelines. |
| 373 | TEST_P(ConfirmationUIAidlTest, PortableMessageTest1) { |
| 374 | static constexpr char test_prompt[] = |
| 375 | "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" |
| 376 | "WWWWWWWWWWWWWW"; |
| 377 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 378 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 379 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 380 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 381 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 382 | ASSERT_TRUE(confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}).isOk()); |
| 383 | |
| 384 | confirmator_->abort(); |
| 385 | |
| 386 | // Wait for the callback. |
| 387 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 388 | ASSERT_EQ(IConfirmationUI::ABORTED, conf_cb->error_); |
| 389 | ASSERT_EQ(0U, conf_cb->confirmationToken_.size()); |
| 390 | ASSERT_EQ(0U, conf_cb->formattedMessage_.size()); |
| 391 | } |
| 392 | |
| 393 | // Tests if the confirmation dialog can successfully render 100 'W' characters as required by |
| 394 | // the design guidelines in magnified mode. |
| 395 | TEST_P(ConfirmationUIAidlTest, PortableMessageTest1Magnified) { |
| 396 | static constexpr char test_prompt[] = |
| 397 | "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW" |
| 398 | "WWWWWWWWWWWWWW"; |
| 399 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 400 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 401 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 402 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 403 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 404 | ASSERT_TRUE(confirmator_ |
| 405 | ->promptUserConfirmation(conf_cb, prompt_text, extra, "en", |
| 406 | {UIOption::ACCESSIBILITY_MAGNIFIED}) |
| 407 | .isOk()); |
| 408 | |
| 409 | confirmator_->abort(); |
| 410 | |
| 411 | // Wait for the callback. |
| 412 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 413 | ASSERT_EQ(IConfirmationUI::ABORTED, conf_cb->error_); |
| 414 | ASSERT_EQ(0U, conf_cb->confirmationToken_.size()); |
| 415 | ASSERT_EQ(0U, conf_cb->formattedMessage_.size()); |
| 416 | } |
| 417 | |
| 418 | // Tests if the confirmation dialog can successfully render 8 groups of 12 'W' characters as |
| 419 | // required by the design guidelines. |
| 420 | TEST_P(ConfirmationUIAidlTest, PortableMessageTest2) { |
| 421 | static constexpr char test_prompt[] = |
| 422 | "WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW " |
| 423 | "WWWWWWWWWWWW WWWWWWWWWWWW"; |
| 424 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 425 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 426 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 427 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 428 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 429 | ASSERT_TRUE(confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}).isOk()); |
| 430 | |
| 431 | confirmator_->abort(); |
| 432 | |
| 433 | // Wait for the callback. |
| 434 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 435 | ASSERT_EQ(IConfirmationUI::ABORTED, conf_cb->error_); |
| 436 | ASSERT_EQ(0U, conf_cb->confirmationToken_.size()); |
| 437 | ASSERT_EQ(0U, conf_cb->formattedMessage_.size()); |
| 438 | } |
| 439 | |
| 440 | // Tests if the confirmation dialog can successfully render 8 groups of 12 'W' characters as |
| 441 | // required by the design guidelines in magnified mode. |
| 442 | TEST_P(ConfirmationUIAidlTest, PortableMessageTest2Magnified) { |
| 443 | static constexpr char test_prompt[] = |
| 444 | "WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW " |
| 445 | "WWWWWWWWWWWW WWWWWWWWWWWW"; |
| 446 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 447 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 448 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 449 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 450 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 451 | ASSERT_TRUE(confirmator_ |
| 452 | ->promptUserConfirmation(conf_cb, prompt_text, extra, "en", |
| 453 | {UIOption::ACCESSIBILITY_MAGNIFIED}) |
| 454 | .isOk()); |
| 455 | |
| 456 | confirmator_->abort(); |
| 457 | |
| 458 | // Wait for the callback. |
| 459 | EXPECT_EQ(std::cv_status::no_timeout, wait()); |
| 460 | ASSERT_EQ(IConfirmationUI::ABORTED, conf_cb->error_); |
| 461 | ASSERT_EQ(0U, conf_cb->confirmationToken_.size()); |
| 462 | ASSERT_EQ(0U, conf_cb->formattedMessage_.size()); |
| 463 | } |
| 464 | |
| 465 | // Passing malformed UTF-8 to the confirmation UI |
| 466 | // This test passes a string that ends in the middle of a multibyte character |
| 467 | TEST_P(ConfirmationUIAidlTest, MalformedUTF8Test1) { |
| 468 | static constexpr char test_prompt[] = {char(0xc0), 0}; |
| 469 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 470 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 471 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 472 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 473 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 474 | auto result = confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}); |
| 475 | ASSERT_EQ(IConfirmationUI::UI_ERROR_MALFORMED_UTF8ENCODING, getReturnCode(result)); |
| 476 | } |
| 477 | |
| 478 | // Passing malformed UTF-8 to the confirmation UI |
| 479 | // This test passes a string with a 5-byte character. |
| 480 | TEST_P(ConfirmationUIAidlTest, MalformedUTF8Test2) { |
| 481 | static constexpr char test_prompt[] = {char(0xf8), char(0x82), char(0x82), |
| 482 | char(0x82), char(0x82), 0}; |
| 483 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 484 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 485 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 486 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 487 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 488 | auto result = confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}); |
| 489 | ASSERT_EQ(IConfirmationUI::UI_ERROR_MALFORMED_UTF8ENCODING, getReturnCode(result)); |
| 490 | } |
| 491 | |
| 492 | // Passing malformed UTF-8 to the confirmation UI |
| 493 | // This test passes a string with a 2-byte character followed by a stray non UTF-8 character. |
| 494 | TEST_P(ConfirmationUIAidlTest, MalformedUTF8Test3) { |
| 495 | static constexpr char test_prompt[] = {char(0xc0), char(0x82), char(0x83), 0}; |
| 496 | static constexpr uint8_t test_extra[] = {0x1, 0x2, 0x3}; |
| 497 | shared_ptr<ConfirmationTestCallback> conf_cb = |
| 498 | ::ndk::SharedRefBase::make<ConfirmationTestCallback>(*this); |
| 499 | vector<uint8_t> prompt_text(test_prompt, test_prompt + sizeof(test_prompt)); |
| 500 | vector<uint8_t> extra(test_extra, test_extra + 3); |
| 501 | auto result = confirmator_->promptUserConfirmation(conf_cb, prompt_text, extra, "en", {}); |
| 502 | ASSERT_EQ(IConfirmationUI::UI_ERROR_MALFORMED_UTF8ENCODING, getReturnCode(result)); |
| 503 | } |
| 504 | |
| 505 | // Test the implementation of HMAC SHA 256 against a golden blob. |
| 506 | TEST(ConfirmationUITestSelfTest, HMAC256SelfTest) { |
| 507 | const char key_str[32] = "keykeykeykeykeykeykeykeykeykeyk"; |
| 508 | const uint8_t(&key)[32] = *reinterpret_cast<const uint8_t(*)[32]>(key_str); |
| 509 | auto expected = hex2str("2377fbcaa7fb3f6c20cfa1d9ebc60e9922cf58c909e25e300f3cb57f7805c886"); |
| 510 | auto result = HMacer::hmac256(key, "value1", "value2", "value3"); |
| 511 | |
| 512 | #ifdef DEBUG_CONFRIMATIONUI_UTILS_TEST |
| 513 | hexdump(std::cout, reinterpret_cast<const uint8_t*>(expected.data()), 32) << std::endl; |
| 514 | hexdump(std::cout, result.value().data(), 32) << std::endl; |
| 515 | #endif |
| 516 | |
| 517 | ByteBufferProxy expected_bytes(expected); |
| 518 | ASSERT_TRUE(result.isOk()); |
| 519 | ASSERT_EQ(expected, result.value()); |
| 520 | } |
| 521 | |
| 522 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ConfirmationUIAidlTest); |
| 523 | INSTANTIATE_TEST_SUITE_P( |
| 524 | PerInstance, ConfirmationUIAidlTest, |
| 525 | testing::ValuesIn(::android::getAidlHalInstanceNames(IConfirmationUI::descriptor)), |
| 526 | ::android::PrintInstanceNameToString); |
| 527 | |
| 528 | } // namespace aidl::android::hardware::confirmationui::test |
| 529 | |
| 530 | int main(int argc, char** argv) { |
| 531 | ::testing::InitGoogleTest(&argc, argv); |
| 532 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 533 | ABinderProcess_startThreadPool(); |
| 534 | return RUN_ALL_TESTS(); |
| 535 | } |