Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / chromeos / dbus / shill_third_party_vpn_driver_client_unittest.cc
blob6c3f47697e240e39704e765b136f84950ad20d2d
1 // Copyright 2014 The Chromium 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.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "chromeos/dbus/shill_client_unittest_base.h"
10 #include "chromeos/dbus/shill_third_party_vpn_driver_client.h"
11 #include "chromeos/dbus/shill_third_party_vpn_observer.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
14 using testing::_;
16 namespace chromeos {
18 namespace {
20 const char kExampleIPConfigPath[] = "/foo/bar";
22 class MockShillThirdPartyVpnObserver : public ShillThirdPartyVpnObserver {
23 public:
24 MockShillThirdPartyVpnObserver() {}
25 ~MockShillThirdPartyVpnObserver() override {}
26 MOCK_METHOD1(OnPacketReceived, void(const std::vector<char>& data));
27 MOCK_METHOD1(OnPlatformMessage, void(uint32_t message));
30 } // namespace
32 class ShillThirdPartyVpnDriverClientTest : public ShillClientUnittestBase {
33 public:
34 ShillThirdPartyVpnDriverClientTest()
35 : ShillClientUnittestBase(shill::kFlimflamThirdPartyVpnInterface,
36 dbus::ObjectPath(kExampleIPConfigPath)) {}
38 void SetUp() override {
39 ShillClientUnittestBase::SetUp();
41 // Create a client with the mock bus.
42 client_.reset(ShillThirdPartyVpnDriverClient::Create());
43 client_->Init(mock_bus_.get());
44 // Run the message loop to run the signal connection result callback.
45 message_loop_.RunUntilIdle();
48 void TearDown() override { ShillClientUnittestBase::TearDown(); }
50 MOCK_METHOD0(MockSuccess, void());
51 static void Failure(const std::string& error_name,
52 const std::string& error_message) {
53 ADD_FAILURE() << error_name << ": " << error_message;
56 protected:
57 scoped_ptr<ShillThirdPartyVpnDriverClient> client_;
60 TEST_F(ShillThirdPartyVpnDriverClientTest, PlatformSignal) {
61 uint32_t connected_state = 123456;
62 const size_t kPacketSize = 5;
63 std::vector<char> data_packet(kPacketSize, 1);
64 dbus::Signal pmessage_signal(shill::kFlimflamThirdPartyVpnInterface,
65 shill::kOnPlatformMessageFunction);
67 dbus::MessageWriter writer(&pmessage_signal);
68 writer.AppendUint32(connected_state);
71 dbus::Signal preceived_signal(shill::kFlimflamThirdPartyVpnInterface,
72 shill::kOnPacketReceivedFunction);
74 dbus::MessageWriter writer(&preceived_signal);
75 writer.AppendArrayOfBytes(
76 reinterpret_cast<const uint8_t*>(vector_as_array(&data_packet)),
77 data_packet.size());
80 // Expect each signal to be triggered once.
81 MockShillThirdPartyVpnObserver observer;
82 EXPECT_CALL(observer, OnPlatformMessage(connected_state)).Times(1);
83 EXPECT_CALL(observer, OnPacketReceived(data_packet)).Times(1);
85 client_->AddShillThirdPartyVpnObserver(kExampleIPConfigPath, &observer);
87 // Run the signal callback.
88 SendPlatformMessageSignal(&pmessage_signal);
89 SendPacketReceievedSignal(&preceived_signal);
91 testing::Mock::VerifyAndClearExpectations(&observer);
93 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
94 uint32_t connection_state = 2;
96 PrepareForMethodCall(shill::kUpdateConnectionStateFunction,
97 base::Bind(&ExpectUint32Argument, connection_state),
98 response.get());
100 EXPECT_CALL(*this, MockSuccess()).Times(0);
101 client_->UpdateConnectionState(
102 kExampleIPConfigPath, connection_state,
103 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
104 base::Unretained(this)),
105 base::Bind(&Failure));
107 client_->RemoveShillThirdPartyVpnObserver(kExampleIPConfigPath);
108 testing::Mock::VerifyAndClearExpectations(this);
110 EXPECT_CALL(*this, MockSuccess()).Times(1);
112 // Check after removing the observer that there is no further signals.
113 EXPECT_CALL(observer, OnPlatformMessage(connected_state)).Times(0);
114 EXPECT_CALL(observer, OnPacketReceived(data_packet)).Times(0);
116 // Run the signal callback.
117 SendPlatformMessageSignal(&pmessage_signal);
118 SendPacketReceievedSignal(&preceived_signal);
120 testing::Mock::VerifyAndClearExpectations(&observer);
122 message_loop_.RunUntilIdle();
125 TEST_F(ShillThirdPartyVpnDriverClientTest, SetParameters) {
126 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
128 base::DictionaryValue parameters;
129 const std::string kAddress("1.1.1.1");
130 parameters.SetStringWithoutPathExpansion(
131 shill::kAddressParameterThirdPartyVpn, kAddress);
133 EXPECT_CALL(*this, MockSuccess()).Times(1);
135 PrepareForMethodCall(
136 shill::kSetParametersFunction,
137 base::Bind(&ExpectDictionaryValueArgument, &parameters, true),
138 response.get());
140 client_->SetParameters(
141 kExampleIPConfigPath, parameters,
142 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
143 base::Unretained(this)),
144 base::Bind(&Failure));
146 message_loop_.RunUntilIdle();
149 TEST_F(ShillThirdPartyVpnDriverClientTest, UpdateConnectionState) {
150 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
151 uint32_t connection_state = 2;
153 EXPECT_CALL(*this, MockSuccess()).Times(1);
155 PrepareForMethodCall(shill::kUpdateConnectionStateFunction,
156 base::Bind(&ExpectUint32Argument, connection_state),
157 response.get());
159 client_->UpdateConnectionState(
160 kExampleIPConfigPath, connection_state,
161 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
162 base::Unretained(this)),
163 base::Bind(&Failure));
165 message_loop_.RunUntilIdle();
168 TEST_F(ShillThirdPartyVpnDriverClientTest, SendPacket) {
169 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
171 const size_t kPacketSize = 5;
172 const std::vector<char> data(kPacketSize, 0);
174 EXPECT_CALL(*this, MockSuccess()).Times(1);
176 PrepareForMethodCall(shill::kSendPacketFunction,
177 base::Bind(&ExpectArrayOfBytesArgument,
178 std::string(data.begin(), data.end())),
179 response.get());
181 client_->SendPacket(
182 kExampleIPConfigPath, data,
183 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
184 base::Unretained(this)),
185 base::Bind(&Failure));
187 message_loop_.RunUntilIdle();
190 } // namespace chromeos