forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_hive_visitor_unittest.cc
69 lines (50 loc) · 1.89 KB
/
user_hive_visitor_unittest.cc
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
// Copyright 2016 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/installer/setup/user_hive_visitor.h"
#include <string>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/win/registry.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace installer {
namespace {
class UserHiveVisitor {
public:
UserHiveVisitor() = default;
UserHiveVisitor(const UserHiveVisitor&) = delete;
UserHiveVisitor& operator=(const UserHiveVisitor&) = delete;
bool OnUserHive(const wchar_t* sid, base::win::RegKey* key) {
EXPECT_NE(nullptr, sid);
EXPECT_STRNE(L"", sid);
EXPECT_NE(nullptr, key);
EXPECT_TRUE(key->Valid());
sids_visited_.push_back(sid);
return !early_exit_;
}
void set_early_exit(bool early_exit) { early_exit_ = early_exit; }
const std::vector<std::wstring> sids_visited() const { return sids_visited_; }
private:
std::vector<std::wstring> sids_visited_;
bool early_exit_ = false;
};
} // namespace
// Tests that the visitor visits at least one user hive. This will succeed even
// when run as an ordinary user, as the current user's hive is always available.
TEST(UserHiveVisitorTest, VisitAllUserHives) {
UserHiveVisitor visitor;
VisitUserHives(base::BindRepeating(&UserHiveVisitor::OnUserHive,
base::Unretained(&visitor)));
EXPECT_GT(visitor.sids_visited().size(), 0U);
}
// Tests that only one user hive is visited when the visitor returns false to
// stop the iteration.
TEST(UserHiveVisitor, VisitOneHive) {
UserHiveVisitor visitor;
visitor.set_early_exit(true);
VisitUserHives(base::BindRepeating(&UserHiveVisitor::OnUserHive,
base::Unretained(&visitor)));
EXPECT_EQ(1U, visitor.sids_visited().size());
}
} // namespace installer