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. 4 5 #include "chromeos/network/network_state.h" 6 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" 13 14 namespace chromeos { 15 16 namespace { 17 18 // StringValue that skips the DCHECK in the constructor for valid UTF8. 19 class TestStringValue : public base::Value { 20 public: 21 explicit TestStringValue(const std::string& in_value) 22 : base::Value(TYPE_STRING), 23 value_(in_value) { 24 } 25 26 virtual ~TestStringValue() { 27 } 28 29 // Overridden from Value: 30 virtual bool GetAsString(std::string* out_value) const OVERRIDE { 31 if (out_value) 32 *out_value = value_; 33 return true; 34 } 35 36 virtual TestStringValue* DeepCopy() const OVERRIDE { 37 return new TestStringValue(value_); 38 } 39 40 virtual bool Equals(const 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; 45 } 46 47 private: 48 std::string value_; 49 }; 50 51 class NetworkStateTest : public testing::Test { 52 public: 53 NetworkStateTest() : network_state_("test_path") { 54 } 55 56 protected: 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); 61 return res; 62 } 63 64 bool SignalInitialPropertiesReceived() { 65 return network_state_.InitialPropertiesReceived(properties_); 66 } 67 68 NetworkState network_state_; 69 70 private: 71 base::DictionaryValue properties_; 72 73 DISALLOW_COPY_AND_ASSIGN(NetworkStateTest); 74 }; 75 76 } // namespace 77 78 // Seting kNameProperty should set network name after call to 79 // InitialPropertiesReceived() in SetStringProperty(). 80 TEST_F(NetworkStateTest, SsidAscii) { 81 std::string wifi_setname = "SSID TEST"; 82 std::string wifi_setname_result = "SSID TEST"; 83 EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_setname)); 84 EXPECT_EQ(network_state_.name(), wifi_setname_result); 85 } 86 87 TEST_F(NetworkStateTest, SsidAsciiWithNull) { 88 std::string wifi_setname = "SSID TEST\x00xxx"; 89 std::string wifi_setname_result = "SSID TEST"; 90 EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_setname)); 91 EXPECT_EQ(network_state_.name(), wifi_setname_result); 92 } 93 94 // UTF8 SSID 95 TEST_F(NetworkStateTest, SsidUtf8) { 96 std::string wifi_utf8 = "UTF-8 \u3042\u3044\u3046"; 97 std::string wifi_utf8_result = "UTF-8 \xE3\x81\x82\xE3\x81\x84\xE3\x81\x86"; 98 EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_utf8)); 99 EXPECT_EQ(network_state_.name(), wifi_utf8_result); 100 } 101 102 // Truncates invalid UTF-8 103 TEST_F(NetworkStateTest, SsidTruncateInvalid) { 104 std::string wifi_setname2 = "SSID TEST \x01\xff!"; 105 std::string wifi_setname2_result = "SSID TEST \xEF\xBF\xBD\xEF\xBF\xBD!"; 106 EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_setname2)); 107 EXPECT_TRUE(SignalInitialPropertiesReceived()); 108 EXPECT_EQ(network_state_.name(), wifi_setname2_result); 109 } 110 111 // latin1 SSID -> UTF8 SSID (Hex) 112 TEST_F(NetworkStateTest, SsidLatin) { 113 std::string wifi_latin1 = "latin-1 \xc0\xcb\xcc\xd6\xfb"; 114 std::string wifi_latin1_hex = 115 base::HexEncode(wifi_latin1.c_str(), wifi_latin1.length()); 116 std::string wifi_latin1_result = "latin-1 \u00c0\u00cb\u00cc\u00d6\u00fb"; 117 EXPECT_FALSE(SetStringProperty(flimflam::kWifiHexSsid, wifi_latin1_hex)); 118 EXPECT_TRUE(SignalInitialPropertiesReceived()); 119 EXPECT_EQ(network_state_.name(), wifi_latin1_result); 120 } 121 122 // Hex SSID 123 TEST_F(NetworkStateTest, SsidHex) { 124 std::string wifi_hex = "5468697320697320484558205353494421"; 125 std::string wifi_hex_result = "This is HEX SSID!"; 126 EXPECT_FALSE(SetStringProperty(flimflam::kWifiHexSsid, wifi_hex)); 127 EXPECT_TRUE(SignalInitialPropertiesReceived()); 128 EXPECT_EQ(network_state_.name(), wifi_hex_result); 129 } 130 131 } // namespace chromeos 132