1 // Copyright (c) 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.
5 #include "net/base/network_change_notifier.h"
7 #include "net/base/network_interfaces.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 // Note: This test is subject to the host's OS and network connection. This test
13 // is not future-proof. New standards will come about necessitating the need to
14 // alter the ranges of these tests.
15 TEST(NetworkChangeNotifierTest
, NetMaxBandwidthRange
) {
16 NetworkChangeNotifier::ConnectionType connection_type
=
17 NetworkChangeNotifier::CONNECTION_NONE
;
18 double max_bandwidth
= 0.0;
19 NetworkChangeNotifier::GetMaxBandwidthAndConnectionType(&max_bandwidth
,
22 // Always accept infinity as it's the default value if the bandwidth is
24 if (max_bandwidth
== std::numeric_limits
<double>::infinity()) {
25 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE
, connection_type
);
29 switch (connection_type
) {
30 case NetworkChangeNotifier::CONNECTION_UNKNOWN
:
31 EXPECT_EQ(std::numeric_limits
<double>::infinity(), max_bandwidth
);
33 case NetworkChangeNotifier::CONNECTION_ETHERNET
:
34 EXPECT_GE(10.0, max_bandwidth
);
35 EXPECT_LE(10000.0, max_bandwidth
);
37 case NetworkChangeNotifier::CONNECTION_WIFI
:
38 EXPECT_GE(1.0, max_bandwidth
);
39 EXPECT_LE(7000.0, max_bandwidth
);
41 case NetworkChangeNotifier::CONNECTION_2G
:
42 EXPECT_GE(0.01, max_bandwidth
);
43 EXPECT_LE(0.384, max_bandwidth
);
45 case NetworkChangeNotifier::CONNECTION_3G
:
46 EXPECT_GE(2.0, max_bandwidth
);
47 EXPECT_LE(42.0, max_bandwidth
);
49 case NetworkChangeNotifier::CONNECTION_4G
:
50 EXPECT_GE(100.0, max_bandwidth
);
51 EXPECT_LE(100.0, max_bandwidth
);
53 case NetworkChangeNotifier::CONNECTION_NONE
:
54 EXPECT_EQ(0.0, max_bandwidth
);
56 case NetworkChangeNotifier::CONNECTION_BLUETOOTH
:
57 EXPECT_GE(1.0, max_bandwidth
);
58 EXPECT_LE(24.0, max_bandwidth
);
63 TEST(NetworkChangeNotifierTest
, ConnectionTypeFromInterfaceList
) {
64 NetworkInterfaceList list
;
67 EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list
),
68 NetworkChangeNotifier::CONNECTION_NONE
);
70 for (int i
= NetworkChangeNotifier::CONNECTION_UNKNOWN
;
71 i
<= NetworkChangeNotifier::CONNECTION_LAST
; i
++) {
72 // Check individual types.
73 NetworkInterface interface
;
74 interface
.type
= static_cast<NetworkChangeNotifier::ConnectionType
>(i
);
76 list
.push_back(interface
);
77 EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list
), i
);
79 for (int j
= NetworkChangeNotifier::CONNECTION_UNKNOWN
;
80 j
<= NetworkChangeNotifier::CONNECTION_LAST
; j
++) {
82 interface
.type
= static_cast<NetworkChangeNotifier::ConnectionType
>(i
);
83 list
.push_back(interface
);
84 interface
.type
= static_cast<NetworkChangeNotifier::ConnectionType
>(j
);
85 list
.push_back(interface
);
86 EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list
),
87 i
== j
? i
: NetworkChangeNotifier::CONNECTION_UNKNOWN
);
92 TEST(NetworkChangeNotifierTest
, IgnoreTeredoOnWindows
) {
93 NetworkInterfaceList list
;
94 NetworkInterface interface_teredo
;
95 interface_teredo
.type
= NetworkChangeNotifier::CONNECTION_ETHERNET
;
96 interface_teredo
.friendly_name
= "Teredo Tunneling Pseudo-Interface";
97 list
.push_back(interface_teredo
);
100 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
101 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list
));
103 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_ETHERNET
,
104 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list
));
108 TEST(NetworkChangeNotifierTest
, IgnoreVMInterfaces
) {
109 NetworkInterfaceList list
;
110 NetworkInterface interface_vmnet_linux
;
111 interface_vmnet_linux
.type
= NetworkChangeNotifier::CONNECTION_ETHERNET
;
112 interface_vmnet_linux
.name
= "vmnet1";
113 interface_vmnet_linux
.friendly_name
= "vmnet1";
114 list
.push_back(interface_vmnet_linux
);
116 NetworkInterface interface_vmnet_win
;
117 interface_vmnet_win
.type
= NetworkChangeNotifier::CONNECTION_ETHERNET
;
118 interface_vmnet_win
.name
= "virtualdevice";
119 interface_vmnet_win
.friendly_name
= "VMware Network Adapter VMnet1";
120 list
.push_back(interface_vmnet_win
);
122 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE
,
123 NetworkChangeNotifier::ConnectionTypeFromInterfaceList(list
));