15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "base/callback.h"
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/message_loop/message_loop_proxy.h"
10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/sequenced_task_runner.h"
11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/policy/core/common/cloud/mock_user_cloud_policy_store.h"
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/external_data_fetcher.h"
14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/mock_configuration_policy_provider.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/schema_registry.h"
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace em = enterprise_management;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using testing::AnyNumber;
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using testing::AtLeast;
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using testing::Mock;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using testing::_;
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace policy {
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class UserCloudPolicyManagerTest : public testing::Test {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  UserCloudPolicyManagerTest() : store_(NULL) {}
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUp() OVERRIDE {
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Set up a policy map for testing.
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    policy_map_.Set("key",
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    POLICY_LEVEL_MANDATORY,
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    POLICY_SCOPE_USER,
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    new base::StringValue("value"),
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    NULL);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        .CopyFrom(policy_map_);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void TearDown() OVERRIDE {
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (manager_) {
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      manager_->RemoveObserver(&observer_);
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      manager_->Shutdown();
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CreateManager() {
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    store_ = new MockUserCloudPolicyStore();
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    EXPECT_CALL(*store_, Load());
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    manager_.reset(new UserCloudPolicyManager(
56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        scoped_ptr<UserCloudPolicyStore>(store_),
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        base::FilePath(),
58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        scoped_ptr<CloudExternalDataManager>(),
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        loop_.message_loop_proxy(),
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        loop_.message_loop_proxy(),
61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)        loop_.message_loop_proxy()));
62f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    manager_->Init(&schema_registry_);
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    manager_->AddObserver(&observer_);
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Mock::VerifyAndClearExpectations(store_);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Required by the refresh scheduler that's created by the manager.
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop loop_;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Convenience policy objects.
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PolicyMap policy_map_;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PolicyBundle expected_bundle_;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Policy infrastructure.
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SchemaRegistry schema_registry_;
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockConfigurationPolicyObserver observer_;
77d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  MockUserCloudPolicyStore* store_;  // Not owned.
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<UserCloudPolicyManager> manager_;
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest);
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(UserCloudPolicyManagerTest, DisconnectAndRemovePolicy) {
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Load policy, make sure it goes away when DisconnectAndRemovePolicy() is
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // called.
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CreateManager();
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  store_->policy_map_.CopyFrom(policy_map_);
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  store_->NotifyStoreLoaded();
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_CALL(*store_, Clear());
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  manager_->DisconnectAndRemovePolicy();
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(manager_->core()->service());
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace policy
100