cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 1 | // Copyright 2016 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 <string> |
| 6 | |
| 7 | #include "base/macros.h" |
| 8 | #include "components/os_crypt/key_storage_linux.h" |
| 9 | #include "components/os_crypt/os_crypt.h" |
| 10 | #include "components/os_crypt/os_crypt_mocker_linux.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 15 | std::unique_ptr<KeyStorageLinux> GetNullKeyStorage() { |
dvadym | e26d614b | 2017-05-05 15:33:15 | [diff] [blame] | 16 | return nullptr; |
| 17 | } |
| 18 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 19 | class OSCryptLinuxTest : public testing::Test { |
| 20 | public: |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 21 | OSCryptLinuxTest() : key_("something") { key_ptr_ = &key_; } |
| 22 | |
| 23 | ~OSCryptLinuxTest() override { key_ptr_ = nullptr; }; |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 24 | |
| 25 | void SetUp() override { |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 26 | OSCryptMockerLinux::SetUp(); |
| 27 | UseMockKeyStorageForTesting(nullptr, OSCryptLinuxTest::GetKey); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void TearDown() override { OSCryptMockerLinux::TearDown(); } |
| 31 | |
| 32 | protected: |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 33 | void SetEncryptionKey(const std::string& key) { key_ = key; } |
| 34 | |
| 35 | // Get the key of the currently running test. |
| 36 | static std::string* GetKey() { return key_ptr_; } |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 37 | |
| 38 | private: |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 39 | std::string key_; |
| 40 | // Points to the |key_| of the currently running test. |
| 41 | static std::string* key_ptr_; |
| 42 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 43 | DISALLOW_COPY_AND_ASSIGN(OSCryptLinuxTest); |
| 44 | }; |
| 45 | |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 46 | std::string* OSCryptLinuxTest::key_ptr_; |
| 47 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 48 | TEST_F(OSCryptLinuxTest, VerifyV0) { |
| 49 | const std::string originaltext = "hello"; |
| 50 | std::string ciphertext; |
| 51 | std::string decipheredtext; |
| 52 | |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 53 | SetEncryptionKey(std::string()); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 54 | ciphertext = originaltext; // No encryption |
| 55 | ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); |
| 56 | ASSERT_EQ(originaltext, decipheredtext); |
| 57 | } |
| 58 | |
| 59 | TEST_F(OSCryptLinuxTest, VerifyV10) { |
| 60 | const std::string originaltext = "hello"; |
| 61 | std::string ciphertext; |
| 62 | std::string decipheredtext; |
| 63 | |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 64 | SetEncryptionKey("peanuts"); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 65 | ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext)); |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 66 | SetEncryptionKey("not_peanuts"); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 67 | ciphertext = ciphertext.substr(3).insert(0, "v10"); |
| 68 | ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); |
| 69 | ASSERT_EQ(originaltext, decipheredtext); |
| 70 | } |
| 71 | |
| 72 | TEST_F(OSCryptLinuxTest, VerifyV11) { |
| 73 | const std::string originaltext = "hello"; |
| 74 | std::string ciphertext; |
| 75 | std::string decipheredtext; |
| 76 | |
Christos Froussios | 2d15a8e | 2017-07-21 16:30:34 | [diff] [blame] | 77 | SetEncryptionKey(std::string()); |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 78 | ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext)); |
| 79 | ASSERT_EQ(ciphertext.substr(0, 3), "v11"); |
| 80 | ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); |
| 81 | ASSERT_EQ(originaltext, decipheredtext); |
| 82 | } |
| 83 | |
dvadym | e26d614b | 2017-05-05 15:33:15 | [diff] [blame] | 84 | TEST_F(OSCryptLinuxTest, IsEncryptionAvailable) { |
| 85 | EXPECT_TRUE(OSCrypt::IsEncryptionAvailable()); |
| 86 | // Restore default GetKeyStorage and GetPassword functions. |
| 87 | UseMockKeyStorageForTesting(nullptr, nullptr); |
| 88 | // Mock only GetKeyStorage function. |
| 89 | UseMockKeyStorageForTesting(GetNullKeyStorage, nullptr); |
| 90 | EXPECT_FALSE(OSCrypt::IsEncryptionAvailable()); |
| 91 | } |
| 92 | |
cfroussios | 3b5a4e4 | 2016-05-31 11:02:18 | [diff] [blame] | 93 | } // namespace |