blob: a11ff4dd68782aeae48f6ed01b22694d574059c1 [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Vitaly Buka1175a9b2015-08-15 10:42:17 -070014
15#include "buffet/buffet_config.h"
16
17#include <set>
18
19#include <base/bind.h>
Alex Vakulenko41705852015-10-13 10:12:06 -070020#include <brillo/data_encoding.h>
Vitaly Buka1175a9b2015-08-15 10:42:17 -070021#include <gtest/gtest.h>
22
23namespace buffet {
24
25TEST(BuffetConfigTest, LoadConfig) {
Alex Vakulenko41705852015-10-13 10:12:06 -070026 brillo::KeyValueStore config_store;
Vitaly Buka1175a9b2015-08-15 10:42:17 -070027 config_store.SetString("client_id", "conf_client_id");
28 config_store.SetString("client_secret", "conf_client_secret");
29 config_store.SetString("api_key", "conf_api_key");
30 config_store.SetString("oauth_url", "conf_oauth_url");
31 config_store.SetString("service_url", "conf_service_url");
32 config_store.SetString("oem_name", "conf_oem_name");
33 config_store.SetString("model_name", "conf_model_name");
34 config_store.SetString("model_id", "ABCDE");
35 config_store.SetString("polling_period_ms", "12345");
36 config_store.SetString("backup_polling_period_ms", "6589");
37 config_store.SetBoolean("wifi_auto_setup_enabled", false);
38 config_store.SetBoolean("ble_setup_enabled", true);
Alex Vakulenko0022b752015-10-02 11:09:59 -070039 config_store.SetString("pairing_modes", "pinCode,embeddedCode");
Vitaly Buka1175a9b2015-08-15 10:42:17 -070040 config_store.SetString("embedded_code", "567");
41 config_store.SetString("name", "conf_name");
42 config_store.SetString("description", "conf_description");
43 config_store.SetString("location", "conf_location");
44 config_store.SetString("local_anonymous_access_role", "user");
45 config_store.SetBoolean("local_pairing_enabled", false);
46 config_store.SetBoolean("local_discovery_enabled", false);
47
48 // Following will be ignored.
49 config_store.SetString("device_kind", "conf_device_kind");
50 config_store.SetString("device_id", "conf_device_id");
51 config_store.SetString("refresh_token", "conf_refresh_token");
52 config_store.SetString("robot_account", "conf_robot_account");
53 config_store.SetString("last_configured_ssid", "conf_last_configured_ssid");
54
55 weave::Settings settings;
Vitaly Bukabecd4612015-08-16 23:31:55 -070056 BuffetConfig config{{}};
Vitaly Buka1175a9b2015-08-15 10:42:17 -070057 EXPECT_TRUE(config.LoadDefaults(config_store, &settings));
58
59 EXPECT_EQ("conf_client_id", settings.client_id);
60 EXPECT_EQ("conf_client_secret", settings.client_secret);
61 EXPECT_EQ("conf_api_key", settings.api_key);
62 EXPECT_EQ("conf_oauth_url", settings.oauth_url);
63 EXPECT_EQ("conf_service_url", settings.service_url);
64 EXPECT_EQ("conf_oem_name", settings.oem_name);
65 EXPECT_EQ("conf_model_name", settings.model_name);
66 EXPECT_EQ("ABCDE", settings.model_id);
Vitaly Buka1175a9b2015-08-15 10:42:17 -070067 EXPECT_FALSE(settings.wifi_auto_setup_enabled);
Alex Vakulenko0022b752015-10-02 11:09:59 -070068 std::set<weave::PairingType> pairing_types{weave::PairingType::kPinCode,
69 weave::PairingType::kEmbeddedCode};
Vitaly Buka1175a9b2015-08-15 10:42:17 -070070 EXPECT_EQ(pairing_types, settings.pairing_modes);
71 EXPECT_EQ("567", settings.embedded_code);
72 EXPECT_EQ("conf_name", settings.name);
73 EXPECT_EQ("conf_description", settings.description);
74 EXPECT_EQ("conf_location", settings.location);
Alex Vakulenko0022b752015-10-02 11:09:59 -070075 EXPECT_EQ(weave::AuthScope::kUser, settings.local_anonymous_access_role);
Vitaly Buka1175a9b2015-08-15 10:42:17 -070076 EXPECT_FALSE(settings.local_pairing_enabled);
77 EXPECT_FALSE(settings.local_discovery_enabled);
78}
79
Alex Vakulenkodf381642015-10-08 07:34:23 -070080class BuffetConfigTestWithFakes : public testing::Test,
81 public BuffetConfig::FileIO,
82 public Encryptor {
83 public:
84 void SetUp() {
85 BuffetConfig::Options config_options;
86 config_options.settings = base::FilePath{"settings_file"};
87 config_.reset(new BuffetConfig{config_options});
88 config_->SetEncryptor(this);
89 config_->SetFileIO(this);
90 };
91
92 // buffet::Encryptor methods.
93 bool EncryptWithAuthentication(const std::string& plaintext,
94 std::string* ciphertext) override {
Alex Vakulenko41705852015-10-13 10:12:06 -070095 *ciphertext = brillo::data_encoding::Base64Encode(plaintext);
Alex Vakulenkodf381642015-10-08 07:34:23 -070096 return encryptor_result_;
97 };
98 bool DecryptWithAuthentication(const std::string& ciphertext,
99 std::string* plaintext) override {
100 return encryptor_result_ &&
Alex Vakulenko41705852015-10-13 10:12:06 -0700101 brillo::data_encoding::Base64Decode(ciphertext, plaintext);
Alex Vakulenkodf381642015-10-08 07:34:23 -0700102 };
103
104 // buffet::BuffetConfig::FileIO methods.
105 bool ReadFile(const base::FilePath& path, std::string* content) override {
106 if (fake_file_content_.count(path.value()) == 0) {
107 return false;
108 }
109 *content = fake_file_content_[path.value()];
110 return io_result_;
111 };
112 bool WriteFile(const base::FilePath& path,
113 const std::string& content) override {
114 if (io_result_) {
115 fake_file_content_[path.value()] = content;
116 }
117 return io_result_;
118 };
119
120 protected:
121 std::map<std::string, std::string> fake_file_content_;
122 bool encryptor_result_ = true;
123 bool io_result_ = true;
124 std::unique_ptr<BuffetConfig> config_;
125};
126
127TEST_F(BuffetConfigTestWithFakes, EncryptionEnabled) {
Vitaly Bukae709fa72016-01-29 16:24:31 -0800128 config_->SaveSettings("config", "test", {});
129 ASSERT_NE("test", fake_file_content_["settings_file.config"]);
130 ASSERT_EQ("test", config_->LoadSettings("config"));
Alex Vakulenkodf381642015-10-08 07:34:23 -0700131}
132
133TEST_F(BuffetConfigTestWithFakes, EncryptionFailure) {
Vitaly Bukae709fa72016-01-29 16:24:31 -0800134 config_->SaveSettings("config", "test", {});
135 ASSERT_FALSE(fake_file_content_["settings_file.config"].empty());
Alex Vakulenkodf381642015-10-08 07:34:23 -0700136 encryptor_result_ = false;
Vitaly Bukae709fa72016-01-29 16:24:31 -0800137 config_->SaveSettings("config", "test2", {});
Alex Vakulenkodf381642015-10-08 07:34:23 -0700138 // Encryption fails -> file cleared.
Vitaly Bukae709fa72016-01-29 16:24:31 -0800139 ASSERT_TRUE(fake_file_content_["settings_file.config"].empty());
Alex Vakulenkodf381642015-10-08 07:34:23 -0700140}
141
142TEST_F(BuffetConfigTestWithFakes, DecryptionFailure) {
Vitaly Bukae709fa72016-01-29 16:24:31 -0800143 config_->SaveSettings("config", "test", {});
144 ASSERT_FALSE(fake_file_content_["settings_file.config"].empty());
Alex Vakulenkodf381642015-10-08 07:34:23 -0700145 encryptor_result_ = false;
146 // Decryption fails -> empty settings loaded.
Vitaly Bukae709fa72016-01-29 16:24:31 -0800147 ASSERT_TRUE(config_->LoadSettings("config").empty());
Alex Vakulenkodf381642015-10-08 07:34:23 -0700148}
149
150TEST_F(BuffetConfigTestWithFakes, SettingsIOFailure) {
Vitaly Bukae709fa72016-01-29 16:24:31 -0800151 config_->SaveSettings("config", "test", {});
152 std::string original = fake_file_content_["settings_file.config"];
Alex Vakulenkodf381642015-10-08 07:34:23 -0700153 ASSERT_FALSE(original.empty());
154 io_result_ = false;
Vitaly Bukae709fa72016-01-29 16:24:31 -0800155 ASSERT_TRUE(config_->LoadSettings("config").empty());
156 config_->SaveSettings("config2", "test", {});
157 ASSERT_EQ(original, fake_file_content_["settings_file.config"]);
Alex Vakulenkodf381642015-10-08 07:34:23 -0700158}
159
Vitaly Buka1175a9b2015-08-15 10:42:17 -0700160} // namespace buffet