File: network_event_log_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 (159 lines) | stat: -rw-r--r-- 6,791 bytes parent folder | download | duplicates (9)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 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_event_log.h"

#include "base/test/task_environment.h"
#include "chromeos/ash/components/dbus/shill/shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_handler_test_helper.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

class NetworkEventLogTest : public testing::Test {
 public:
  void SetUp() override {
    SetupDefaultShillState();
    base::RunLoop().RunUntilIdle();
  }

  const NetworkState* GetNetworkState(const std::string& service_path) {
    return NetworkHandler::Get()->network_state_handler()->GetNetworkState(
        service_path);
  }

 private:
  void SetupDefaultShillState() {
    // Make sure any observer calls complete before clearing devices and
    // services.
    base::RunLoop().RunUntilIdle();
    auto* device_test = ShillDeviceClient::Get()->GetTestInterface();
    device_test->ClearDevices();
    device_test->AddDevice("/device/eth0", shill::kTypeEthernet,
                           "stub_eth_device");
    device_test->AddDevice("/device/wlan0", shill::kTypeWifi,
                           "stub_wifi_device");
    device_test->AddDevice("/device/cellular0", shill::kTypeCellular,
                           "stub_cellular_device");

    auto* service_test = ShillServiceClient::Get()->GetTestInterface();
    service_test->ClearServices();
    const bool add_to_visible = true;

    // Ethernet
    service_test->AddService("/service/0", "ethernet_guid", "ethernet",
                             shill::kTypeEthernet, shill::kStateOnline,
                             add_to_visible);

    // Cellular
    service_test->AddService("/service/1", "cellular_guid", "cellular1",
                             shill::kTypeCellular, shill::kStateIdle,
                             add_to_visible);
    service_test->SetServiceProperty("/service/1",
                                     shill::kNetworkTechnologyProperty,
                                     base::Value(shill::kNetworkTechnologyLte));

    // WiFi
    service_test->AddService("/service/2", "wifi2_guid", "wifi2",
                             shill::kTypeWifi, shill::kStateIdle,
                             add_to_visible);
    service_test->SetServiceProperty("/service/2",
                                     shill::kSecurityClassProperty,
                                     base::Value(shill::kSecurityClassNone));

    service_test->AddService("/service/3", "wifi3_guid", "wifi3",
                             shill::kTypeWifi, shill::kStateIdle,
                             add_to_visible);
    service_test->SetServiceProperty("/service/3",
                                     shill::kSecurityClassProperty,
                                     base::Value(shill::kSecurityClassWep));

    service_test->AddService("/service/4", "wifi4_guid", "wifi4",
                             shill::kTypeWifi, shill::kStateIdle,
                             add_to_visible);
    service_test->SetServiceProperty("/service/4",
                                     shill::kSecurityClassProperty,
                                     base::Value(shill::kSecurityClass8021x));

    // VPN
    service_test->AddService("/service/5", "vpn5_guid", "vpn5", shill::kTypeVPN,
                             shill::kStateIdle, add_to_visible);
    auto provider_properties = base::Value::Dict().Set(
        shill::kTypeProperty, shill::kProviderL2tpIpsec);
    service_test->SetServiceProperty(
        "/service/5", shill::kProviderProperty,
        base::Value(std::move(provider_properties)));

    service_test->AddService("/service/6", "vpn6_guid", "vpn6", shill::kTypeVPN,
                             shill::kStateIdle, add_to_visible);
    auto provider_properties2 =
        base::Value::Dict().Set(shill::kTypeProperty, shill::kProviderOpenVpn);
    service_test->SetServiceProperty(
        "/service/6", shill::kProviderProperty,
        base::Value(std::move(provider_properties2)));

    base::RunLoop().RunUntilIdle();
  }

  base::test::TaskEnvironment environment_;
  NetworkHandlerTestHelper network_handler_test_helper_;
};

// See FakeShillManagerClient for default fake networks.
TEST_F(NetworkEventLogTest, NetworkId) {
  const NetworkState* service0 = GetNetworkState("/service/0");
  ASSERT_TRUE(service0);
  EXPECT_EQ("ethernet_0", NetworkId(service0));

  const NetworkState* service1 = GetNetworkState("/service/1");
  ASSERT_TRUE(service1);
  EXPECT_EQ("cellular_LTE_1", NetworkId(service1));

  const NetworkState* service2 = GetNetworkState("/service/2");
  ASSERT_TRUE(service2);
  EXPECT_EQ("wifi_none_2", NetworkId(service2));
  const NetworkState* service3 = GetNetworkState("/service/3");
  ASSERT_TRUE(service3);
  EXPECT_EQ("wifi_wep_3", NetworkId(service3));
  const NetworkState* service4 = GetNetworkState("/service/4");
  ASSERT_TRUE(service4);
  EXPECT_EQ("wifi_802_1x_4", NetworkId(service4));

  const NetworkState* service5 = GetNetworkState("/service/5");
  ASSERT_TRUE(service5);
  EXPECT_EQ("vpn_l2tpipsec_5", NetworkId(service5));
  const NetworkState* service6 = GetNetworkState("/service/6");
  ASSERT_TRUE(service6);
  EXPECT_EQ("vpn_openvpn_6", NetworkId(service6));
}

TEST_F(NetworkEventLogTest, NetworkPathId) {
  EXPECT_EQ("ethernet_0", NetworkPathId("/service/0"));
  EXPECT_EQ("cellular_LTE_1", NetworkPathId("/service/1"));
  EXPECT_EQ("wifi_none_2", NetworkPathId("/service/2"));
  EXPECT_EQ("wifi_wep_3", NetworkPathId("/service/3"));
  EXPECT_EQ("wifi_802_1x_4", NetworkPathId("/service/4"));
  EXPECT_EQ("vpn_l2tpipsec_5", NetworkPathId("/service/5"));
  EXPECT_EQ("vpn_openvpn_6", NetworkPathId("/service/6"));
  EXPECT_EQ("service_7", NetworkPathId("/service/7"));
  EXPECT_EQ("service_invalid_path", NetworkPathId("service_invalid_path"));
}

TEST_F(NetworkEventLogTest, NetworkGuidId) {
  EXPECT_EQ("ethernet_0", NetworkGuidId("ethernet_guid"));
  EXPECT_EQ("cellular_LTE_1", NetworkGuidId("cellular_guid"));
  EXPECT_EQ("wifi_none_2", NetworkGuidId("wifi2_guid"));
  EXPECT_EQ("wifi_wep_3", NetworkGuidId("wifi3_guid"));
  EXPECT_EQ("wifi_802_1x_4", NetworkGuidId("wifi4_guid"));
  EXPECT_EQ("vpn_l2tpipsec_5", NetworkGuidId("vpn5_guid"));
  EXPECT_EQ("vpn_openvpn_6", NetworkGuidId("vpn6_guid"));
  EXPECT_EQ("wifi99_guid", NetworkGuidId("wifi99_guid"));
}

}  // namespace ash