File: bulk_printers_calculator_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 (312 lines) | stat: -rw-r--r-- 11,434 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
// Copyright 2017 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/printing/enterprise/bulk_printers_calculator.h"

#include <string>
#include <vector>

#include "base/functional/bind.h"
#include "base/test/task_environment.h"
#include "chromeos/printing/printer_configuration.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {
namespace {

// The number of correct printers in BulkPolicyContentsJson.
constexpr size_t kNumValidPrinters = 3;

// An example bulk printer configuration file.
constexpr char kBulkPolicyContentsJson[] = R"json(
[
  {
    "guid": "First",
    "display_name": "LexaPrint",
    "description": "Laser on the test shelf",
    "uri": "ipp://192.168.1.5",
    "ppd_resource": {
      "effective_model": "MS610de"
    }
  }, {
    "guid": "Incorrect uri",
    "display_name": "aaa",
    "description": "bbbb",
    "uri":"ipp://:",
    "ppd_resource":{
      "effective_model": "fff"
    }
  }, {
    "guid": "Second",
    "display_name": "Color Laser",
    "uri":"ipps://print-server.intranet.example.com:443/ipp/cl2k4",
    "ppd_resource":{
      "effective_model": "ColorLaser2k4"
    }
  }, {
    "guid": "Third",
    "display_name": "YaLP",
    "description": "Fancy Fancy Fancy",
    "uri": "ipp://192.168.1.8",
    "ppd_resource": {
      "autoconf": true
    }
  }
])json";

// A different bulk printer configuration file.
constexpr char kMoreContentsJson[] = R"json(
[
  {
    "guid": "ThirdPrime",
    "display_name": "Printy McPrinter",
    "description": "Laser on the test shelf",
    "uri": "ipp://192.168.1.5",
    "ppd_resource": {
      "effective_model": "MS610de"
    }
  }
])json";

// Observer that counts the number of times it has been called.
class TestObserver : public BulkPrintersCalculator::Observer {
 public:
  void OnPrintersChanged(const BulkPrintersCalculator* sender) override {
    last_valid = sender->IsComplete();
    called++;
  }

  // Counts the number of times the observer is invoked.
  int called = 0;
  // Holds the most recent value of valid.
  bool last_valid = false;
};

class BulkPrintersCalculatorTest : public testing::Test {
 public:
  BulkPrintersCalculatorTest() : task_environment_() {
    external_printers_ = BulkPrintersCalculator::Create();
  }
  ~BulkPrintersCalculatorTest() override {
    // Delete the printer before the task environment.
    external_printers_.reset();
  }

 protected:
  std::unique_ptr<BulkPrintersCalculator> external_printers_;
  base::test::TaskEnvironment task_environment_;
};

// Verify that we're initiall unset and empty.
TEST_F(BulkPrintersCalculatorTest, InitialConditions) {
  EXPECT_FALSE(external_printers_->IsDataPolicySet());
  EXPECT_TRUE(external_printers_->GetPrinters().empty());
}

// Verify that the object can be destroyed while parsing is in progress.
TEST_F(BulkPrintersCalculatorTest, DestructionIsSafe) {
  {
    std::unique_ptr<BulkPrintersCalculator> printers =
        BulkPrintersCalculator::Create();
    printers->SetAccessMode(BulkPrintersCalculator::BLOCKLIST_ONLY);
    printers->SetBlocklist({"Third"});
    printers->SetData(std::make_unique<std::string>(kBulkPolicyContentsJson));
    // Data is valid.  Computation is proceeding.
  }
  // printers is out of scope.  Destructor has run.  Pump the message queue to
  // see if anything strange happens.
  task_environment_.RunUntilIdle();
}

// Verifies that IsDataPolicySet returns false until data is set.
TEST_F(BulkPrintersCalculatorTest, PolicyUnsetWithMissingData) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  EXPECT_FALSE(external_printers_->IsDataPolicySet());
  external_printers_->SetData(std::move(data));
  EXPECT_TRUE(external_printers_->IsDataPolicySet());
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());
}

// Verify printer list after all attributes have been set.
TEST_F(BulkPrintersCalculatorTest, AllPoliciesResultInPrinters) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->SetAccessMode(
      BulkPrintersCalculator::AccessMode::ALL_ACCESS);
  external_printers_->SetData(std::move(data));
  EXPECT_TRUE(external_printers_->IsDataPolicySet());

  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());
  const auto& printers = external_printers_->GetPrinters();
  EXPECT_EQ(kNumValidPrinters, printers.size());
  EXPECT_EQ("LexaPrint", printers.at("First").display_name());
  EXPECT_EQ("Color Laser", printers.at("Second").display_name());
  EXPECT_EQ("YaLP", printers.at("Third").display_name());
}

// The external policy was cleared, results should be invalidated.
TEST_F(BulkPrintersCalculatorTest, PolicyClearedNowUnset) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->SetAccessMode(
      BulkPrintersCalculator::AccessMode::ALL_ACCESS);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));

  task_environment_.RunUntilIdle();
  ASSERT_TRUE(external_printers_->IsDataPolicySet());

  external_printers_->ClearData();
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(external_printers_->IsDataPolicySet());
  EXPECT_TRUE(external_printers_->GetPrinters().empty());
}

