Chromium Code Reviews| Index: chrome/browser/chromeos/attestation/attestation_policy_observer_unittest.cc |
| diff --git a/chrome/browser/chromeos/attestation/attestation_policy_observer_unittest.cc b/chrome/browser/chromeos/attestation/attestation_policy_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..235d50ae75b6e91e4a14a38c4b7b49eab3a27a74 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/attestation/attestation_policy_observer_unittest.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/chromeos/attestation/attestation_policy_observer.h" |
| +#include "chrome/browser/chromeos/settings/cros_settings.h" |
| +#include "chrome/browser/chromeos/settings/cros_settings_names.h" |
| +#include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| +#include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| +#include "chromeos/attestation/mock_attestation_flow.h" |
| +#include "chromeos/dbus/mock_cryptohome_client.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::_; |
| +using testing::Invoke; |
| +using testing::StrictMock; |
| +using testing::WithArgs; |
| + |
| +namespace chromeos { |
| +namespace attestation { |
| + |
| +namespace { |
| + |
| +void DBusCallbackFalse(const BoolDBusMethodCallback& callback) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false)); |
| +} |
| + |
| +void DBusCallbackTrue(const BoolDBusMethodCallback& callback) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); |
| +} |
| + |
| +void DBusDataCallback(const CryptohomeClient::DataMethodCallback& callback) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true, "fake")); |
| +} |
| + |
| +void CertCallbackSuccess(const AttestationFlow::CertificateCallback& callback) { |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, base::Bind(callback, true, "fake_cert")); |
| +} |
| + |
| +} // namespace |
| + |
| +class AttestationPolicyObserverTest : public ::testing::Test { |
| + public: |
| + AttestationPolicyObserverTest() { |
| + // Remove the real DeviceSettingsProvider and replace it with a stub. |
| + CrosSettings* cros_settings = CrosSettings::Get(); |
| + device_settings_provider_ = |
| + cros_settings->GetProvider(kDeviceAttestationEnabled); |
| + cros_settings->RemoveSettingsProvider(device_settings_provider_); |
| + cros_settings->AddSettingsProvider(&stub_settings_provider_); |
|
Mattias Nissler (ping if slow)
2013/04/10 17:31:03
Ah, CrosSettings mocking... sucks. I wish we had a
dkrahn
2013/04/12 01:17:29
+1, I was surprised to see this but it gets the jo
|
| + cros_settings->SetBoolean(kDeviceAttestationEnabled, true); |
| + policy_client_.SetDMToken("fake_dm_token"); |
| + observer_.set_cryptohome_client(&cryptohome_client_); |
| + observer_.set_attestation_flow(&attestation_flow_); |
| + } |
| + |
| + virtual ~AttestationPolicyObserverTest() { |
| + // Restore the real DeviceSettingsProvider. |
| + CrosSettings* cros_settings = CrosSettings::Get(); |
| + cros_settings->RemoveSettingsProvider(&stub_settings_provider_); |
| + cros_settings->AddSettingsProvider(device_settings_provider_); |
| + } |
| + |
| + protected: |
| + void Run() { |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
|
Mattias Nissler (ping if slow)
2013/04/10 17:31:03
You can just do base::RunLoop().RunUntilIdle() in
dkrahn
2013/04/12 01:17:29
Done.
|
| + } |
| + |
| + MessageLoop message_loop_; |
| + AttestationPolicyObserver observer_; |
| + CrosSettingsProvider* device_settings_provider_; |
| + StubCrosSettingsProvider stub_settings_provider_; |
| + StrictMock<MockCryptohomeClient> cryptohome_client_; |
| + StrictMock<MockAttestationFlow> attestation_flow_; |
| + StrictMock<policy::MockCloudPolicyClient> policy_client_; |
| +}; |
| + |
| +TEST_F(AttestationPolicyObserverTest, FeatureDisabled) { |
| + CrosSettings* cros_settings = CrosSettings::Get(); |
| + cros_settings->SetBoolean(kDeviceAttestationEnabled, false); |
| + observer_.Connect(&policy_client_); |
| + Run(); |
| +} |
| + |
| +TEST_F(AttestationPolicyObserverTest, UnregisteredPolicyClient) { |
| + policy_client_.SetDMToken(""); |
| + observer_.Connect(&policy_client_); |
| + Run(); |
| +} |
| + |
| +TEST_F(AttestationPolicyObserverTest, NewCertificate) { |
| + EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) |
| + .WillOnce(WithArgs<2>(Invoke(DBusCallbackFalse))); |
| + EXPECT_CALL(attestation_flow_, GetCertificate(_, _)) |
| + .WillOnce(WithArgs<1>(Invoke(CertCallbackSuccess))); |
| + observer_.Connect(&policy_client_); |
| + Run(); |
| +} |
| + |
| +TEST_F(AttestationPolicyObserverTest, KeyExists) { |
| + EXPECT_CALL(cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _)) |
| + .WillOnce(WithArgs<2>(Invoke(DBusCallbackTrue))); |
| + EXPECT_CALL(cryptohome_client_, TpmAttestationGetCertificate(_, _, _)) |
| + .WillOnce(WithArgs<2>(Invoke(DBusDataCallback))); |
| + observer_.Connect(&policy_client_); |
| + Run(); |
| +} |
| + |
| +} // namespace attestation |
| +} // namespace chromeos |