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