File: browser_dm_token_storage_linux_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 (257 lines) | stat: -rw-r--r-- 9,107 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
// Copyright 2018 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/policy/browser_dm_token_storage_linux.h"

#include <iostream>
#include <memory>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_path_override.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::IsEmpty;

namespace policy {

namespace {

const char kDmTokenBaseDir[] = FILE_PATH_LITERAL("Policy/Enrollment/");

const char kMachineId[] = "a1254c624234b270985170c3549725f1";
const char kExpectedClientId[] =
    "JXduKRDItaY72B6vHikFl9U95m8";  // Corresponds to kMachineId.
const char kDMToken[] = "fake-dm-token";

#if !BUILDFLAG(IS_CHROMEOS)
const char kEnrollmentTokenFilename[] =
    FILE_PATH_LITERAL("enrollment/CloudManagementEnrollmentToken");
const char kEnrollmentToken[] = "fake-enrollment-token";
#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace

class MockBrowserDMTokenStorageLinux : public BrowserDMTokenStorageLinux {
 public:
  std::string ReadMachineIdFile() override { return kMachineId; }
};

class BrowserDMTokenStorageLinuxTest : public testing::Test {
 private:
  content::BrowserTaskEnvironment task_environment_;
};

TEST_F(BrowserDMTokenStorageLinuxTest, InitClientId) {
  MockBrowserDMTokenStorageLinux storage;
  EXPECT_EQ(kExpectedClientId, storage.InitClientId());
}

TEST_F(BrowserDMTokenStorageLinuxTest, InitEnrollmentToken) {
#if BUILDFLAG(IS_CHROMEOS)
  MockBrowserDMTokenStorageLinux storage;
  EXPECT_EQ(std::string(), storage.InitEnrollmentToken());
#else
  std::unique_ptr<base::ScopedPathOverride> path_override;
  base::ScopedTempDir fake_policy_dir;

  ASSERT_TRUE(fake_policy_dir.CreateUniqueTempDir());
  path_override = std::make_unique<base::ScopedPathOverride>(
      chrome::DIR_POLICY_FILES, fake_policy_dir.GetPath());

  base::FilePath dir_policy_files_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_POLICY_FILES, &dir_policy_files_path));
  base::FilePath enrollment_token_file_path =
      dir_policy_files_path.Append(kEnrollmentTokenFilename);

  ASSERT_TRUE(base::CreateDirectory(enrollment_token_file_path.DirName()));

  ASSERT_TRUE(base::WriteFile(base::FilePath(enrollment_token_file_path),
                              kEnrollmentToken));

  MockBrowserDMTokenStorageLinux storage;
  EXPECT_EQ(kEnrollmentToken, storage.InitEnrollmentToken());
#endif  // BUILDFLAG(IS_CHROMEOS)
}

TEST_F(BrowserDMTokenStorageLinuxTest, InitDMToken) {
  std::unique_ptr<base::ScopedPathOverride> path_override;
  base::ScopedTempDir fake_user_data_dir;

  ASSERT_TRUE(fake_user_data_dir.CreateUniqueTempDir());
  path_override = std::make_unique<base::ScopedPathOverride>(
      chrome::DIR_USER_DATA, fake_user_data_dir.GetPath());

  base::FilePath dir_user_data_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_USER_DATA, &dir_user_data_path));

  base::FilePath dm_token_dir_path = dir_user_data_path.Append(kDmTokenBaseDir);
  ASSERT_TRUE(base::CreateDirectory(dm_token_dir_path));

  base::FilePath dm_token_file_path =
      dm_token_dir_path.Append(kExpectedClientId);
  ASSERT_TRUE(base::WriteFile(base::FilePath(dm_token_file_path), kDMToken));

  MockBrowserDMTokenStorageLinux storage;
  EXPECT_EQ(kDMToken, storage.InitDMToken());
}

TEST_F(BrowserDMTokenStorageLinuxTest, InitDMTokenWithoutDirectory) {
  std::unique_ptr<base::ScopedPathOverride> path_override;
  base::ScopedTempDir fake_user_data_dir;

  ASSERT_TRUE(fake_user_data_dir.CreateUniqueTempDir());
  path_override = std::make_unique<base::ScopedPathOverride>(
      chrome::DIR_USER_DATA, fake_user_data_dir.GetPath());

  base::FilePath dm_token_dir_path =
      fake_user_data_dir.GetPath().Append(kDmTokenBaseDir);

  MockBrowserDMTokenStorageLinux storage;
  EXPECT_EQ(std::string(), storage.InitDMToken());

  EXPECT_FALSE(base::PathExists(dm_token_dir_path));
}

class TestStoreDMTokenDelegate {
 public:
  TestStoreDMTokenDelegate() : called_(false), success_(false) {}
  ~TestStoreDMTokenDelegate() = default;

  void OnDMTokenUpdated(bool success) {
    run_loop_.Quit();
    called_ = true;
    success_ = success;
  }

  bool WasCalled() {
    bool was_called = called_;
    called_ = false;
    return was_called;
  }

  bool success() { return success_; }

  void Wait() { run_loop_.Run(); }

 private:
  bool called_;
  bool success_;
  base::RunLoop run_loop_;
};