// Verify that the blocklist policy is applied correctly.  Printers in the
// blocklist policy should not be available.  Printers not in the blocklist
// should be available.
TEST_F(BulkPrintersCalculatorTest, BlocklistPolicySet) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));
  external_printers_->SetAccessMode(BulkPrintersCalculator::BLOCKLIST_ONLY);
  task_environment_.RunUntilIdle();
  external_printers_->SetBlocklist({"Second", "Third"});
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());

  task_environment_.RunUntilIdle();
  const auto& printers = external_printers_->GetPrinters();
  EXPECT_EQ(1U, printers.size());
  EXPECT_EQ("LexaPrint", printers.at("First").display_name());
}

// Verify that the allowlist policy is correctly applied.  Only printers
// available in the allowlist are available.
TEST_F(BulkPrintersCalculatorTest, AllowlistPolicySet) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));
  external_printers_->SetAccessMode(BulkPrintersCalculator::ALLOWLIST_ONLY);
  task_environment_.RunUntilIdle();
  external_printers_->SetAllowlist({"First"});

  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());
  const auto& printers = external_printers_->GetPrinters();
  EXPECT_EQ(1U, printers.size());
  EXPECT_EQ("LexaPrint", printers.at("First").display_name());
}

// Verify that an empty blocklist results in no printer limits.
TEST_F(BulkPrintersCalculatorTest, EmptyBlocklistAllPrinters) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));
  external_printers_->SetAccessMode(BulkPrintersCalculator::BLOCKLIST_ONLY);
  task_environment_.RunUntilIdle();
  external_printers_->SetBlocklist({});

  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());
  const auto& printers = external_printers_->GetPrinters();
  EXPECT_EQ(kNumValidPrinters, printers.size());
}

// Verify that an empty allowlist results in no printers.
TEST_F(BulkPrintersCalculatorTest, EmptyAllowlistNoPrinters) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));
  external_printers_->SetAccessMode(BulkPrintersCalculator::ALLOWLIST_ONLY);
  task_environment_.RunUntilIdle();
  external_printers_->SetAllowlist({});

  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());
  const auto& printers = external_printers_->GetPrinters();
  EXPECT_EQ(0U, printers.size());
}

// Verify that switching from allowlist to blocklist behaves correctly.
TEST_F(BulkPrintersCalculatorTest, BlocklistToAllowlistSwap) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));
  external_printers_->SetAccessMode(BulkPrintersCalculator::BLOCKLIST_ONLY);
  external_printers_->SetAllowlist({"First"});
  external_printers_->SetBlocklist({"First"});

  // This should result in 2 printers.  But we're switching the mode anyway.

  external_printers_->SetAccessMode(BulkPrintersCalculator::ALLOWLIST_ONLY);
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsComplete());
  const auto& printers = external_printers_->GetPrinters();
  EXPECT_EQ(1U, printers.size());
  EXPECT_EQ("LexaPrint", printers.at("First").display_name());
}

// Verify that updated configurations are handled properly.
TEST_F(BulkPrintersCalculatorTest, MultipleUpdates) {
  auto data = std::make_unique<std::string>(kBulkPolicyContentsJson);
  external_printers_->ClearData();
  external_printers_->SetData(std::move(data));
  external_printers_->SetAccessMode(BulkPrintersCalculator::ALL_ACCESS);
  // There will be 3 printers here.  But we don't want to wait for computation
  // to complete to verify the final value gets used.

  auto new_data = std::make_unique<std::string>(kMoreContentsJson);
  external_printers_->SetData(std::move(new_data));
  task_environment_.RunUntilIdle();
  const auto& printers = external_printers_->GetPrinters();
  ASSERT_EQ(1U, printers.size());
  EXPECT_EQ("ThirdPrime", printers.at("ThirdPrime").id());
}

// Verifies that the observer is called at the expected times.
TEST_F(BulkPrintersCalculatorTest, ObserverTest) {
  TestObserver obs;
  external_printers_->AddObserver(&obs);

  external_printers_->SetAccessMode(BulkPrintersCalculator::ALL_ACCESS);
  external_printers_->SetAllowlist(std::vector<std::string>());
  external_printers_->SetBlocklist(std::vector<std::string>());
  external_printers_->ClearData();
  task_environment_.RunUntilIdle();
  EXPECT_EQ(1, obs.called);

  external_printers_->SetData(
      std::make_unique<std::string>(kBulkPolicyContentsJson));

  task_environment_.RunUntilIdle();
  EXPECT_TRUE(external_printers_->IsDataPolicySet());
  EXPECT_EQ(2, obs.called);
  EXPECT_TRUE(obs.last_valid);  // ready now
  // Printer list is correct after notification.
  EXPECT_EQ(kNumValidPrinters, external_printers_->GetPrinters().size());

  external_printers_->SetAccessMode(BulkPrintersCalculator::ALLOWLIST_ONLY);
  task_environment_.RunUntilIdle();
  EXPECT_EQ(3, obs.called);  // effective list changed.  Notified.
  EXPECT_TRUE(obs.last_valid);

  external_printers_->SetAccessMode(BulkPrintersCalculator::BLOCKLIST_ONLY);
  task_environment_.RunUntilIdle();
  EXPECT_EQ(4, obs.called);  // effective list changed.  Notified.
  EXPECT_TRUE(obs.last_valid);

  external_printers_->ClearData();
  task_environment_.RunUntilIdle();
  EXPECT_EQ(5, obs.called);  // Called for transition to invalid policy.
  EXPECT_TRUE(obs.last_valid);
  EXPECT_TRUE(external_printers_->GetPrinters().empty());

  // cleanup
  external_printers_->RemoveObserver(&obs);
}

}  // namespace
}  // namespace ash