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_linux.h"
7 #include "base/files/file_util.h"
8 #include "base/values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 using protocol::PairingRegistry
;
15 class PairingRegistryDelegateLinuxTest
: public testing::Test
{
17 void SetUp() override
{
18 // Create a temporary directory in order to get a unique name and use a
19 // subdirectory to ensure that PairingRegistryDelegateLinux::Save() creates
20 // the parent directory if it doesn't exist.
21 base::CreateNewTempDirectory("chromoting-test", &temp_dir_
);
22 temp_registry_
= temp_dir_
.Append("paired-clients");
25 void TearDown() override
{ base::DeleteFile(temp_dir_
, true); }
28 base::FilePath temp_dir_
;
29 base::FilePath temp_registry_
;
32 TEST_F(PairingRegistryDelegateLinuxTest
, SaveAndLoad
) {
33 scoped_ptr
<PairingRegistryDelegateLinux
> delegate(
34 new PairingRegistryDelegateLinux());
35 delegate
->SetRegistryPathForTesting(temp_registry_
);
37 // Check that registry is initially empty.
38 EXPECT_TRUE(delegate
->LoadAll()->empty());
40 // Add a couple of pairings.
41 PairingRegistry::Pairing
pairing1(base::Time::Now(), "xxx", "xxx", "xxx");
42 PairingRegistry::Pairing
pairing2(base::Time::Now(), "yyy", "yyy", "yyy");
43 EXPECT_TRUE(delegate
->Save(pairing1
));
44 EXPECT_TRUE(delegate
->Save(pairing2
));
46 // Verify that there are two pairings in the store now.
47 EXPECT_EQ(delegate
->LoadAll()->GetSize(), 2u);
49 // Verify that they can be retrieved.
50 EXPECT_EQ(delegate
->Load(pairing1
.client_id()), pairing1
);
51 EXPECT_EQ(delegate
->Load(pairing2
.client_id()), pairing2
);
53 // Delete the first pairing.
54 EXPECT_TRUE(delegate
->Delete(pairing1
.client_id()));
56 // Verify that there is only one pairing left.
57 EXPECT_EQ(delegate
->Load(pairing1
.client_id()), PairingRegistry::Pairing());
58 EXPECT_EQ(delegate
->Load(pairing2
.client_id()), pairing2
);
60 // Verify that the only value that left is |pairing2|.
61 EXPECT_EQ(delegate
->LoadAll()->GetSize(), 1u);
62 scoped_ptr
<base::ListValue
> pairings
= delegate
->LoadAll();
63 base::DictionaryValue
* json
;
64 EXPECT_TRUE(pairings
->GetDictionary(0, &json
));
65 EXPECT_EQ(PairingRegistry::Pairing::CreateFromValue(*json
), pairing2
);
67 // Delete the rest and verify.
68 EXPECT_TRUE(delegate
->DeleteAll());
69 EXPECT_TRUE(delegate
->LoadAll()->empty());
72 // Verifies that the delegate is stateless by using two different instances.
73 TEST_F(PairingRegistryDelegateLinuxTest
, Stateless
) {
74 scoped_ptr
<PairingRegistryDelegateLinux
> save_delegate(
75 new PairingRegistryDelegateLinux());
76 scoped_ptr
<PairingRegistryDelegateLinux
> load_delegate(
77 new PairingRegistryDelegateLinux());
78 save_delegate
->SetRegistryPathForTesting(temp_registry_
);
79 load_delegate
->SetRegistryPathForTesting(temp_registry_
);
81 PairingRegistry::Pairing
pairing(base::Time::Now(), "xxx", "xxx", "xxx");
82 EXPECT_TRUE(save_delegate
->Save(pairing
));
83 EXPECT_EQ(load_delegate
->Load(pairing
.client_id()), pairing
);
86 } // namespace remoting