File: network_onc_utils_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 (288 lines) | stat: -rw-r--r-- 11,163 bytes parent folder | download | duplicates (6)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2012 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/onc/network_onc_utils.h"

#include <string>

#include "base/check.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chromeos/ash/components/network/managed_network_configuration_handler.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_ui_data.h"
#include "chromeos/components/onc/onc_signature.h"
#include "chromeos/components/onc/onc_test_utils.h"
#include "chromeos/components/onc/variable_expander.h"
#include "chromeos/test/chromeos_test_utils.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/test_helper.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::onc {

namespace test_utils = ::chromeos::onc::test_utils;

class ONCUtilsTest : public testing::Test {
 public:
  ONCUtilsTest() = default;
  ONCUtilsTest(const ONCUtilsTest&) = delete;
  ONCUtilsTest& operator=(const ONCUtilsTest&) = delete;
  ~ONCUtilsTest() override = default;

  void SetUp() override {
    user_manager::UserManagerImpl::RegisterPrefs(local_state_.registry());
    auto fake_user_manager =
        std::make_unique<user_manager::FakeUserManager>(&local_state_);
    auto account_id =
        AccountId::FromUserEmailGaiaId("account@test.com", GaiaId("fakegaia"));
    const user_manager::User* user = fake_user_manager->AddGaiaUser(
        account_id, user_manager::UserType::kRegular);
    fake_user_manager->UserLoggedIn(
        account_id, user_manager::TestHelper::GetFakeUsernameHash(account_id));
    fake_user_manager->SwitchActiveUser(account_id);

    scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
        std::move(fake_user_manager));

    network_handler_test_helper_ =
        std::make_unique<ash::NetworkHandlerTestHelper>();
    network_handler_test_helper_->AddDefaultProfiles();
    network_handler_test_helper_->profile_test()->AddProfile(
        "/profile/1", user->username_hash());
    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    network_handler_test_helper_.reset();
    scoped_user_manager_.reset();
  }

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_;
  TestingPrefServiceSimple local_state_;
  std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
  std::unique_ptr<ash::NetworkHandlerTestHelper> network_handler_test_helper_;
};

TEST_F(ONCUtilsTest,
       ImportNetworksForUser_ImportONCShouldNotOverrideExistingPolicy) {
  const char kPolicyGuid[] = "policy_guid";
  const char kWifiSSID[] = "wifi_ssid";
  const char kWifiPassphrase[] = "test_phassphrase";
  const char kWifiOncName[] = "wifi_onc_name";

  base::Value::Dict wifi_config =
      base::Value::Dict()
          .Set(::onc::network_config::kGUID, kPolicyGuid)
          .Set(::onc::network_config::kName, kWifiOncName)
          .Set(::onc::network_config::kType, ::onc::network_config::kWiFi)
          .Set(::onc::network_config::kWiFi,
               base::Value::Dict()
                   .Set(::onc::wifi::kSSID, kWifiSSID)
                   .Set(::onc::wifi::kPassphrase, kWifiPassphrase)
                   .Set(::onc::wifi::kSecurity, ::onc::wifi::kWEP_PSK));
  const user_manager::User* user =
      user_manager::UserManager::Get()->GetActiveUser();

  // Set user policy
  NetworkHandler::Get()->managed_network_configuration_handler()->SetPolicy(
      ::onc::ONC_SOURCE_USER_POLICY, user->username_hash(),
      base::Value::List().Append(wifi_config.Clone()), base::Value::Dict());

  // Set shared policy
  NetworkHandler::Get()->managed_network_configuration_handler()->SetPolicy(
      ::onc::ONC_SOURCE_DEVICE_POLICY, std::string(), base::Value::List(),
      base::Value::Dict());
  base::RunLoop().RunUntilIdle();

  // Modify the wifi config to "None" security and attempt to import it for the
  // user.
  wifi_config.Set(::onc::network_config::kWiFi,
                  base::Value::Dict()
                      .Set(::onc::wifi::kSSID, kWifiSSID)
                      .Set(::onc::wifi::kSecurity, ::onc::wifi::kSecurityNone));
  std::string error;
  ImportNetworksForUser(user, base::Value::List().Append(wifi_config.Clone()),
                        &error);

  // Verify the import network should not override the existing policy
  // configured network.
  std::string service_path =
      network_handler_test_helper_->service_test()->FindServiceMatchingGUID(
          kPolicyGuid);
  ASSERT_FALSE(service_path.empty());

  const base::Value::Dict* properties =
      network_handler_test_helper_->service_test()->GetServiceProperties(
          service_path);
  ASSERT_TRUE(properties);
  const std::string* security =
      properties->FindString(shill::kSecurityClassProperty);
  ASSERT_TRUE(security);
  EXPECT_EQ(*security, shill::kSecurityClassWep);
}

TEST_F(ONCUtilsTest, ImportNetworksForUser_ImportONCWithRemoveField) {
  const char kPolicyGuid[] = "policy_guid";
  const char kWifiSSID[] = "wifi_ssid";
  const char kWifiPassphrase[] = "test_phassphrase";
  const char kWifiOncName[] = "wifi_onc_name";

  base::Value::Dict wifi_config =
      base::Value::Dict()
          .Set(::onc::network_config::kGUID, kPolicyGuid)
          .Set(::onc::network_config::kName, kWifiOncName)
          .Set(::onc::network_config::kType, ::onc::network_config::kWiFi)
          .Set(::onc::kRemove, true)
          .Set(::onc::network_config::kWiFi,
               base::Value::Dict()
                   .Set(::onc::wifi::kSSID, kWifiSSID)
                   .Set(::onc::wifi::kPassphrase, kWifiPassphrase)
                   .Set(::onc::wifi::kSecurity, ::onc::wifi::kWEP_PSK));
  const user_manager::User* user =
      user_manager::UserManager::Get()->GetActiveUser();

  // Set user policy
  NetworkHandler::Get()->managed_network_configuration_handler()->SetPolicy(
      ::onc::ONC_SOURCE_USER_POLICY, user->username_hash(), base::Value::List(),
      base::Value::Dict());

  // Set shared policy
  NetworkHandler::Get()->managed_network_configuration_handler()->SetPolicy(
      ::onc::ONC_SOURCE_DEVICE_POLICY, std::string(), base::Value::List(),
      base::Value::Dict());

  std::string error;
  ImportNetworksForUser(user, base::Value::List().Append(wifi_config.Clone()),
                        &error);
  ASSERT_TRUE(error.empty());

  // Verify the no network should be imported.
  std::string service_path =
      network_handler_test_helper_->service_test()->FindServiceMatchingGUID(
          kPolicyGuid);
  ASSERT_TRUE(service_path.empty());
}

