blob: a67d56f5ff31eab86deb6b07a5a18ff67fe85e19 [file] [log] [blame]
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001// Copyright (c) 2011 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/service.h"
6
7#include <map>
8#include <string>
9#include <vector>
10
11#include <dbus-c++/dbus.h>
12#include <chromeos/dbus/service_constants.h>
13#include <gtest/gtest.h>
14#include <gmock/gmock.h>
15
16#include "shill/dbus_adaptor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070017#include "shill/entry.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070018#include "shill/ethernet_service.h"
19#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070020#include "shill/mock_adaptors.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070021#include "shill/mock_control.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070022#include "shill/mock_profile.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070023#include "shill/mock_service.h"
24#include "shill/property_store_unittest.h"
25#include "shill/shill_event.h"
26
27using std::map;
28using std::string;
29using std::vector;
30using ::testing::_;
31using ::testing::NiceMock;
32using ::testing::Return;
33using ::testing::Test;
34
35namespace shill {
36
37class ServiceTest : public PropertyStoreTest {
38 public:
Chris Masone95207da2011-06-29 16:50:49 -070039 static const char kMockServiceName[];
40 static const char kMockDeviceRpcId[];
Chris Masone7aa5f902011-07-11 11:13:35 -070041 static const char kEntryName[];
42 static const char kProfileName[];
Chris Masone95207da2011-06-29 16:50:49 -070043
Chris Masoneb925cc82011-06-22 15:39:57 -070044 ServiceTest()
45 : service_(new MockService(&control_interface_,
46 &dispatcher_,
Chris Masone7aa5f902011-07-11 11:13:35 -070047 new MockProfile(&control_interface_, &glib_),
48 new Entry(kEntryName),
Chris Masone95207da2011-06-29 16:50:49 -070049 kMockServiceName)) {
Chris Masoneb925cc82011-06-22 15:39:57 -070050 }
51
Chris Masone3bd3c8c2011-06-13 08:20:26 -070052 virtual ~ServiceTest() {}
Chris Masoneb925cc82011-06-22 15:39:57 -070053
54 protected:
Chris Masone95207da2011-06-29 16:50:49 -070055 scoped_refptr<MockService> service_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070056};
57
Chris Masone95207da2011-06-29 16:50:49 -070058const char ServiceTest::kMockServiceName[] = "mock-service";
59
60const char ServiceTest::kMockDeviceRpcId[] = "mock-device-rpc";
61
Chris Masone7aa5f902011-07-11 11:13:35 -070062const char ServiceTest::kEntryName[] = "entry";
63
64const char ServiceTest::kProfileName[] = "profile";
65
Chris Masonea8a2c252011-06-27 22:16:30 -070066TEST_F(ServiceTest, GetProperties) {
Chris Masone95207da2011-06-29 16:50:49 -070067 EXPECT_CALL(*service_.get(), CalculateState()).WillRepeatedly(Return(""));
68 EXPECT_CALL(*service_.get(), GetDeviceRpcId())
69 .WillRepeatedly(Return(ServiceTest::kMockDeviceRpcId));
Chris Masonea8a2c252011-06-27 22:16:30 -070070 map<string, ::DBus::Variant> props;
71 Error error(Error::kInvalidProperty, "");
72 {
73 ::DBus::Error dbus_error;
74 string expected("portal_list");
Chris Masone27c4aa52011-07-02 13:10:14 -070075 service_->store()->SetStringProperty(flimflam::kCheckPortalProperty,
76 expected,
77 &error);
78 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070079 ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end());
80 EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(),
81 expected);
82 }
83 {
84 ::DBus::Error dbus_error;
85 bool expected = true;
Chris Masone27c4aa52011-07-02 13:10:14 -070086 service_->store()->SetBoolProperty(flimflam::kAutoConnectProperty,
87 expected,
88 &error);
89 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070090 ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end());
91 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
92 expected);
93 }
94 {
95 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -070096 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070097 ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end());
98 EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false);
99 }
100 {
101 ::DBus::Error dbus_error;
102 int32 expected = 127;
Chris Masone27c4aa52011-07-02 13:10:14 -0700103 service_->store()->SetInt32Property(flimflam::kPriorityProperty,
104 expected,
105 &error);
106 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700107 ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end());
108 EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(),
109 expected);
110 }
Chris Masone95207da2011-06-29 16:50:49 -0700111 {
112 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700113 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masone95207da2011-06-29 16:50:49 -0700114 ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end());
115 EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_string(),
116 string(ServiceTest::kMockDeviceRpcId));
117 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700118}
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700119
Chris Masonea8a2c252011-06-27 22:16:30 -0700120TEST_F(ServiceTest, Dispatch) {
121 {
122 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700123 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700124 flimflam::kSaveCredentialsProperty,
125 PropertyStoreTest::kBoolV,
126 &error));
127 }
128 {
129 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700130 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700131 flimflam::kPriorityProperty,
132 PropertyStoreTest::kInt32V,
133 &error));
134 }
135 {
136 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700137 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700138 flimflam::kEAPEAPProperty,
139 PropertyStoreTest::kStringV,
140 &error));
141 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700142 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700143 {
144 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700145 EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700146 flimflam::kFavoriteProperty,
147 PropertyStoreTest::kBoolV,
148 &error));
149 EXPECT_EQ(invalid_args_, error.name());
150 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700151}
152
Chris Masone7aa5f902011-07-11 11:13:35 -0700153TEST_F(ServiceTest, MoveEntry) {
154 // Create a Profile with an Entry in it that should back our Service.
155 EntryRefPtr entry(new Entry(kProfileName));
156 ProfileRefPtr profile(new Profile(&control_interface_, &glib_));
157 profile->entries_[kEntryName] = entry;
158
159 scoped_refptr<MockService> service(new MockService(&control_interface_,
160 &dispatcher_,
161 profile,
162 entry,
163 kMockServiceName));
164 // Now, move the entry to another profile.
165 ProfileRefPtr profile2(new Profile(&control_interface_, &glib_));
166 map<string, EntryRefPtr>::iterator it = profile->entries_.find(kEntryName);
167 ASSERT_TRUE(it != profile->entries_.end());
168
169 profile2->AdoptEntry(it->first, it->second);
170 profile->entries_.erase(it);
171 // Force destruction of the original Profile, to ensure that the Entry
172 // is kept alive and populated with data.
173 profile = NULL;
174 {
175 Error error(Error::kInvalidProperty, "");
176 ::DBus::Error dbus_error;
177 map<string, ::DBus::Variant> props;
178 bool expected = true;
179 service->store()->SetBoolProperty(flimflam::kAutoConnectProperty,
180 expected,
181 &error);
182 DBusAdaptor::GetProperties(service->store(), &props, &dbus_error);
183 ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end());
184 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
185 expected);
186 }
187}
188
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700189} // namespace shill