Roll src/third_party/WebKit 10b2b4a:a6818f9 (svn 202548:202549)
[chromium-blink-merge.git] / chromeos / dbus / shill_third_party_vpn_driver_client_unittest.cc
blob3c0770231d4243595ff4374c0dd0a3d6fea42658
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 MOCK_METHOD1(MockSuccessWithWarning, void(const std::string& warning));
52 static void Failure(const std::string& error_name,
53 const std::string& error_message) {
54 ADD_FAILURE() << error_name << ": " << error_message;
57 protected:
58 scoped_ptr<ShillThirdPartyVpnDriverClient> client_;
61 TEST_F(ShillThirdPartyVpnDriverClientTest, PlatformSignal) {
62 uint32_t connected_state = 123456;
63 const size_t kPacketSize = 5;
64 std::vector<char> data_packet(kPacketSize, 1);
65 dbus::Signal pmessage_signal(shill::kFlimflamThirdPartyVpnInterface,
66 shill::kOnPlatformMessageFunction);
68 dbus::MessageWriter writer(&pmessage_signal);
69 writer.AppendUint32(connected_state);
72 dbus::Signal preceived_signal(shill::kFlimflamThirdPartyVpnInterface,
73 shill::kOnPacketReceivedFunction);
75 dbus::MessageWriter writer(&preceived_signal);
76 writer.AppendArrayOfBytes(
77 reinterpret_cast<const uint8_t*>(vector_as_array(&data_packet)),
78 data_packet.size());
81 // Expect each signal to be triggered once.
82 MockShillThirdPartyVpnObserver observer;
83 EXPECT_CALL(observer, OnPlatformMessage(connected_state)).Times(1);
84 EXPECT_CALL(observer, OnPacketReceived(data_packet)).Times(1);
86 client_->AddShillThirdPartyVpnObserver(kExampleIPConfigPath, &observer);
88 // Run the signal callback.
89 SendPlatformMessageSignal(&pmessage_signal);
90 SendPacketReceievedSignal(&preceived_signal);
92 testing::Mock::VerifyAndClearExpectations(&observer);
94 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
95 uint32_t connection_state = 2;
97 PrepareForMethodCall(shill::kUpdateConnectionStateFunction,
98 base::Bind(&ExpectUint32Argument, connection_state),
99 response.get());
101 EXPECT_CALL(*this, MockSuccess()).Times(0);
102 client_->UpdateConnectionState(
103 kExampleIPConfigPath, connection_state,
104 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
105 base::Unretained(this)),
106 base::Bind(&Failure));
108 client_->RemoveShillThirdPartyVpnObserver(kExampleIPConfigPath);
109 testing::Mock::VerifyAndClearExpectations(this);
111 EXPECT_CALL(*this, MockSuccess()).Times(1);
113 // Check after removing the observer that there is no further signals.
114 EXPECT_CALL(observer, OnPlatformMessage(connected_state)).Times(0);
115 EXPECT_CALL(observer, OnPacketReceived(data_packet)).Times(0);
117 // Run the signal callback.
118 SendPlatformMessageSignal(&pmessage_signal);
119 SendPacketReceievedSignal(&preceived_signal);
121 testing::Mock::VerifyAndClearExpectations(&observer);
123 message_loop_.RunUntilIdle();
126 TEST_F(ShillThirdPartyVpnDriverClientTest, SetParameters) {
127 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
128 dbus::MessageWriter writer(response.get());
129 writer.AppendString(std::string("deadbeef"));
131 base::DictionaryValue parameters;
132 const std::string kAddress("1.1.1.1");
133 parameters.SetStringWithoutPathExpansion(
134 shill::kAddressParameterThirdPartyVpn, kAddress);
136 EXPECT_CALL(*this, MockSuccessWithWarning(std::string("deadbeef"))).Times(1);
138 PrepareForMethodCall(
139 shill::kSetParametersFunction,
140 base::Bind(&ExpectDictionaryValueArgument, &parameters, true),
141 response.get());
143 client_->SetParameters(
144 kExampleIPConfigPath, parameters,
145 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccessWithWarning,
146 base::Unretained(this)),
147 base::Bind(&Failure));
149 message_loop_.RunUntilIdle();
152 TEST_F(ShillThirdPartyVpnDriverClientTest, UpdateConnectionState) {
153 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
154 uint32_t connection_state = 2;
156 EXPECT_CALL(*this, MockSuccess()).Times(1);
158 PrepareForMethodCall(shill::kUpdateConnectionStateFunction,
159 base::Bind(&ExpectUint32Argument, connection_state),
160 response.get());
162 client_->UpdateConnectionState(
163 kExampleIPConfigPath, connection_state,
164 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
165 base::Unretained(this)),
166 base::Bind(&Failure));
168 message_loop_.RunUntilIdle();
171 TEST_F(ShillThirdPartyVpnDriverClientTest, SendPacket) {
172 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
174 const size_t kPacketSize = 5;
175 const std::vector<char> data(kPacketSize, 0);
177 EXPECT_CALL(*this, MockSuccess()).Times(1);
179 PrepareForMethodCall(shill::kSendPacketFunction,
180 base::Bind(&ExpectArrayOfBytesArgument,
181 std::string(data.begin(), data.end())),
182 response.get());
184 client_->SendPacket(
185 kExampleIPConfigPath, data,
186 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess,
187 base::Unretained(this)),
188 base::Bind(&Failure));
190 message_loop_.RunUntilIdle();
193 } // namespace chromeos