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.
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"
20 const char kExampleIPConfigPath
[] = "/foo/bar";
22 class MockShillThirdPartyVpnObserver
: public ShillThirdPartyVpnObserver
{
24 MockShillThirdPartyVpnObserver() {}
25 ~MockShillThirdPartyVpnObserver() override
{}
26 MOCK_METHOD1(OnPacketReceived
, void(const std::vector
<char>& data
));
27 MOCK_METHOD1(OnPlatformMessage
, void(uint32_t message
));
32 class ShillThirdPartyVpnDriverClientTest
: public ShillClientUnittestBase
{
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
;
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
)),
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
),
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
, ¶meters
, true),
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
),
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())),
185 kExampleIPConfigPath
, data
,
186 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
187 base::Unretained(this)),
188 base::Bind(&Failure
));
190 message_loop_
.RunUntilIdle();
193 } // namespace chromeos