summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/cros/network_library_unittest.cc
blob: 211b924bbcafafb0ef04eaee2f808a66ab5b6c05 (plain)
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
// Copyright (c) 2011 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 "chrome/browser/chromeos/cros/network_library.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

namespace {

int32 GetPrefixLength(std::string netmask) {
  return NetworkIPConfig(std::string(), IPCONFIG_TYPE_UNKNOWN, std::string(),
      netmask, std::string(), std::string()).GetPrefixLength();
}

}  // namespace

TEST(NetworkLibraryTest, NetmaskToPrefixlen) {
  // Valid netmasks
  EXPECT_EQ(32, GetPrefixLength("255.255.255.255"));
  EXPECT_EQ(31, GetPrefixLength("255.255.255.254"));
  EXPECT_EQ(30, GetPrefixLength("255.255.255.252"));
  EXPECT_EQ(29, GetPrefixLength("255.255.255.248"));
  EXPECT_EQ(28, GetPrefixLength("255.255.255.240"));
  EXPECT_EQ(27, GetPrefixLength("255.255.255.224"));
  EXPECT_EQ(26, GetPrefixLength("255.255.255.192"));
  EXPECT_EQ(25, GetPrefixLength("255.255.255.128"));
  EXPECT_EQ(24, GetPrefixLength("255.255.255.0"));
  EXPECT_EQ(23, GetPrefixLength("255.255.254.0"));
  EXPECT_EQ(22, GetPrefixLength("255.255.252.0"));
  EXPECT_EQ(21, GetPrefixLength("255.255.248.0"));
  EXPECT_EQ(20, GetPrefixLength("255.255.240.0"));
  EXPECT_EQ(19, GetPrefixLength("255.255.224.0"));
  EXPECT_EQ(18, GetPrefixLength("255.255.192.0"));
  EXPECT_EQ(17, GetPrefixLength("255.255.128.0"));
  EXPECT_EQ(16, GetPrefixLength("255.255.0.0"));
  EXPECT_EQ(15, GetPrefixLength("255.254.0.0"));
  EXPECT_EQ(14, GetPrefixLength("255.252.0.0"));
  EXPECT_EQ(13, GetPrefixLength("255.248.0.0"));
  EXPECT_EQ(12, GetPrefixLength("255.240.0.0"));
  EXPECT_EQ(11, GetPrefixLength("255.224.0.0"));
  EXPECT_EQ(10, GetPrefixLength("255.192.0.0"));
  EXPECT_EQ(9, GetPrefixLength("255.128.0.0"));
  EXPECT_EQ(8, GetPrefixLength("255.0.0.0"));
  EXPECT_EQ(7, GetPrefixLength("254.0.0.0"));
  EXPECT_EQ(6, GetPrefixLength("252.0.0.0"));
  EXPECT_EQ(5, GetPrefixLength("248.0.0.0"));
  EXPECT_EQ(4, GetPrefixLength("240.0.0.0"));
  EXPECT_EQ(3, GetPrefixLength("224.0.0.0"));
  EXPECT_EQ(2, GetPrefixLength("192.0.0.0"));
  EXPECT_EQ(1, GetPrefixLength("128.0.0.0"));
  EXPECT_EQ(0, GetPrefixLength("0.0.0.0"));
  // Invalid netmasks
  EXPECT_EQ(-1, GetPrefixLength("255.255.255"));
  EXPECT_EQ(-1, GetPrefixLength("255.255.255.255.255"));
  EXPECT_EQ(-1, GetPrefixLength("255.255.255.255.0"));
  EXPECT_EQ(-1, GetPrefixLength("255.255.255.256"));
  EXPECT_EQ(-1, GetPrefixLength("255.255.255.1"));
  EXPECT_EQ(-1, GetPrefixLength("255.255.240.255"));
  EXPECT_EQ(-1, GetPrefixLength("255.0.0.255"));
  EXPECT_EQ(-1, GetPrefixLength("255.255.255.FF"));
  EXPECT_EQ(-1, GetPrefixLength("255,255,255,255"));
  EXPECT_EQ(-1, GetPrefixLength("255 255 255 255"));
}

TEST(NetworkLibraryTest, DecodeNonAsciiSSID) {

  // Sets network name.
  {
    std::string wifi_setname = "SSID TEST";
    std::string wifi_setname_result = "SSID TEST";
    WifiNetwork* wifi = new WifiNetwork("fw");
    wifi->SetName(wifi_setname);
    EXPECT_EQ(wifi->name(), wifi_setname_result);
    delete wifi;
  }

  // Truncates invalid UTF-8
  {
    std::string wifi_setname2 = "SSID TEST \x01\xff!";
    std::string wifi_setname2_result = "SSID TEST \xEF\xBF\xBD\xEF\xBF\xBD!";
    WifiNetwork* wifi = new WifiNetwork("fw");
    wifi->SetName(wifi_setname2);
    EXPECT_EQ(wifi->name(), wifi_setname2_result);
    delete wifi;
  }

  // UTF8 SSID
  {
    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";
    WifiNetwork* wifi = new WifiNetwork("fw");
    wifi->SetSsid(wifi_utf8);
    EXPECT_EQ(wifi->name(), wifi_utf8_result);
    delete wifi;
  }

  // latin1 SSID -> UTF8 SSID
  {
    std::string wifi_latin1 = "latin-1 \xc0\xcb\xcc\xd6\xfb";
    std::string wifi_latin1_result = "latin-1 \u00c0\u00cb\u00cc\u00d6\u00fb";
    WifiNetwork* wifi = new WifiNetwork("fw");
    wifi->SetSsid(wifi_latin1);
    EXPECT_EQ(wifi->name(), wifi_latin1_result);
    delete wifi;
  }

  // Hex SSID
  {
    std::string wifi_hex = "5468697320697320484558205353494421";
    std::string wifi_hex_result = "This is HEX SSID!";
    WifiNetwork* wifi = new WifiNetwork("fw");
    wifi->SetHexSsid(wifi_hex);
    EXPECT_EQ(wifi->name(), wifi_hex_result);
    delete wifi;
  }
}

}  // namespace chromeos