Home | History | Annotate | Download | only in host
      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_linux.h"
      6 
      7 #include "base/file_util.h"
      8 #include "base/timer/timer.h"
      9 #include "base/values.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace remoting {
     13 
     14 using protocol::PairingRegistry;
     15 
     16 class PairingRegistryDelegateLinuxTest : public testing::Test {
     17  public:
     18   virtual void SetUp() OVERRIDE {
     19     // Create a temporary directory in order to get a unique name and use a
     20     // subdirectory to ensure that PairingRegistryDelegateLinux::Save() creates
     21     // the parent directory if it doesn't exist.
     22     base::CreateNewTempDirectory("chromoting-test", &temp_dir_);
     23     temp_registry_ = temp_dir_.Append("paired-clients");
     24   }
     25 
     26   virtual void TearDown() OVERRIDE {
     27     base::DeleteFile(temp_dir_, true);
     28   }
     29 
     30  protected:
     31   base::FilePath temp_dir_;
     32   base::FilePath temp_registry_;
     33 };
     34 
     35 TEST_F(PairingRegistryDelegateLinuxTest, SaveAndLoad) {
     36   scoped_ptr<PairingRegistryDelegateLinux> delegate(
     37       new PairingRegistryDelegateLinux());
     38   delegate->SetRegistryPathForTesting(temp_registry_);
     39 
     40   // Check that registry is initially empty.
     41   EXPECT_TRUE(delegate->LoadAll()->empty());
     42 
     43   // Add a couple of pairings.
     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::DictionaryValue* json;
     67   EXPECT_TRUE(pairings->GetDictionary(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) {
     77   scoped_ptr<PairingRegistryDelegateLinux> save_delegate(
     78       new PairingRegistryDelegateLinux());
     79   scoped_ptr<PairingRegistryDelegateLinux> load_delegate(
     80       new PairingRegistryDelegateLinux());
     81   save_delegate->SetRegistryPathForTesting(temp_registry_);
     82   load_delegate->SetRegistryPathForTesting(temp_registry_);
     83 
     84   PairingRegistry::Pairing pairing(base::Time::Now(), "xxx", "xxx", "xxx");
     85   EXPECT_TRUE(save_delegate->Save(pairing));
     86   EXPECT_EQ(load_delegate->Load(pairing.client_id()), pairing);
     87 }
     88 
     89 }  // namespace remoting
     90