File: network_name_util_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 (157 lines) | stat: -rw-r--r-- 5,903 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
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
// Copyright 2021 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_name_util.h"

#include <stddef.h>

#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/components/network/test_cellular_esim_profile_handler.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

namespace {

const char kTestEuiccPath[] = "euicc_path";
const char kTestIccid[] = "iccid";
const char kTestProfileName[] = "test_profile_name";
const char kTestProfileNickname[] = "test_profile_nickname";
const char kTestServiceProviderName[] = "test_service_provider_name";
const char kTestEidName[] = "eid";
const char kTestEthName[] = "test_eth_name";
const char kTestNameFromShill[] = "shill_network_name";

const char kTestCellularDevicePath[] = "device/cellular1";
const char kTestESimCellularServicePath[] = "/service/cellular1";
const char kTestEthServicePath[] = "/service/eth0";

}  // namespace

class NetworkNameUtilTest : public testing::Test {
 public:
  NetworkNameUtilTest() {}
  ~NetworkNameUtilTest() override = default;

  // testing::Test:
  void SetUp() override {
    cellular_inhibitor_ = std::make_unique<CellularInhibitor>();
    cellular_esim_profile_handler_ =
        std::make_unique<TestCellularESimProfileHandler>();

    network_state_test_helper_.hermes_manager_test()->AddEuicc(
        dbus::ObjectPath(kTestEuiccPath), kTestEidName, /*is_active=*/true,
        /*physical_slot=*/0);
    network_state_test_helper_.device_test()->AddDevice(
        kTestCellularDevicePath, shill::kTypeCellular, "fake_cellular_device");
    cellular_esim_profile_handler_->Init(
        network_state_test_helper_.network_state_handler(),
        cellular_inhibitor_.get());
    base::RunLoop().RunUntilIdle();
  }

  void AddESimProfile(const std::string& name,
                      const std::string& nickname,
                      const std::string& service_provider_name,
                      hermes::profile::State state,
                      const std::string& service_path) {
    network_state_test_helper_.hermes_euicc_test()->AddCarrierProfile(
        dbus::ObjectPath(service_path), dbus::ObjectPath(kTestEuiccPath),
        kTestIccid, name, nickname, service_provider_name, "activation_code",
        service_path, state, hermes::profile::ProfileClass::kOperational,
        HermesEuiccClient::TestInterface::AddCarrierProfileBehavior::
            kAddProfileWithService);
    base::RunLoop().RunUntilIdle();
  }

  void AddEthernet() {
    ShillServiceClient::TestInterface* service_test =
        network_state_test_helper_.service_test();
    service_test->AddService(kTestEthServicePath, "test_guid1", kTestEthName,
                             shill::kTypeEthernet, shill::kStateIdle, true);
    base::RunLoop().RunUntilIdle();
  }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  NetworkStateTestHelper network_state_test_helper_{
      /*use_default_devices_and_services=*/false};
  std::unique_ptr<CellularInhibitor> cellular_inhibitor_;
  std::unique_ptr<TestCellularESimProfileHandler>
      cellular_esim_profile_handler_;
};

TEST_F(NetworkNameUtilTest, EsimNetworkGetNetworkName) {
  AddESimProfile(kTestProfileName, kTestProfileNickname,
                 kTestServiceProviderName, hermes::profile::State::kActive,
                 kTestESimCellularServicePath);

  const NetworkState* network =
      network_state_test_helper_.network_state_handler()->GetNetworkState(
          kTestESimCellularServicePath);

  std::string name = network_name_util::GetNetworkName(
      cellular_esim_profile_handler_.get(), network);

  EXPECT_EQ(name, kTestProfileNickname);
}

TEST_F(NetworkNameUtilTest, EsimNetworNetworkNamePriority) {
  AddESimProfile("", "", kTestServiceProviderName,
                 hermes::profile::State::kActive, kTestESimCellularServicePath);
  const NetworkState* network =
      network_state_test_helper_.network_state_handler()->GetNetworkState(
          kTestESimCellularServicePath);

  std::string name = network_name_util::GetNetworkName(
      cellular_esim_profile_handler_.get(), network);

  EXPECT_EQ(name, kTestServiceProviderName);
}

TEST_F(NetworkNameUtilTest, EthernetNetworkGetNetworkName) {
  AddEthernet();
  const NetworkState* network =
      network_state_test_helper_.network_state_handler()->GetNetworkState(
          kTestEthServicePath);

  std::string name = network_name_util::GetNetworkName(
      cellular_esim_profile_handler_.get(), network);

  EXPECT_EQ(name, kTestEthName);
}

TEST_F(NetworkNameUtilTest, NameComesFromHermes) {
  AddESimProfile(kTestProfileName, kTestProfileNickname,
                 kTestServiceProviderName, hermes::profile::State::kActive,
                 kTestESimCellularServicePath);

  // Change the network's name in Shill. Now, Hermes and Shill have different
  // names associated with the profile.
  network_state_test_helper_.SetServiceProperty(
      kTestESimCellularServicePath, shill::kNameProperty,
      base::Value(kTestNameFromShill));
  base::RunLoop().RunUntilIdle();

  const NetworkState* network =
      network_state_test_helper_.network_state_handler()->GetNetworkState(
          kTestESimCellularServicePath);

  std::string name = network_name_util::GetNetworkName(
      cellular_esim_profile_handler_.get(), network);

  EXPECT_EQ(name, kTestProfileNickname);
}

}  // namespace ash