Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "shill/profile_dbus_property_exporter.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/memory/scoped_ptr.h> |
| 10 | #include <chromeos/dbus/service_constants.h> |
| 11 | #include <gtest/gtest.h> |
| 12 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 13 | #include "shill/eap_credentials.h" |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 14 | #include "shill/error.h" |
| 15 | #include "shill/mock_store.h" |
| 16 | #include "shill/service.h" |
| 17 | #include "shill/wifi_service.h" |
| 18 | |
| 19 | using std::string; |
| 20 | using testing::_; |
| 21 | using testing::DoAll; |
| 22 | using testing::Return; |
| 23 | using testing::SetArgumentPointee; |
| 24 | using testing::StrEq; |
| 25 | using testing::StrNe; |
| 26 | using testing::StrictMock; |
| 27 | |
| 28 | namespace shill { |
| 29 | |
| 30 | class ProfileDBusPropertyExporterTest : public testing::Test { |
| 31 | public: |
| 32 | ProfileDBusPropertyExporterTest() {} |
| 33 | |
| 34 | virtual void SetUp() { |
| 35 | SetUpWithEntryName("entry_name"); |
| 36 | } |
| 37 | |
| 38 | void SetUpWithEntryName(const string &entry_name) { |
| 39 | entry_name_ = entry_name; |
| 40 | storage_.reset(new StrictMock<MockStore>()); |
| 41 | EXPECT_CALL(*storage_.get(), ContainsGroup(StrEq(entry_name_))) |
| 42 | .WillRepeatedly(Return(true)); |
| 43 | EXPECT_CALL(*storage_.get(), ContainsGroup(StrNe(entry_name_))) |
| 44 | .WillRepeatedly(Return(false)); |
| 45 | exporter_.reset(new ProfileDBusPropertyExporter(storage_.get(), |
| 46 | entry_name_)); |
| 47 | } |
| 48 | |
| 49 | void ExpectBoolProperty(const string &key, bool value) { |
| 50 | EXPECT_CALL(*storage_.get(), GetBool(StrEq(entry_name_), StrEq(key), _)) |
| 51 | .WillOnce(DoAll(SetArgumentPointee<2>(value), Return(true))); |
| 52 | } |
| 53 | |
| 54 | void ExpectStringProperty(const string &key, const string &value) { |
| 55 | EXPECT_CALL(*storage_.get(), GetString(StrEq(entry_name_), StrEq(key), _)) |
| 56 | .WillOnce(DoAll(SetArgumentPointee<2>(value), Return(true))); |
| 57 | } |
| 58 | |
| 59 | void ExpectUnknownProperties() { |
| 60 | EXPECT_CALL(*storage_.get(), GetBool(StrEq(entry_name_), _, _)) |
| 61 | .WillRepeatedly(Return(false)); |
| 62 | EXPECT_CALL(*storage_.get(), GetString(StrEq(entry_name_), _, _)) |
| 63 | .WillRepeatedly(Return(false)); |
| 64 | } |
| 65 | |
| 66 | bool GetBoolProperty( |
| 67 | ProfileDBusPropertyExporter::PropertyList *props, const string key) { |
| 68 | return (*props)[key].reader().get_bool(); |
| 69 | } |
| 70 | |
| 71 | string GetStringProperty( |
| 72 | ProfileDBusPropertyExporter::PropertyList *props, const string key) { |
| 73 | return (*props)[key].reader().get_string(); |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | string entry_name_; |
| 78 | scoped_ptr<MockStore> storage_; |
| 79 | scoped_ptr<ProfileDBusPropertyExporter> exporter_; |
| 80 | }; |
| 81 | |
| 82 | TEST_F(ProfileDBusPropertyExporterTest, LoadWrongGroup) { |
| 83 | ProfileDBusPropertyExporter exporter(storage_.get(), "not_entry_name"); |
| 84 | ProfileDBusPropertyExporter::PropertyList props; |
| 85 | Error e; |
| 86 | EXPECT_FALSE(exporter.LoadServiceProperties(&props, &e)); |
| 87 | EXPECT_EQ(Error::kNotFound, e.type()); |
| 88 | } |
| 89 | |
| 90 | TEST_F(ProfileDBusPropertyExporterTest, InvalidTechnology) { |
| 91 | SetUpWithEntryName("unknown_technology"); |
| 92 | ProfileDBusPropertyExporter::PropertyList props; |
| 93 | Error e; |
| 94 | EXPECT_FALSE(exporter_->LoadServiceProperties(&props, &e)); |
| 95 | EXPECT_EQ(Error::kInternalError, e.type()); |
| 96 | } |
| 97 | |
| 98 | TEST_F(ProfileDBusPropertyExporterTest, MinimalProperties) { |
| 99 | SetUpWithEntryName("ethernet_service"); |
| 100 | ExpectUnknownProperties(); |
| 101 | ProfileDBusPropertyExporter::PropertyList props; |
| 102 | Error e; |
| 103 | EXPECT_TRUE(exporter_->LoadServiceProperties(&props, &e)); |
| 104 | EXPECT_EQ(1, props.size()); |
| 105 | EXPECT_EQ(flimflam::kTypeEthernet, |
| 106 | GetStringProperty(&props, flimflam::kTypeProperty)); |
| 107 | } |
| 108 | |
| 109 | TEST_F(ProfileDBusPropertyExporterTest, OverrideTypeProperty) { |
| 110 | SetUpWithEntryName("ethernet_service"); |
| 111 | const string service_type = "special_type"; |
| 112 | ExpectUnknownProperties(); |
| 113 | ExpectStringProperty(Service::kStorageType, service_type); |
| 114 | ProfileDBusPropertyExporter::PropertyList props; |
| 115 | Error e; |
| 116 | EXPECT_TRUE(exporter_->LoadServiceProperties(&props, &e)); |
| 117 | EXPECT_EQ(1, props.size()); |
| 118 | EXPECT_EQ(service_type, GetStringProperty(&props, flimflam::kTypeProperty)); |
| 119 | } |
| 120 | |
| 121 | TEST_F(ProfileDBusPropertyExporterTest, AllServiceProperties) { |
| 122 | SetUpWithEntryName("ethernet_service"); |
| 123 | const bool auto_connect(true); |
| 124 | ExpectBoolProperty(Service::kStorageAutoConnect, auto_connect); |
| 125 | const string error("no carrier"); |
| 126 | ExpectStringProperty(Service::kStorageError, error); |
| 127 | const string guid("guid"); |
| 128 | ExpectStringProperty(Service::kStorageGUID, guid); |
| 129 | const string name("fastnet"); |
| 130 | ExpectStringProperty(Service::kStorageName, name); |
| 131 | const string type("100baset"); |
| 132 | ExpectStringProperty(Service::kStorageType, type); |
| 133 | const string uidata("ui data"); |
| 134 | ExpectStringProperty(Service::kStorageUIData, uidata); |
| 135 | |
| 136 | ProfileDBusPropertyExporter::PropertyList props; |
| 137 | Error e; |
| 138 | EXPECT_TRUE(exporter_->LoadServiceProperties(&props, &e)); |
| 139 | EXPECT_EQ(auto_connect, GetBoolProperty(&props, |
| 140 | flimflam::kAutoConnectProperty)); |
| 141 | EXPECT_EQ(error, GetStringProperty(&props, flimflam::kErrorProperty)); |
| 142 | EXPECT_EQ(guid, GetStringProperty(&props, flimflam::kGuidProperty)); |
| 143 | EXPECT_EQ(name, GetStringProperty(&props, flimflam::kNameProperty)); |
| 144 | EXPECT_EQ(type, GetStringProperty(&props, flimflam::kTypeProperty)); |
| 145 | EXPECT_EQ(uidata, GetStringProperty(&props, flimflam::kUIDataProperty)); |
| 146 | } |
| 147 | |
| 148 | TEST_F(ProfileDBusPropertyExporterTest, MinimalWiFiServiceProperties) { |
| 149 | SetUpWithEntryName("wifi_address_ssid_superfly_unbreakable_crypto"); |
| 150 | ExpectUnknownProperties(); |
| 151 | ProfileDBusPropertyExporter::PropertyList props; |
| 152 | Error e; |
| 153 | EXPECT_TRUE(exporter_->LoadServiceProperties(&props, &e)); |
| 154 | EXPECT_EQ("superfly", GetStringProperty(&props, flimflam::kModeProperty)); |
| 155 | EXPECT_EQ("unbreakable_crypto", |
| 156 | GetStringProperty(&props, flimflam::kSecurityProperty)); |
| 157 | } |
| 158 | |
| 159 | TEST_F(ProfileDBusPropertyExporterTest, AllWiFiServiceProperties) { |
| 160 | SetUpWithEntryName("wifi_service"); |
| 161 | ExpectUnknownProperties(); |
| 162 | const bool hidden_ssid(true); |
| 163 | ExpectBoolProperty(WiFiService::kStorageHiddenSSID, hidden_ssid); |
| 164 | const string mode("superfly"); |
| 165 | ExpectStringProperty(WiFiService::kStorageMode, mode); |
| 166 | const string security("unbreakablecrypto"); |
| 167 | ExpectStringProperty(WiFiService::kStorageSecurity, security); |
Paul Stewart | 4ebbdec | 2012-12-13 12:15:46 -0800 | [diff] [blame] | 168 | const string ca_cert_id("ca-cert-id"); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 169 | ExpectStringProperty(EapCredentials::kStorageEapCACertID, ca_cert_id); |
Paul Stewart | 4ebbdec | 2012-12-13 12:15:46 -0800 | [diff] [blame] | 170 | const string cert_id("cert-id"); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 171 | ExpectStringProperty(EapCredentials::kStorageEapCertID, cert_id); |
Paul Stewart | 4ebbdec | 2012-12-13 12:15:46 -0800 | [diff] [blame] | 172 | const string key_id("key-id"); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 173 | ExpectStringProperty(EapCredentials::kStorageEapKeyID, key_id); |
Paul Stewart | 4ebbdec | 2012-12-13 12:15:46 -0800 | [diff] [blame] | 174 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 175 | |
| 176 | ProfileDBusPropertyExporter::PropertyList props; |
| 177 | Error e; |
| 178 | EXPECT_TRUE(exporter_->LoadServiceProperties(&props, &e)); |
| 179 | EXPECT_EQ(hidden_ssid, GetBoolProperty(&props, flimflam::kWifiHiddenSsid)); |
| 180 | EXPECT_EQ(mode, GetStringProperty(&props, flimflam::kModeProperty)); |
| 181 | EXPECT_EQ(security, GetStringProperty(&props, flimflam::kSecurityProperty)); |
Paul Stewart | 4ebbdec | 2012-12-13 12:15:46 -0800 | [diff] [blame] | 182 | EXPECT_EQ(ca_cert_id, |
| 183 | GetStringProperty(&props, flimflam::kEapCaCertIDProperty)); |
| 184 | EXPECT_EQ(cert_id, GetStringProperty(&props, flimflam::kEAPCertIDProperty)); |
| 185 | EXPECT_EQ(key_id, GetStringProperty(&props, flimflam::kEAPKeyIDProperty)); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace shill |