blob: 5a8014cf13632ac0e750cbf7bc8ba45465867485 [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"
19#include "shill/mock_control.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include "shill/mock_service.h"
21#include "shill/property_store_unittest.h"
22#include "shill/shill_event.h"
23
24using std::map;
25using std::string;
26using std::vector;
27using ::testing::_;
28using ::testing::NiceMock;
29using ::testing::Return;
30using ::testing::Test;
Chris Masoneb925cc82011-06-22 15:39:57 -070031using ::testing::Values;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070032
33namespace shill {
34
35class ServiceTest : public PropertyStoreTest {
36 public:
Chris Masoneb925cc82011-06-22 15:39:57 -070037 ServiceTest()
38 : service_(new MockService(&control_interface_,
39 &dispatcher_,
Chris Masoneb925cc82011-06-22 15:39:57 -070040 "mock-service")) {
41 }
42
Chris Masone3bd3c8c2011-06-13 08:20:26 -070043 virtual ~ServiceTest() {}
Chris Masoneb925cc82011-06-22 15:39:57 -070044
45 protected:
46 ServiceRefPtr service_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070047};
48
Chris Masonea8a2c252011-06-27 22:16:30 -070049TEST_F(ServiceTest, GetProperties) {
50 map<string, ::DBus::Variant> props;
51 Error error(Error::kInvalidProperty, "");
52 {
53 ::DBus::Error dbus_error;
54 string expected("portal_list");
55 service_->SetStringProperty(flimflam::kCheckPortalProperty,
56 expected,
57 &error);
58 DBusAdaptor::GetProperties(service_.get(), &props, &dbus_error);
59 ASSERT_FALSE(props.find(flimflam::kCheckPortalProperty) == props.end());
60 EXPECT_EQ(props[flimflam::kCheckPortalProperty].reader().get_string(),
61 expected);
62 }
63 {
64 ::DBus::Error dbus_error;
65 bool expected = true;
66 service_->SetBoolProperty(flimflam::kAutoConnectProperty, expected, &error);
67 DBusAdaptor::GetProperties(service_.get(), &props, &dbus_error);
68 ASSERT_FALSE(props.find(flimflam::kAutoConnectProperty) == props.end());
69 EXPECT_EQ(props[flimflam::kAutoConnectProperty].reader().get_bool(),
70 expected);
71 }
72 {
73 ::DBus::Error dbus_error;
74 DBusAdaptor::GetProperties(service_.get(), &props, &dbus_error);
75 ASSERT_FALSE(props.find(flimflam::kConnectableProperty) == props.end());
76 EXPECT_EQ(props[flimflam::kConnectableProperty].reader().get_bool(), false);
77 }
78 {
79 ::DBus::Error dbus_error;
80 int32 expected = 127;
81 service_->SetInt32Property(flimflam::kPriorityProperty, expected, &error);
82 DBusAdaptor::GetProperties(service_.get(), &props, &dbus_error);
83 ASSERT_FALSE(props.find(flimflam::kPriorityProperty) == props.end());
84 EXPECT_EQ(props[flimflam::kPriorityProperty].reader().get_int32(),
85 expected);
86 }
87}
Chris Masone3bd3c8c2011-06-13 08:20:26 -070088
Chris Masonea8a2c252011-06-27 22:16:30 -070089TEST_F(ServiceTest, Dispatch) {
90 {
91 ::DBus::Error error;
92 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(),
93 flimflam::kSaveCredentialsProperty,
94 PropertyStoreTest::kBoolV,
95 &error));
96 }
97 {
98 ::DBus::Error error;
99 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(),
100 flimflam::kPriorityProperty,
101 PropertyStoreTest::kInt32V,
102 &error));
103 }
104 {
105 ::DBus::Error error;
106 EXPECT_TRUE(DBusAdaptor::DispatchOnType(service_.get(),
107 flimflam::kEAPEAPProperty,
108 PropertyStoreTest::kStringV,
109 &error));
110 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700111 // Ensure that an attempt to write a R/O property returns InvalidArgs error.
Chris Masonea8a2c252011-06-27 22:16:30 -0700112 {
113 ::DBus::Error error;
114 EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_.get(),
115 flimflam::kFavoriteProperty,
116 PropertyStoreTest::kBoolV,
117 &error));
118 EXPECT_EQ(invalid_args_, error.name());
119 }
Chris Masoneb925cc82011-06-22 15:39:57 -0700120}
121
122TEST_P(ServiceTest, TestProperty) {
123 // Ensure that an attempt to write unknown properties returns InvalidProperty.
124 ::DBus::Error error;
125 EXPECT_FALSE(DBusAdaptor::DispatchOnType(service_.get(),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700126 "",
Chris Masoneb925cc82011-06-22 15:39:57 -0700127 GetParam(),
Chris Masonea8a2c252011-06-27 22:16:30 -0700128 &error));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700129 EXPECT_EQ(invalid_prop_, error.name());
130}
131
Chris Masoneb925cc82011-06-22 15:39:57 -0700132INSTANTIATE_TEST_CASE_P(
133 ServiceTestInstance,
134 ServiceTest,
135 Values(PropertyStoreTest::kBoolV,
136 PropertyStoreTest::kByteV,
137 PropertyStoreTest::kStringV,
138 PropertyStoreTest::kInt16V,
139 PropertyStoreTest::kInt32V,
140 PropertyStoreTest::kUint16V,
141 PropertyStoreTest::kUint32V,
142 PropertyStoreTest::kStringsV,
143 PropertyStoreTest::kStringmapV,
144 PropertyStoreTest::kStringmapsV));
145
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700146} // namespace shill