File: device_name_policy_handler_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (381 lines) | stat: -rw-r--r-- 15,650 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// 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 "chrome/browser/ash/policy/handlers/device_name_policy_handler_impl.h"

#include "ash/constants/ash_features.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chrome/browser/ash/settings/scoped_testing_cros_settings.h"
#include "chrome/browser/ash/settings/stub_cros_settings_provider.h"
#include "chromeos/ash/components/install_attributes/stub_install_attributes.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/system/fake_statistics_provider.h"
#include "components/policy/core/common/cloud/test/policy_builder.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {
namespace {

class FakeObserver : public DeviceNamePolicyHandler::Observer {
 public:
  FakeObserver() = default;
  ~FakeObserver() override = default;

  size_t num_calls() const { return num_calls_; }

  // DeviceNamePolicyHandler::Observer:
  void OnHostnamePolicyChanged() override { ++num_calls_; }

 private:
  size_t num_calls_ = 0;
};

}  // namespace

class DeviceNamePolicyHandlerImplTest : public testing::Test {
 public:
  DeviceNamePolicyHandlerImplTest() = default;

  // testing::Test
  void SetUp() override {
    testing::Test::SetUp();
    network_handler_test_helper_ =
        std::make_unique<ash::NetworkHandlerTestHelper>();
  }

  void TearDown() override {
    handler_->RemoveObserver(&fake_observer_);
    handler_.reset();
  }

  // Sets kDeviceHostnameTemplate policy which will eventually cause
  // OnDeviceHostnamePropertyChangedAndMachineStatisticsLoaded() to run
  // and set the hostname_.
  void SetTemplate(const std::string& hostname_template) {
    scoped_testing_cros_settings_.device_settings()->Set(
        ash::kDeviceHostnameTemplate, base::Value(hostname_template));
    // Makes sure that template is set before continuing.
    base::RunLoop().RunUntilIdle();
  }

  void UnsetTemplate() {
    scoped_testing_cros_settings_.device_settings()->Set(
        ash::kDeviceHostnameTemplate, base::Value());
    // Makes sure that template is set before continuing.
    base::RunLoop().RunUntilIdle();
  }

  // Sets kDeviceHostnameUserConfigurable policy which will eventually cause
  // the class to stop making direct calls to NetworkStateHandler::SetHostname()
  void SetConfigurable(bool configurable) {
    scoped_testing_cros_settings_.device_settings()->SetBoolean(
        ash::kDeviceHostnameUserConfigurable, configurable);
    // Makes sure that template is set before continuing.
    base::RunLoop().RunUntilIdle();
  }

  void UnsetConfigurable() {
    scoped_testing_cros_settings_.device_settings()->Set(
        ash::kDeviceHostnameUserConfigurable, base::Value());
    // Makes sure that template is set before continuing.
    base::RunLoop().RunUntilIdle();
  }

  void InitializeHandler(bool is_hostname_setting_flag_enabled,
                         bool is_device_managed) {
    if (is_hostname_setting_flag_enabled)
      feature_list_.InitAndEnableFeature(ash::features::kEnableHostnameSetting);

    if (is_device_managed) {
      attributes_ = std::make_unique<ash::ScopedStubInstallAttributes>(
          ash::StubInstallAttributes::CreateCloudManaged(
              PolicyBuilder::kFakeDomain, PolicyBuilder::kFakeDeviceId));
    } else {
      attributes_ = std::make_unique<ash::ScopedStubInstallAttributes>(
          ash::StubInstallAttributes::CreateConsumerOwned());
    }

    handler_ = base::WrapUnique(new DeviceNamePolicyHandlerImpl(
        ash::CrosSettings::Get(), &fake_statistics_provider_,
        ash::NetworkHandler::Get()->network_state_handler()));
    handler_->AddObserver(&fake_observer_);
    base::RunLoop().RunUntilIdle();
  }

  size_t GetNumObserverCalls() const { return fake_observer_.num_calls(); }

  // Verifies that for unmanaged devices the policy state is kNoPolicy by
  // default and the hostname chosen by the administrator is nullopt. Flag state
  // does not matter.
  void VerifyDefaultStateUnmanagedDevice() {
    EXPECT_EQ(DeviceNamePolicyHandler::DeviceNamePolicy::kNoPolicy,
              handler_->GetDeviceNamePolicy());

    // GetHostnameChosenByAdministrator() should therefore return null.
    const std::optional<std::string> hostname =
        handler_->GetHostnameChosenByAdministrator();
    EXPECT_FALSE(hostname);
  }

