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
3 // found in the LICENSE file.
5 #include "chrome/browser/signin/local_auth.h"
7 #include "base/base64.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/test/base/testing_browser_process.h"
11 #include "chrome/test/base/testing_pref_service_syncable.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "chrome/test/base/testing_profile_manager.h"
14 #include "components/os_crypt/os_crypt.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 TEST(LocalAuthTest
, SetAndCheckCredentials
) {
19 TestingProfileManager
testing_profile_manager(
20 TestingBrowserProcess::GetGlobal());
21 ASSERT_TRUE(testing_profile_manager
.SetUp());
22 Profile
* prof
= testing_profile_manager
.CreateTestingProfile("p1");
23 ProfileInfoCache
& cache
=
24 testing_profile_manager
.profile_manager()->GetProfileInfoCache();
25 EXPECT_EQ(1U, cache
.GetNumberOfProfiles());
26 EXPECT_EQ("", cache
.GetLocalAuthCredentialsOfProfileAtIndex(0));
28 #if defined(OS_MACOSX)
29 OSCrypt::UseMockKeychain(true);
32 std::string
password("Some Password");
33 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof
, password
));
35 LocalAuth::SetLocalAuthCredentials(prof
, password
);
36 std::string passhash
= cache
.GetLocalAuthCredentialsOfProfileAtIndex(0);
38 // We perform basic validation on the written record to ensure bugs don't slip
39 // in that cannot be seen from the API:
40 // - The encoding exists (we can guarantee future backward compatibility).
41 // - The plaintext version of the password is not mistakenly stored anywhere.
42 EXPECT_FALSE(passhash
.empty());
43 EXPECT_EQ('2', passhash
[0]);
44 EXPECT_EQ(passhash
.find(password
), std::string::npos
);
46 std::string decodedhash
;
47 base::Base64Decode(passhash
.substr(1), &decodedhash
);
48 EXPECT_FALSE(decodedhash
.empty());
49 EXPECT_EQ(decodedhash
.find(password
), std::string::npos
);
51 EXPECT_TRUE(LocalAuth::ValidateLocalAuthCredentials(prof
, password
));
52 EXPECT_FALSE(LocalAuth::ValidateLocalAuthCredentials(prof
, password
+ "1"));
54 LocalAuth::SetLocalAuthCredentials(prof
, password
); // makes different salt
55 EXPECT_NE(passhash
, cache
.GetLocalAuthCredentialsOfProfileAtIndex(0));
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();
67 #if defined(OS_MACOSX)
68 OSCrypt::UseMockKeychain(true);
71 std::string
password("Some Password");
72 size_t profile_index
= cache
.GetIndexOfProfileWithPath(prof
->GetPath());
73 LocalAuth::SetLocalAuthCredentialsWithEncoding(profile_index
, password
, '1');
75 // Ensure we indeed persisted the correct encoding.
76 std::string oldpasshash
= cache
.GetLocalAuthCredentialsOfProfileAtIndex(
78 EXPECT_EQ('1', oldpasshash
[0]);
80 // Validate, ensure we can validate against the old encoding.
81 EXPECT_TRUE(LocalAuth::ValidateLocalAuthCredentials(prof
, password
));
83 // Ensure we updated the encoding.
84 std::string newpasshash
= cache
.GetLocalAuthCredentialsOfProfileAtIndex(
86 EXPECT_EQ('2', newpasshash
[0]);
87 // Encoding '2' writes fewer bytes than encoding '1'.
88 EXPECT_LE(newpasshash
.length(), oldpasshash
.length());
90 // Validate, ensure we validate against the new encoding.
91 EXPECT_TRUE(LocalAuth::ValidateLocalAuthCredentials(prof
, password
));
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));
101 EXPECT_EQ(two_chars
, LocalAuth::TruncateStringByBits(two_chars
, 14));
102 EXPECT_EQ(two_chars
, LocalAuth::TruncateStringByBits(three_chars
, 14));
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));