blob: f19812ec025fbd35abcf778f22e63056df2b0a5a [file] [log] [blame]
Vitaly Buka1175a9b2015-08-15 10:42:17 -07001// Copyright 2014 The Chromium OS 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 "buffet/buffet_config.h"
6
7#include <set>
8
9#include <base/bind.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070010#include <brillo/data_encoding.h>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070011#include <gtest/gtest.h>
12
13namespace buffet {
14
15TEST(BuffetConfigTest, LoadConfig) {
Alex Vakulenko41705852015-10-13 10:12:06 -070016 brillo::KeyValueStore config_store;
Vitaly Buka1175a9b2015-08-15 10:42:17 -070017 config_store.SetString("client_id", "conf_client_id");
18 config_store.SetString("client_secret", "conf_client_secret");
19 config_store.SetString("api_key", "conf_api_key");
20 config_store.SetString("oauth_url", "conf_oauth_url");
21 config_store.SetString("service_url", "conf_service_url");
22 config_store.SetString("oem_name", "conf_oem_name");
23 config_store.SetString("model_name", "conf_model_name");
24 config_store.SetString("model_id", "ABCDE");
25 config_store.SetString("polling_period_ms", "12345");
26 config_store.SetString("backup_polling_period_ms", "6589");
27 config_store.SetBoolean("wifi_auto_setup_enabled", false);
28 config_store.SetBoolean("ble_setup_enabled", true);
Alex Vakulenko0022b752015-10-02 11:09:59 -070029 config_store.SetString("pairing_modes", "pinCode,embeddedCode");
Vitaly Buka1175a9b2015-08-15 10:42:17 -070030 config_store.SetString("embedded_code", "567");
31 config_store.SetString("name", "conf_name");
32 config_store.SetString("description", "conf_description");
33 config_store.SetString("location", "conf_location");
34 config_store.SetString("local_anonymous_access_role", "user");
35 config_store.SetBoolean("local_pairing_enabled", false);
36 config_store.SetBoolean("local_discovery_enabled", false);
37
38 // Following will be ignored.
39 config_store.SetString("device_kind", "conf_device_kind");
40 config_store.SetString("device_id", "conf_device_id");
41 config_store.SetString("refresh_token", "conf_refresh_token");
42 config_store.SetString("robot_account", "conf_robot_account");
43 config_store.SetString("last_configured_ssid", "conf_last_configured_ssid");
44
45 weave::Settings settings;
Vitaly Bukabecd4612015-08-16 23:31:55 -070046 BuffetConfig config{{}};
Vitaly Buka1175a9b2015-08-15 10:42:17 -070047 EXPECT_TRUE(config.LoadDefaults(config_store, &settings));
48
49 EXPECT_EQ("conf_client_id", settings.client_id);
50 EXPECT_EQ("conf_client_secret", settings.client_secret);
51 EXPECT_EQ("conf_api_key", settings.api_key);
52 EXPECT_EQ("conf_oauth_url", settings.oauth_url);
53 EXPECT_EQ("conf_service_url", settings.service_url);
54 EXPECT_EQ("conf_oem_name", settings.oem_name);
55 EXPECT_EQ("conf_model_name", settings.model_name);
56 EXPECT_EQ("ABCDE", settings.model_id);
Vitaly Buka1175a9b2015-08-15 10:42:17 -070057 EXPECT_FALSE(settings.wifi_auto_setup_enabled);
Alex Vakulenko0022b752015-10-02 11:09:59 -070058 std::set<weave::PairingType> pairing_types{weave::PairingType::kPinCode,
59 weave::PairingType::kEmbeddedCode};
Vitaly Buka1175a9b2015-08-15 10:42:17 -070060 EXPECT_EQ(pairing_types, settings.pairing_modes);
61 EXPECT_EQ("567", settings.embedded_code);
62 EXPECT_EQ("conf_name", settings.name);
63 EXPECT_EQ("conf_description", settings.description);
64 EXPECT_EQ("conf_location", settings.location);
Alex Vakulenko0022b752015-10-02 11:09:59 -070065 EXPECT_EQ(weave::AuthScope::kUser, settings.local_anonymous_access_role);
Vitaly Buka1175a9b2015-08-15 10:42:17 -070066 EXPECT_FALSE(settings.local_pairing_enabled);
67 EXPECT_FALSE(settings.local_discovery_enabled);
68}
69
Alex Vakulenkodf381642015-10-08 07:34:23 -070070class BuffetConfigTestWithFakes : public testing::Test,
71 public BuffetConfig::FileIO,
72 public Encryptor {
73 public:
74 void SetUp() {
75 BuffetConfig::Options config_options;
76 config_options.settings = base::FilePath{"settings_file"};
77 config_.reset(new BuffetConfig{config_options});
78 config_->SetEncryptor(this);
79 config_->SetFileIO(this);
80 };
81
82 // buffet::Encryptor methods.
83 bool EncryptWithAuthentication(const std::string& plaintext,
84 std::string* ciphertext) override {
Alex Vakulenko41705852015-10-13 10:12:06 -070085 *ciphertext = brillo::data_encoding::Base64Encode(plaintext);
Alex Vakulenkodf381642015-10-08 07:34:23 -070086 return encryptor_result_;
87 };
88 bool DecryptWithAuthentication(const std::string& ciphertext,
89 std::string* plaintext) override {
90 return encryptor_result_ &&
Alex Vakulenko41705852015-10-13 10:12:06 -070091 brillo::data_encoding::Base64Decode(ciphertext, plaintext);
Alex Vakulenkodf381642015-10-08 07:34:23 -070092 };
93
94 // buffet::BuffetConfig::FileIO methods.
95 bool ReadFile(const base::FilePath& path, std::string* content) override {
96 if (fake_file_content_.count(path.value()) == 0) {
97 return false;
98 }
99 *content = fake_file_content_[path.value()];
100 return io_result_;
101 };
102 bool WriteFile(const base::FilePath& path,
103 const std::string& content) override {
104 if (io_result_) {
105 fake_file_content_[path.value()] = content;
106 }
107 return io_result_;
108 };
109
110 protected:
111 std::map<std::string, std::string> fake_file_content_;
112 bool encryptor_result_ = true;
113 bool io_result_ = true;
114 std::unique_ptr<BuffetConfig> config_;
115};
116
117TEST_F(BuffetConfigTestWithFakes, EncryptionEnabled) {
118 config_->SaveSettings("test");
119 ASSERT_NE("test", fake_file_content_["settings_file"]);
120 ASSERT_EQ("test", config_->LoadSettings());
121}
122
123TEST_F(BuffetConfigTestWithFakes, EncryptionFailure) {
124 config_->SaveSettings("test");
125 ASSERT_FALSE(fake_file_content_["settings_file"].empty());
126 encryptor_result_ = false;
127 config_->SaveSettings("test2");
128 // Encryption fails -> file cleared.
129 ASSERT_TRUE(fake_file_content_["settings_file"].empty());
130}
131
132TEST_F(BuffetConfigTestWithFakes, DecryptionFailure) {
133 config_->SaveSettings("test");
134 ASSERT_FALSE(fake_file_content_["settings_file"].empty());
135 encryptor_result_ = false;
136 // Decryption fails -> empty settings loaded.
137 ASSERT_TRUE(config_->LoadSettings().empty());
138}
139
140TEST_F(BuffetConfigTestWithFakes, SettingsIOFailure) {
141 config_->SaveSettings("test");
142 std::string original = fake_file_content_["settings_file"];
143 ASSERT_FALSE(original.empty());
144 io_result_ = false;
145 ASSERT_TRUE(config_->LoadSettings().empty());
146 config_->SaveSettings("test2");
147 ASSERT_EQ(original, fake_file_content_["settings_file"]);
148}
149
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700150} // namespace buffet