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 instance_with_provided_salt.GenerateHashFromPassphrase(passphrase, |
| 24 &passphrase_hash); |
| 25 // As the method generates a Base64 encoded 128 bit key, we expect the |
| 26 // passphrase hash to have length at least 7 bytes. |
| 27 EXPECT_GE(passphrase_hash.size(), 7u); |
| 28 |
| 29 // When calling the function with a (slightly) different parameter, we |
| 30 // expect to get a different result. |
| 31 std::string passphrase2 = passphrase + "4"; |
| 32 std::string passphrase_hash2; |
| 33 instance_with_provided_salt.GenerateHashFromPassphrase(passphrase2, |
| 34 &passphrase_hash2); |
| 35 EXPECT_GE(passphrase_hash2.size(), 7u); |
| 36 EXPECT_STRNE(passphrase_hash.c_str(), passphrase_hash2.c_str()); |
| 37 |
| 38 // When calling the function again with the first parameter, we expect to |
| 39 // get the same result as in the first call. |
| 40 instance_with_provided_salt.GenerateHashFromPassphrase(passphrase, |
| 41 &passphrase_hash2); |
| 42 EXPECT_STREQ(passphrase_hash.c_str(), passphrase_hash2.c_str()); |
| 43 |
| 44 // When calling the function on the instance with the other salt, but |
| 45 // with the same passphrase, we expect to get a different result. |
| 46 other_instance_with_provided_salt.GenerateHashFromPassphrase( |
| 47 passphrase, |
| 48 &passphrase_hash2); |
| 49 EXPECT_STRNE(passphrase_hash.c_str(), passphrase_hash2.c_str()); |
| 50 } |
| 51 |
| 52 // This test checks the class ManagedUserPassphraseTest when no salt is |
| 53 // provided as parameter of the constructor. |
| 54 TEST(ManagedUserPassphraseTest, WithEmptySalt) { |
| 55 ManagedUserPassphrase instance_with_empty_salt((std::string())); |
| 56 ManagedUserPassphrase other_instance_with_empty_salt((std::string())); |
| 57 std::string salt = instance_with_empty_salt.GetSalt(); |
| 58 std::string salt2 = other_instance_with_empty_salt.GetSalt(); |
| 59 |
| 60 // We expect that the class will generate a salt randomly, and for different |
| 61 // instances a different salt is calculated. |
| 62 EXPECT_GT(salt.size(), 0u); |
| 63 EXPECT_GT(salt2.size(), 0u); |
| 64 EXPECT_STRNE(salt.c_str(), salt2.c_str()); |
| 65 } |
OLD | NEW |