  // Verifies that for managed devices the policy state is
  // kPolicyHostnameNotConfigurable by default and the hostname chosen by the
  // administrator is nullopt. Flag state does not matter.
  void VerifyDefaultStateManagedDevice() {
    EXPECT_EQ(DeviceNamePolicyHandler::DeviceNamePolicy::
                  kPolicyHostnameNotConfigurable,
              handler_->GetDeviceNamePolicy());

    // GetHostnameChosenByAdministrator() should therefore return null.
    const std::optional<std::string> hostname =
        handler_->GetHostnameChosenByAdministrator();
    EXPECT_FALSE(hostname);
  }

  // Verifies that when |kDeviceHostnameTemplate| policy is set, the device name
  // policy is set to |kPolicyHostnameChosenByAdmin| and is unaffected by the
  // |kDeviceHostnameUserConfigurable| policy. Also verifies that the hostname
  // is the one set by the template. Flag state does not matter.
  void VerifyStateWithAdminPolicy() {
    // Check that DeviceNamePolicy changes from kPolicyHostnameNotConfigurable
    // to kPolicyHostnameChosenByAdmin on setting template.
    EXPECT_EQ(DeviceNamePolicyHandler::DeviceNamePolicy::
                  kPolicyHostnameNotConfigurable,
              handler_->GetDeviceNamePolicy());
    const std::string hostname_template = "chromebook";
    SetTemplate(hostname_template);
    DeviceNamePolicyHandler::DeviceNamePolicy after =
        handler_->GetDeviceNamePolicy();
    EXPECT_EQ(
        DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameChosenByAdmin,
        after);
    // Check GetHostnameChosenByAdministrator() returns the expected hostname
    // value.
    const std::optional<std::string> hostname_chosen_by_administrator =
        handler_->GetHostnameChosenByAdministrator();
    EXPECT_EQ(hostname_chosen_by_administrator, hostname_template);

    // Setting kDeviceHostnameUserConfigurable policy should not affect the
    // DeviceNamePolicy because template is set.
    SetConfigurable(true);
    EXPECT_EQ(
        DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameChosenByAdmin,
        handler_->GetDeviceNamePolicy());
    SetConfigurable(false);
    EXPECT_EQ(
        DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameChosenByAdmin,
        handler_->GetDeviceNamePolicy());
  }

  // Verifies the number of calls received by the observer for any changes in
  // |kDeviceHostnameTemplate| policy and hostname. Flag state does not matter.
  void VerifyObserverNumCalls() {
    // Neither hostname or policy changes on initialization of handler
    EXPECT_EQ(0u, GetNumObserverCalls());

    // Both hostname and policy change, hence observer should be notified once
    EXPECT_EQ(DeviceNamePolicyHandler::DeviceNamePolicy::
                  kPolicyHostnameNotConfigurable,
              handler_->GetDeviceNamePolicy());
    EXPECT_FALSE(handler_->GetHostnameChosenByAdministrator());
    std::string hostname_template = "template1";
    SetTemplate(hostname_template);
    EXPECT_EQ(
        DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameChosenByAdmin,
        handler_->GetDeviceNamePolicy());
    EXPECT_EQ(hostname_template, handler_->GetHostnameChosenByAdministrator());
    EXPECT_EQ(1u, GetNumObserverCalls());

    // Hostname changes every time, hence observer should be notified each time.
    hostname_template = "template2";
    SetTemplate(hostname_template);
    EXPECT_EQ(hostname_template, handler_->GetHostnameChosenByAdministrator());
    EXPECT_EQ(2u, GetNumObserverCalls());
    hostname_template = "template3";
    SetTemplate(hostname_template);
    EXPECT_EQ(hostname_template, handler_->GetHostnameChosenByAdministrator());
    EXPECT_EQ(3u, GetNumObserverCalls());

    // Hostname is unchanged, hence observer should not be notified.
    const std::string const_template = "const_template";
    SetTemplate(const_template);
    EXPECT_EQ(const_template, handler_->GetHostnameChosenByAdministrator());
    EXPECT_EQ(4u, GetNumObserverCalls());
    SetTemplate(const_template);
    EXPECT_EQ(const_template, handler_->GetHostnameChosenByAdministrator());
    EXPECT_EQ(4u, GetNumObserverCalls());
  }

  std::unique_ptr<DeviceNamePolicyHandler> handler_;

 private:
  base::test::TaskEnvironment task_environment_;
  base::test::ScopedFeatureList feature_list_;
  std::unique_ptr<ash::NetworkHandlerTestHelper> network_handler_test_helper_;
  ash::ScopedTestingCrosSettings scoped_testing_cros_settings_;
  std::unique_ptr<ash::ScopedStubInstallAttributes> attributes_;
  ash::system::ScopedFakeStatisticsProvider fake_statistics_provider_;
  FakeObserver fake_observer_;
};

