blob: 367107cea6065cdf9092a7995e3207ad3a47c92f [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"
17#include "shill/ethernet_service.h"
18#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070019#include "shill/mock_adaptors.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include "shill/mock_control.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070021#include "shill/mock_service.h"
22#include "shill/property_store_unittest.h"
23#include "shill/shill_event.h"
24
25using std::map;
26using std::string;
27using std::vector;
28using ::testing::_;
29using ::testing::NiceMock;
30using ::testing::Return;
31using ::testing::Test;
Chris Masoneb925cc82011-06-22 15:39:57 -070032using ::testing::Values;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070033
34namespace shill {
35
36class ServiceTest : public PropertyStoreTest {
37 public:
Chris Masone95207da2011-06-29 16:50:49 -070038 static const char kMockServiceName[];
39 static const char kMockDeviceRpcId[];
40
Chris Masoneb925cc82011-06-22 15:39:57 -070041 ServiceTest()
42 : service_(new MockService(&control_interface_,
43 &dispatcher_,
Chris Masone95207da2011-06-29 16:50:49 -070044 kMockServiceName)) {
Chris Masoneb925cc82011-06-22 15:39:57 -070045 }
46
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047 virtual ~ServiceTest() {}
Chris Masoneb925cc82011-06-22 15:39:57 -070048
49 protected:
Chris Masone95207da2011-06-29 16:50:49 -070050 scoped_refptr<MockService> service_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070051};
52
Chris Masone95207da2011-06-29 16:50:49 -070053const char ServiceTest::kMockServiceName[] = "mock-service";
54
55const char ServiceTest::kMockDeviceRpcId[] = "mock-device-rpc";
56
Chris Masonea8a2c252011-06-27 22:16:30 -070057TEST_F(ServiceTest, GetProperties) {
Chris Masone95207da2011-06-29 16:50:49 -070058 EXPECT_CALL(*service_.get(), CalculateState()).WillRepeatedly(Return(""));
59 EXPECT_CALL(*service_.get(), GetDeviceRpcId())
60 .WillRepeatedly(Return(ServiceTest::kMockDeviceRpcId));
Chris Masonea8a2c252011-06-27 22:16:30 -070061 map<string, ::DBus::Variant> props;
62 Error error(Error::kInvalidProperty, "");
63 {
64 ::DBus::Error dbus_error;
65 string expected("portal_list");
Chris Masone27c4aa52011-07-02 13:10:14 -070066 service_->store()->SetStringProperty(flimflam::kCheckPortalProperty,
67 expected,
68 &error);
69 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070070 ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end());
71 EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(),
72 expected);
73 }
74 {
75 ::DBus::Error dbus_error;
76 bool expected = true;
Chris Masone27c4aa52011-07-02 13:10:14 -070077 service_->store()->SetBoolProperty(flimflam::kAutoConnectProperty,
78 expected,
79 &error);
80 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070081 ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end());
82 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
83 expected);
84 }
85 {
86 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -070087 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070088 ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end());
89 EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false);
90 }
91 {
92 ::DBus::Error dbus_error;
93 int32 expected = 127;
Chris Masone27c4aa52011-07-02 13:10:14 -070094 service_->store()->SetInt32Property(flimflam::kPriorityProperty,
95 expected,
96 &error);
97 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masonea8a2c252011-06-27 22:16:30 -070098 ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end());
99 EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(),
100 expected);
101 }
Chris Masone95207da2011-06-29 16:50:49 -0700102 {
103 ::DBus::Error dbus_error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700104 DBusAdaptor::GetProperties(service_->store(), &props, &dbus_error);
Chris Masone95207da2011-06-29 16:50:49 -0700105 ASSERT_FALSE(props.find(flimflam::kDeviceProperty) == props.end());
106 EXPECT_EQ(props[flimflam::kDeviceProperty].reader().get_string(),
107 string(ServiceTest::kMockDeviceRpcId));
108 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700109}
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700110
Chris Masonea8a2c252011-06-27 22:16:30 -0700111TEST_F(ServiceTest, Dispatch) {
112 {
113 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700114 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700115 flimflam::kSaveCredentialsProperty,
116 PropertyStoreTest::kBoolV,
117 &error));
118 }
119 {
120 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700121 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700122 flimflam::kPriorityProperty,
123 PropertyStoreTest::kInt32V,
124 &error));
125 }
126 {
127 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700128 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700129 flimflam::kEAPEAPProperty,
130 PropertyStoreTest::kStringV,
131 &error));
132 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700133 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700134 {
135 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700136 EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700137 flimflam::kFavoriteProperty,
138 PropertyStoreTest::kBoolV,
139 &error));
140 EXPECT_EQ(invalid_args_, error.name());
141 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700142}
143
144TEST_P(ServiceTest, TestProperty) {
145 // Ensure that an attempt to write unknown properties returns InvalidProperty.
146 ::DBus::Error error;
Chris Masone27c4aa52011-07-02 13:10:14 -0700147 EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_->store(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700148 "",
Chris Masoneb925cc82011-06-22 15:39:57 -0700149 GetParam(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700150 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700151 EXPECT_EQ(invalid_prop_, error.name());
152}
153
Chris Masoneb925cc82011-06-22 15:39:57 -0700154INSTANTIATE_TEST_CASE_P(
155 ServiceTestInstance,
156 ServiceTest,
157 Values(PropertyStoreTest::kBoolV,
158 PropertyStoreTest::kByteV,
159 PropertyStoreTest::kStringV,
160 PropertyStoreTest::kInt16V,
161 PropertyStoreTest::kInt32V,
162 PropertyStoreTest::kUint16V,
163 PropertyStoreTest::kUint32V,
164 PropertyStoreTest::kStringsV,
165 PropertyStoreTest::kStringmapV,
166 PropertyStoreTest::kStringmapsV));
167
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700168} // namespace shill