summaryrefslogtreecommitdiffstats
path: root/chromeos/network/network_change_notifier_chromeos_unittest.cc
blob: 04d549fc2ec17e99446597c2fb81b81c6916b220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) 2012 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 "chromeos/network/network_change_notifier_chromeos.h"

#include <string>

#include "base/basictypes.h"
#include "base/strings/string_split.h"
#include "chromeos/network/network_change_notifier_factory_chromeos.h"
#include "chromeos/network/network_state.h"
#include "net/base/network_change_notifier.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

namespace {

const char kDnsServers1[] = "192.168.0.1,192.168.0.2";
const char kDnsServers2[] = "192.168.3.1,192.168.3.2";
const char kIpAddress1[] = "192.168.1.1";
const char kIpAddress2[] = "192.168.1.2";
const char kService1[] = "/service/1";
const char kService2[] = "/service/2";
const char kService3[] = "/service/3";

struct NotifierState {
  net::NetworkChangeNotifier::ConnectionType type;
  const char* service_path;
  const char* ip_address;
  const char* dns_servers;
};

struct DefaultNetworkState {
  bool is_connected;
  const char* type;
  const char* network_technology;
  const char* service_path;
  const char* ip_address;
  const char* dns_servers;
};

struct NotifierUpdateTestCase {
  const char* test_description;
  NotifierState initial_state;
  DefaultNetworkState default_network_state;
  NotifierState expected_state;
  bool expected_type_changed;
  bool expected_ip_changed;
  bool expected_dns_changed;
};

} // namespace

using net::NetworkChangeNotifier;

TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) {
  struct TypeMapping {
    const char* shill_type;
    const char* technology;
    NetworkChangeNotifier::ConnectionType connection_type;
  };
  TypeMapping type_mappings[] = {
    { shill::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET },
    { shill::kTypeWifi, "", NetworkChangeNotifier::CONNECTION_WIFI },
    { shill::kTypeWimax, "", NetworkChangeNotifier::CONNECTION_4G },
    { "unknown type", "unknown technology",
      NetworkChangeNotifier::CONNECTION_UNKNOWN },
    { shill::kTypeCellular, shill::kNetworkTechnology1Xrtt,
      NetworkChangeNotifier::CONNECTION_2G },
    { shill::kTypeCellular, shill::kNetworkTechnologyGprs,
      NetworkChangeNotifier::CONNECTION_2G },
    { shill::kTypeCellular, shill::kNetworkTechnologyEdge,
      NetworkChangeNotifier::CONNECTION_2G },
    { shill::kTypeCellular, shill::kNetworkTechnologyEvdo,
      NetworkChangeNotifier::CONNECTION_3G },
    { shill::kTypeCellular, shill::kNetworkTechnologyGsm,
      NetworkChangeNotifier::CONNECTION_3G },
    { shill::kTypeCellular, shill::kNetworkTechnologyUmts,
      NetworkChangeNotifier::CONNECTION_3G },
    { shill::kTypeCellular, shill::kNetworkTechnologyHspa,
      NetworkChangeNotifier::CONNECTION_3G },
    { shill::kTypeCellular, shill::kNetworkTechnologyHspaPlus,
      NetworkChangeNotifier::CONNECTION_4G },
    { shill::kTypeCellular, shill::kNetworkTechnologyLte,
      NetworkChangeNotifier::CONNECTION_4G },
    { shill::kTypeCellular, shill::kNetworkTechnologyLteAdvanced,
      NetworkChangeNotifier::CONNECTION_4G },
    { shill::kTypeCellular, "unknown technology",
      NetworkChangeNotifier::CONNECTION_2G }
  };

  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_mappings); ++i) {
    NetworkChangeNotifier::ConnectionType type =
        NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
            type_mappings[i].shill_type, type_mappings[i].technology);
    EXPECT_EQ(type_mappings[i].connection_type, type);
  }
}

class NetworkChangeNotifierChromeosUpdateTest : public testing::Test {
 protected:
  NetworkChangeNotifierChromeosUpdateTest() : default_network_("") {
  }
  virtual ~NetworkChangeNotifierChromeosUpdateTest() {}

  void SetNotifierState(const NotifierState& notifier_state) {
    notifier_.connection_type_ = notifier_state.type;
    notifier_.service_path_ = notifier_state.service_path;
    notifier_.ip_address_ = notifier_state.ip_address;
    std::vector<std::string> dns_servers;
    base::SplitString(notifier_state.dns_servers, ',', &dns_servers);
    notifier_.dns_servers_ = dns_servers;
  }

