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.
5 #include "remoting/host/pairing_registry_delegate_win.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"
17 using protocol::PairingRegistry
;
19 class PairingRegistryDelegateWinTest
: public testing::Test
{
21 virtual void SetUp() override
{
22 key_name_
= base::GenerateGUID();
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
);
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
);
35 virtual void TearDown() override
{
37 unprivileged_
.Close();
39 SHDeleteKey(HKEY_CURRENT_USER
,
40 base::UTF8ToWide(key_name_
).c_str()) == ERROR_SUCCESS
);
44 std::string key_name_
;
45 base::win::RegKey privileged_
;
46 base::win::RegKey unprivileged_
;
49 TEST_F(PairingRegistryDelegateWinTest
, SaveAndLoad
) {
50 scoped_ptr
<PairingRegistryDelegateWin
> delegate(
51 new PairingRegistryDelegateWin());
52 delegate
->SetRootKeys(privileged_
.Handle(), unprivileged_
.Handle());
54 // Check that registry is initially empty.
55 EXPECT_TRUE(delegate
->LoadAll()->empty());
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
));
63 // Verify that there are two pairings in the store now.
64 EXPECT_EQ(delegate
->LoadAll()->GetSize(), 2u);
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
);
70 // Delete the first pairing.
71 EXPECT_TRUE(delegate
->Delete(pairing1
.client_id()));
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
);
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
);
84 // Delete the rest and verify.
85 EXPECT_TRUE(delegate
->DeleteAll());
86 EXPECT_TRUE(delegate
->LoadAll()->empty());
89 // Verifies that the delegate is stateless by using two different instances.
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());
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
);
103 TEST_F(PairingRegistryDelegateWinTest
, Unprivileged
) {
104 scoped_ptr
<PairingRegistryDelegateWin
> delegate(
105 new PairingRegistryDelegateWin());
106 delegate
->SetRootKeys(privileged_
.Handle(), unprivileged_
.Handle());
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
);
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(nullptr, unprivileged_
.Handle());
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());
123 // Verify that the shared secret if not available.
124 EXPECT_TRUE(unprivileged_pairing
.shared_secret().empty());
126 // Verify that a pairing cannot be saved.
127 EXPECT_FALSE(delegate
->Save(pairing
));
130 } // namespace remoting