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 "chromeos/dbus/shill_client_unittest_base.h"
9 #include "chromeos/dbus/shill_third_party_vpn_driver_client.h"
10 #include "chromeos/dbus/shill_third_party_vpn_observer.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h"
19 const char kExampleIPConfigPath
[] = "/foo/bar";
21 class MockShillThirdPartyVpnObserver
: public ShillThirdPartyVpnObserver
{
23 MockShillThirdPartyVpnObserver() {}
24 ~MockShillThirdPartyVpnObserver() override
{}
25 MOCK_METHOD1(OnPacketReceived
, void(const std::string
& data
));
26 MOCK_METHOD1(OnPlatformMessage
, void(uint32_t message
));
31 class ShillThirdPartyVpnDriverClientTest
: public ShillClientUnittestBase
{
33 ShillThirdPartyVpnDriverClientTest()
34 : ShillClientUnittestBase(shill::kFlimflamThirdPartyVpnInterface
,
35 dbus::ObjectPath(kExampleIPConfigPath
)) {}
37 void SetUp() override
{
38 ShillClientUnittestBase::SetUp();
40 // Create a client with the mock bus.
41 client_
.reset(ShillThirdPartyVpnDriverClient::Create());
42 client_
->Init(mock_bus_
.get());
43 // Run the message loop to run the signal connection result callback.
44 message_loop_
.RunUntilIdle();
47 void TearDown() override
{ ShillClientUnittestBase::TearDown(); }
49 MOCK_METHOD0(MockSuccess
, void());
50 static void Failure(const std::string
& error_name
,
51 const std::string
& error_message
) {
52 ADD_FAILURE() << error_name
<< ": " << error_message
;
56 scoped_ptr
<ShillThirdPartyVpnDriverClient
> client_
;
59 TEST_F(ShillThirdPartyVpnDriverClientTest
, PlatformSignal
) {
60 uint32_t connected_state
= 123456;
61 const int kPacketSize
= 5;
62 std::string
data_packet(1, kPacketSize
);
63 dbus::Signal
pmessage_signal(shill::kFlimflamThirdPartyVpnInterface
,
64 shill::kOnPlatformMessageFunction
);
66 dbus::MessageWriter
writer(&pmessage_signal
);
67 writer
.AppendUint32(connected_state
);
70 dbus::Signal
preceived_signal(shill::kFlimflamThirdPartyVpnInterface
,
71 shill::kOnPacketReceivedFunction
);
73 dbus::MessageWriter
writer(&preceived_signal
);
74 writer
.AppendArrayOfBytes(
75 reinterpret_cast<const unsigned char*>(data_packet
.data()),
79 // Expect each signal to be triggered once.
80 MockShillThirdPartyVpnObserver observer
;
81 EXPECT_CALL(observer
, OnPlatformMessage(connected_state
)).Times(1);
82 EXPECT_CALL(observer
, OnPacketReceived(data_packet
)).Times(1);
84 client_
->AddShillThirdPartyVpnObserver(kExampleIPConfigPath
, &observer
);
86 // Run the signal callback.
87 SendPlatformMessageSignal(&pmessage_signal
);
88 SendPacketReceievedSignal(&preceived_signal
);
90 testing::Mock::VerifyAndClearExpectations(&observer
);
92 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
93 uint32_t connection_state
= 2;
95 PrepareForMethodCall(shill::kUpdateConnectionStateFunction
,
96 base::Bind(&ExpectUint32Argument
, connection_state
),
99 EXPECT_CALL(*this, MockSuccess()).Times(0);
100 client_
->UpdateConnectionState(
101 kExampleIPConfigPath
, connection_state
,
102 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
103 base::Unretained(this)),
104 base::Bind(&Failure
));
106 client_
->RemoveShillThirdPartyVpnObserver(kExampleIPConfigPath
);
107 testing::Mock::VerifyAndClearExpectations(this);
109 EXPECT_CALL(*this, MockSuccess()).Times(1);
111 // Check after removing the observer that there is no further signals.
112 EXPECT_CALL(observer
, OnPlatformMessage(connected_state
)).Times(0);
113 EXPECT_CALL(observer
, OnPacketReceived(data_packet
)).Times(0);
115 // Run the signal callback.
116 SendPlatformMessageSignal(&pmessage_signal
);
117 SendPacketReceievedSignal(&preceived_signal
);
119 testing::Mock::VerifyAndClearExpectations(&observer
);
121 message_loop_
.RunUntilIdle();
124 TEST_F(ShillThirdPartyVpnDriverClientTest
, SetParameters
) {
125 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
127 base::DictionaryValue parameters
;
128 const std::string
kAddress("1.1.1.1");
129 parameters
.SetStringWithoutPathExpansion(
130 shill::kAddressParameterThirdPartyVpn
, kAddress
);
132 EXPECT_CALL(*this, MockSuccess()).Times(1);
134 PrepareForMethodCall(
135 shill::kSetParametersFunction
,
136 base::Bind(&ExpectDictionaryValueArgument
, ¶meters
, true),
139 client_
->SetParameters(
140 kExampleIPConfigPath
, parameters
,
141 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
142 base::Unretained(this)),
143 base::Bind(&Failure
));
145 message_loop_
.RunUntilIdle();
148 TEST_F(ShillThirdPartyVpnDriverClientTest
, UpdateConnectionState
) {
149 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
150 uint32_t connection_state
= 2;
152 EXPECT_CALL(*this, MockSuccess()).Times(1);
154 PrepareForMethodCall(shill::kUpdateConnectionStateFunction
,
155 base::Bind(&ExpectUint32Argument
, connection_state
),
158 client_
->UpdateConnectionState(
159 kExampleIPConfigPath
, connection_state
,
160 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
161 base::Unretained(this)),
162 base::Bind(&Failure
));
164 message_loop_
.RunUntilIdle();
167 TEST_F(ShillThirdPartyVpnDriverClientTest
, SendPacket
) {
168 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
170 const std::string
data(5, 0);
172 EXPECT_CALL(*this, MockSuccess()).Times(1);
174 PrepareForMethodCall(shill::kSendPacketFunction
,
175 base::Bind(&ExpectArrayOfBytesArgument
, data
),
179 kExampleIPConfigPath
, data
,
180 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
181 base::Unretained(this)),
182 base::Bind(&Failure
));
184 message_loop_
.RunUntilIdle();
187 } // namespace chromeos