Chromium Code Reviews| 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 content::TestBrowserThreadBundle thread_bundle_; | 24 content::TestBrowserThreadBundle thread_bundle_; |
| 25 | |
| 26 void SetUp() override { OSCryptMocker::SetUpWithSingleton(); } | |
|
vabr (Chromium)
2016/05/24 13:33:44
Could you do this in the constructor and destructo
cfroussios
2016/05/25 13:13:09
Done.
| |
| 27 | |
| 28 void TearDown() override { OSCryptMocker::TearDown(); } | |
| 25 }; | 29 }; |
| 26 | 30 |
| 27 TEST_F(LocalAuthTest, SetAndCheckCredentials) { | 31 TEST_F(LocalAuthTest, SetAndCheckCredentials) { |
| 28 TestingProfileManager testing_profile_manager( | 32 TestingProfileManager testing_profile_manager( |
| 29 TestingBrowserProcess::GetGlobal()); | 33 TestingBrowserProcess::GetGlobal()); |
| 30 ASSERT_TRUE(testing_profile_manager.SetUp()); | 34 ASSERT_TRUE(testing_profile_manager.SetUp()); |
| 31 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); | 35 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); |
| 32 ProfileAttributesEntry* entry; | 36 ProfileAttributesEntry* entry; |
| 33 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> | 37 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> |
| 34 GetProfileAttributesWithPath(prof->GetPath(), &entry)); | 38 GetProfileAttributesWithPath(prof->GetPath(), &entry)); |
| 35 EXPECT_EQ("", entry->GetLocalAuthCredentials()); | 39 EXPECT_EQ("", entry->GetLocalAuthCredentials()); |
| 36 | 40 |
| 37 #if defined(OS_MACOSX) | |
| 38 OSCrypt::UseMockKeychain(true); | |
| 39 #endif | |
| 40 | |
| 41 std::string password("Some Password"); | 41 std::string password("Some Password"); |
| 42 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); | 42 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof, password)); |
| 43 | 43 |
| 44 LocalAuth::SetLocalAuthCredentials(prof, password); | 44 LocalAuth::SetLocalAuthCredentials(prof, password); |
| 45 std::string passhash = entry->GetLocalAuthCredentials(); | 45 std::string passhash = entry->GetLocalAuthCredentials(); |
| 46 | 46 |
| 47 // We perform basic validation on the written record to ensure bugs don't slip | 47 // We perform basic validation on the written record to ensure bugs don't slip |
| 48 // in that cannot be seen from the API: | 48 // in that cannot be seen from the API: |
| 49 // - The encoding exists (we can guarantee future backward compatibility). | 49 // - The encoding exists (we can guarantee future backward compatibility). |
| 50 // - The plaintext version of the password is not mistakenly stored anywhere. | 50 // - 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 | 63 LocalAuth::SetLocalAuthCredentials(prof, password); // makes different salt |
| 64 EXPECT_NE(passhash, entry->GetLocalAuthCredentials()); | 64 EXPECT_NE(passhash, entry->GetLocalAuthCredentials()); |
| 65 } | 65 } |
| 66 | 66 |
| 67 TEST_F(LocalAuthTest, SetUpgradeAndCheckCredentials) { | 67 TEST_F(LocalAuthTest, SetUpgradeAndCheckCredentials) { |
| 68 TestingProfileManager testing_profile_manager( | 68 TestingProfileManager testing_profile_manager( |
| 69 TestingBrowserProcess::GetGlobal()); | 69 TestingBrowserProcess::GetGlobal()); |
| 70 ASSERT_TRUE(testing_profile_manager.SetUp()); | 70 ASSERT_TRUE(testing_profile_manager.SetUp()); |
| 71 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); | 71 Profile* prof = testing_profile_manager.CreateTestingProfile("p1"); |
| 72 | 72 |
| 73 #if defined(OS_MACOSX) | |
| 74 OSCrypt::UseMockKeychain(true); | |
| 75 #endif | |
| 76 | |
| 77 std::string password("Some Password"); | 73 std::string password("Some Password"); |
| 78 ProfileAttributesEntry* entry; | 74 ProfileAttributesEntry* entry; |
| 79 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> | 75 ASSERT_TRUE(testing_profile_manager.profile_attributes_storage()-> |
| 80 GetProfileAttributesWithPath(prof->GetPath(), &entry)); | 76 GetProfileAttributesWithPath(prof->GetPath(), &entry)); |
| 81 LocalAuth::SetLocalAuthCredentialsWithEncoding(entry, password, '1'); | 77 LocalAuth::SetLocalAuthCredentialsWithEncoding(entry, password, '1'); |
| 82 | 78 |
| 83 // Ensure we indeed persisted the correct encoding. | 79 // Ensure we indeed persisted the correct encoding. |
| 84 std::string oldpasshash = entry->GetLocalAuthCredentials(); | 80 std::string oldpasshash = entry->GetLocalAuthCredentials(); |
| 85 EXPECT_EQ('1', oldpasshash[0]); | 81 EXPECT_EQ('1', oldpasshash[0]); |
| 86 | 82 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 112 TEST_F(LocalAuthTest, TruncateStringUnevenly) { | 108 TEST_F(LocalAuthTest, TruncateStringUnevenly) { |
| 113 std::string two_chars = "Az"; | 109 std::string two_chars = "Az"; |
| 114 std::string three_chars = "AzC"; | 110 std::string three_chars = "AzC"; |
| 115 // 'z' = 0x7A, ':' = 0x3A. | 111 // 'z' = 0x7A, ':' = 0x3A. |
| 116 std::string two_chars_truncated = "A:"; | 112 std::string two_chars_truncated = "A:"; |
| 117 EXPECT_EQ(two_chars_truncated, | 113 EXPECT_EQ(two_chars_truncated, |
| 118 LocalAuth::TruncateStringByBits(two_chars, 14)); | 114 LocalAuth::TruncateStringByBits(two_chars, 14)); |
| 119 EXPECT_EQ(two_chars_truncated, | 115 EXPECT_EQ(two_chars_truncated, |
| 120 LocalAuth::TruncateStringByBits(three_chars, 14)); | 116 LocalAuth::TruncateStringByBits(three_chars, 14)); |
| 121 } | 117 } |
| OLD | NEW |