File: network_config_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (81 lines) | stat: -rw-r--r-- 3,444 bytes parent folder | download | duplicates (8)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/network_config.h"

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/values.h"
#include "net/base/ip_address.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash {
namespace {

TEST(NetworkConfigTest, ParseValue) {
  base::Value::Dict properties;
  properties.Set(shill::kNetworkConfigIPv4AddressProperty, "1.2.3.4/24");
  properties.Set(shill::kNetworkConfigIPv4GatewayProperty, "1.2.3.5");
  properties.Set(shill::kNetworkConfigIPv6AddressesProperty,
                 base::Value::List().Append("fd00::1/64").Append("fd00::2/64"));
  properties.Set(shill::kNetworkConfigIPv6GatewayProperty, "fd01::2");
  properties.Set(shill::kNetworkConfigNameServersProperty,
                 base::Value::List().Append("4.3.2.1").Append("fdfd::1").Append(
                     "0.0.0.0"));
  properties.Set(shill::kNetworkConfigSearchDomainsProperty,
                 base::Value::List().Append("domain1").Append("domain2"));
  properties.Set(shill::kNetworkConfigMTUProperty, 1400);
  properties.Set(
      shill::kNetworkConfigIncludedRoutesProperty,
      base::Value::List().Append("10.10.10.0/24").Append("fd01::/64"));
  properties.Set(
      shill::kNetworkConfigExcludedRoutesProperty,
      base::Value::List().Append("10.20.30.0/24").Append("fd02::/64"));

  std::unique_ptr<NetworkConfig> config =
      NetworkConfig::ParseFromServicePropertyValue(
          base::Value(std::move(properties)));

  ASSERT_TRUE(config);
  EXPECT_EQ(config->ipv4_address->addr.ToString(), "1.2.3.4");
  EXPECT_EQ(config->ipv4_address->prefix_len, 24);
  EXPECT_EQ(config->ipv4_gateway->ToString(), "1.2.3.5");
  ASSERT_EQ(config->ipv6_addresses.size(), 2u);
  EXPECT_EQ(config->ipv6_addresses[0].addr.ToString(), "fd00::1");
  EXPECT_EQ(config->ipv6_addresses[0].prefix_len, 64);
  EXPECT_EQ(config->ipv6_addresses[1].addr.ToString(), "fd00::2");
  EXPECT_EQ(config->ipv6_addresses[1].prefix_len, 64);
  EXPECT_EQ(config->ipv6_gateway->ToString(), "fd01::2");
  ASSERT_EQ(config->dns_servers.size(), 2u);
  EXPECT_EQ(config->dns_servers[0].ToString(), "4.3.2.1");
  EXPECT_EQ(config->dns_servers[1].ToString(), "fdfd::1");
  ASSERT_EQ(config->search_domains.size(), 2u);
  EXPECT_EQ(config->search_domains[0], "domain1");
  EXPECT_EQ(config->search_domains[1], "domain2");
  EXPECT_EQ(config->mtu, 1400);
  ASSERT_EQ(config->included_routes.size(), 2u);
  EXPECT_EQ(config->included_routes[0].addr.ToString(), "10.10.10.0");
  EXPECT_EQ(config->included_routes[0].prefix_len, 24);
  EXPECT_EQ(config->included_routes[1].addr.ToString(), "fd01::");
  EXPECT_EQ(config->included_routes[1].prefix_len, 64);
  ASSERT_EQ(config->excluded_routes.size(), 2u);
  EXPECT_EQ(config->excluded_routes[0].addr.ToString(), "10.20.30.0");
  EXPECT_EQ(config->excluded_routes[0].prefix_len, 24);
  EXPECT_EQ(config->excluded_routes[1].addr.ToString(), "fd02::");
  EXPECT_EQ(config->excluded_routes[1].prefix_len, 64);
}

TEST(NetworkConfigTest, ParseEmptyValue) {
  std::unique_ptr<NetworkConfig> config =
      NetworkConfig::ParseFromServicePropertyValue(
          base::Value(base::Value::Dict()));
  EXPECT_FALSE(config);
}

}  // namespace
}  // namespace ash