Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 1 | // 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 Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame^] | 10 | #include <chromeos/data_encoding.h> |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | namespace buffet { |
| 14 | |
| 15 | TEST(BuffetConfigTest, LoadConfig) { |
| 16 | chromeos::KeyValueStore config_store; |
| 17 | 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 Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 29 | config_store.SetString("pairing_modes", "pinCode,embeddedCode"); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 30 | 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 Buka | becd461 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 46 | BuffetConfig config{{}}; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 47 | 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 Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 57 | EXPECT_FALSE(settings.wifi_auto_setup_enabled); |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 58 | std::set<weave::PairingType> pairing_types{weave::PairingType::kPinCode, |
| 59 | weave::PairingType::kEmbeddedCode}; |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 60 | 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 Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 65 | EXPECT_EQ(weave::AuthScope::kUser, settings.local_anonymous_access_role); |
Vitaly Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 66 | EXPECT_FALSE(settings.local_pairing_enabled); |
| 67 | EXPECT_FALSE(settings.local_discovery_enabled); |
| 68 | } |
| 69 | |
Alex Vakulenko | df38164 | 2015-10-08 07:34:23 -0700 | [diff] [blame^] | 70 | class 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 { |
| 85 | *ciphertext = chromeos::data_encoding::Base64Encode(plaintext); |
| 86 | return encryptor_result_; |
| 87 | }; |
| 88 | bool DecryptWithAuthentication(const std::string& ciphertext, |
| 89 | std::string* plaintext) override { |
| 90 | return encryptor_result_ && |
| 91 | chromeos::data_encoding::Base64Decode(ciphertext, plaintext); |
| 92 | }; |
| 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 | |
| 117 | TEST_F(BuffetConfigTestWithFakes, EncryptionEnabled) { |
| 118 | config_->SaveSettings("test"); |
| 119 | ASSERT_NE("test", fake_file_content_["settings_file"]); |
| 120 | ASSERT_EQ("test", config_->LoadSettings()); |
| 121 | } |
| 122 | |
| 123 | TEST_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 | |
| 132 | TEST_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 | |
| 140 | TEST_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 Buka | 1175a9b | 2015-08-15 10:42:17 -0700 | [diff] [blame] | 150 | } // namespace buffet |