Fix style on iOS for distilled content.
[chromium-blink-merge.git] / chromeos / network / network_state_unittest.cc
blob3ba359a5f608a1e305b2a9268e28efe3165c4a0c
1 // Copyright (c) 2013 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 "chromeos/network/network_state.h"
7 #include "base/basictypes.h"
8 #include "base/i18n/streaming_utf8_validator.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 namespace chromeos {
17 namespace {
19 // StringValue that skips the DCHECK in the constructor for valid UTF8.
20 class TestStringValue : public base::Value {
21 public:
22 explicit TestStringValue(const std::string& in_value)
23 : base::Value(TYPE_STRING),
24 value_(in_value) {
27 ~TestStringValue() override {}
29 // Overridden from Value:
30 bool GetAsString(std::string* out_value) const override {
31 if (out_value)
32 *out_value = value_;
33 return true;
36 TestStringValue* DeepCopy() const override {
37 return new TestStringValue(value_);
40 bool Equals(const base::Value* other) const override {
41 if (other->GetType() != GetType())
42 return false;
43 std::string lhs, rhs;
44 return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
47 private:
48 std::string value_;
51 class NetworkStateTest : public testing::Test {
52 public:
53 NetworkStateTest() : network_state_("test_path") {
56 protected:
57 bool SetProperty(const std::string& key, scoped_ptr<base::Value> value) {
58 const bool result = network_state_.PropertyChanged(key, *value);
59 properties_.SetWithoutPathExpansion(key, value.release());
60 return result;
63 bool SetStringProperty(const std::string& key, const std::string& value) {
64 return SetProperty(key, make_scoped_ptr(new TestStringValue(value)));
67 bool SignalInitialPropertiesReceived() {
68 return network_state_.InitialPropertiesReceived(properties_);
71 NetworkState network_state_;
73 private:
74 base::DictionaryValue properties_;
76 DISALLOW_COPY_AND_ASSIGN(NetworkStateTest);
79 } // namespace
81 // Setting kNameProperty should set network name after call to
82 // InitialPropertiesReceived().
83 TEST_F(NetworkStateTest, NameAscii) {
84 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
86 std::string network_setname = "Name TEST";
87 EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_setname));
88 EXPECT_FALSE(SignalInitialPropertiesReceived());
89 EXPECT_EQ(network_state_.name(), network_setname);
92 TEST_F(NetworkStateTest, NameAsciiWithNull) {
93 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
95 std::string network_setname = "Name TEST\x00xxx";
96 std::string network_setname_result = "Name TEST";
97 EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_setname));
98 EXPECT_FALSE(SignalInitialPropertiesReceived());
99 EXPECT_EQ(network_state_.name(), network_setname_result);
102 // Truncates invalid UTF-8
103 TEST_F(NetworkStateTest, NameTruncateInvalid) {
104 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
106 std::string network_setname = "SSID TEST \x01\xff!";
107 std::string network_setname_result = "SSID TEST \xEF\xBF\xBD\xEF\xBF\xBD!";
108 EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_setname));
109 EXPECT_TRUE(SignalInitialPropertiesReceived());
110 EXPECT_EQ(network_state_.name(), network_setname_result);
113 // If HexSSID doesn't exist, fallback to NameProperty.
114 TEST_F(NetworkStateTest, SsidFromName) {
115 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
117 std::string wifi_utf8 = "UTF-8 \u3042\u3044\u3046";
118 std::string wifi_utf8_result = "UTF-8 \xE3\x81\x82\xE3\x81\x84\xE3\x81\x86";
119 EXPECT_TRUE(SetStringProperty(shill::kNameProperty, wifi_utf8));
120 EXPECT_FALSE(SignalInitialPropertiesReceived());
121 EXPECT_EQ(network_state_.name(), wifi_utf8_result);
124 // latin1 SSID -> UTF8 SSID (Hex)
125 TEST_F(NetworkStateTest, SsidLatin) {
126 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
128 std::string wifi_latin1 = "latin-1 \xc0\xcb\xcc\xd6\xfb";
129 std::string wifi_latin1_hex =
130 base::HexEncode(wifi_latin1.c_str(), wifi_latin1.length());
131 std::string wifi_latin1_result = "latin-1 \u00c0\u00cb\u00cc\u00d6\u00fb";
132 EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_latin1_hex));
133 EXPECT_TRUE(SignalInitialPropertiesReceived());
134 EXPECT_EQ(network_state_.name(), wifi_latin1_result);
137 // Hex SSID
138 TEST_F(NetworkStateTest, SsidHex) {
139 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
141 std::string wifi_hex_result = "This is HEX SSID!";
142 std::string wifi_hex =
143 base::HexEncode(wifi_hex_result.c_str(), wifi_hex_result.length());
144 EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
145 EXPECT_TRUE(SignalInitialPropertiesReceived());
146 EXPECT_EQ(network_state_.name(), wifi_hex_result);
149 // Non-UTF-8 SSID should be preserved in |raw_ssid_| field.
150 TEST_F(NetworkStateTest, SsidNonUtf8) {
151 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
153 std::string non_utf8_ssid = "\xc0";
154 ASSERT_FALSE(base::StreamingUtf8Validator::Validate(non_utf8_ssid));
156 std::vector<uint8_t> non_utf8_ssid_bytes;
157 non_utf8_ssid_bytes.push_back(static_cast<uint8_t>(non_utf8_ssid.data()[0]));
159 std::string wifi_hex =
160 base::HexEncode(non_utf8_ssid.data(), non_utf8_ssid.size());
161 EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
162 EXPECT_TRUE(SignalInitialPropertiesReceived());
163 EXPECT_EQ(network_state_.raw_ssid(), non_utf8_ssid_bytes);
166 // Multiple updates for Hex SSID should work fine.
167 TEST_F(NetworkStateTest, SsidHexMultipleUpdates) {
168 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
170 std::string wifi_hex_result = "This is HEX SSID!";
171 std::string wifi_hex =
172 base::HexEncode(wifi_hex_result.c_str(), wifi_hex_result.length());
173 EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
174 EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, wifi_hex));
177 TEST_F(NetworkStateTest, CaptivePortalState) {
178 std::string network_name = "test";
179 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeWifi));
180 EXPECT_TRUE(SetStringProperty(shill::kNameProperty, network_name));
181 std::string hex_ssid =
182 base::HexEncode(network_name.c_str(), network_name.length());
183 EXPECT_TRUE(SetStringProperty(shill::kWifiHexSsid, hex_ssid));
185 // State != portal -> is_captive_portal == false
186 EXPECT_TRUE(SetStringProperty(shill::kStateProperty, shill::kStateReady));
187 SignalInitialPropertiesReceived();
188 EXPECT_FALSE(network_state_.is_captive_portal());
190 // State == portal, kPortalDetection* not set -> is_captive_portal = true
191 EXPECT_TRUE(SetStringProperty(shill::kStateProperty, shill::kStatePortal));
192 SignalInitialPropertiesReceived();
193 EXPECT_TRUE(network_state_.is_captive_portal());
195 // Set kPortalDetectionFailed* properties to states that should not trigger
196 // is_captive_portal.
197 SetStringProperty(shill::kPortalDetectionFailedPhaseProperty,
198 shill::kPortalDetectionPhaseUnknown);
199 SetStringProperty(shill::kPortalDetectionFailedStatusProperty,
200 shill::kPortalDetectionStatusTimeout);
201 SignalInitialPropertiesReceived();
202 EXPECT_FALSE(network_state_.is_captive_portal());
204 // Set just the phase property to the expected captive portal state.
205 // is_captive_portal should still be false.
206 SetStringProperty(shill::kPortalDetectionFailedPhaseProperty,
207 shill::kPortalDetectionPhaseContent);
208 SignalInitialPropertiesReceived();
209 EXPECT_FALSE(network_state_.is_captive_portal());
211 // Set the status property to the expected captive portal state property.
212 // is_captive_portal should now be true.
213 SetStringProperty(shill::kPortalDetectionFailedStatusProperty,
214 shill::kPortalDetectionStatusFailure);
215 SignalInitialPropertiesReceived();
216 EXPECT_TRUE(network_state_.is_captive_portal());
219 // Third-party VPN provider.
220 TEST_F(NetworkStateTest, VPNThirdPartyProvider) {
221 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty, shill::kTypeVPN));
222 EXPECT_TRUE(SetStringProperty(shill::kNameProperty, "VPN"));
224 scoped_ptr<base::DictionaryValue> provider(new base::DictionaryValue);
225 provider->SetStringWithoutPathExpansion(shill::kTypeProperty,
226 shill::kProviderThirdPartyVpn);
227 provider->SetStringWithoutPathExpansion(
228 shill::kHostProperty, "third-party-vpn-provider-extension-id");
229 EXPECT_TRUE(SetProperty(shill::kProviderProperty, provider.Pass()));
230 SignalInitialPropertiesReceived();
231 EXPECT_EQ(network_state_.vpn_provider_type(), shill::kProviderThirdPartyVpn);
232 EXPECT_EQ(network_state_.third_party_vpn_provider_extension_id(),
233 "third-party-vpn-provider-extension-id");
236 } // namespace chromeos