Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 1 | // Copyright 2015 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/privet/privetd_conf_parser.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <base/files/file_util.h> |
| 13 | #include <base/files/scoped_temp_dir.h> |
| 14 | #include <base/logging.h> |
| 15 | #include <chromeos/strings/string_utils.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include "buffet/privet/security_delegate.h" |
| 19 | |
| 20 | using chromeos::string_utils::Join; |
| 21 | using chromeos::KeyValueStore; |
| 22 | using std::map; |
| 23 | using std::string; |
| 24 | |
| 25 | namespace privetd { |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | const char kWiFiBootstrapMode[] = "wifi_bootstrapping_mode"; |
| 30 | const char kGcdBootstrapMode[] = "gcd_bootstrapping_mode"; |
| 31 | const char kConnectTimeout[] = "connect_timeout_seconds"; |
| 32 | const char kBootstrapTimeout[] = "bootstrap_timeout_seconds"; |
| 33 | const char kMonitorTimeout[] = "monitor_timeout_seconds"; |
| 34 | const char kPairingModes[] = "pairing_modes"; |
| 35 | const char kEmbeddedCodePath[] = "embedded_code_path"; |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | class PrivetdConfParserTest : public testing::Test { |
| 40 | public: |
| 41 | using ConfDict = map<string, string>; |
| 42 | |
| 43 | void SetUp() override { |
| 44 | CHECK(temp_dir_.CreateUniqueTempDir()); |
| 45 | temp_file_ = temp_dir_.path().Append("temp.conf"); |
| 46 | } |
| 47 | |
| 48 | bool IsValid(const ConfDict& conf_dict) { |
| 49 | KeyValueStore store; |
| 50 | FillKeyValueStore(conf_dict, &store); |
| 51 | PrivetdConfigParser config; |
| 52 | return config.Parse(store); |
| 53 | } |
| 54 | |
| 55 | void FillKeyValueStore(const ConfDict& conf_dict, KeyValueStore* store) { |
| 56 | std::vector<string> file_pieces; |
| 57 | for (const auto& it : conf_dict) { |
| 58 | file_pieces.push_back(Join("=", it.first, it.second)); |
| 59 | } |
| 60 | string blob{Join("\n", file_pieces)}; |
| 61 | int expected_len = blob.length(); |
Vitaly Buka | 075b3d4 | 2015-06-09 08:34:25 -0700 | [diff] [blame] | 62 | CHECK(expected_len == |
| 63 | base::WriteFile(temp_file_, blob.c_str(), expected_len)); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 64 | CHECK(store->Load(temp_file_)); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | base::FilePath temp_file_; |
| 69 | base::ScopedTempDir temp_dir_; |
| 70 | }; |
| 71 | |
| 72 | TEST_F(PrivetdConfParserTest, ShouldRejectInvalidTimeouts) { |
| 73 | EXPECT_FALSE(IsValid({{kConnectTimeout, "-1"}})); |
| 74 | EXPECT_FALSE(IsValid({{kConnectTimeout, "a"}})); |
| 75 | EXPECT_FALSE(IsValid({{kConnectTimeout, ""}})); |
| 76 | EXPECT_FALSE(IsValid({{kConnectTimeout, "30 430"}})); |
| 77 | } |
| 78 | |
| 79 | TEST_F(PrivetdConfParserTest, ShouldRejectInvalidWiFiBootstrapModes) { |
| 80 | EXPECT_FALSE(IsValid({{kWiFiBootstrapMode, ""}})); |
| 81 | EXPECT_FALSE(IsValid({{kWiFiBootstrapMode, "clown_shoes"}})); |
| 82 | EXPECT_FALSE(IsValid({{kWiFiBootstrapMode, "off is invalid"}})); |
| 83 | EXPECT_FALSE(IsValid({{kWiFiBootstrapMode, "30"}})); |
| 84 | } |
| 85 | |
| 86 | TEST_F(PrivetdConfParserTest, ShouldRejectInvalidGcdBootstrapModes) { |
| 87 | EXPECT_FALSE(IsValid({{kGcdBootstrapMode, ""}})); |
| 88 | EXPECT_FALSE(IsValid({{kGcdBootstrapMode, "clown_shoes"}})); |
| 89 | EXPECT_FALSE(IsValid({{kGcdBootstrapMode, "off is invalid"}})); |
| 90 | EXPECT_FALSE(IsValid({{kGcdBootstrapMode, "30"}})); |
| 91 | } |
| 92 | |
| 93 | TEST_F(PrivetdConfParserTest, ShouldParseSettings) { |
| 94 | const std::set<std::string> kExpectedWiFiInterfaces{"eth1", "clown shoes"}; |
| 95 | const uint32_t kExpectedConnectTimeout{1}; |
| 96 | const uint32_t kExpectedBootstrapTimeout{2}; |
| 97 | const uint32_t kExpectedMonitorTimeout{3}; |
| 98 | const std::set<PairingType> kExpectedPairingModes{PairingType::kEmbeddedCode, |
| 99 | PairingType::kPinCode}; |
| 100 | static const char kExpectedEmbeddedCodePath[]{"123ABC"}; |
| 101 | const ConfDict conf_dict{ |
| 102 | {kWiFiBootstrapMode, "automatic"}, |
| 103 | {kGcdBootstrapMode, "automatic"}, |
| 104 | {kWiFiBootstrapInterfaces, Join(",", kExpectedWiFiInterfaces)}, |
| 105 | {kConnectTimeout, std::to_string(kExpectedConnectTimeout)}, |
| 106 | {kBootstrapTimeout, std::to_string(kExpectedBootstrapTimeout)}, |
| 107 | {kMonitorTimeout, std::to_string(kExpectedMonitorTimeout)}, |
| 108 | {kPairingModes, "pinCode"}, |
| 109 | {kEmbeddedCodePath, kExpectedEmbeddedCodePath}, |
| 110 | }; |
| 111 | KeyValueStore store; |
| 112 | FillKeyValueStore(conf_dict, &store); |
| 113 | PrivetdConfigParser parser; |
| 114 | EXPECT_TRUE(parser.Parse(store)); |
| 115 | EXPECT_EQ(WiFiBootstrapMode::kAutomatic, parser.wifi_bootstrap_mode()); |
| 116 | EXPECT_EQ(GcdBootstrapMode::kAutomatic, parser.gcd_bootstrap_mode()); |
| 117 | EXPECT_EQ(kExpectedWiFiInterfaces, parser.automatic_wifi_interfaces()); |
| 118 | EXPECT_EQ(kExpectedConnectTimeout, parser.connect_timeout_seconds()); |
| 119 | EXPECT_EQ(kExpectedBootstrapTimeout, parser.bootstrap_timeout_seconds()); |
| 120 | EXPECT_EQ(kExpectedMonitorTimeout, parser.monitor_timeout_seconds()); |
| 121 | EXPECT_EQ(kExpectedPairingModes, parser.pairing_modes()); |
| 122 | EXPECT_EQ(kExpectedEmbeddedCodePath, parser.embedded_code_path().value()); |
| 123 | } |
| 124 | |
| 125 | TEST_F(PrivetdConfParserTest, CriticalDefaults) { |
| 126 | PrivetdConfigParser parser; |
| 127 | EXPECT_EQ(WiFiBootstrapMode::kDisabled, parser.wifi_bootstrap_mode()); |
| 128 | EXPECT_EQ(GcdBootstrapMode::kDisabled, parser.gcd_bootstrap_mode()); |
| 129 | EXPECT_GT(parser.connect_timeout_seconds(), 0); |
| 130 | EXPECT_GT(parser.bootstrap_timeout_seconds(), 0); |
| 131 | EXPECT_GT(parser.monitor_timeout_seconds(), 0); |
| 132 | EXPECT_EQ(std::set<PairingType>{PairingType::kPinCode}, |
| 133 | parser.pairing_modes()); |
| 134 | EXPECT_TRUE(parser.embedded_code_path().empty()); |
| 135 | } |
| 136 | |
| 137 | } // namespace privetd |