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/memory/scoped_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
18 // StringValue that skips the DCHECK in the constructor for valid UTF8.
19 class TestStringValue
: public base::Value
{
21 explicit TestStringValue(const std::string
& in_value
)
22 : base::Value(TYPE_STRING
),
26 virtual ~TestStringValue() {
29 // Overridden from Value:
30 virtual bool GetAsString(std::string
* out_value
) const OVERRIDE
{
36 virtual TestStringValue
* DeepCopy() const OVERRIDE
{
37 return new TestStringValue(value_
);
40 virtual bool Equals(const base::Value
* other
) const OVERRIDE
{
41 if (other
->GetType() != GetType())
44 return GetAsString(&lhs
) && other
->GetAsString(&rhs
) && lhs
== rhs
;
51 class NetworkStateTest
: public testing::Test
{
53 NetworkStateTest() : network_state_("test_path") {
57 bool SetStringProperty(const std::string
& key
, const std::string
& value
) {
58 TestStringValue
* string_value
= new TestStringValue(value
);
59 bool res
= network_state_
.PropertyChanged(key
, *string_value
);
60 properties_
.SetWithoutPathExpansion(key
, string_value
);
64 bool SignalInitialPropertiesReceived() {
65 return network_state_
.InitialPropertiesReceived(properties_
);
68 NetworkState network_state_
;
71 base::DictionaryValue properties_
;
73 DISALLOW_COPY_AND_ASSIGN(NetworkStateTest
);
78 // Setting kNameProperty should set network name after call to
79 // InitialPropertiesReceived().
80 TEST_F(NetworkStateTest
, NameAscii
) {
81 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty
, shill::kTypeVPN
));
83 std::string network_setname
= "Name TEST";
84 EXPECT_TRUE(SetStringProperty(shill::kNameProperty
, network_setname
));
85 EXPECT_FALSE(SignalInitialPropertiesReceived());
86 EXPECT_EQ(network_state_
.name(), network_setname
);
89 TEST_F(NetworkStateTest
, NameAsciiWithNull
) {
90 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty
, shill::kTypeVPN
));
92 std::string network_setname
= "Name TEST\x00xxx";
93 std::string network_setname_result
= "Name TEST";
94 EXPECT_TRUE(SetStringProperty(shill::kNameProperty
, network_setname
));
95 EXPECT_FALSE(SignalInitialPropertiesReceived());
96 EXPECT_EQ(network_state_
.name(), network_setname_result
);
99 // Truncates invalid UTF-8
100 TEST_F(NetworkStateTest
, NameTruncateInvalid
) {
101 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty
, shill::kTypeVPN
));
103 std::string network_setname
= "SSID TEST \x01\xff!";
104 std::string network_setname_result
= "SSID TEST \xEF\xBF\xBD\xEF\xBF\xBD!";
105 EXPECT_TRUE(SetStringProperty(shill::kNameProperty
, network_setname
));
106 EXPECT_TRUE(SignalInitialPropertiesReceived());
107 EXPECT_EQ(network_state_
.name(), network_setname_result
);
110 // If HexSSID doesn't exist, fallback to NameProperty.
111 TEST_F(NetworkStateTest
, SsidFromName
) {
112 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty
, shill::kTypeWifi
));
114 std::string wifi_utf8
= "UTF-8 \u3042\u3044\u3046";
115 std::string wifi_utf8_result
= "UTF-8 \xE3\x81\x82\xE3\x81\x84\xE3\x81\x86";
116 EXPECT_TRUE(SetStringProperty(shill::kNameProperty
, wifi_utf8
));
117 EXPECT_FALSE(SignalInitialPropertiesReceived());
118 EXPECT_EQ(network_state_
.name(), wifi_utf8_result
);
121 // latin1 SSID -> UTF8 SSID (Hex)
122 TEST_F(NetworkStateTest
, SsidLatin
) {
123 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty
, shill::kTypeWifi
));
125 std::string wifi_latin1
= "latin-1 \xc0\xcb\xcc\xd6\xfb";
126 std::string wifi_latin1_hex
=
127 base::HexEncode(wifi_latin1
.c_str(), wifi_latin1
.length());
128 std::string wifi_latin1_result
= "latin-1 \u00c0\u00cb\u00cc\u00d6\u00fb";
129 EXPECT_FALSE(SetStringProperty(shill::kWifiHexSsid
, wifi_latin1_hex
));
130 EXPECT_TRUE(SignalInitialPropertiesReceived());
131 EXPECT_EQ(network_state_
.name(), wifi_latin1_result
);
135 TEST_F(NetworkStateTest
, SsidHex
) {
136 EXPECT_TRUE(SetStringProperty(shill::kTypeProperty
, shill::kTypeWifi
));
138 std::string wifi_hex_result
= "This is HEX SSID!";
139 std::string wifi_hex
=
140 base::HexEncode(wifi_hex_result
.c_str(), wifi_hex_result
.length());
141 EXPECT_FALSE(SetStringProperty(shill::kWifiHexSsid
, wifi_hex
));
142 EXPECT_TRUE(SignalInitialPropertiesReceived());
143 EXPECT_EQ(network_state_
.name(), wifi_hex_result
);
146 } // namespace chromeos