File: network_test_helper_base.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 (224 lines) | stat: -rw-r--r-- 7,965 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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_test_helper_base.h"

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "chromeos/ash/components/dbus/hermes/hermes_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/network_profile_handler.h"
#include "chromeos/components/onc/onc_utils.h"
#include "dbus/object_path.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

namespace {

void FailErrorCallback(const std::string& error_name,
                       const std::string& error_message) {}

const char kUserHash[] = "user_hash";
const char kProfilePathUser[] = "user_profile_path";

}  // namespace

NetworkTestHelperBase::NetworkTestHelperBase() {
  if (!ShillManagerClient::Get()) {
    shill_clients::InitializeFakes();
    shill_clients_initialized_ = true;
  }

  if (!HermesManagerClient::Get()) {
    hermes_clients::InitializeFakes();
    hermes_clients_initialized_ = true;
  }

  manager_test_ = ShillManagerClient::Get()->GetTestInterface();
  profile_test_ = ShillProfileClient::Get()->GetTestInterface();
  device_test_ = ShillDeviceClient::Get()->GetTestInterface();
  service_test_ = ShillServiceClient::Get()->GetTestInterface();
  ip_config_test_ = ShillIPConfigClient::Get()->GetTestInterface();

  hermes_euicc_test_ = HermesEuiccClient::Get()->GetTestInterface();
  hermes_manager_test_ = HermesManagerClient::Get()->GetTestInterface();
  hermes_profile_test_ = HermesProfileClient::Get()->GetTestInterface();
}

NetworkTestHelperBase::~NetworkTestHelperBase() {
  if (shill_clients_initialized_)
    shill_clients::Shutdown();
  if (hermes_clients_initialized_)
    hermes_clients::Shutdown();
}

void NetworkTestHelperBase::ResetDevicesAndServices() {
  ClearDevices();
  ClearServices();

  // A Wifi device should always exist and default to enabled.
  manager_test_->AddTechnology(shill::kTypeWifi, true /* enabled */);
  const char* kDevicePath = "/device/wifi1";
  device_test_->AddDevice(kDevicePath, shill::kTypeWifi, "wifi_device1");

  // Set initial IPConfigs for the wifi device. The IPConfigs are set up in
  // FakeShillManagerClient::SetupDefaultEnvironment() and do not get cleared.
  base::Value::List ip_configs;
  ip_configs.Append("ipconfig_v4_path");
  ip_configs.Append("ipconfig_v6_path");
  device_test_->SetDeviceProperty(kDevicePath, shill::kIPConfigsProperty,
                                  base::Value(std::move(ip_configs)),
                                  /*notify_changed=*/false);
  base::RunLoop().RunUntilIdle();
}

void NetworkTestHelperBase::ClearDevices() {
  base::RunLoop().RunUntilIdle();  // Process any pending updates
  device_test_->ClearDevices();
  base::RunLoop().RunUntilIdle();
}

void NetworkTestHelperBase::ClearServices() {
  base::RunLoop().RunUntilIdle();  // Process any pending updates
  service_test_->ClearServices();
  base::RunLoop().RunUntilIdle();
}

void NetworkTestHelperBase::ClearProfiles() {
  profile_test_->ClearProfiles();
  manager_test_->ClearProfiles();
}

std::string NetworkTestHelperBase::ConfigureService(
    const std::string& shill_json_string) {
  last_created_service_path_.clear();

  std::optional<base::Value::Dict> shill_json_dict =
      chromeos::onc::ReadDictionaryFromJson(shill_json_string);
  if (!shill_json_dict.has_value()) {
    LOG(ERROR) << "Error parsing json: " << shill_json_string;
    return last_created_service_path_;
  }

  // As a result of the ConfigureService() and RunUntilIdle() calls below,
  // ConfigureCallback() will be invoked before the end of this function, so
  // |last_created_service_path| will be set before it is returned. In
  // error cases, ConfigureCallback() will not run, resulting in "" being
  // returned from this function.
  ShillManagerClient::Get()->ConfigureService(
      *shill_json_dict,
      base::BindOnce(&NetworkTestHelperBase::ConfigureCallback,
                     weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&FailErrorCallback));
  base::RunLoop().RunUntilIdle();

  return last_created_service_path_;
}

std::string NetworkTestHelperBase::ConfigureWiFi(const std::string& state) {
  // ResetDevicesAndServices already adds a WiFi device.
  std::string path = base::StringPrintf("/service/wifi_%d", wifi_index_);
  std::string guid = base::StringPrintf("wifi_guid_%d", wifi_index_);
  std::string ssid = base::StringPrintf("wifi%d", wifi_index_);
  service_test()->AddService(path, guid, ssid, shill::kTypeWifi, state,
                             /*visible=*/true);
  profile_test()->AddService(NetworkProfileHandler::GetSharedProfilePath(),
                             path);
  base::RunLoop().RunUntilIdle();
  wifi_index_++;
  return path;
}

void NetworkTestHelperBase::ConfigureCallback(const dbus::ObjectPath& result) {
  last_created_service_path_ = result.value();
}

std::optional<double> NetworkTestHelperBase::GetServiceDoubleProperty(
    const std::string& service_path,
    const std::string& key) {
  const base::Value::Dict* properties =
      service_test_->GetServiceProperties(service_path);
  if (properties) {
    return properties->FindDouble(key);
  }
  return std::nullopt;
}

std::string NetworkTestHelperBase::GetServiceStringProperty(
    const std::string& service_path,
    const std::string& key) {
  const base::Value::Dict* properties =
      service_test_->GetServiceProperties(service_path);
  if (properties) {
    const std::string* result = properties->FindString(key);
    if (result)
      return *result;
  }
  return std::string();
}

std::optional<base::Value::List> NetworkTestHelperBase::GetServiceListProperty(
    const std::string& service_path,
    const std::string& key) {
  const base::Value::Dict* properties =
      service_test_->GetServiceProperties(service_path);
  if (properties) {
    const base::Value::List* result = properties->FindList(key);
    if (result) {
      return result->Clone();
    }
  }
  return std::nullopt;
}

void NetworkTestHelperBase::SetServiceProperty(const std::string& service_path,
                                               const std::string& key,
                                               const base::Value& value) {
  service_test_->SetServiceProperty(service_path, key, value);
  base::RunLoop().RunUntilIdle();
}

std::string NetworkTestHelperBase::GetProfileStringProperty(
    const std::string& profile_path,
    const std::string& key) {
  base::Value::Dict properties =
      profile_test_->GetProfileProperties(profile_path);
  std::string* result = properties.FindString(key);
  if (result) {
    return *result;
  }
  return std::string();
}

void NetworkTestHelperBase::SetProfileProperty(const std::string& profile_path,
                                               const std::string& key,
                                               const base::Value& value) {
  ShillProfileClient::Get()->SetProperty(dbus::ObjectPath(profile_path), key,
                                         value, base::BindOnce([] {}),
                                         base::BindOnce(&FailErrorCallback));
  base::RunLoop().RunUntilIdle();
}

const char* NetworkTestHelperBase::ProfilePathUser() {
  return kProfilePathUser;
}

const char* NetworkTestHelperBase::UserHash() {
  return kUserHash;
}

void NetworkTestHelperBase::AddDefaultProfiles() {
  profile_test_->AddProfile(NetworkProfileHandler::GetSharedProfilePath(),
                            std::string() /* shared profile */);
  profile_test_->AddProfile(kProfilePathUser, kUserHash);
  base::RunLoop().RunUntilIdle();
}

}  // namespace ash