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
236
237
238
239
|
// Copyright (c) 2013 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_state.h"
#include <stdint.h>
#include <utility>
#include "base/i18n/streaming_utf8_validator.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace {
// StringValue that skips the DCHECK in the constructor for valid UTF8.
class TestStringValue : public base::Value {
public:
explicit TestStringValue(const std::string& in_value)
: base::Value(TYPE_STRING),
value_(in_value) {
}
~TestStringValue() override {}
// Overridden from Value:
bool GetAsString(std::string* out_value) const override {
if (out_value)
*out_value = value_;
return true;
}
TestStringValue* DeepCopy() const override {
return new TestStringValue(value_);
}
bool Equals(const base::Value* other) const override {
if (other->GetType() != GetType())
return false;
std::string lhs, rhs;
return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
}
private:
std::string value_;
};
class NetworkStateTest : public testing::Test {
public:
NetworkStateTest() : network_state_("test_path") {
}
protected:
bool SetProperty(const std::string& key, scoped_ptr<base::Value> value) {
const bool result = network_state_.PropertyChanged(key, *value);
properties_.SetWithoutPathExpansion(key, value.release());
return result;
}
bool SetStringProperty(const std::string& key, const std::string& value) {
return SetProperty(key, make_scoped_ptr(new TestStringValue(value)));
}
bool SignalInitialPropertiesReceived() {
return network_state_.InitialPropertiesReceived(properties_);
}
NetworkState network_state_;
private:
base::DictionaryValue properties_;
DISALLOW_COPY_AND_ASSIGN(NetworkStateTest);
};
} // namespace
// Setting kNameProperty should set network name after call to
// InitialPropertiesReceived().
TEST_F(NetworkStateTest, NameAscii) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
std::string network_setname = "Name TEST";
EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_setname));
EXPECT_FALSE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.name(), network_setname);
}
TEST_F(NetworkStateTest, NameAsciiWithNull) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
std::string network_setname = "Name TEST\x00xxx";
std::string network_setname_result = "Name TEST";
EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_setname));
EXPECT_FALSE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.name(), network_setname_result);
}
// Truncates invalid UTF-8
TEST_F(NetworkStateTest, NameTruncateInvalid) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
std::string network_setname = "SSID TEST \x01\xff!";
std::string network_setname_result = "SSID TEST \xEF\xBF\xBD\xEF\xBF\xBD!";
EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_setname));
EXPECT_TRUE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.name(), network_setname_result);
}
// If HexSSID doesn't exist, fallback to NameProperty.
TEST_F(NetworkStateTest, SsidFromName) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
std::string wifi_utf8 = "UTF-8 \u3042\u3044\u3046";
std::string wifi_utf8_result = "UTF-8 \xE3\x81\x82\xE3\x81\x84\xE3\x81\x86";
EXPECT_TRUE(SetStringProperty(shill::kNameProperty, wifi_utf8));
EXPECT_FALSE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.name(), wifi_utf8_result);
}
// latin1 SSID -> UTF8 SSID (Hex)
TEST_F(NetworkStateTest, SsidLatin) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
std::string wifi_latin1 = "latin-1 \xc0\xcb\xcc\xd6\xfb";
std::string wifi_latin1_hex =
base::HexEncode(wifi_latin1.c_str(), wifi_latin1.length());
std::string wifi_latin1_result = "latin-1 \u00c0\u00cb\u00cc\u00d6\u00fb";
EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_latin1_hex));
EXPECT_TRUE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.name(), wifi_latin1_result);
}
// Hex SSID
TEST_F(NetworkStateTest, SsidHex) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
std::string wifi_hex_result = "This is HEX SSID!";
std::string wifi_hex =
base::HexEncode(wifi_hex_result.c_str(), wifi_hex_result.length());
EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
EXPECT_TRUE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.name(), wifi_hex_result);
}
// Non-UTF-8 SSID should be preserved in |raw_ssid_| field.
TEST_F(NetworkStateTest, SsidNonUtf8) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
std::string non_utf8_ssid = "\xc0";
ASSERT_FALSE(base::StreamingUtf8Validator::Validate(non_utf8_ssid));
std::vector<uint8_t> non_utf8_ssid_bytes;
non_utf8_ssid_bytes.push_back(static_cast<uint8_t>(non_utf8_ssid.data()[0]));
std::string wifi_hex =
base::HexEncode(non_utf8_ssid.data(), non_utf8_ssid.size());
EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
EXPECT_TRUE(SignalInitialPropertiesReceived());
EXPECT_EQ(network_state_.raw_ssid(), non_utf8_ssid_bytes);
}
// Multiple updates for Hex SSID should work fine.
TEST_F(NetworkStateTest, SsidHexMultipleUpdates) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
std::string wifi_hex_result = "This is HEX SSID!";
std::string wifi_hex =
base::HexEncode(wifi_hex_result.c_str(), wifi_hex_result.length());
EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
}
TEST_F(NetworkStateTest, CaptivePortalState) {
std::string network_name = "test";
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_name));
std::string hex_ssid =
base::HexEncode(network_name.c_str(), network_name.length());
EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, hex_ssid));
// State != portal -> is_captive_portal == false
EXPECT_TRUE(SetStringProperty(shill::kStateProperty, shill::kStateReady));
SignalInitialPropertiesReceived();
EXPECT_FALSE(network_state_.is_captive_portal());
// State == portal, kPortalDetection* not set -> is_captive_portal = true
EXPECT_TRUE(SetStringProperty(shill::kStateProperty, shill::kStatePortal));
SignalInitialPropertiesReceived();
EXPECT_TRUE(network_state_.is_captive_portal());
// Set kPortalDetectionFailed* properties to states that should not trigger
// is_captive_portal.
SetStringProperty(shill::kPortalDetectionFailedPhaseProperty,
shill::kPortalDetectionPhaseUnknown);
SetStringProperty(shill::kPortalDetectionFailedStatusProperty,
shill::kPortalDetectionStatusTimeout);
SignalInitialPropertiesReceived();
EXPECT_FALSE(network_state_.is_captive_portal());
// Set just the phase property to the expected captive portal state.
// is_captive_portal should still be false.
SetStringProperty(shill::kPortalDetectionFailedPhaseProperty,
shill::kPortalDetectionPhaseContent);
SignalInitialPropertiesReceived();
EXPECT_FALSE(network_state_.is_captive_portal());
// Set the status property to the expected captive portal state property.
// is_captive_portal should now be true.
SetStringProperty(shill::kPortalDetectionFailedStatusProperty,
shill::kPortalDetectionStatusFailure);
SignalInitialPropertiesReceived();
EXPECT_TRUE(network_state_.is_captive_portal());
}
// Third-party VPN provider.
TEST_F(NetworkStateTest, VPNThirdPartyProvider) {
EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
EXPECT_TRUE(SetStringProperty(shill::kNameProperty, "VPN"));
scoped_ptr<base::DictionaryValue> provider(new base::DictionaryValue);
provider->SetStringWithoutPathExpansion(shill::kTypeProperty,
shill::kProviderThirdPartyVpn);
provider->SetStringWithoutPathExpansion(
shill::kHostProperty, "third-party-vpn-provider-extension-id");
EXPECT_TRUE(SetProperty(shill::kProviderProperty, std::move(provider)));
SignalInitialPropertiesReceived();
EXPECT_EQ(network_state_.vpn_provider_type(), shill::kProviderThirdPartyVpn);
EXPECT_EQ(network_state_.third_party_vpn_provider_extension_id(),
"third-party-vpn-provider-extension-id");
}
} // namespace chromeos
|