| 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 "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/test/base/testing_browser_process.h" | 10 #include "chrome/test/base/testing_browser_process.h" |
| 11 #include "chrome/test/base/testing_pref_service_syncable.h" | 11 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "chrome/test/base/testing_profile_manager.h" | 13 #include "chrome/test/base/testing_profile_manager.h" |
| 14 #include "components/os_crypt/os_crypt.h" | 14 #include "components/os_crypt/os_crypt.h" |
| 15 | 15 |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 using namespace chrome; | |
| 19 | |
| 20 TEST(LocalAuthTest, SetAndCheckCredentials) { | 18 TEST(LocalAuthTest, SetAndCheckCredentials) { |
| 21 TestingProfileManager testing_profile_manager( | 19 TestingProfileManager testing_profile_manager( |
| 22 TestingBrowserProcess::GetGlobal()); | 20 TestingBrowserProcess::GetGlobal()); |
| 23 ASSERT_TRUE(testing_profile_manager.SetUp()); | 21 ASSERT_TRUE(testing_profile_manager.SetUp()); |
| 24 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); | 22 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); |
| 25 ProfileInfoCache& cache = | 23 ProfileInfoCache& cache = |
| 26 testing_profile_manager.profile_manager()->GetProfileInfoCache(); | 24 testing_profile_manager.profile_manager()->GetProfileInfoCache(); |
| 27 EXPECT_EQ(1U, cache.GetNumberOfProfiles()); | 25 EXPECT_EQ(1U, cache.GetNumberOfProfiles()); |
| 28 EXPECT_EQ("", cache.GetLocalAuthCredentialsOfProfileAtIndex(0)); | 26 EXPECT_EQ("", cache.GetLocalAuthCredentialsOfProfileAtIndex(0)); |
| 29 | 27 |
| 30 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
| 31 OSCrypt::UseMockKeychain(true); | 29 OSCrypt::UseMockKeychain(true); |
| 32 #endif | 30 #endif |
| 33 | 31 |
| 34 std::string password("Some Password"); | 32 std::string password("Some Password"); |
| 35 EXPECT_FALSE(ValidateLocalAuthCredentials(prof, password)); | 33 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); |
| 36 | 34 |
| 37 SetLocalAuthCredentials(prof, password); | 35 LocalAuth::SetLocalAuthCredentials(prof, password); |
| 38 std::string passhash = cache.GetLocalAuthCredentialsOfProfileAtIndex(0); | 36 std::string passhash = cache.GetLocalAuthCredentialsOfProfileAtIndex(0); |
| 39 | 37 |
| 40 // We perform basic validation on the written record to ensure bugs don't slip | 38 // We perform basic validation on the written record to ensure bugs don't slip |
| 41 // in that cannot be seen from the API: | 39 // in that cannot be seen from the API: |
| 42 // - The encoding exists (we can guarantee future backward compatibility). | 40 // - The encoding exists (we can guarantee future backward compatibility). |
| 43 // - The plaintext version of the password is not mistakenly stored anywhere. | 41 // - The plaintext version of the password is not mistakenly stored anywhere. |
| 44 EXPECT_FALSE(passhash.empty()); | 42 EXPECT_FALSE(passhash.empty()); |
| 45 EXPECT_EQ('1', passhash[0]); | 43 EXPECT_EQ('2', passhash[0]); |
| 46 EXPECT_EQ(passhash.find(password), std::string::npos); | 44 EXPECT_EQ(passhash.find(password), std::string::npos); |
| 47 | 45 |
| 48 std::string decodedhash; | 46 std::string decodedhash; |
| 49 base::Base64Decode(passhash.substr(1), &decodedhash); | 47 base::Base64Decode(passhash.substr(1), &decodedhash); |
| 50 EXPECT_FALSE(decodedhash.empty()); | 48 EXPECT_FALSE(decodedhash.empty()); |
| 51 EXPECT_EQ(decodedhash.find(password), std::string::npos); | 49 EXPECT_EQ(decodedhash.find(password), std::string::npos); |
| 52 | 50 |
| 53 EXPECT_TRUE(ValidateLocalAuthCredentials(prof, password)); | 51 EXPECT_TRUE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); |
| 54 EXPECT_FALSE(ValidateLocalAuthCredentials(prof, password + "1")); | 52 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof, password + "1")); |
| 55 | 53 |
| 56 SetLocalAuthCredentials(prof, password); // makes different salt | 54 LocalAuth::SetLocalAuthCredentials(prof, password); // makes different salt |
| 57 EXPECT_NE(passhash, cache.GetLocalAuthCredentialsOfProfileAtIndex(0)); | 55 EXPECT_NE(passhash, cache.GetLocalAuthCredentialsOfProfileAtIndex(0)); |
| 58 } | 56 } |
| 57 |
| 58 |
| 59 TEST(LocalAuthTest, SetUpgradeAndCheckCredentials) { |
| 60 TestingProfileManager testing_profile_manager( |
| 61 TestingBrowserProcess::GetGlobal()); |
| 62 ASSERT_TRUE(testing_profile_manager.SetUp()); |
| 63 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); |
| 64 ProfileInfoCache& cache = |
| 65 testing_profile_manager.profile_manager()->GetProfileInfoCache(); |
| 66 |
| 67 #if defined(OS_MACOSX) |
| 68 OSCrypt::UseMockKeychain(true); |
| 69 #endif |
| 70 |
| 71 std::string password("Some Password"); |
| 72 size_t profile_index = cache.GetIndexOfProfileWithPath(prof->GetPath()); |
| 73 LocalAuth::SetLocalAuthCredentialsWithEncoding(profile_index, password, '1'); |
| 74 |
| 75 // Ensure we indeed persisted the correct encoding. |
| 76 std::string oldpasshash = cache.GetLocalAuthCredentialsOfProfileAtIndex( |
| 77 profile_index); |
| 78 EXPECT_EQ('1', oldpasshash[0]); |
| 79 |
| 80 // Validate, ensure we can validate against the old encoding. |
| 81 EXPECT_TRUE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); |
| 82 |
| 83 // Ensure we updated the encoding. |
| 84 std::string newpasshash = cache.GetLocalAuthCredentialsOfProfileAtIndex( |
| 85 profile_index); |
| 86 EXPECT_EQ('2', newpasshash[0]); |
| 87 // Encoding '2' writes fewer bytes than encoding '1'. |
| 88 EXPECT_LE(newpasshash.length(), oldpasshash.length()); |
| 89 |
| 90 // Validate, ensure we validate against the new encoding. |
| 91 EXPECT_TRUE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); |
| 92 } |
| 93 |
| 94 // Test truncation where each byte is left whole. |
| 95 TEST(LocalAuthTest, TruncateStringEvenly) { |
| 96 std::string two_chars = "A6"; |
| 97 std::string three_chars = "A6C"; |
| 98 EXPECT_EQ(two_chars, LocalAuth::TruncateStringByBits(two_chars, 16)); |
| 99 EXPECT_EQ(two_chars, LocalAuth::TruncateStringByBits(three_chars, 16)); |
| 100 |
| 101 EXPECT_EQ(two_chars, LocalAuth::TruncateStringByBits(two_chars, 14)); |
| 102 EXPECT_EQ(two_chars, LocalAuth::TruncateStringByBits(three_chars, 14)); |
| 103 } |
| 104 |
| 105 // Test truncation that affects the results within a byte. |
| 106 TEST(LocalAuthTest, TruncateStringUnevenly) { |
| 107 std::string two_chars = "Az"; |
| 108 std::string three_chars = "AzC"; |
| 109 // 'z' = 0x7A, ':' = 0x3A. |
| 110 std::string two_chars_truncated = "A:"; |
| 111 EXPECT_EQ(two_chars_truncated, |
| 112 LocalAuth::TruncateStringByBits(two_chars, 14)); |
| 113 EXPECT_EQ(two_chars_truncated, |
| 114 LocalAuth::TruncateStringByBits(three_chars, 14)); |
| 115 } |
| OLD | NEW |