aboutsummaryrefslogtreecommitdiff
path: root/buffet/buffet_config_unittest.cc
blob: 373a18c769f63ca7a1dd6f21a008856ebeb7098d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "buffet/buffet_config.h"

#include <set>

#include <base/bind.h>
#include <brillo/data_encoding.h>
#include <gtest/gtest.h>

namespace buffet {

TEST(BuffetConfigTest, LoadConfig) {
  brillo::KeyValueStore config_store;
  config_store.SetString("client_id", "conf_client_id");
  config_store.SetString("client_secret", "conf_client_secret");
  config_store.SetString("api_key", "conf_api_key");
  config_store.SetString("oauth_url", "conf_oauth_url");
  config_store.SetString("service_url", "conf_service_url");
  config_store.SetString("oem_name", "conf_oem_name");
  config_store.SetString("model_name", "conf_model_name");
  config_store.SetString("model_id", "ABCDE");
  config_store.SetString("polling_period_ms", "12345");
  config_store.SetString("backup_polling_period_ms", "6589");
  config_store.SetBoolean("wifi_auto_setup_enabled", false);
  config_store.SetBoolean("ble_setup_enabled", true);
  config_store.SetString("pairing_modes", "pinCode,embeddedCode");
  config_store.SetString("embedded_code", "567");
  config_store.SetString("name", "conf_name");
  config_store.SetString("description", "conf_description");
  config_store.SetString("location", "conf_location");
  config_store.SetString("local_anonymous_access_role", "user");
  config_store.SetBoolean("local_access_enabled", false);
  config_store.SetBoolean("allow_endpoints_override", true);

  // Following will be ignored.
  config_store.SetString("device_kind", "conf_device_kind");
  config_store.SetString("device_id", "conf_device_id");
  config_store.SetString("refresh_token", "conf_refresh_token");
  config_store.SetString("robot_account", "conf_robot_account");
  config_store.SetString("last_configured_ssid", "conf_last_configured_ssid");

  weave::Settings settings;
  BuffetConfig config{{}};
  EXPECT_TRUE(config.LoadDefaults(config_store, &settings));

  EXPECT_EQ("conf_client_id", settings.client_id);
  EXPECT_EQ("conf_client_secret", settings.client_secret);
  EXPECT_EQ("conf_api_key", settings.api_key);
  EXPECT_EQ("conf_oauth_url", settings.oauth_url);
  EXPECT_EQ("conf_service_url", settings.service_url);
  EXPECT_EQ("conf_oem_name", settings.oem_name);
  EXPECT_EQ("conf_model_name", settings.model_name);
  EXPECT_EQ("ABCDE", settings.model_id);
  EXPECT_FALSE(settings.wifi_auto_setup_enabled);
  std::set<weave::PairingType> pairing_types{weave::PairingType::kPinCode,
                                             weave::PairingType::kEmbeddedCode};
  EXPECT_EQ(pairing_types, settings.pairing_modes);
  EXPECT_EQ("567", settings.embedded_code);
  EXPECT_EQ("conf_name", settings.name);
  EXPECT_EQ("conf_description", settings.description);
  EXPECT_EQ("conf_location", settings.location);
  EXPECT_EQ(weave::AuthScope::kUser, settings.local_anonymous_access_role);
  EXPECT_FALSE(settings.local_access_enabled);
  EXPECT_TRUE(settings.allow_endpoints_override);
}

class BuffetConfigTestWithFakes : public testing::Test,
                                  public BuffetConfig::FileIO,
                                  public Encryptor {
 public:
  void SetUp() {
    BuffetConfig::Options config_options;
    config_options.settings = base::FilePath{"settings_file"};
    config_.reset(new BuffetConfig{config_options});
    config_->SetEncryptor(this);
    config_->SetFileIO(this);
  };

  // buffet::Encryptor methods.
  bool EncryptWithAuthentication(const std::string& plaintext,
                                 std::string* ciphertext) override {
    *ciphertext = brillo::data_encoding::Base64Encode(plaintext);
    return encryptor_result_;
  };
  bool DecryptWithAuthentication(const std::string& ciphertext,
                                 std::string* plaintext) override {
    return encryptor_result_ &&
           brillo::data_encoding::Base64Decode(ciphertext, plaintext);
  };

  // buffet::BuffetConfig::FileIO methods.
  bool ReadFile(const base::FilePath& path, std::string* content) override {
    if (fake_file_content_.count(path.value()) == 0) {
      return false;
    }
    *content = fake_file_content_[path.value()];
    return io_result_;
  };
  bool WriteFile(const base::FilePath& path,
                 const std::string& content) override {
    if (io_result_) {
      fake_file_content_[path.value()] = content;
    }
    return io_result_;
  };

 protected:
  std::map<std::string, std::string> fake_file_content_;
  bool encryptor_result_ = true;
  bool io_result_ = true;
  std::unique_ptr<BuffetConfig> config_;
};

TEST_F(BuffetConfigTestWithFakes, EncryptionEnabled) {
  config_->SaveSettings("config", "test", {});
  ASSERT_NE("test", fake_file_content_["settings_file.config"]);
  ASSERT_EQ("test", config_->LoadSettings("config"));
}

TEST_F(BuffetConfigTestWithFakes, EncryptionFailure) {
  config_->SaveSettings("config", "test", {});
  ASSERT_FALSE(fake_file_content_["settings_file.config"].empty());
  encryptor_result_ = false;
  config_->SaveSettings("config", "test2", {});
  // Encryption fails -> file cleared.
  ASSERT_TRUE(fake_file_content_["settings_file.config"].empty());
}

TEST_F(BuffetConfigTestWithFakes, DecryptionFailure) {
  config_->SaveSettings("config", "test", {});
  ASSERT_FALSE(fake_file_content_["settings_file.config"].empty());
  encryptor_result_ = false;
  // Decryption fails -> empty settings loaded.
  ASSERT_TRUE(config_->LoadSettings("config").empty());
}

TEST_F(BuffetConfigTestWithFakes, SettingsIOFailure) {
  config_->SaveSettings("config", "test", {});
  std::string original = fake_file_content_["settings_file.config"];
  ASSERT_FALSE(original.empty());
  io_result_ = false;
  ASSERT_TRUE(config_->LoadSettings("config").empty());
  config_->SaveSettings("config2", "test", {});
  ASSERT_EQ(original, fake_file_content_["settings_file.config"]);
}

}  // namespace buffet