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