Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "u2fd/webauthn_storage.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 10 | #include <base/check.h> |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 11 | #include <base/files/file_path.h> |
| 12 | #include <base/files/file_util.h> |
| 13 | #include <base/files/scoped_temp_dir.h> |
| 14 | #include <base/strings/string_number_conversions.h> |
| 15 | #include <base/time/time.h> |
| 16 | #include <gmock/gmock.h> |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | namespace u2f { |
| 20 | namespace { |
| 21 | |
| 22 | constexpr char kSanitizedUser[] = "SanitizedUser"; |
| 23 | |
| 24 | constexpr char kCredentialId[] = "CredentialId"; |
| 25 | constexpr char kCredentialSecret[65] = {[0 ... 63] = 'E', '\0'}; |
| 26 | constexpr char kRpId[] = "example.com"; |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 27 | constexpr char kRpDisplayName[] = "Example Site"; |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 28 | constexpr char kUserId[] = "deadbeef"; |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 29 | constexpr char kUserDisplayName[] = "example_user"; |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 30 | constexpr double kCreatedTime = 12345; |
| 31 | |
Yicheng Li | 4d27fa7 | 2020-12-10 11:09:21 -0800 | [diff] [blame] | 32 | brillo::Blob HexArrayToBlob(const char* array) { |
| 33 | brillo::Blob blob; |
| 34 | CHECK(base::HexStringToBytes(array, &blob)); |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 35 | return blob; |
| 36 | } |
| 37 | |
| 38 | using ::testing::_; |
| 39 | using ::testing::Return; |
| 40 | |
| 41 | class WebAuthnStorageTest : public ::testing::Test { |
| 42 | public: |
| 43 | WebAuthnStorageTest() { |
| 44 | CHECK(temp_dir_.CreateUniqueTempDir()); |
| 45 | root_path_ = |
| 46 | temp_dir_.GetPath().AppendASCII("webauthn_storage_unittest_root"); |
| 47 | webauthn_storage_ = std::make_unique<WebAuthnStorage>(); |
| 48 | // Since there is no session manager, allow accesses by default. |
| 49 | webauthn_storage_->set_allow_access(true); |
| 50 | webauthn_storage_->set_sanitized_user(kSanitizedUser); |
| 51 | webauthn_storage_->SetRootPathForTesting(root_path_); |
| 52 | } |
| 53 | |
| 54 | ~WebAuthnStorageTest() override { |
hscham | 53cf73a | 2020-11-30 15:58:42 +0900 | [diff] [blame] | 55 | EXPECT_TRUE(base::DeletePathRecursively(temp_dir_.GetPath())); |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
| 59 | base::ScopedTempDir temp_dir_; |
| 60 | base::FilePath root_path_; |
| 61 | std::unique_ptr<WebAuthnStorage> webauthn_storage_; |
| 62 | }; |
| 63 | |
| 64 | TEST_F(WebAuthnStorageTest, WriteAndReadRecord) { |
| 65 | const WebAuthnRecord record{kCredentialId, |
Yicheng Li | 4d27fa7 | 2020-12-10 11:09:21 -0800 | [diff] [blame] | 66 | HexArrayToBlob(kCredentialSecret), |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 67 | kRpId, |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 68 | kRpDisplayName, |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 69 | kUserId, |
| 70 | kUserDisplayName, |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 71 | kCreatedTime, |
| 72 | /* is_resident_key = */ true}; |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 73 | |
| 74 | EXPECT_TRUE(webauthn_storage_->WriteRecord(record)); |
| 75 | |
| 76 | webauthn_storage_->Reset(); |
| 77 | webauthn_storage_->set_allow_access(true); |
| 78 | webauthn_storage_->set_sanitized_user(kSanitizedUser); |
| 79 | |
| 80 | EXPECT_TRUE(webauthn_storage_->LoadRecords()); |
| 81 | |
| 82 | base::Optional<WebAuthnRecord> record_loaded = |
| 83 | webauthn_storage_->GetRecordByCredentialId(kCredentialId); |
| 84 | EXPECT_TRUE(record_loaded); |
| 85 | EXPECT_EQ(record.secret, record_loaded->secret); |
| 86 | EXPECT_EQ(record.rp_id, record_loaded->rp_id); |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 87 | EXPECT_EQ(record.rp_display_name, record_loaded->rp_display_name); |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 88 | EXPECT_EQ(record.user_id, record_loaded->user_id); |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 89 | EXPECT_EQ(record.user_display_name, record_loaded->user_display_name); |
| 90 | EXPECT_EQ(record.timestamp, record_loaded->timestamp); |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 91 | EXPECT_TRUE(record.is_resident_key); |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST_F(WebAuthnStorageTest, WriteAndReadRecordWithEmptyUserIdAndDisplayName) { |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 95 | const WebAuthnRecord record{kCredentialId, |
| 96 | HexArrayToBlob(kCredentialSecret), |
Yicheng Li | 4d27fa7 | 2020-12-10 11:09:21 -0800 | [diff] [blame] | 97 | kRpId, |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 98 | kRpDisplayName, |
| 99 | /* user_id = */ std::string(), |
| 100 | /* user_display_name = */ std::string(), |
| 101 | kCreatedTime, |
| 102 | /* is_resident_key = */ false}; |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 103 | |
| 104 | EXPECT_TRUE(webauthn_storage_->WriteRecord(record)); |
| 105 | |
| 106 | webauthn_storage_->Reset(); |
| 107 | webauthn_storage_->set_allow_access(true); |
| 108 | webauthn_storage_->set_sanitized_user(kSanitizedUser); |
| 109 | |
| 110 | EXPECT_TRUE(webauthn_storage_->LoadRecords()); |
| 111 | |
| 112 | base::Optional<WebAuthnRecord> record_loaded = |
| 113 | webauthn_storage_->GetRecordByCredentialId(kCredentialId); |
| 114 | EXPECT_TRUE(record_loaded); |
| 115 | EXPECT_EQ(record.secret, record_loaded->secret); |
| 116 | EXPECT_EQ(record.rp_id, record_loaded->rp_id); |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 117 | EXPECT_EQ(record.rp_display_name, record_loaded->rp_display_name); |
Yicheng Li | aeb3d68 | 2020-11-19 11:07:57 -0800 | [diff] [blame] | 118 | EXPECT_TRUE(record_loaded->user_id.empty()); |
| 119 | EXPECT_TRUE(record_loaded->user_display_name.empty()); |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 120 | EXPECT_EQ(record.timestamp, record_loaded->timestamp); |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 121 | EXPECT_FALSE(record.is_resident_key); |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Yicheng Li | 4d27fa7 | 2020-12-10 11:09:21 -0800 | [diff] [blame] | 124 | TEST_F(WebAuthnStorageTest, LoadManyRecords) { |
| 125 | for (int i = 0; i < 30; i++) { |
| 126 | const WebAuthnRecord record{std::string(kCredentialId) + std::to_string(i), |
| 127 | HexArrayToBlob(kCredentialSecret), |
| 128 | kRpId, |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 129 | kRpDisplayName, |
Yicheng Li | 4d27fa7 | 2020-12-10 11:09:21 -0800 | [diff] [blame] | 130 | kUserId, |
| 131 | kUserDisplayName, |
Yicheng Li | d73908d | 2021-02-17 09:58:11 -0800 | [diff] [blame] | 132 | kCreatedTime, |
| 133 | /* is_resident_key = */ true}; |
Yicheng Li | 4d27fa7 | 2020-12-10 11:09:21 -0800 | [diff] [blame] | 134 | |
| 135 | EXPECT_TRUE(webauthn_storage_->WriteRecord(record)); |
| 136 | } |
| 137 | |
| 138 | webauthn_storage_->Reset(); |
| 139 | webauthn_storage_->set_allow_access(true); |
| 140 | webauthn_storage_->set_sanitized_user(kSanitizedUser); |
| 141 | |
| 142 | EXPECT_TRUE(webauthn_storage_->LoadRecords()); |
| 143 | } |
| 144 | |
Yicheng Li | 1090c90 | 2020-11-10 11:31:43 -0800 | [diff] [blame] | 145 | } // namespace |
| 146 | } // namespace u2f |