TEST_F(ONCUtilsTest, ProxySettingsToProxyConfig) {
  base::Value::List list_of_tests =
      test_utils::ReadTestList("proxy_config.json");

  // Additional ONC -> ProxyConfig test cases to test fixup.
  base::Value::List additional_tests =
      test_utils::ReadTestList("proxy_config_from_onc.json");
  for (const base::Value& value : additional_tests) {
    list_of_tests.Append(value.Clone());
  }

  int index = 0;
  for (const base::Value& test_case : list_of_tests) {
    SCOPED_TRACE("Test case #" + base::NumberToString(index++));
    const base::Value::Dict& test_case_dict = test_case.GetDict();

    const base::Value* expected_proxy_config =
        test_case_dict.Find("ProxyConfig");
    ASSERT_TRUE(expected_proxy_config);

    const base::Value::Dict* onc_proxy_settings =
        test_case_dict.FindDict("ONC_ProxySettings");
    ASSERT_TRUE(onc_proxy_settings);

    std::optional<base::Value::Dict> actual_proxy_config =
        ConvertOncProxySettingsToProxyConfig(*onc_proxy_settings);
    ASSERT_TRUE(actual_proxy_config.has_value());
    EXPECT_EQ(*expected_proxy_config, actual_proxy_config);
  }
}

TEST_F(ONCUtilsTest, ProxyConfigToOncProxySettings) {
  base::Value::List list_of_tests =
      test_utils::ReadTestList("proxy_config.json");

  int index = 0;
  for (const base::Value& test_case : list_of_tests) {
    SCOPED_TRACE("Test case #" + base::NumberToString(index++));
    const base::Value::Dict& test_case_dict = test_case.GetDict();

    const base::Value::Dict* shill_proxy_config =
        test_case_dict.FindDict("ProxyConfig");
    ASSERT_TRUE(shill_proxy_config);

    const base::Value::Dict* onc_proxy_settings =
        test_case_dict.FindDict("ONC_ProxySettings");
    ASSERT_TRUE(onc_proxy_settings);

    std::optional<base::Value::Dict> actual_proxy_settings =
        ConvertProxyConfigToOncProxySettings(*shill_proxy_config);
    ASSERT_TRUE(actual_proxy_settings.has_value());
    EXPECT_TRUE(
        test_utils::Equals(onc_proxy_settings, &actual_proxy_settings.value()));
  }
}

TEST(ONCPasswordVariable, PasswordAvailable) {
  const auto wifi_onc = test_utils::ReadTestDictionary(
      "wifi_eap_ttls_with_password_variable.onc");

  EXPECT_TRUE(HasUserPasswordSubstitutionVariable(
      chromeos::onc::kNetworkConfigurationSignature, wifi_onc));
}

TEST(ONCPasswordVariable, PasswordNotAvailable) {
  const auto wifi_onc = test_utils::ReadTestDictionary("wifi_eap_ttls.onc");

  EXPECT_FALSE(HasUserPasswordSubstitutionVariable(
      chromeos::onc::kNetworkConfigurationSignature, wifi_onc));
}

TEST(ONCPasswordVariable, PasswordHardcoded) {
  const auto wifi_onc = test_utils::ReadTestDictionary(
      "wifi_eap_ttls_with_hardcoded_password.onc");

  EXPECT_FALSE(HasUserPasswordSubstitutionVariable(
      chromeos::onc::kNetworkConfigurationSignature, wifi_onc));
}

TEST(ONCPasswordVariable, MultipleNetworksPasswordAvailable) {
  const auto network_dictionary = test_utils::ReadTestDictionary(
      "managed_toplevel_with_password_variable.onc");
  const base::Value::List* network_list =
      network_dictionary.FindList("NetworkConfigurations");
  ASSERT_TRUE(network_list);

  EXPECT_TRUE(HasUserPasswordSubstitutionVariable(*network_list));
}

TEST(ONCPasswordVariable, MultipleNetworksPasswordNotAvailable) {
  const auto network_dictionary = test_utils::ReadTestDictionary(
      "managed_toplevel_with_no_password_variable.onc");

  const base::Value::List* network_list =
      network_dictionary.FindList("NetworkConfigurations");
  ASSERT_TRUE(network_list);

  EXPECT_FALSE(HasUserPasswordSubstitutionVariable(*network_list));
}

TEST(ONCPasswordVariable, MultipleNetworksPasswordAvailableForL2tpVpn) {
  const auto network_dictionary = test_utils::ReadTestDictionary(
      "managed_toplevel_with_password_variable_in_l2tp_vpn.onc");
  const base::Value::List* network_list =
      network_dictionary.FindList("NetworkConfigurations");
  ASSERT_TRUE(network_list);

  EXPECT_TRUE(HasUserPasswordSubstitutionVariable(*network_list));
}

}  // namespace ash::onc