Home | History | Annotate | Download | only in src
      1 // Copyright 2015 The Weave 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 "src/config.h"
      6 
      7 #include <set>
      8 
      9 #include <base/bind.h>
     10 #include <gmock/gmock.h>
     11 #include <gtest/gtest.h>
     12 #include <weave/provider/test/mock_config_store.h>
     13 #include <weave/test/unittest_utils.h>
     14 
     15 #include "src/data_encoding.h"
     16 
     17 using testing::_;
     18 using testing::Invoke;
     19 using testing::Return;
     20 using testing::WithArgs;
     21 
     22 namespace weave {
     23 
     24 const char kConfigName[] = "config";
     25 
     26 class ConfigTest : public ::testing::Test {
     27  protected:
     28   void SetUp() override {
     29     Reload();
     30   }
     31 
     32   void Reload() {
     33     EXPECT_CALL(*this, OnConfigChanged(_)).Times(1);
     34     config_.reset(new Config{&config_store_});
     35     config_->AddOnChangedCallback(
     36         base::Bind(&ConfigTest::OnConfigChanged, base::Unretained(this)));
     37   }
     38 
     39   const Config::Settings& GetSettings() const { return config_->GetSettings(); }
     40 
     41   const Config::Settings& GetDefaultSettings() const {
     42     return default_.GetSettings();
     43   }
     44 
     45   MOCK_METHOD1(OnConfigChanged, void(const Settings&));
     46 
     47   provider::test::MockConfigStore config_store_;
     48   std::unique_ptr<Config> config_{new Config{nullptr}};
     49   Config default_{&config_store_};
     50 };
     51 
     52 TEST_F(ConfigTest, NoStorage) {
     53   Config config{nullptr};
     54   Config::Transaction change{&config};
     55   change.Commit();
     56 }
     57 
     58 TEST_F(ConfigTest, Defaults) {
     59   config_.reset(new Config{nullptr});
     60   EXPECT_EQ("", GetSettings().client_id);
     61   EXPECT_EQ("", GetSettings().client_secret);
     62   EXPECT_EQ("", GetSettings().api_key);
     63   EXPECT_EQ("https://accounts.google.com/o/oauth2/", GetSettings().oauth_url);
     64   EXPECT_EQ("https://www.googleapis.com/weave/v1/", GetSettings().service_url);
     65   EXPECT_EQ("talk.google.com:5223", GetSettings().xmpp_endpoint);
     66   EXPECT_EQ("", GetSettings().oem_name);
     67   EXPECT_EQ("", GetSettings().model_name);
     68   EXPECT_EQ("", GetSettings().model_id);
     69   EXPECT_FALSE(GetSettings().device_id.empty());
     70   EXPECT_EQ("", GetSettings().firmware_version);
     71   EXPECT_TRUE(GetSettings().wifi_auto_setup_enabled);
     72   EXPECT_EQ("", GetSettings().test_privet_ssid);
     73   EXPECT_EQ(std::set<PairingType>{PairingType::kPinCode},
     74             GetSettings().pairing_modes);
     75   EXPECT_EQ("", GetSettings().embedded_code);
     76   EXPECT_EQ("", GetSettings().name);
     77   EXPECT_EQ("", GetSettings().description);
     78   EXPECT_EQ("", GetSettings().location);
     79   EXPECT_EQ(AuthScope::kViewer, GetSettings().local_anonymous_access_role);
     80   EXPECT_TRUE(GetSettings().local_pairing_enabled);
     81   EXPECT_TRUE(GetSettings().local_discovery_enabled);
     82   EXPECT_EQ("", GetSettings().cloud_id);
     83   EXPECT_EQ("", GetSettings().refresh_token);
     84   EXPECT_EQ("", GetSettings().robot_account);
     85   EXPECT_EQ("", GetSettings().last_configured_ssid);
     86   EXPECT_EQ(std::vector<uint8_t>(), GetSettings().secret);
     87   EXPECT_EQ(RootClientTokenOwner::kNone, GetSettings().root_client_token_owner);
     88 }
     89 
     90 TEST_F(ConfigTest, LoadStateV0) {
     91   EXPECT_CALL(config_store_, LoadSettings(kConfigName))
     92       .WillOnce(Return(R"({
     93     "device_id": "state_device_id"
     94   })"));
     95 
     96   Reload();
     97 
     98   EXPECT_EQ("state_device_id", GetSettings().cloud_id);
     99   EXPECT_FALSE(GetSettings().device_id.empty());
    100   EXPECT_NE(GetSettings().cloud_id, GetSettings().device_id);
    101 
    102   EXPECT_CALL(config_store_, LoadSettings(kConfigName))
    103       .WillOnce(Return(R"({
    104     "device_id": "state_device_id",
    105     "cloud_id": "state_cloud_id"
    106   })"));
    107 
    108   Reload();
    109 
    110   EXPECT_EQ("state_cloud_id", GetSettings().cloud_id);
    111   EXPECT_EQ("state_device_id", GetSettings().device_id);
    112 }
    113 
    114 TEST_F(ConfigTest, LoadStateUnnamed) {
    115   EXPECT_CALL(config_store_, LoadSettings(kConfigName)).WillOnce(Return(""));
    116 
    117   EXPECT_CALL(config_store_, LoadSettings()).Times(1);
    118 
    119   Reload();
    120 }
    121 
    122 TEST_F(ConfigTest, LoadStateNamed) {
    123   EXPECT_CALL(config_store_, LoadSettings(kConfigName)).WillOnce(Return("{}"));
    124 
    125   EXPECT_CALL(config_store_, LoadSettings()).Times(0);
    126 
    127   Reload();
    128 }
    129 
    130 TEST_F(ConfigTest, LoadState) {
    131   auto state = R"({
    132     "version": 1,
    133     "api_key": "state_api_key",
    134     "client_id": "state_client_id",
    135     "client_secret": "state_client_secret",
    136     "cloud_id": "state_cloud_id",
    137     "description": "state_description",
    138     "device_id": "state_device_id",
    139     "last_configured_ssid": "state_last_configured_ssid",
    140     "local_anonymous_access_role": "user",
    141     "root_client_token_owner": "client",
    142     "local_discovery_enabled": false,
    143     "local_pairing_enabled": false,
    144     "location": "state_location",
    145     "name": "state_name",
    146     "oauth_url": "state_oauth_url",
    147     "refresh_token": "state_refresh_token",
    148     "robot_account": "state_robot_account",
    149     "secret": "c3RhdGVfc2VjcmV0",
    150     "service_url": "state_service_url",
    151     "xmpp_endpoint": "state_xmpp_endpoint"
    152   })";
    153   EXPECT_CALL(config_store_, LoadSettings(kConfigName)).WillOnce(Return(state));
    154 
    155   Reload();
    156 
    157   EXPECT_EQ("state_client_id", GetSettings().client_id);
    158   EXPECT_EQ("state_client_secret", GetSettings().client_secret);
    159   EXPECT_EQ("state_api_key", GetSettings().api_key);
    160   EXPECT_EQ("state_oauth_url", GetSettings().oauth_url);
    161   EXPECT_EQ("state_service_url", GetSettings().service_url);
    162   EXPECT_EQ("state_xmpp_endpoint", GetSettings().xmpp_endpoint);
    163   EXPECT_EQ(GetDefaultSettings().oem_name, GetSettings().oem_name);
    164   EXPECT_EQ(GetDefaultSettings().model_name, GetSettings().model_name);
    165   EXPECT_EQ(GetDefaultSettings().model_id, GetSettings().model_id);
    166   EXPECT_EQ("state_device_id", GetSettings().device_id);
    167   EXPECT_EQ(GetDefaultSettings().wifi_auto_setup_enabled,
    168             GetSettings().wifi_auto_setup_enabled);
    169   EXPECT_EQ(GetDefaultSettings().test_privet_ssid,
    170             GetSettings().test_privet_ssid);
    171   EXPECT_EQ(GetDefaultSettings().pairing_modes, GetSettings().pairing_modes);
    172   EXPECT_EQ(GetDefaultSettings().embedded_code, GetSettings().embedded_code);
    173   EXPECT_EQ("state_name", GetSettings().name);
    174   EXPECT_EQ("state_description", GetSettings().description);
    175   EXPECT_EQ("state_location", GetSettings().location);
    176   EXPECT_EQ(AuthScope::kUser, GetSettings().local_anonymous_access_role);
    177   EXPECT_FALSE(GetSettings().local_pairing_enabled);
    178   EXPECT_FALSE(GetSettings().local_discovery_enabled);
    179   EXPECT_EQ("state_cloud_id", GetSettings().cloud_id);
    180   EXPECT_EQ("state_refresh_token", GetSettings().refresh_token);
    181   EXPECT_EQ("state_robot_account", GetSettings().robot_account);
    182   EXPECT_EQ("state_last_configured_ssid", GetSettings().last_configured_ssid);
    183   EXPECT_EQ("c3RhdGVfc2VjcmV0", Base64Encode(GetSettings().secret));
    184   EXPECT_EQ(RootClientTokenOwner::kClient,
    185             GetSettings().root_client_token_owner);
    186 }
    187 
    188 TEST_F(ConfigTest, Setters) {
    189   Config::Transaction change{config_.get()};
    190 
    191   change.set_client_id("set_client_id");
    192   EXPECT_EQ("set_client_id", GetSettings().client_id);
    193 
    194   change.set_client_secret("set_client_secret");
    195   EXPECT_EQ("set_client_secret", GetSettings().client_secret);
    196 
    197   change.set_api_key("set_api_key");
    198   EXPECT_EQ("set_api_key", GetSettings().api_key);
    199 
    200   change.set_oauth_url("set_oauth_url");
    201   EXPECT_EQ("set_oauth_url", GetSettings().oauth_url);
    202 
    203   change.set_service_url("set_service_url");
    204   EXPECT_EQ("set_service_url", GetSettings().service_url);
    205 
    206   change.set_xmpp_endpoint("set_xmpp_endpoint");
    207   EXPECT_EQ("set_xmpp_endpoint", GetSettings().xmpp_endpoint);
    208 
    209   change.set_name("set_name");
    210   EXPECT_EQ("set_name", GetSettings().name);
    211 
    212   change.set_description("set_description");
    213   EXPECT_EQ("set_description", GetSettings().description);
    214 
    215   change.set_location("set_location");
    216   EXPECT_EQ("set_location", GetSettings().location);
    217 
    218   change.set_local_anonymous_access_role(AuthScope::kViewer);
    219   EXPECT_EQ(AuthScope::kViewer, GetSettings().local_anonymous_access_role);
    220 
    221   change.set_local_anonymous_access_role(AuthScope::kNone);
    222   EXPECT_EQ(AuthScope::kNone, GetSettings().local_anonymous_access_role);
    223 
    224   change.set_local_anonymous_access_role(AuthScope::kUser);
    225   EXPECT_EQ(AuthScope::kUser, GetSettings().local_anonymous_access_role);
    226 
    227   change.set_local_discovery_enabled(false);
    228   EXPECT_FALSE(GetSettings().local_discovery_enabled);
    229 
    230   change.set_local_pairing_enabled(false);
    231   EXPECT_FALSE(GetSettings().local_pairing_enabled);
    232 
    233   change.set_local_discovery_enabled(true);
    234   EXPECT_TRUE(GetSettings().local_discovery_enabled);
    235 
    236   change.set_local_pairing_enabled(true);
    237   EXPECT_TRUE(GetSettings().local_pairing_enabled);
    238 
    239   change.set_cloud_id("set_cloud_id");
    240   EXPECT_EQ("set_cloud_id", GetSettings().cloud_id);
    241 
    242   change.set_device_id("set_device_id");
    243   EXPECT_EQ("set_device_id", GetSettings().device_id);
    244 
    245   change.set_refresh_token("set_token");
    246   EXPECT_EQ("set_token", GetSettings().refresh_token);
    247 
    248   change.set_robot_account("set_account");
    249   EXPECT_EQ("set_account", GetSettings().robot_account);
    250 
    251   change.set_last_configured_ssid("set_last_configured_ssid");
    252   EXPECT_EQ("set_last_configured_ssid", GetSettings().last_configured_ssid);
    253 
    254   const std::vector<uint8_t> secret{1, 2, 3, 4, 5};
    255   change.set_secret(secret);
    256   EXPECT_EQ(secret, GetSettings().secret);
    257 
    258   change.set_root_client_token_owner(RootClientTokenOwner::kCloud);
    259   EXPECT_EQ(RootClientTokenOwner::kCloud,
    260             GetSettings().root_client_token_owner);
    261 
    262   EXPECT_CALL(*this, OnConfigChanged(_)).Times(1);
    263 
    264   EXPECT_CALL(config_store_, SaveSettings(kConfigName, _, _))
    265       .WillOnce(WithArgs<1, 2>(
    266           Invoke([](const std::string& json, const DoneCallback& callback) {
    267             auto expected = R"({
    268           'version': 1,
    269           'api_key': 'set_api_key',
    270           'client_id': 'set_client_id',
    271           'client_secret': 'set_client_secret',
    272           'cloud_id': 'set_cloud_id',
    273           'description': 'set_description',
    274           'device_id': 'set_device_id',
    275           'last_configured_ssid': 'set_last_configured_ssid',
    276           'local_anonymous_access_role': 'user',
    277           'root_client_token_owner': 'cloud',
    278           'local_discovery_enabled': true,
    279           'local_pairing_enabled': true,
    280           'location': 'set_location',
    281           'name': 'set_name',
    282           'oauth_url': 'set_oauth_url',
    283           'refresh_token': 'set_token',
    284           'robot_account': 'set_account',
    285           'secret': 'AQIDBAU=',
    286           'service_url': 'set_service_url',
    287           'xmpp_endpoint': 'set_xmpp_endpoint'
    288         })";
    289             EXPECT_JSON_EQ(expected, *test::CreateValue(json));
    290             callback.Run(nullptr);
    291           })));
    292 
    293   change.Commit();
    294 }
    295 
    296 }  // namespace weave
    297