  void VerifyNotifierState(const NotifierState& notifier_state) {
    EXPECT_EQ(notifier_state.type, notifier_.connection_type_);
    EXPECT_EQ(notifier_state.service_path, notifier_.service_path_);
    EXPECT_EQ(notifier_state.ip_address, notifier_.ip_address_);
    std::vector<std::string> dns_servers;
    base::SplitString(notifier_state.dns_servers, ',', &dns_servers);
    EXPECT_EQ(dns_servers, notifier_.dns_servers_);
  }

  // Sets the default network state used for notifier updates.
  void SetDefaultNetworkState(
      const DefaultNetworkState& default_network_state) {
    default_network_.visible_ = true;
    if (default_network_state.is_connected)
      default_network_.connection_state_ = shill::kStateOnline;
    else
      default_network_.connection_state_ = shill::kStateConfiguration;
    default_network_.type_ = default_network_state.type;
    default_network_.network_technology_ =
        default_network_state.network_technology;
    default_network_.path_ = default_network_state.service_path;
    default_network_.ip_address_ = default_network_state.ip_address;
    std::vector<std::string> dns_servers;
    base::SplitString(default_network_state.dns_servers, ',', &dns_servers);
    default_network_.dns_servers_ = dns_servers;
  }

  // Process an default network update based on the state of |default_network_|.
  void ProcessDefaultNetworkUpdate(bool* type_changed,
                                   bool* ip_changed,
                                   bool* dns_changed) {
    notifier_.UpdateState(&default_network_, type_changed, ip_changed,
                          dns_changed);
  }

 private:
  NetworkState default_network_;
  NetworkChangeNotifierChromeos notifier_;
};

NotifierUpdateTestCase test_cases[] = {
  { "Online -> Offline",
    { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
      kDnsServers1 },
    { false, shill::kTypeEthernet, "", kService1, "", "" },
    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
    true, true, true
  },
  { "Offline -> Offline",
    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
    { false, shill::kTypeEthernet, "", kService1, kIpAddress1,
      kDnsServers1 },
    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
    false, false, false
  },
  { "Offline -> Online",
    { NetworkChangeNotifier::CONNECTION_NONE, "", "", "" },
    { true, shill::kTypeEthernet, "", kService1, kIpAddress1, kDnsServers1 },
    { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
      kDnsServers1 },
    true, true, true
  },
  { "Online -> Online (new default service, different connection type)",
    { NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, kIpAddress1,
      kDnsServers1 },
    { true, shill::kTypeWifi, "", kService2, kIpAddress1, kDnsServers1 },
    { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1,
      kDnsServers1 },
    true, true, true
  },
  { "Online -> Online (new default service, same connection type)",
    { NetworkChangeNotifier::CONNECTION_WIFI, kService2, kIpAddress1,
      kDnsServers1 },
    { true, shill::kTypeWifi, "", kService3, kIpAddress1, kDnsServers1 },
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1,
      kDnsServers1 },
    false, true, true
  },
  { "Online -> Online (same default service, first IP address update)",
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, "", kDnsServers1 },
    { true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 },
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
      kDnsServers1 },
    false, false, false
  },
  { "Online -> Online (same default service, new IP address, same DNS)",
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress1,
      kDnsServers1 },
    { true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers1 },
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
      kDnsServers1 },
    false, true, false
  },
  { "Online -> Online (same default service, same IP address, new DNS)",
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
      kDnsServers1 },
    { true, shill::kTypeWifi, "", kService3, kIpAddress2, kDnsServers2 },
    { NetworkChangeNotifier::CONNECTION_WIFI, kService3, kIpAddress2,
      kDnsServers2 },
    false, false, true
  }
};

TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetwork) {
  for (size_t i = 0; i < arraysize(test_cases); ++i) {
    SCOPED_TRACE(test_cases[i].test_description);
    SetNotifierState(test_cases[i].initial_state);
    SetDefaultNetworkState(test_cases[i].default_network_state);
    bool type_changed = false, ip_changed = false, dns_changed = false;
    ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
    VerifyNotifierState(test_cases[i].expected_state);
    EXPECT_TRUE(type_changed == test_cases[i].expected_type_changed);
    EXPECT_TRUE(ip_changed == test_cases[i].expected_ip_changed);
    EXPECT_TRUE(dns_changed == test_cases[i].expected_dns_changed);
  }
}

}  // namespace chromeos