OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/pairing_registry_delegate_linux.h" | 5 #include "remoting/host/pairing_registry_delegate_linux.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/task_runner.h" | |
11 #include "base/thread_task_runner_handle.h" | |
12 #include "base/timer/timer.h" | 8 #include "base/timer/timer.h" |
| 9 #include "base/values.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
14 | 11 |
15 namespace remoting { | 12 namespace remoting { |
16 | 13 |
17 using protocol::PairingRegistry; | 14 using protocol::PairingRegistry; |
18 | 15 |
19 class PairingRegistryDelegateLinuxTest : public testing::Test { | 16 class PairingRegistryDelegateLinuxTest : public testing::Test { |
20 public: | 17 public: |
21 void SaveComplete(PairingRegistry::Delegate* delegate, | 18 virtual void SetUp() OVERRIDE { |
22 const std::string& expected_json, | 19 // Create a temporary directory in order to get a unique name and use a |
23 bool success) { | 20 // subdirectory to ensure that PairingRegistryDelegateLinux::Save() creates |
24 EXPECT_TRUE(success); | 21 // the parent directory if it doesn't exist. |
25 // Load the pairings again to make sure we get what we've just written. | 22 file_util::CreateNewTempDirectory("chromoting-test", &temp_dir_); |
26 delegate->Load( | 23 temp_registry_ = temp_dir_.Append("paired-clients"); |
27 base::Bind(&PairingRegistryDelegateLinuxTest::VerifyLoad, | |
28 base::Unretained(this), | |
29 expected_json)); | |
30 } | 24 } |
31 | 25 |
32 void VerifyLoad(const std::string& expected, | 26 virtual void TearDown() OVERRIDE { |
33 const std::string& actual) { | 27 base::DeleteFile(temp_dir_, true); |
34 EXPECT_EQ(actual, expected); | |
35 base::MessageLoop::current()->Quit(); | |
36 } | 28 } |
| 29 |
| 30 protected: |
| 31 base::FilePath temp_dir_; |
| 32 base::FilePath temp_registry_; |
37 }; | 33 }; |
38 | 34 |
39 TEST_F(PairingRegistryDelegateLinuxTest, SaveAndLoad) { | 35 TEST_F(PairingRegistryDelegateLinuxTest, SaveAndLoad) { |
40 base::MessageLoop message_loop; | 36 scoped_ptr<PairingRegistryDelegateLinux> delegate( |
41 base::RunLoop run_loop; | 37 new PairingRegistryDelegateLinux()); |
| 38 delegate->SetRegistryPathForTesting(temp_registry_); |
42 | 39 |
43 // Create a temporary directory in order to get a unique name and use a | 40 // Check that registry is initially empty. |
44 // subdirectory to ensure that the AddPairing method creates the parent | 41 EXPECT_TRUE(delegate->LoadAll()->empty()); |
45 // directory if it doesn't exist. | |
46 base::FilePath temp_dir; | |
47 file_util::CreateNewTempDirectory("chromoting-test", &temp_dir); | |
48 base::FilePath temp_file = temp_dir.Append("dir").Append("registry.json"); | |
49 | 42 |
50 scoped_refptr<base::TaskRunner> task_runner = | 43 // Add a couple of pairings. |
51 base::ThreadTaskRunnerHandle::Get(); | 44 PairingRegistry::Pairing pairing1(base::Time::Now(), "xxx", "xxx", "xxx"); |
| 45 PairingRegistry::Pairing pairing2(base::Time::Now(), "yyy", "yyy", "yyy"); |
| 46 EXPECT_TRUE(delegate->Save(pairing1)); |
| 47 EXPECT_TRUE(delegate->Save(pairing2)); |
| 48 |
| 49 // Verify that there are two pairings in the store now. |
| 50 EXPECT_EQ(delegate->LoadAll()->GetSize(), 2u); |
| 51 |
| 52 // Verify that they can be retrieved. |
| 53 EXPECT_EQ(delegate->Load(pairing1.client_id()), pairing1); |
| 54 EXPECT_EQ(delegate->Load(pairing2.client_id()), pairing2); |
| 55 |
| 56 // Delete the first pairing. |
| 57 EXPECT_TRUE(delegate->Delete(pairing1.client_id())); |
| 58 |
| 59 // Verify that there is only one pairing left. |
| 60 EXPECT_EQ(delegate->Load(pairing1.client_id()), PairingRegistry::Pairing()); |
| 61 EXPECT_EQ(delegate->Load(pairing2.client_id()), pairing2); |
| 62 |
| 63 // Verify that the only value that left is |pairing2|. |
| 64 EXPECT_EQ(delegate->LoadAll()->GetSize(), 1u); |
| 65 scoped_ptr<base::ListValue> pairings = delegate->LoadAll(); |
| 66 base::Value* json; |
| 67 EXPECT_TRUE(pairings->Get(0, &json)); |
| 68 EXPECT_EQ(PairingRegistry::Pairing::CreateFromValue(*json), pairing2); |
| 69 |
| 70 // Delete the rest and verify. |
| 71 EXPECT_TRUE(delegate->DeleteAll()); |
| 72 EXPECT_TRUE(delegate->LoadAll()->empty()); |
| 73 } |
| 74 |
| 75 // Verifies that the delegate is stateless by using two different instances. |
| 76 TEST_F(PairingRegistryDelegateLinuxTest, Stateless) { |
52 scoped_ptr<PairingRegistryDelegateLinux> save_delegate( | 77 scoped_ptr<PairingRegistryDelegateLinux> save_delegate( |
53 new PairingRegistryDelegateLinux(task_runner)); | 78 new PairingRegistryDelegateLinux()); |
54 scoped_ptr<PairingRegistryDelegateLinux> load_delegate( | 79 scoped_ptr<PairingRegistryDelegateLinux> load_delegate( |
55 new PairingRegistryDelegateLinux(task_runner)); | 80 new PairingRegistryDelegateLinux()); |
56 save_delegate->SetFilenameForTesting(temp_file); | 81 save_delegate->SetRegistryPathForTesting(temp_registry_); |
57 load_delegate->SetFilenameForTesting(temp_file); | 82 load_delegate->SetRegistryPathForTesting(temp_registry_); |
58 | 83 |
59 // Save the pairings, then load them using a different delegate to ensure | 84 PairingRegistry::Pairing pairing(base::Time::Now(), "xxx", "xxx", "xxx"); |
60 // that the test isn't passing due to cached values. Note that the delegate | 85 EXPECT_TRUE(save_delegate->Save(pairing)); |
61 // doesn't require that the strings it loads and saves are valid JSON, so | 86 EXPECT_EQ(load_delegate->Load(pairing.client_id()), pairing); |
62 // we can simplify the test a bit. | 87 } |
63 std::string test_data = "test data"; | |
64 save_delegate->Save( | |
65 test_data, | |
66 base::Bind(&PairingRegistryDelegateLinuxTest::SaveComplete, | |
67 base::Unretained(this), | |
68 load_delegate.get(), | |
69 test_data)); | |
70 | |
71 run_loop.Run(); | |
72 | |
73 base::DeleteFile(temp_dir, true); | |
74 }; | |
75 | 88 |
76 } // namespace remoting | 89 } // namespace remoting |
OLD | NEW |