network_change_notifier_chromeos_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_change_notifier_chromeos.h"
6
7#include <string>
8
9#include "base/basictypes.h"
10#include "base/strings/string_split.h"
11#include "chromeos/network/network_change_notifier_factory_chromeos.h"
12#include "chromeos/network/network_state.h"
13#include "net/base/network_change_notifier.h"
14#include "testing/gtest/include/gtest/gtest.h"
15#include "third_party/cros_system_api/dbus/service_constants.h"
16
17namespace chromeos {
18
19namespace {
20
21const char kDnsServers1[] = "192.168.0.1,192.168.0.2";
22const char kDnsServers2[] = "192.168.3.1,192.168.3.2";
23const char kIpAddress1[] = "192.168.1.1";
24const char kIpAddress2[] = "192.168.1.2";
25const char kService1[] = "/service/1";
26const char kService2[] = "/service/2";
27const char kService3[] = "/service/3";
28
29struct NotifierState {
30  net::NetworkChangeNotifier::ConnectionType type;
31  const char* service_path;
32  const char* ip_address;
33  const char* dns_servers;
34};
35
36struct DefaultNetworkState {
37  bool is_connected;
38  const char* type;
39  const char* technology;
40  const char* service_path;
41  const char* ip_address;
42  const char* dns_servers;
43};
44
45struct NotifierUpdateTestCase {
46  const char* test_description;
47  NotifierState initial_state;
48  DefaultNetworkState default_network_state;
49  NotifierState expected_state;
50  bool expected_type_changed;
51  bool expected_ip_changed;
52  bool expected_dns_changed;
53};
54
55} // namespace
56
57using net::NetworkChangeNotifier;
58
59TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) {
60  struct TypeMapping {
61    const char* shill_type;
62    const char* technology;
63    NetworkChangeNotifier::ConnectionType connection_type;
64  };
65  TypeMapping type_mappings[] = {
66    { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET },
67    { flimflam::kTypeWifi, "", NetworkChangeNotifier::CONNECTION_WIFI },
68    { flimflam::kTypeWimax, "", NetworkChangeNotifier::CONNECTION_4G },
69    { "unknown type", "unknown technology",
70      NetworkChangeNotifier::CONNECTION_UNKNOWN },
71    { flimflam::kTypeCellular, flimflam::kNetworkTechnology1Xrtt,
72      NetworkChangeNotifier::CONNECTION_2G },
73    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyGprs,
74      NetworkChangeNotifier::CONNECTION_2G },
75    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyEdge,
76      NetworkChangeNotifier::CONNECTION_2G },
77    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyEvdo,
78      NetworkChangeNotifier::CONNECTION_3G },
79    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyGsm,
80      NetworkChangeNotifier::CONNECTION_3G },
81    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyUmts,
82      NetworkChangeNotifier::CONNECTION_3G },
83    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspa,
84      NetworkChangeNotifier::CONNECTION_3G },
85    { flimflam::kTypeCellular,  flimflam::kNetworkTechnologyHspaPlus,
86      NetworkChangeNotifier::CONNECTION_4G },
87    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyLte,
88      NetworkChangeNotifier::CONNECTION_4G },
89    { flimflam::kTypeCellular, flimflam::kNetworkTechnologyLteAdvanced,
90      NetworkChangeNotifier::CONNECTION_4G },
91    { flimflam::kTypeCellular, "unknown technology",
92      NetworkChangeNotifier::CONNECTION_2G }
93  };
94
95  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_mappings); ++i) {
96    NetworkChangeNotifier::ConnectionType type =
97        NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
98            type_mappings[i].shill_type, type_mappings[i].technology);
99    EXPECT_EQ(type_mappings[i].connection_type, type);
100  }
101}
102
103class NetworkChangeNotifierChromeosUpdateTest : public testing::Test {
104 protected:
105  NetworkChangeNotifierChromeosUpdateTest() : default_network_("") {
106  }
107  virtual ~NetworkChangeNotifierChromeosUpdateTest() {}
108
109  void SetNotifierState(const NotifierState& notifier_state) {
110    notifier_.connection_type_ = notifier_state.type;
111    notifier_.service_path_ = notifier_state.service_path;
112    notifier_.ip_address_ = notifier_state.ip_address;
113    std::vector<std::string> dns_servers;
114    base::SplitString(notifier_state.dns_servers, ',', &dns_servers);
115    notifier_.dns_servers_ = dns_servers;
116  }
117
118  void VerifyNotifierState(const NotifierState& notifier_state) {
119    EXPECT_EQ(notifier_state.type, notifier_.connection_type_);
120    EXPECT_EQ(notifier_state.service_path, notifier_.service_path_);
121    EXPECT_EQ(notifier_state.ip_address, notifier_.ip_address_);
122    std::vector<std::string> dns_servers;
123    base::SplitString(notifier_state.dns_servers, ',', &dns_servers);
124    EXPECT_EQ(dns_servers, notifier_.dns_servers_);
125  }
126
127  // Sets the default network state used for notifier updates.
128  void SetDefaultNetworkState(
129      const DefaultNetworkState& default_network_state) {
130    if (default_network_state.is_connected)
131      default_network_.connection_state_ = flimflam::kStateOnline;
132    else
133      default_network_.connection_state_ = flimflam::kStateConfiguration;
134    default_network_.type_ = default_network_state.type;
135    default_network_.technology_ = default_network_state.technology;
136    default_network_.path_ = default_network_state.service_path;
137    default_network_.set_ip_address(default_network_state.ip_address);
138    std::vector<std::string> dns_servers;
139    base::SplitString(default_network_state.dns_servers, ',', &dns_servers);
140    default_network_.set_dns_servers(dns_servers);
141  }
142
143  // Process an default network update based on the state of |default_network_|.
144  void ProcessDefaultNetworkUpdate(bool* type_changed,
145                                   bool* ip_changed,
146                                   bool* dns_changed) {
147    notifier_.UpdateState(&default_network_, type_changed, ip_changed,
148                          dns_changed);
149  }
150
151 private:
152  NetworkState default_network_;
153  NetworkChangeNotifierChromeos notifier_;
154};
155
156NotifierUpdateTestCase test_cases[] = {
157  { "Online -> Offline",
158    { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
159      kDnsServers1 },
160    { false, flimflam::kTypeEthernet, "", kService1, "", "" },
161    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
162    true, true, true
163  },
164  { "Offline -> Offline",
165    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
166    { false, flimflam::kTypeEthernet, "", kService1, kIpAddress1,
167      kDnsServers1 },
168    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
169    false, false, false
170  },
171  { "Offline -> Online",
172    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
173    { true, flimflam::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1 },
174    { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
175      kDnsServers1 },
176    true, true, true
177  },
178  { "Online -> Online (new default service, different connection type)",
179    { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
180      kDnsServers1 },
181    { true, flimflam::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1 },
182    { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1,
183      kDnsServers1 },
184    true, true, true
185  },
186  { "Online -> Online (new default service, same connection type)",
187    { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1,
188      kDnsServers1 },
189    { true, flimflam::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1 },
190    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1,
191      kDnsServers1 },
192    false, true, true
193  },
194  { "Online -> Online (same default service, first IP address update)",
195    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, "", kDnsServers1 },
196    { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 },
197    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
198      kDnsServers1 },
199    false, false, false
200  },
201  { "Online -> Online (same default service, new IP address, same DNS)",
202    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1,
203      kDnsServers1 },
204    { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 },
205    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
206      kDnsServers1 },
207    false, true, false
208  },
209  { "Online -> Online (same default service, same IP address, new DNS)",
210    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
211      kDnsServers1 },
212    { true, flimflam::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2 },
213    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
214      kDnsServers2 },
215    false, false, true
216  }
217};
218
219TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetwork) {
220  for (size_t i = 0; i < arraysize(test_cases); ++i) {
221    SCOPED_TRACE(test_cases[i].test_description);
222    SetNotifierState(test_cases[i].initial_state);
223    SetDefaultNetworkState(test_cases[i].default_network_state);
224    bool type_changed = false, ip_changed = false, dns_changed = false;
225    ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
226    VerifyNotifierState(test_cases[i].expected_state);
227    EXPECT_TRUE(type_changed == test_cases[i].expected_type_changed);
228    EXPECT_TRUE(ip_changed == test_cases[i].expected_ip_changed);
229    EXPECT_TRUE(dns_changed == test_cases[i].expected_dns_changed);
230  }
231}
232
233}  // namespace chromeos
234