OLD | NEW |
| (Empty) |
1 // Copyright (c) 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. | |
4 | |
5 #include "chrome/browser/managed_mode/managed_user_passphrase.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 // This test checks the class ManagedUserPassphrase when providing a salt | |
12 // as parameter of the constructor. | |
13 TEST(ManagedUserPassphraseTest, WithSaltProvided) { | |
14 std::string salt = "my special salt"; | |
15 std::string salt2 = "my other special salt"; | |
16 ManagedUserPassphrase instance_with_provided_salt(salt); | |
17 ManagedUserPassphrase other_instance_with_provided_salt(salt2); | |
18 | |
19 // We expect that the provided salt is used internally as well. | |
20 EXPECT_STREQ(salt.c_str(), instance_with_provided_salt.GetSalt().c_str()); | |
21 std::string passphrase_hash; | |
22 std::string passphrase = "some_passphrase123"; | |
23 EXPECT_TRUE(instance_with_provided_salt.GenerateHashFromPassphrase( | |
24 passphrase, | |
25 &passphrase_hash)); | |
26 // As the method generates a Base64 encoded 128 bit key, we expect the | |
27 // passphrase hash to have length at least 7 bytes. | |
28 EXPECT_GE(passphrase_hash.size(), 7u); | |
29 | |
30 // When calling the function with a (slightly) different parameter, we | |
31 // expect to get a different result. | |
32 std::string passphrase2 = passphrase + "4"; | |
33 std::string passphrase_hash2; | |
34 EXPECT_TRUE(instance_with_provided_salt.GenerateHashFromPassphrase( | |
35 passphrase2, | |
36 &passphrase_hash2)); | |
37 EXPECT_GE(passphrase_hash2.size(), 7u); | |
38 EXPECT_STRNE(passphrase_hash.c_str(), passphrase_hash2.c_str()); | |
39 | |
40 // When calling the function again with the first parameter, we expect to | |
41 // get the same result as in the first call. | |
42 EXPECT_TRUE(instance_with_provided_salt.GenerateHashFromPassphrase( | |
43 passphrase, | |
44 &passphrase_hash2)); | |
45 EXPECT_STREQ(passphrase_hash.c_str(), passphrase_hash2.c_str()); | |
46 | |
47 // When calling the function on the instance with the other salt, but | |
48 // with the same passphrase, we expect to get a different result. | |
49 EXPECT_TRUE(other_instance_with_provided_salt.GenerateHashFromPassphrase( | |
50 passphrase, | |
51 &passphrase_hash2)); | |
52 EXPECT_STRNE(passphrase_hash.c_str(), passphrase_hash2.c_str()); | |
53 } | |
54 | |
55 // This test checks the class ManagedUserPassphraseTest when no salt is | |
56 // provided as parameter of the constructor. | |
57 TEST(ManagedUserPassphraseTest, WithEmptySalt) { | |
58 ManagedUserPassphrase instance_with_empty_salt((std::string())); | |
59 ManagedUserPassphrase other_instance_with_empty_salt((std::string())); | |
60 std::string salt = instance_with_empty_salt.GetSalt(); | |
61 std::string salt2 = other_instance_with_empty_salt.GetSalt(); | |
62 | |
63 // We expect that the class will generate a salt randomly, and for different | |
64 // instances a different salt is calculated. | |
65 EXPECT_GT(salt.size(), 0u); | |
66 EXPECT_GT(salt2.size(), 0u); | |
67 EXPECT_STRNE(salt.c_str(), salt2.c_str()); | |
68 } | |
OLD | NEW |