network_state_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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
14namespace chromeos {
15
16namespace {
17
18class TestStringValue : public base::Value {
19 public:
20  TestStringValue(const std::string& in_value)
21      : base::Value(TYPE_STRING),
22        value_(in_value) {
23  }
24
25  ~TestStringValue() {
26  }
27
28  // Overridden from Value:
29  virtual bool GetAsString(std::string* out_value) const OVERRIDE {
30    if (out_value)
31      *out_value = value_;
32    return true;
33  }
34
35  virtual TestStringValue* DeepCopy() const OVERRIDE {
36    return new TestStringValue(value_);
37  }
38
39  virtual bool Equals(const Value* other) const OVERRIDE {
40    if (other->GetType() != GetType())
41      return false;
42    std::string lhs, rhs;
43    return GetAsString(&lhs) && other->GetAsString(&rhs) && lhs == rhs;
44  }
45
46 private:
47  std::string value_;
48};
49
50class NetworkStateTest : public testing::Test {
51 public:
52  NetworkStateTest() : network_state_("test_path") {
53  }
54
55 protected:
56  bool SetStringProperty(const std::string& key, const std::string& value) {
57    if (!network_state_.PropertyChanged(key, TestStringValue(value)))
58      return false;
59    network_state_.InitialPropertiesReceived();
60    return true;
61  }
62
63  NetworkState network_state_;
64
65 private:
66  DISALLOW_COPY_AND_ASSIGN(NetworkStateTest);
67};
68
69}  // namespace
70
71// Seting kNameProperty should set network name after call to
72// InitialPropertiesReceived() in SetStringProperty().
73TEST_F(NetworkStateTest, SsidAscii) {
74  std::string wifi_setname = "SSID TEST";
75  std::string wifi_setname_result = "SSID TEST";
76  EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_setname));
77  EXPECT_EQ(network_state_.name(), wifi_setname_result);
78}
79
80TEST_F(NetworkStateTest, SsidAsciiWithNull) {
81  std::string wifi_setname = "SSID TEST\x00xxx";
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// UTF8 SSID
88TEST_F(NetworkStateTest, SsidUtf8) {
89  std::string wifi_utf8 = "UTF-8 \u3042\u3044\u3046";
90  std::string wifi_utf8_result = "UTF-8 \xE3\x81\x82\xE3\x81\x84\xE3\x81\x86";
91  EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_utf8));
92  EXPECT_EQ(network_state_.name(), wifi_utf8_result);
93}
94
95// Truncates invalid UTF-8
96TEST_F(NetworkStateTest, SsidTruncateInvalid) {
97  std::string wifi_setname2 = "SSID TEST \x01\xff!";
98  std::string wifi_setname2_result = "SSID TEST \xEF\xBF\xBD\xEF\xBF\xBD!";
99  EXPECT_TRUE(SetStringProperty(flimflam::kNameProperty, wifi_setname2));
100  EXPECT_EQ(network_state_.name(), wifi_setname2_result);
101}
102
103// latin1 SSID -> UTF8 SSID (Hex)
104TEST_F(NetworkStateTest, SsidLatin) {
105  std::string wifi_latin1 = "latin-1 \xc0\xcb\xcc\xd6\xfb";
106  std::string wifi_latin1_hex =
107      base::HexEncode(wifi_latin1.c_str(), wifi_latin1.length());
108  std::string wifi_latin1_result = "latin-1 \u00c0\u00cb\u00cc\u00d6\u00fb";
109  EXPECT_TRUE(SetStringProperty(flimflam::kWifiHexSsid, wifi_latin1_hex));
110  EXPECT_EQ(network_state_.name(), wifi_latin1_result);
111}
112
113// Hex SSID
114TEST_F(NetworkStateTest, SsidHex) {
115  std::string wifi_hex = "5468697320697320484558205353494421";
116  std::string wifi_hex_result = "This is HEX SSID!";
117  SetStringProperty(flimflam::kWifiHexSsid, wifi_hex);
118  EXPECT_EQ(network_state_.name(), wifi_hex_result);
119}
120
121}  // namespace chromeos
122