TEST_F(DeviceNamePolicyHandlerImplTest, NoPoliciesFlagOnManagedDevice) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/true,
                    /*is_device_managed=*/true);
  VerifyDefaultStateManagedDevice();
}

TEST_F(DeviceNamePolicyHandlerImplTest, NoPoliciesFlagOnUnmanagedDevice) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/true,
                    /*is_device_managed=*/false);
  VerifyDefaultStateUnmanagedDevice();
}

TEST_F(DeviceNamePolicyHandlerImplTest, NoPoliciesFlagOffManagedDevice) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/false,
                    /*is_device_managed=*/true);
  VerifyDefaultStateManagedDevice();
}

TEST_F(DeviceNamePolicyHandlerImplTest, NoPoliciesFlagOffUnmanagedDevice) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/false,
                    /*is_device_managed=*/false);
  VerifyDefaultStateUnmanagedDevice();
}

// The tests below apply only to managed devices since unmanaged devices do not
// have any policies applied.

TEST_F(DeviceNamePolicyHandlerImplTest, DeviceHostnameTemplatePolicyOnFlagOn) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/true,
                    /*is_device_managed=*/true);
  VerifyStateWithAdminPolicy();
}

TEST_F(DeviceNamePolicyHandlerImplTest, DeviceHostnameTemplatePolicyOnFlagOff) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/false,
                    /*is_device_managed=*/true);
  VerifyStateWithAdminPolicy();
}

// Verifies that when |kDeviceHostnameTemplate| policy is not set and flag
// is on, setting kDeviceHostnameUserConfigurable policy should change the
// DeviceNamePolicy.
TEST_F(DeviceNamePolicyHandlerImplTest, DeviceHostnameTemplatePolicyOffFlagOn) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/true,
                    /*is_device_managed=*/true);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  SetConfigurable(true);
  EXPECT_EQ(DeviceNamePolicyHandler::DeviceNamePolicy::
                kPolicyHostnameConfigurableByManagedUser,
            handler_->GetDeviceNamePolicy());
  SetConfigurable(false);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  UnsetConfigurable();
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
}

// Verifies that when |kDeviceHostnameTemplate| policy is not set and flag
// is off, setting kDeviceHostnameUserConfigurable policy should not change the
// DeviceNamePolicy.
TEST_F(DeviceNamePolicyHandlerImplTest,
       DeviceHostnameTemplatePolicyOffFlagOffManagedDevices) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/false,
                    /*is_device_managed=*/true);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  SetConfigurable(true);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  SetConfigurable(false);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  UnsetConfigurable();
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
}

// Verifies that OnHostnamePolicyChanged() correctly notifies observer when
// hostname and/or policy changes, while the flag is on.
TEST_F(DeviceNamePolicyHandlerImplTest, ObserverTestsFlagOn) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/true,
                    /*is_device_managed=*/true);
  VerifyObserverNumCalls();

  // Policy changes every time, hence observer should be notified each time.
  UnsetTemplate();
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(5u, GetNumObserverCalls());
  SetTemplate("hostname_template");
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameChosenByAdmin,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(6u, GetNumObserverCalls());
  UnsetTemplate();
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(7u, GetNumObserverCalls());
  SetConfigurable(true);
  EXPECT_EQ(DeviceNamePolicyHandler::DeviceNamePolicy::
                kPolicyHostnameConfigurableByManagedUser,
            handler_->GetDeviceNamePolicy());
  EXPECT_EQ(8u, GetNumObserverCalls());
  SetConfigurable(false);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(9u, GetNumObserverCalls());
}

// Verifies that OnHostnamePolicyChanged() correctly notifies observer when
// hostname and/or policy changes, while the flag is off.
TEST_F(DeviceNamePolicyHandlerImplTest, ObserverTestsFlagOff) {
  InitializeHandler(/*is_hostname_setting_flag_enabled=*/false,
                    /*is_device_managed=*/true);
  VerifyObserverNumCalls();

  // Policy changes every time but observer should be notified only for changes
  // in the hostname template policy since flag is off.
  UnsetTemplate();
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(5u, GetNumObserverCalls());
  SetTemplate("hostname_template");
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameChosenByAdmin,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(6u, GetNumObserverCalls());
  UnsetTemplate();
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(7u, GetNumObserverCalls());
  SetConfigurable(false);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(7u, GetNumObserverCalls());
  SetConfigurable(true);
  EXPECT_EQ(
      DeviceNamePolicyHandler::DeviceNamePolicy::kPolicyHostnameNotConfigurable,
      handler_->GetDeviceNamePolicy());
  EXPECT_EQ(7u, GetNumObserverCalls());
}

}  // namespace policy