network_connection_handler_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 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. 4 5#include "chromeos/network/network_connection_handler.h" 6 7#include "base/bind.h" 8#include "base/memory/scoped_ptr.h" 9#include "base/message_loop.h" 10#include "chromeos/dbus/dbus_thread_manager.h" 11#include "chromeos/dbus/shill_manager_client.h" 12#include "chromeos/dbus/shill_service_client.h" 13#include "chromeos/network/network_configuration_handler.h" 14#include "chromeos/network/network_state_handler.h" 15#include "chromeos/network/onc/onc_utils.h" 16#include "testing/gtest/include/gtest/gtest.h" 17#include "third_party/cros_system_api/dbus/service_constants.h" 18 19namespace { 20 21const char* kSuccessResult = "success"; 22 23} // namespace 24 25namespace chromeos { 26 27class NetworkConnectionHandlerTest : public testing::Test { 28 public: 29 NetworkConnectionHandlerTest() { 30 } 31 virtual ~NetworkConnectionHandlerTest() { 32 } 33 34 virtual void SetUp() OVERRIDE { 35 // Initialize DBusThreadManager with a stub implementation. 36 DBusThreadManager::InitializeWithStub(); 37 message_loop_.RunUntilIdle(); 38 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface() 39 ->ClearServices(); 40 message_loop_.RunUntilIdle(); 41 network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); 42 network_configuration_handler_.reset( 43 NetworkConfigurationHandler::InitializeForTest( 44 network_state_handler_.get())); 45 network_connection_handler_.reset(new NetworkConnectionHandler); 46 network_connection_handler_->Init(network_state_handler_.get(), 47 network_configuration_handler_.get()); 48 } 49 50 virtual void TearDown() OVERRIDE { 51 network_connection_handler_.reset(); 52 network_configuration_handler_.reset(); 53 network_state_handler_.reset(); 54 DBusThreadManager::Shutdown(); 55 } 56 57 protected: 58 bool Configure(const std::string& json_string) { 59 scoped_ptr<base::DictionaryValue> json_dict = 60 onc::ReadDictionaryFromJson(json_string); 61 if (!json_dict) { 62 LOG(ERROR) << "Error parsing json: " << json_string; 63 return false; 64 } 65 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( 66 *json_dict, 67 ObjectPathCallback(), ShillManagerClient::ErrorCallback()); 68 message_loop_.RunUntilIdle(); 69 return true; 70 } 71 72 void Connect(const std::string& service_path) { 73 const bool ignore_error_state = false; 74 network_connection_handler_->ConnectToNetwork( 75 service_path, 76 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, 77 base::Unretained(this)), 78 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, 79 base::Unretained(this)), 80 ignore_error_state); 81 message_loop_.RunUntilIdle(); 82 } 83 84 void Disconnect(const std::string& service_path) { 85 network_connection_handler_->DisconnectNetwork( 86 service_path, 87 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, 88 base::Unretained(this)), 89 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, 90 base::Unretained(this))); 91 message_loop_.RunUntilIdle(); 92 } 93 94 void SuccessCallback() { 95 result_ = kSuccessResult; 96 } 97 98 void ErrorCallback(const std::string& error_name, 99 scoped_ptr<base::DictionaryValue> error_data) { 100 result_ = error_name; 101 } 102 103 std::string GetResultAndReset() { 104 std::string result; 105 result.swap(result_); 106 return result; 107 } 108 109 std::string GetServiceStringProperty(const std::string& service_path, 110 const std::string& key) { 111 std::string result; 112 const base::DictionaryValue* properties = 113 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()-> 114 GetServiceProperties(service_path); 115 if (properties) 116 properties->GetStringWithoutPathExpansion(key, &result); 117 return result; 118 } 119 120 scoped_ptr<NetworkStateHandler> network_state_handler_; 121 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_; 122 scoped_ptr<NetworkConnectionHandler> network_connection_handler_; 123 base::MessageLoopForUI message_loop_; 124 std::string result_; 125 126 private: 127 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); 128}; 129 130namespace { 131 132const char* kConfigConnectable = 133 "{ \"GUID\": \"wifi0\", \"Type\": \"wifi\", \"State\": \"idle\" }"; 134const char* kConfigConnected = 135 "{ \"GUID\": \"wifi1\", \"Type\": \"wifi\", \"State\": \"online\" }"; 136const char* kConfigConnecting = 137 "{ \"GUID\": \"wifi2\", \"Type\": \"wifi\", \"State\": \"association\" }"; 138const char* kConfigRequiresPassphrase = 139 "{ \"GUID\": \"wifi3\", \"Type\": \"wifi\", " 140 "\"PassphraseRequired\": true }"; 141const char* kConfigRequiresActivation = 142 "{ \"GUID\": \"cellular1\", \"Type\": \"cellular\"," 143 " \"Cellular.ActivationState\": \"not-activated\" }"; 144 145} // namespace 146 147TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectSuccess) { 148 EXPECT_TRUE(Configure(kConfigConnectable)); 149 Connect("wifi0"); 150 EXPECT_EQ(kSuccessResult, GetResultAndReset()); 151 EXPECT_EQ(flimflam::kStateOnline, 152 GetServiceStringProperty("wifi0", flimflam::kStateProperty)); 153} 154 155// Handles basic failure cases. 156TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectFailure) { 157 Connect("no-network"); 158 EXPECT_EQ(NetworkConnectionHandler::kErrorNotFound, GetResultAndReset()); 159 160 EXPECT_TRUE(Configure(kConfigConnected)); 161 Connect("wifi1"); 162 EXPECT_EQ(NetworkConnectionHandler::kErrorConnected, GetResultAndReset()); 163 164 EXPECT_TRUE(Configure(kConfigConnecting)); 165 Connect("wifi2"); 166 EXPECT_EQ(NetworkConnectionHandler::kErrorConnecting, GetResultAndReset()); 167 168 EXPECT_TRUE(Configure(kConfigRequiresPassphrase)); 169 Connect("wifi3"); 170 EXPECT_EQ(NetworkConnectionHandler::kErrorPassphraseRequired, 171 GetResultAndReset()); 172 173 EXPECT_TRUE(Configure(kConfigRequiresActivation)); 174 Connect("cellular1"); 175 EXPECT_EQ(NetworkConnectionHandler::kErrorActivationRequired, 176 GetResultAndReset()); 177} 178 179namespace { 180 181const char* kConfigRequiresCertificate = 182 "{ \"GUID\": \"wifi4\", \"Type\": \"wifi\", \"Connectable\": false," 183 " \"Security\": \"802_1x\"," 184 " \"UIData\": \"{" 185 " \\\"certificate_type\\\": \\\"pattern\\\"," 186 " \\\"certificate_pattern\\\": {" 187 " \\\"Subject\\\": { \\\"CommonName\\\": \\\"Foo\\\" }" 188 " } }\" }"; 189 190} // namespace 191 192// Handle certificates. TODO(stevenjb): Add certificate stubs to improve 193// test coverage. 194TEST_F(NetworkConnectionHandlerTest, 195 NetworkConnectionHandlerConnectCertificate) { 196 EXPECT_TRUE(Configure(kConfigRequiresCertificate)); 197 Connect("wifi4"); 198 EXPECT_EQ(NetworkConnectionHandler::kErrorCertificateRequired, 199 GetResultAndReset()); 200} 201 202TEST_F(NetworkConnectionHandlerTest, 203 NetworkConnectionHandlerDisconnectSuccess) { 204 EXPECT_TRUE(Configure(kConfigConnected)); 205 Disconnect("wifi1"); 206 EXPECT_EQ(kSuccessResult, GetResultAndReset()); 207} 208 209TEST_F(NetworkConnectionHandlerTest, 210 NetworkConnectionHandlerDisconnectFailure) { 211 Connect("no-network"); 212 EXPECT_EQ(NetworkConnectionHandler::kErrorNotFound, GetResultAndReset()); 213 214 EXPECT_TRUE(Configure(kConfigConnectable)); 215 Disconnect("wifi0"); 216 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset()); 217} 218 219} // namespace chromeos 220