blob: 58d6da381c99bf076d3e542a5f9f5476b24d88e7 [file] [log] [blame]
cfroussios3b5a4e42016-05-31 11:02:181// 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
13namespace {
14
Christos Froussios2d15a8e2017-07-21 16:30:3415std::unique_ptr<KeyStorageLinux> GetNullKeyStorage() {
dvadyme26d614b2017-05-05 15:33:1516 return nullptr;
17}
18
cfroussios3b5a4e42016-05-31 11:02:1819class OSCryptLinuxTest : public testing::Test {
20 public:
Christos Froussios2d15a8e2017-07-21 16:30:3421 OSCryptLinuxTest() : key_("something") { key_ptr_ = &key_; }
22
23 ~OSCryptLinuxTest() override { key_ptr_ = nullptr; };
cfroussios3b5a4e42016-05-31 11:02:1824
25 void SetUp() override {
Christos Froussios2d15a8e2017-07-21 16:30:3426 OSCryptMockerLinux::SetUp();
27 UseMockKeyStorageForTesting(nullptr, OSCryptLinuxTest::GetKey);
cfroussios3b5a4e42016-05-31 11:02:1828 }
29
30 void TearDown() override { OSCryptMockerLinux::TearDown(); }
31
32 protected:
Christos Froussios2d15a8e2017-07-21 16:30:3433 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_; }
cfroussios3b5a4e42016-05-31 11:02:1837
38 private:
Christos Froussios2d15a8e2017-07-21 16:30:3439 std::string key_;
40 // Points to the |key_| of the currently running test.
41 static std::string* key_ptr_;
42
cfroussios3b5a4e42016-05-31 11:02:1843 DISALLOW_COPY_AND_ASSIGN(OSCryptLinuxTest);
44};
45
Christos Froussios2d15a8e2017-07-21 16:30:3446std::string* OSCryptLinuxTest::key_ptr_;
47
cfroussios3b5a4e42016-05-31 11:02:1848TEST_F(OSCryptLinuxTest, VerifyV0) {
49 const std::string originaltext = "hello";
50 std::string ciphertext;
51 std::string decipheredtext;
52
Christos Froussios2d15a8e2017-07-21 16:30:3453 SetEncryptionKey(std::string());
cfroussios3b5a4e42016-05-31 11:02:1854 ciphertext = originaltext; // No encryption
55 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
56 ASSERT_EQ(originaltext, decipheredtext);
57}
58
59TEST_F(OSCryptLinuxTest, VerifyV10) {
60 const std::string originaltext = "hello";
61 std::string ciphertext;
62 std::string decipheredtext;
63
Christos Froussios2d15a8e2017-07-21 16:30:3464 SetEncryptionKey("peanuts");
cfroussios3b5a4e42016-05-31 11:02:1865 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
Christos Froussios2d15a8e2017-07-21 16:30:3466 SetEncryptionKey("not_peanuts");
cfroussios3b5a4e42016-05-31 11:02:1867 ciphertext = ciphertext.substr(3).insert(0, "v10");
68 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
69 ASSERT_EQ(originaltext, decipheredtext);
70}
71
72TEST_F(OSCryptLinuxTest, VerifyV11) {
73 const std::string originaltext = "hello";
74 std::string ciphertext;
75 std::string decipheredtext;
76
Christos Froussios2d15a8e2017-07-21 16:30:3477 SetEncryptionKey(std::string());
cfroussios3b5a4e42016-05-31 11:02:1878 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
dvadyme26d614b2017-05-05 15:33:1584TEST_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
cfroussios3b5a4e42016-05-31 11:02:1893} // namespace