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 static void Failure(const std::string
& error_name
,
52 const std::string
& error_message
) {
53 ADD_FAILURE() << error_name
<< ": " << error_message
;
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
)),
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
),
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
, ¶meters
, true),
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
),
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())),
182 kExampleIPConfigPath
, data
,
183 base::Bind(&ShillThirdPartyVpnDriverClientTest::MockSuccess
,
184 base::Unretained(this)),
185 base::Bind(&Failure
));
187 message_loop_
.RunUntilIdle();
190 } // namespace chromeos