OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/setup/user_hive_visitor.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/strings/string16.h" |
| 12 #include "base/win/registry.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace installer { |
| 16 |
| 17 namespace { |
| 18 |
| 19 class UserHiveVisitor { |
| 20 public: |
| 21 UserHiveVisitor() = default; |
| 22 |
| 23 bool OnUserHive(const wchar_t* sid, base::win::RegKey* key) { |
| 24 EXPECT_NE(nullptr, sid); |
| 25 EXPECT_STRNE(L"", sid); |
| 26 EXPECT_NE(nullptr, key); |
| 27 EXPECT_TRUE(key->Valid()); |
| 28 sids_visited_.push_back(sid); |
| 29 return !early_exit_; |
| 30 } |
| 31 |
| 32 void set_early_exit(bool early_exit) { early_exit_ = early_exit; } |
| 33 |
| 34 const std::vector<base::string16> sids_visited() const { |
| 35 return sids_visited_; |
| 36 } |
| 37 |
| 38 private: |
| 39 std::vector<base::string16> sids_visited_; |
| 40 bool early_exit_ = false; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(UserHiveVisitor); |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
| 47 // Tests that the visitor visits at least one user hive. This will succeed even |
| 48 // when run as an ordinary user, as the current user's hive is always available. |
| 49 TEST(UserHiveVisitorTest, VisitAllUserHives) { |
| 50 UserHiveVisitor visitor; |
| 51 |
| 52 VisitUserHives( |
| 53 base::Bind(&UserHiveVisitor::OnUserHive, base::Unretained(&visitor))); |
| 54 |
| 55 EXPECT_GT(visitor.sids_visited().size(), 0U); |
| 56 } |
| 57 |
| 58 // Tests that only one user hive is visited when the visitor returns false to |
| 59 // stop the iteration. |
| 60 TEST(UserHiveVisitor, VisitOneHive) { |
| 61 UserHiveVisitor visitor; |
| 62 |
| 63 visitor.set_early_exit(true); |
| 64 VisitUserHives( |
| 65 base::Bind(&UserHiveVisitor::OnUserHive, base::Unretained(&visitor))); |
| 66 |
| 67 EXPECT_EQ(1U, visitor.sids_visited().size()); |
| 68 } |
| 69 |
| 70 } // namespace installer |
OLD | NEW |