blob: 2c1d6b32c13b7489fc61ddc8f776783664eb7b7f [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
Paul Stewartc43cbbe2013-04-11 06:29:30 -070013#include "shill/eap_credentials.h"
Paul Stewart0756db92012-01-27 08:34:47 -080014#include "shill/error.h"
15#include "shill/mock_store.h"
16#include "shill/service.h"
17#include "shill/wifi_service.h"
18
19using std::string;
20using testing::_;
21using testing::DoAll;
22using testing::Return;
23using testing::SetArgumentPointee;
24using testing::StrEq;
25using testing::StrNe;
26using testing::StrictMock;
27
28namespace shill {
29
30class 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
82TEST_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
90TEST_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
98TEST_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
109TEST_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
121TEST_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
148TEST_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
159TEST_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 Stewart4ebbdec2012-12-13 12:15:46 -0800168 const string ca_cert_id("ca-cert-id");
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700169 ExpectStringProperty(EapCredentials::kStorageEapCACertID, ca_cert_id);
Paul Stewart4ebbdec2012-12-13 12:15:46 -0800170 const string cert_id("cert-id");
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700171 ExpectStringProperty(EapCredentials::kStorageEapCertID, cert_id);
Paul Stewart4ebbdec2012-12-13 12:15:46 -0800172 const string key_id("key-id");
Paul Stewartc43cbbe2013-04-11 06:29:30 -0700173 ExpectStringProperty(EapCredentials::kStorageEapKeyID, key_id);
Paul Stewart4ebbdec2012-12-13 12:15:46 -0800174
Paul Stewart0756db92012-01-27 08:34:47 -0800175
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 Stewart4ebbdec2012-12-13 12:15:46 -0800182 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 Stewart0756db92012-01-27 08:34:47 -0800186}
187
188} // namespace shill