Chromium Code Reviews| Index: chromeos/dbus/shill_third_party_vpn_driver_client_unittest.cc |
| diff --git a/chromeos/dbus/shill_third_party_vpn_driver_client_unittest.cc b/chromeos/dbus/shill_third_party_vpn_driver_client_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6d4e09cb68b871a3c0c9d053c47951321ef7593f |
| --- /dev/null |
| +++ b/chromeos/dbus/shill_third_party_vpn_driver_client_unittest.cc |
| @@ -0,0 +1,149 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "chromeos/dbus/shill_client_unittest_base.h" |
| +#include "chromeos/dbus/shill_third_party_vpn_driver_client.h" |
| +#include "chromeos/dbus/shill_third_party_vpn_observer.h" |
| +#include "third_party/cros_system_api/dbus/service_constants.h" |
| + |
| +using testing::_; |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const char kExampleIPConfigPath[] = "/foo/bar"; |
| + |
| +class MockShillThirdPartyVpnObserver : public ShillThirdPartyVpnObserver { |
| + public: |
| + MockShillThirdPartyVpnObserver() {} |
| + ~MockShillThirdPartyVpnObserver() {} |
| + MOCK_METHOD2(OnPacketReceived, void(const uint8_t* data, size_t length)); |
| + MOCK_METHOD1(OnPlatformMessage, void(uint32_t message)); |
| +}; |
| + |
| +} // namespace |
| + |
| +class ShillThirdPartyVpnDriverClientTest : public ShillClientUnittestBase { |
| + public: |
| + ShillThirdPartyVpnDriverClientTest() |
| + : ShillClientUnittestBase(shill::kFlimflamThirdPartyVpnInterface, |
| + dbus::ObjectPath(kExampleIPConfigPath)) {} |
| + |
| + virtual void SetUp() { |
| + ShillClientUnittestBase::SetUp(); |
| + |
| + // Create a client with the mock bus. |
| + client_.reset(ShillThirdPartyVpnDriverClient::Create()); |
| + client_->Init(mock_bus_.get()); |
| + // Run the message loop to run the signal connection result callback. |
| + message_loop_.RunUntilIdle(); |
| + } |
| + |
| + virtual void TearDown() { ShillClientUnittestBase::TearDown(); } |
| + |
| + static void Success() {} |
| + static void Failure(const std::string& error_name, |
| + const std::string& error_message) { |
| + EXPECT_FALSE(true) << error_name << error_message; |
|
bartfab (slow)
2014/11/13 12:18:47
Instead of EXPECT_FALSE(true), you should use ADD_
kaliamoorthi
2014/11/13 16:10:52
Acknowledged.
|
| + } |
| + |
| + protected: |
| + scoped_ptr<ShillThirdPartyVpnDriverClient> client_; |
| +}; |
| + |
| +TEST_F(ShillThirdPartyVpnDriverClientTest, PlatformSignal) { |
| + uint32_t connected_state = 123456; |
| + const int kPacketSize = 5; |
| + uint8_t data[kPacketSize] = {}; |
| + dbus::Signal pmessage_signal(shill::kFlimflamThirdPartyVpnInterface, |
| + shill::kOnPlatformMessageFunction); |
| + { |
| + dbus::MessageWriter writer(&pmessage_signal); |
| + writer.AppendUint32(connected_state); |
| + } |
| + |
| + dbus::Signal preceived_signal(shill::kFlimflamThirdPartyVpnInterface, |
| + shill::kOnPacketReceivedFunction); |
| + { |
| + dbus::MessageWriter writer(&preceived_signal); |
| + writer.AppendArrayOfBytes(data, kPacketSize); |
| + } |
| + |
| + // Expect each signal to be triggered once. |
| + MockShillThirdPartyVpnObserver observer; |
| + EXPECT_CALL(observer, OnPlatformMessage(connected_state)).Times(1); |
| + EXPECT_CALL(observer, OnPacketReceived(_, kPacketSize)).Times(1); |
| + |
| + client_->AddShillThirdPartyVpnObserver(kExampleIPConfigPath, |
| + &observer); |
| + |
| + // Run the signal callback. |
| + SendPlatformMessageSignal(&pmessage_signal); |
| + SendPacketReceievedSignal(&preceived_signal); |
| + |
| + client_->RemoveShillThirdPartyVpnObserver(kExampleIPConfigPath); |
| + |
| + // Check after removing the observer that there is no further signals. |
| + EXPECT_CALL(observer, OnPlatformMessage(connected_state)).Times(0); |
| + EXPECT_CALL(observer, OnPacketReceived(_, kPacketSize)).Times(0); |
| + |
| + // Run the signal callback. |
| + SendPlatformMessageSignal(&pmessage_signal); |
| + SendPacketReceievedSignal(&preceived_signal); |
| + |
| + message_loop_.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(ShillThirdPartyVpnDriverClientTest, SetParameters) { |
| + scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| + |
| + base::DictionaryValue parameters; |
| + const std::string kAddress("1.1.1.1"); |
| + parameters.SetStringWithoutPathExpansion( |
| + shill::kAddressParameterThirdPartyVpn, kAddress); |
| + |
| + PrepareForMethodCall( |
| + shill::kSetParametersFunction, |
| + base::Bind(&ExpectDictionaryValueArgument, ¶meters, true), |
| + response.get()); |
| + |
| + client_->SetParameters(kExampleIPConfigPath, parameters, |
| + base::Bind(&Success), base::Bind(&Failure)); |
| + |
| + message_loop_.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(ShillThirdPartyVpnDriverClientTest, UpdateConnectionState) { |
| + scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| + uint32_t connection_state = 2; |
| + |
| + PrepareForMethodCall(shill::kUpdateConnectionStateFunction, |
| + base::Bind(&ExpectUint32Argument, connection_state), |
| + response.get()); |
| + |
| + client_->UpdateConnectionState(kExampleIPConfigPath, |
| + connection_state, base::Bind(&Success), |
| + base::Bind(&Failure)); |
| + |
| + message_loop_.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(ShillThirdPartyVpnDriverClientTest, SendPacket) { |
| + scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| + |
| + const std::string data(5, 0); |
| + |
| + PrepareForMethodCall(shill::kSendPacketFunction, |
| + base::Bind(&ExpectArrayOfBytesArgument, data), |
| + response.get()); |
| + |
| + client_->SendPacket(kExampleIPConfigPath, data, |
| + base::Bind(&Success), base::Bind(&Failure)); |
| + |
| + message_loop_.RunUntilIdle(); |
| +} |
| + |
| +} // namespace chromeos |