TEST_F(BrowserDMTokenStorageLinuxTest, SaveDMToken) {
  TestStoreDMTokenDelegate callback_delegate;
  std::unique_ptr<base::ScopedPathOverride> path_override;
  base::ScopedTempDir fake_user_data_dir;

  ASSERT_TRUE(fake_user_data_dir.CreateUniqueTempDir());
  path_override = std::make_unique<base::ScopedPathOverride>(
      chrome::DIR_USER_DATA, fake_user_data_dir.GetPath());

  MockBrowserDMTokenStorageLinux storage_delegate;
  auto task = storage_delegate.SaveDMTokenTask(kDMToken,
                                               storage_delegate.InitClientId());
  auto reply = base::BindOnce(&TestStoreDMTokenDelegate::OnDMTokenUpdated,
                              base::Unretained(&callback_delegate));
  storage_delegate.SaveDMTokenTaskRunner()->PostTaskAndReplyWithResult(
      FROM_HERE, std::move(task), std::move(reply));

  callback_delegate.Wait();
  ASSERT_TRUE(callback_delegate.WasCalled());
  ASSERT_TRUE(callback_delegate.success());

  base::FilePath dir_user_data_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_USER_DATA, &dir_user_data_path));
  base::FilePath dm_token_dir_path = dir_user_data_path.Append(kDmTokenBaseDir);
  base::FilePath dm_token_file_path =
      dm_token_dir_path.Append(kExpectedClientId);

  std::string dm_token;
  ASSERT_TRUE(base::ReadFileToString(dm_token_file_path, &dm_token));
  EXPECT_EQ(kDMToken, dm_token);
}

TEST_F(BrowserDMTokenStorageLinuxTest, DeleteDMToken) {
  std::unique_ptr<base::ScopedPathOverride> path_override;
  base::ScopedTempDir fake_user_data_dir;

  ASSERT_TRUE(fake_user_data_dir.CreateUniqueTempDir());
  path_override = std::make_unique<base::ScopedPathOverride>(
      chrome::DIR_USER_DATA, fake_user_data_dir.GetPath());

  // Creating the DMToken file.
  base::FilePath dir_user_data_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_USER_DATA, &dir_user_data_path));
  base::FilePath dm_token_dir_path = dir_user_data_path.Append(kDmTokenBaseDir);
  ASSERT_TRUE(base::CreateDirectory(dm_token_dir_path));

  base::FilePath dm_token_file_path =
      dm_token_dir_path.Append(kExpectedClientId);
  ASSERT_TRUE(base::WriteFile(base::FilePath(dm_token_file_path), kDMToken));
  ASSERT_TRUE(base::PathExists(dm_token_file_path));

  // Deleting the saved DMToken.
  MockBrowserDMTokenStorageLinux storage_delegate;
  TestStoreDMTokenDelegate delete_callback_delegate;
  auto delete_task =
      storage_delegate.DeleteDMTokenTask(storage_delegate.InitClientId());
  auto delete_reply =
      base::BindOnce(&TestStoreDMTokenDelegate::OnDMTokenUpdated,
                     base::Unretained(&delete_callback_delegate));
  storage_delegate.SaveDMTokenTaskRunner()->PostTaskAndReplyWithResult(
      FROM_HERE, std::move(delete_task), std::move(delete_reply));

  delete_callback_delegate.Wait();
  ASSERT_TRUE(delete_callback_delegate.WasCalled());
  ASSERT_TRUE(delete_callback_delegate.success());

  ASSERT_FALSE(base::PathExists(dm_token_file_path));
}

TEST_F(BrowserDMTokenStorageLinuxTest, DeleteEmptyDMToken) {
  std::unique_ptr<base::ScopedPathOverride> path_override;
  base::ScopedTempDir fake_user_data_dir;

  ASSERT_TRUE(fake_user_data_dir.CreateUniqueTempDir());
  path_override = std::make_unique<base::ScopedPathOverride>(
      chrome::DIR_USER_DATA, fake_user_data_dir.GetPath());

  base::FilePath dir_user_data_path;
  ASSERT_TRUE(
      base::PathService::Get(chrome::DIR_USER_DATA, &dir_user_data_path));
  base::FilePath dm_token_dir_path = dir_user_data_path.Append(kDmTokenBaseDir);
  base::FilePath dm_token_file_path =
      dm_token_dir_path.Append(kExpectedClientId);
  ASSERT_FALSE(base::PathExists(dm_token_file_path));

  MockBrowserDMTokenStorageLinux storage_delegate;
  TestStoreDMTokenDelegate callback_delegate;
  auto delete_task =
      storage_delegate.DeleteDMTokenTask(storage_delegate.InitClientId());
  auto delete_reply =
      base::BindOnce(&TestStoreDMTokenDelegate::OnDMTokenUpdated,
                     base::Unretained(&callback_delegate));
  storage_delegate.SaveDMTokenTaskRunner()->PostTaskAndReplyWithResult(
      FROM_HERE, std::move(delete_task), std::move(delete_reply));

  callback_delegate.Wait();
  ASSERT_TRUE(callback_delegate.WasCalled());
  ASSERT_TRUE(callback_delegate.success());

  ASSERT_FALSE(base::PathExists(dm_token_file_path));
}

}  // namespace policy