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