OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/setup/setup_util_unittest.h" | 5 #include "chrome/installer/setup/setup_util_unittest.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 } | 509 } |
510 | 510 |
511 TEST(SetupUtilTest, GetRegistrationDataCommandKey) { | 511 TEST(SetupUtilTest, GetRegistrationDataCommandKey) { |
512 base::string16 app_guid = L"{AAAAAAAA-BBBB-1111-0123-456789ABCDEF}"; | 512 base::string16 app_guid = L"{AAAAAAAA-BBBB-1111-0123-456789ABCDEF}"; |
513 UpdatingAppRegistrationData reg_data(app_guid); | 513 UpdatingAppRegistrationData reg_data(app_guid); |
514 base::string16 key = | 514 base::string16 key = |
515 installer::GetRegistrationDataCommandKey(reg_data, L"test_name"); | 515 installer::GetRegistrationDataCommandKey(reg_data, L"test_name"); |
516 EXPECT_TRUE(base::EndsWith(key, app_guid + L"\\Commands\\test_name", | 516 EXPECT_TRUE(base::EndsWith(key, app_guid + L"\\Commands\\test_name", |
517 base::CompareCase::SENSITIVE)); | 517 base::CompareCase::SENSITIVE)); |
518 } | 518 } |
| 519 |
| 520 namespace installer { |
| 521 |
| 522 class DeleteRegistryKeyPartialTest : public ::testing::Test { |
| 523 protected: |
| 524 using RegKey = base::win::RegKey; |
| 525 |
| 526 void SetUp() override { |
| 527 _registry_override_manager.OverrideRegistry(root_); |
| 528 to_preserve_.push_back(L"preSERve1"); |
| 529 to_preserve_.push_back(L"1evRESerp"); |
| 530 } |
| 531 |
| 532 void CreateSubKeys(bool with_preserves) { |
| 533 ASSERT_FALSE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 534 // These subkeys are added such that 1) keys to preserve are intermixed with |
| 535 // other keys, and 2) the case of the keys to preserve doesn't match the |
| 536 // values in |to_preserve_|. |
| 537 ASSERT_EQ(ERROR_SUCCESS, RegKey(root_, path_.c_str(), KEY_WRITE) |
| 538 .CreateKey(L"0sub", KEY_WRITE)); |
| 539 if (with_preserves) { |
| 540 ASSERT_EQ(ERROR_SUCCESS, RegKey(root_, path_.c_str(), KEY_WRITE) |
| 541 .CreateKey(L"1evreserp", KEY_WRITE)); |
| 542 } |
| 543 ASSERT_EQ(ERROR_SUCCESS, RegKey(root_, path_.c_str(), KEY_WRITE) |
| 544 .CreateKey(L"asub", KEY_WRITE)); |
| 545 if (with_preserves) { |
| 546 ASSERT_EQ(ERROR_SUCCESS, RegKey(root_, path_.c_str(), KEY_WRITE) |
| 547 .CreateKey(L"preserve1", KEY_WRITE)); |
| 548 } |
| 549 ASSERT_EQ(ERROR_SUCCESS, RegKey(root_, path_.c_str(), KEY_WRITE) |
| 550 .CreateKey(L"sub1", KEY_WRITE)); |
| 551 } |
| 552 |
| 553 const HKEY root_ = HKEY_CURRENT_USER; |
| 554 base::string16 path_ = L"key_path"; |
| 555 std::vector<base::string16> to_preserve_; |
| 556 |
| 557 private: |
| 558 registry_util::RegistryOverrideManager _registry_override_manager; |
| 559 }; |
| 560 |
| 561 TEST_F(DeleteRegistryKeyPartialTest, NoKey) { |
| 562 DeleteRegistryKeyPartial(root_, L"does_not_exist", |
| 563 std::vector<base::string16>()); |
| 564 DeleteRegistryKeyPartial(root_, L"does_not_exist", to_preserve_); |
| 565 } |
| 566 |
| 567 TEST_F(DeleteRegistryKeyPartialTest, EmptyKey) { |
| 568 ASSERT_FALSE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 569 ASSERT_TRUE(RegKey(root_, path_.c_str(), KEY_WRITE).Valid()); |
| 570 DeleteRegistryKeyPartial(root_, path_.c_str(), std::vector<base::string16>()); |
| 571 ASSERT_FALSE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 572 |
| 573 ASSERT_TRUE(RegKey(root_, path_.c_str(), KEY_WRITE).Valid()); |
| 574 DeleteRegistryKeyPartial(root_, path_.c_str(), to_preserve_); |
| 575 ASSERT_FALSE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 576 } |
| 577 |
| 578 TEST_F(DeleteRegistryKeyPartialTest, NonEmptyKey) { |
| 579 CreateSubKeys(false); /* !with_preserves */ |
| 580 DeleteRegistryKeyPartial(root_, path_.c_str(), std::vector<base::string16>()); |
| 581 ASSERT_FALSE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 582 |
| 583 CreateSubKeys(false); /* !with_preserves */ |
| 584 ASSERT_TRUE(RegKey(root_, path_.c_str(), KEY_WRITE).Valid()); |
| 585 DeleteRegistryKeyPartial(root_, path_.c_str(), to_preserve_); |
| 586 ASSERT_FALSE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 587 } |
| 588 |
| 589 TEST_F(DeleteRegistryKeyPartialTest, NonEmptyKeyWithPreserve) { |
| 590 CreateSubKeys(true); /* with_preserves */ |
| 591 |
| 592 // Put some values into the main key. |
| 593 { |
| 594 RegKey key(root_, path_.c_str(), KEY_SET_VALUE); |
| 595 ASSERT_TRUE(key.Valid()); |
| 596 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(nullptr, 5U)); |
| 597 ASSERT_EQ( |
| 598 1, base::win::RegistryValueIterator(root_, path_.c_str()).ValueCount()); |
| 599 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(L"foo", L"bar")); |
| 600 ASSERT_EQ( |
| 601 2, base::win::RegistryValueIterator(root_, path_.c_str()).ValueCount()); |
| 602 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(L"baz", L"huh")); |
| 603 ASSERT_EQ( |
| 604 3, base::win::RegistryValueIterator(root_, path_.c_str()).ValueCount()); |
| 605 } |
| 606 |
| 607 ASSERT_TRUE(RegKey(root_, path_.c_str(), KEY_WRITE).Valid()); |
| 608 DeleteRegistryKeyPartial(root_, path_.c_str(), to_preserve_); |
| 609 ASSERT_TRUE(RegKey(root_, path_.c_str(), KEY_READ).Valid()); |
| 610 |
| 611 // Ensure that the preserved subkeys are still present. |
| 612 { |
| 613 base::win::RegistryKeyIterator it(root_, path_.c_str()); |
| 614 ASSERT_EQ(to_preserve_.size(), it.SubkeyCount()); |
| 615 for (it; it.Valid(); ++it) { |
| 616 ASSERT_NE(to_preserve_.end(), |
| 617 std::find_if(to_preserve_.begin(), to_preserve_.end(), |
| 618 [&it](const base::string16& key_name) { |
| 619 return base::ToLowerASCII(it.Name()) == |
| 620 base::ToLowerASCII(key_name); |
| 621 })) |
| 622 << it.Name(); |
| 623 } |
| 624 } |
| 625 |
| 626 // Ensure that all values are absent. |
| 627 { |
| 628 base::win::RegistryValueIterator it(root_, path_.c_str()); |
| 629 ASSERT_EQ(0, it.ValueCount()); |
| 630 } |
| 631 } |
| 632 |
| 633 } // namespace installer |
OLD | NEW |