| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/signin/local_auth.h" | 5 #include "chrome/browser/signin/local_auth.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/profiles/profile_attributes_entry.h" | 11 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 12 #include "chrome/browser/profiles/profile_attributes_storage.h" | 12 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 13 #include "chrome/test/base/testing_browser_process.h" | 13 #include "chrome/test/base/testing_browser_process.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 15 #include "chrome/test/base/testing_profile_manager.h" | 15 #include "chrome/test/base/testing_profile_manager.h" |
| 16 #include "components/os_crypt/os_crypt.h" | 16 #include "components/os_crypt/os_crypt_mocker.h" |
| 17 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 18 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 18 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 | 20 |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 class LocalAuthTest : public testing::Test { | 23 class LocalAuthTest : public testing::Test { |
| 24 public: |
| 25 LocalAuthTest() { OSCryptMocker::SetUpWithSingleton(); } |
| 26 |
| 27 ~LocalAuthTest() override { OSCryptMocker::TearDown(); } |
| 28 |
| 29 private: |
| 24 content::TestBrowserThreadBundle thread_bundle_; | 30 content::TestBrowserThreadBundle thread_bundle_; |
| 25 }; | 31 }; |
| 26 | 32 |
| 27 TEST_F(LocalAuthTest, SetAndCheckCredentials) { | 33 TEST_F(LocalAuthTest, SetAndCheckCredentials) { |
| 28 TestingProfileManager testing_profile_manager( | 34 TestingProfileManager testing_profile_manager( |
| 29 TestingBrowserProcess::GetGlobal()); | 35 TestingBrowserProcess::GetGlobal()); |
| 30 ASSERT_TRUE(testing_profile_manager.SetUp()); | 36 ASSERT_TRUE(testing_profile_manager.SetUp()); |
| 31 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); | 37 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); |
| 32 ProfileAttributesEntry* entry; | 38 ProfileAttributesEntry* entry; |
| 33 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> | 39 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> |
| 34 GetProfileAttributesWithPath(prof->GetPath(), &entry)); | 40 GetProfileAttributesWithPath(prof->GetPath(), &entry)); |
| 35 EXPECT_EQ("", entry->GetLocalAuthCredentials()); | 41 EXPECT_EQ("", entry->GetLocalAuthCredentials()); |
| 36 | 42 |
| 37 #if defined(OS_MACOSX) | |
| 38 OSCrypt::UseMockKeychain(true); | |
| 39 #endif | |
| 40 | |
| 41 std::string password("Some Password"); | 43 std::string password("Some Password"); |
| 42 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); | 44 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); |
| 43 | 45 |
| 44 LocalAuth::SetLocalAuthCredentials(prof, password); | 46 LocalAuth::SetLocalAuthCredentials(prof, password); |
| 45 std::string passhash = entry->GetLocalAuthCredentials(); | 47 std::string passhash = entry->GetLocalAuthCredentials(); |
| 46 | 48 |
| 47 // We perform basic validation on the written record to ensure bugs don't slip | 49 // We perform basic validation on the written record to ensure bugs don't slip |
| 48 // in that cannot be seen from the API: | 50 // in that cannot be seen from the API: |
| 49 // - The encoding exists (we can guarantee future backward compatibility). | 51 // - The encoding exists (we can guarantee future backward compatibility). |
| 50 // - The plaintext version of the password is not mistakenly stored anywhere. | 52 // - The plaintext version of the password is not mistakenly stored anywhere. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 63 LocalAuth::SetLocalAuthCredentials(prof, password); // makes different salt | 65 LocalAuth::SetLocalAuthCredentials(prof, password); // makes different salt |
| 64 EXPECT_NE(passhash, entry->GetLocalAuthCredentials()); | 66 EXPECT_NE(passhash, entry->GetLocalAuthCredentials()); |
| 65 } | 67 } |
| 66 | 68 |
| 67 TEST_F(LocalAuthTest, SetUpgradeAndCheckCredentials) { | 69 TEST_F(LocalAuthTest, SetUpgradeAndCheckCredentials) { |
| 68 TestingProfileManager testing_profile_manager( | 70 TestingProfileManager testing_profile_manager( |
| 69 TestingBrowserProcess::GetGlobal()); | 71 TestingBrowserProcess::GetGlobal()); |
| 70 ASSERT_TRUE(testing_profile_manager.SetUp()); | 72 ASSERT_TRUE(testing_profile_manager.SetUp()); |
| 71 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); | 73 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); |
| 72 | 74 |
| 73 #if defined(OS_MACOSX) | |
| 74 OSCrypt::UseMockKeychain(true); | |
| 75 #endif | |
| 76 | |
| 77 std::string password("Some Password"); | 75 std::string password("Some Password"); |
| 78 ProfileAttributesEntry* entry; | 76 ProfileAttributesEntry* entry; |
| 79 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> | 77 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> |
| 80 GetProfileAttributesWithPath(prof->GetPath(), &entry)); | 78 GetProfileAttributesWithPath(prof->GetPath(), &entry)); |
| 81 LocalAuth::SetLocalAuthCredentialsWithEncoding(entry, password, '1'); | 79 LocalAuth::SetLocalAuthCredentialsWithEncoding(entry, password, '1'); |
| 82 | 80 |
| 83 // Ensure we indeed persisted the correct encoding. | 81 // Ensure we indeed persisted the correct encoding. |
| 84 std::string oldpasshash = entry->GetLocalAuthCredentials(); | 82 std::string oldpasshash = entry->GetLocalAuthCredentials(); |
| 85 EXPECT_EQ('1', oldpasshash[0]); | 83 EXPECT_EQ('1', oldpasshash[0]); |
| 86 | 84 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 112 TEST_F(LocalAuthTest, TruncateStringUnevenly) { | 110 TEST_F(LocalAuthTest, TruncateStringUnevenly) { |
| 113 std::string two_chars = "Az"; | 111 std::string two_chars = "Az"; |
| 114 std::string three_chars = "AzC"; | 112 std::string three_chars = "AzC"; |
| 115 // 'z' = 0x7A, ':' = 0x3A. | 113 // 'z' = 0x7A, ':' = 0x3A. |
| 116 std::string two_chars_truncated = "A:"; | 114 std::string two_chars_truncated = "A:"; |
| 117 EXPECT_EQ(two_chars_truncated, | 115 EXPECT_EQ(two_chars_truncated, |
| 118 LocalAuth::TruncateStringByBits(two_chars, 14)); | 116 LocalAuth::TruncateStringByBits(two_chars, 14)); |
| 119 EXPECT_EQ(two_chars_truncated, | 117 EXPECT_EQ(two_chars_truncated, |
| 120 LocalAuth::TruncateStringByBits(three_chars, 14)); | 118 LocalAuth::TruncateStringByBits(three_chars, 14)); |
| 121 } | 119 } |
| OLD | NEW |