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.
6 #include "chromeos/dbus/shill_client_unittest_base.h"
7 #include "chromeos/dbus/shill_third_party_vpn_driver_client.h"
8 #include "chromeos/dbus/shill_third_party_vpn_observer.h"
9 #include "third_party/cros_system_api/dbus/service_constants.h"
17 const char kExampleIPConfigPath
[] = "/foo/bar";
19 class MockShillThirdPartyVpnObserver
: public ShillThirdPartyVpnObserver
{
21 MockShillThirdPartyVpnObserver() {}
22 ~MockShillThirdPartyVpnObserver() override
{}
23 MOCK_METHOD2(OnPacketReceived
, void(const uint8_t* data
, size_t length
));
24 MOCK_METHOD1(OnPlatformMessage
, void(uint32_t message
));
29 class ShillThirdPartyVpnDriverClientTest
: public ShillClientUnittestBase
{
31 ShillThirdPartyVpnDriverClientTest()
32 : ShillClientUnittestBase(shill::kFlimflamThirdPartyVpnInterface
,
33 dbus::ObjectPath(kExampleIPConfigPath
)) {}
35 void SetUp() override
{
36 ShillClientUnittestBase::SetUp();
38 // Create a client with the mock bus.
39 client_
.reset(ShillThirdPartyVpnDriverClient::Create());
40 client_
->Init(mock_bus_
.get());
41 // Run the message loop to run the signal connection result callback.
42 message_loop_
.RunUntilIdle();
45 void TearDown() override
{ ShillClientUnittestBase::TearDown(); }
47 MOCK_METHOD0(MockSuccess
, void());
48 static void Failure(const std::string
& error_name
,
49 const std::string
& error_message
) {
50 ADD_FAILURE() << error_name
<< ": " << error_message
;
54 scoped_ptr
<ShillThirdPartyVpnDriverClient
> client_
;
57 TEST_F(ShillThirdPartyVpnDriverClientTest
, PlatformSignal
) {
58 uint32_t connected_state
= 123456;
59 const int kPacketSize
= 5;
60 uint8_t data
[kPacketSize
] = {};
61 dbus::Signal
pmessage_signal(shill::kFlimflamThirdPartyVpnInterface
,
62 shill::kOnPlatformMessageFunction
);
64 dbus::MessageWriter
writer(&pmessage_signal
);
65 writer
.AppendUint32(connected_state
);
68 dbus::Signal
preceived_signal(shill::kFlimflamThirdPartyVpnInterface
,
69 shill::kOnPacketReceivedFunction
);
71 dbus::MessageWriter
writer(&preceived_signal
);
72 writer
.AppendArrayOfBytes(data
, kPacketSize
);
75 // Expect each signal to be triggered once.
76 MockShillThirdPartyVpnObserver observer
;
77 EXPECT_CALL(observer
, OnPlatformMessage(connected_state
)).Times(1);
78 EXPECT_CALL(observer
, OnPacketReceived(_
, kPacketSize
)).Times(1);
80 client_
->AddShillThirdPartyVpnObserver(kExampleIPConfigPath
, &observer
);
82 // Run the signal callback.
83 SendPlatformMessageSignal(&pmessage_signal
);
84 SendPacketReceievedSignal(&preceived_signal
);
86 testing::Mock::VerifyAndClearExpectations(&observer
);
88 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
89 uint32_t connection_state
= 2;
91 PrepareForMethodCall(shill::kUpdateConnectionStateFunction
,
92 base::Bind(&ExpectUint32Argument
, connection_state
),
95 EXPECT_CALL(*this, MockSuccess()).Times(0);
96 client_
->UpdateConnectionState(
97 kExampleIPConfigPath
, connection_state
,
98 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
99 base::Unretained(this)),
100 base::Bind(&Failure
));
102 client_
->RemoveShillThirdPartyVpnObserver(kExampleIPConfigPath
);
103 testing::Mock::VerifyAndClearExpectations(this);
105 EXPECT_CALL(*this, MockSuccess()).Times(1);
107 // Check after removing the observer that there is no further signals.
108 EXPECT_CALL(observer
, OnPlatformMessage(connected_state
)).Times(0);
109 EXPECT_CALL(observer
, OnPacketReceived(_
, kPacketSize
)).Times(0);
111 // Run the signal callback.
112 SendPlatformMessageSignal(&pmessage_signal
);
113 SendPacketReceievedSignal(&preceived_signal
);
115 testing::Mock::VerifyAndClearExpectations(&observer
);
117 message_loop_
.RunUntilIdle();
120 TEST_F(ShillThirdPartyVpnDriverClientTest
, SetParameters
) {
121 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
123 base::DictionaryValue parameters
;
124 const std::string
kAddress("1.1.1.1");
125 parameters
.SetStringWithoutPathExpansion(
126 shill::kAddressParameterThirdPartyVpn
, kAddress
);
128 EXPECT_CALL(*this, MockSuccess()).Times(1);
130 PrepareForMethodCall(
131 shill::kSetParametersFunction
,
132 base::Bind(&ExpectDictionaryValueArgument
, ¶meters
, true),
135 client_
->SetParameters(
136 kExampleIPConfigPath
, parameters
,
137 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
138 base::Unretained(this)),
139 base::Bind(&Failure
));
141 message_loop_
.RunUntilIdle();
144 TEST_F(ShillThirdPartyVpnDriverClientTest
, UpdateConnectionState
) {
145 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
146 uint32_t connection_state
= 2;
148 EXPECT_CALL(*this, MockSuccess()).Times(1);
150 PrepareForMethodCall(shill::kUpdateConnectionStateFunction
,
151 base::Bind(&ExpectUint32Argument
, connection_state
),
154 client_
->UpdateConnectionState(
155 kExampleIPConfigPath
, connection_state
,
156 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
157 base::Unretained(this)),
158 base::Bind(&Failure
));
160 message_loop_
.RunUntilIdle();
163 TEST_F(ShillThirdPartyVpnDriverClientTest
, SendPacket
) {
164 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
166 const std::string
data(5, 0);
168 EXPECT_CALL(*this, MockSuccess()).Times(1);
170 PrepareForMethodCall(shill::kSendPacketFunction
,
171 base::Bind(&ExpectArrayOfBytesArgument
, data
),
175 kExampleIPConfigPath
, data
,
176 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
177 base::Unretained(this)),
178 base::Bind(&Failure
));
180 message_loop_
.RunUntilIdle();
183 } // namespace chromeos