1 // Copyright 2013 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 "remoting/host/pairing_registry_delegate_win.h" 6 7 #include <shlwapi.h> 8 9 #include "base/guid.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "base/values.h" 12 #include "base/win/registry.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace remoting { 16 17 using protocol::PairingRegistry; 18 19 class PairingRegistryDelegateWinTest : public testing::Test { 20 public: 21 virtual void SetUp() OVERRIDE { 22 key_name_ = base::GenerateGUID(); 23 24 base::win::RegKey root; 25 EXPECT_TRUE(root.Create(HKEY_CURRENT_USER, UTF8ToWide(key_name_).c_str(), 26 KEY_READ | KEY_WRITE) == ERROR_SUCCESS); 27 28 EXPECT_TRUE(privileged_.Create(root.Handle(), L"privileged", 29 KEY_READ | KEY_WRITE) == ERROR_SUCCESS); 30 EXPECT_TRUE(unprivileged_.Create(root.Handle(), L"unprivileged", 31 KEY_READ | KEY_WRITE) == ERROR_SUCCESS); 32 } 33 34 virtual void TearDown() OVERRIDE { 35 privileged_.Close(); 36 unprivileged_.Close(); 37 EXPECT_TRUE(SHDeleteKey(HKEY_CURRENT_USER, 38 UTF8ToWide(key_name_).c_str()) == ERROR_SUCCESS); 39 } 40 41 protected: 42 std::string key_name_; 43 base::win::RegKey privileged_; 44 base::win::RegKey unprivileged_; 45 }; 46 47 TEST_F(PairingRegistryDelegateWinTest, SaveAndLoad) { 48 scoped_ptr<PairingRegistryDelegateWin> delegate( 49 new PairingRegistryDelegateWin()); 50 delegate->SetRootKeys(privileged_.Handle(), unprivileged_.Handle()); 51 52 // Check that registry is initially empty. 53 EXPECT_TRUE(delegate->LoadAll()->empty()); 54 55 // Add a couple of pairings. 56 PairingRegistry::Pairing pairing1(base::Time::Now(), "xxx", "xxx", "xxx"); 57 PairingRegistry::Pairing pairing2(base::Time::Now(), "yyy", "yyy", "yyy"); 58 EXPECT_TRUE(delegate->Save(pairing1)); 59 EXPECT_TRUE(delegate->Save(pairing2)); 60 61 // Verify that there are two pairings in the store now. 62 EXPECT_EQ(delegate->LoadAll()->GetSize(), 2u); 63 64 // Verify that they can be retrieved. 65 EXPECT_EQ(delegate->Load(pairing1.client_id()), pairing1); 66 EXPECT_EQ(delegate->Load(pairing2.client_id()), pairing2); 67 68 // Delete the first pairing. 69 EXPECT_TRUE(delegate->Delete(pairing1.client_id())); 70 71 // Verify that there is only one pairing left. 72 EXPECT_EQ(delegate->Load(pairing1.client_id()), PairingRegistry::Pairing()); 73 EXPECT_EQ(delegate->Load(pairing2.client_id()), pairing2); 74 75 // Verify that the only remaining value is |pairing2|. 76 EXPECT_EQ(delegate->LoadAll()->GetSize(), 1u); 77 scoped_ptr<base::ListValue> pairings = delegate->LoadAll(); 78 base::DictionaryValue* json; 79 EXPECT_TRUE(pairings->GetDictionary(0, &json)); 80 EXPECT_EQ(PairingRegistry::Pairing::CreateFromValue(*json), pairing2); 81 82 // Delete the rest and verify. 83 EXPECT_TRUE(delegate->DeleteAll()); 84 EXPECT_TRUE(delegate->LoadAll()->empty()); 85 } 86 87 // Verifies that the delegate is stateless by using two different instances. 88 TEST_F(PairingRegistryDelegateWinTest, Stateless) { 89 scoped_ptr<PairingRegistryDelegateWin> load_delegate( 90 new PairingRegistryDelegateWin()); 91 load_delegate->SetRootKeys(privileged_.Handle(), unprivileged_.Handle()); 92 scoped_ptr<PairingRegistryDelegateWin> save_delegate( 93 new PairingRegistryDelegateWin()); 94 save_delegate->SetRootKeys(privileged_.Handle(), unprivileged_.Handle()); 95 96 PairingRegistry::Pairing pairing(base::Time::Now(), "xxx", "xxx", "xxx"); 97 EXPECT_TRUE(save_delegate->Save(pairing)); 98 EXPECT_EQ(load_delegate->Load(pairing.client_id()), pairing); 99 } 100 101 TEST_F(PairingRegistryDelegateWinTest, Unprivileged) { 102 scoped_ptr<PairingRegistryDelegateWin> delegate( 103 new PairingRegistryDelegateWin()); 104 delegate->SetRootKeys(privileged_.Handle(), unprivileged_.Handle()); 105 106 PairingRegistry::Pairing pairing(base::Time::Now(), "xxx", "xxx", "xxx"); 107 EXPECT_TRUE(delegate->Save(pairing)); 108 EXPECT_EQ(delegate->Load(pairing.client_id()), pairing); 109 110 // Strip the delegate from write access and validate that it still can be used 111 // to read the pairings. 112 delegate.reset(new PairingRegistryDelegateWin()); 113 delegate->SetRootKeys(NULL, unprivileged_.Handle()); 114 115 PairingRegistry::Pairing unprivileged_pairing = 116 delegate->Load(pairing.client_id()); 117 EXPECT_EQ(pairing.client_id(), unprivileged_pairing.client_id()); 118 EXPECT_EQ(pairing.client_name(), unprivileged_pairing.client_name()); 119 EXPECT_EQ(pairing.created_time(), unprivileged_pairing.created_time()); 120 121 // Verify that the shared secret if not available. 122 EXPECT_TRUE(unprivileged_pairing.shared_secret().empty()); 123 124 // Verify that a pairing cannot be saved. 125 EXPECT_FALSE(delegate->Save(pairing)); 126 } 127 128 } // namespace remoting 129