Home | History | Annotate | Download | only in cryptohome
      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 "chromeos/cryptohome/system_salt_getter.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "base/run_loop.h"
     10 #include "chromeos/dbus/fake_cryptohome_client.h"
     11 #include "chromeos/dbus/fake_dbus_thread_manager.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 namespace chromeos {
     15 namespace {
     16 
     17 // Used as a GetSystemSaltCallback.
     18 void CopySystemSalt(std::string* out_system_salt,
     19                     const std::string& system_salt) {
     20   *out_system_salt = system_salt;
     21 }
     22 
     23 class SystemSaltGetterTest : public testing::Test {
     24  protected:
     25   SystemSaltGetterTest() : fake_cryptohome_client_(NULL) {}
     26 
     27   virtual void SetUp() OVERRIDE {
     28     FakeDBusThreadManager* dbus_thread_manager = new FakeDBusThreadManager;
     29     fake_cryptohome_client_ = new FakeCryptohomeClient;
     30     dbus_thread_manager->SetCryptohomeClient(
     31         scoped_ptr<CryptohomeClient>(fake_cryptohome_client_));
     32     DBusThreadManager::InitializeForTesting(dbus_thread_manager);
     33 
     34     EXPECT_FALSE(SystemSaltGetter::IsInitialized());
     35     SystemSaltGetter::Initialize();
     36     ASSERT_TRUE(SystemSaltGetter::IsInitialized());
     37     ASSERT_TRUE(SystemSaltGetter::Get());
     38   }
     39 
     40   virtual void TearDown() OVERRIDE {
     41     SystemSaltGetter::Shutdown();
     42     DBusThreadManager::Shutdown();
     43   }
     44 
     45   base::MessageLoopForUI message_loop_;
     46   FakeCryptohomeClient* fake_cryptohome_client_;
     47 };
     48 
     49 TEST_F(SystemSaltGetterTest, GetSystemSalt) {
     50   // Try to get system salt before the service becomes available.
     51   fake_cryptohome_client_->SetServiceIsAvailable(false);
     52   std::string system_salt;
     53   SystemSaltGetter::Get()->GetSystemSalt(
     54       base::Bind(&CopySystemSalt, &system_salt));
     55   base::RunLoop().RunUntilIdle();
     56   EXPECT_TRUE(system_salt.empty());  // System salt is not returned yet.
     57 
     58   // Service becomes available.
     59   fake_cryptohome_client_->SetServiceIsAvailable(true);
     60   base::RunLoop().RunUntilIdle();
     61   const std::string expected_system_salt =
     62       SystemSaltGetter::ConvertRawSaltToHexString(
     63           FakeCryptohomeClient::GetStubSystemSalt());
     64   EXPECT_EQ(expected_system_salt, system_salt);  // System salt is returned.
     65 }
     66 
     67 }  // namespace
     68 }  // namespace chromeos
     69