Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / common / policy_loader_win_unittest.cc
index 13a3ed0..708ef66 100644 (file)
 
 #include "base/base_paths.h"
 #include "base/callback.h"
-#include "base/file_util.h"
 #include "base/files/file_path.h"
+#include "base/files/file_util.h"
 #include "base/files/scoped_temp_dir.h"
 #include "base/json/json_writer.h"
 #include "base/path_service.h"
-#include "base/process/process.h"
+#include "base/process/process_handle.h"
 #include "base/strings/string16.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/string_util.h"
@@ -28,6 +28,7 @@
 #include "base/strings/utf_string_conversions.h"
 #include "base/sys_byteorder.h"
 #include "base/win/registry.h"
+#include "base/win/win_util.h"
 #include "components/policy/core/common/async_policy_provider.h"
 #include "components/policy/core/common/configuration_policy_provider_test.h"
 #include "components/policy/core/common/external_data_fetcher.h"
@@ -154,7 +155,7 @@ class ScopedGroupPolicyRegistrySandbox {
   // Deletes the sandbox keys.
   void DeleteKeys();
 
-  std::wstring key_name_;
+  base::string16 key_name_;
 
   // Keys are created for the lifetime of a test to contain
   // the sandboxed HKCU and HKLM hives, respectively.
@@ -172,35 +173,35 @@ class RegistryTestHarness : public PolicyProviderTestHarness,
   virtual ~RegistryTestHarness();
 
   // PolicyProviderTestHarness:
-  virtual void SetUp() OVERRIDE;
+  virtual void SetUp() override;
 
   virtual ConfigurationPolicyProvider* CreateProvider(
       SchemaRegistry* registry,
-      scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
+      scoped_refptr<base::SequencedTaskRunner> task_runner) override;
 
-  virtual void InstallEmptyPolicy() OVERRIDE;
+  virtual void InstallEmptyPolicy() override;
   virtual void InstallStringPolicy(const std::string& policy_name,
-                                   const std::string& policy_value) OVERRIDE;
+                                   const std::string& policy_value) override;
   virtual void InstallIntegerPolicy(const std::string& policy_name,
-                                    int policy_value) OVERRIDE;
+                                    int policy_value) override;
   virtual void InstallBooleanPolicy(const std::string& policy_name,
-                                    bool policy_value) OVERRIDE;
+                                    bool policy_value) override;
   virtual void InstallStringListPolicy(
       const std::string& policy_name,
-      const base::ListValue* policy_value) OVERRIDE;
+      const base::ListValue* policy_value) override;
   virtual void InstallDictionaryPolicy(
       const std::string& policy_name,
-      const base::DictionaryValue* policy_value) OVERRIDE;
+      const base::DictionaryValue* policy_value) override;
   virtual void Install3rdPartyPolicy(
-      const base::DictionaryValue* policies) OVERRIDE;
+      const base::DictionaryValue* policies) override;
 
   // AppliedGPOListProvider:
   virtual DWORD GetAppliedGPOList(DWORD flags,
                                   LPCTSTR machine_name,
                                   PSID sid_user,
                                   GUID* extension_guid,
-                                  PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE;
-  virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE;
+                                  PGROUP_POLICY_OBJECT* gpo_list) override;
+  virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) override;
 
   // Creates a harness instance that will install policy in HKCU or HKLM,
   // respectively.
@@ -223,35 +224,35 @@ class PRegTestHarness : public PolicyProviderTestHarness,
   virtual ~PRegTestHarness();
 
   // PolicyProviderTestHarness:
-  virtual void SetUp() OVERRIDE;
+  virtual void SetUp() override;
 
   virtual ConfigurationPolicyProvider* CreateProvider(
       SchemaRegistry* registry,
-      scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
+      scoped_refptr<base::SequencedTaskRunner> task_runner) override;
 
-  virtual void InstallEmptyPolicy() OVERRIDE;
+  virtual void InstallEmptyPolicy() override;
   virtual void InstallStringPolicy(const std::string& policy_name,
-                                   const std::string& policy_value) OVERRIDE;
+                                   const std::string& policy_value) override;
   virtual void InstallIntegerPolicy(const std::string& policy_name,
-                                    int policy_value) OVERRIDE;
+                                    int policy_value) override;
   virtual void InstallBooleanPolicy(const std::string& policy_name,
-                                    bool policy_value) OVERRIDE;
+                                    bool policy_value) override;
   virtual void InstallStringListPolicy(
       const std::string& policy_name,
-      const base::ListValue* policy_value) OVERRIDE;
+      const base::ListValue* policy_value) override;
   virtual void InstallDictionaryPolicy(
       const std::string& policy_name,
-      const base::DictionaryValue* policy_value) OVERRIDE;
+      const base::DictionaryValue* policy_value) override;
   virtual void Install3rdPartyPolicy(
-      const base::DictionaryValue* policies) OVERRIDE;
+      const base::DictionaryValue* policies) override;
 
   // AppliedGPOListProvider:
   virtual DWORD GetAppliedGPOList(DWORD flags,
                                   LPCTSTR machine_name,
                                   PSID sid_user,
                                   GUID* extension_guid,
-                                  PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE;
-  virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE;
+                                  PGROUP_POLICY_OBJECT* gpo_list) override;
+  virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) override;
 
   // Creates a harness instance.
   static PolicyProviderTestHarness* Create();
@@ -295,9 +296,8 @@ ScopedGroupPolicyRegistrySandbox::ScopedGroupPolicyRegistrySandbox() {
   // Generate a unique registry key for the override for each test. This
   // makes sure that tests executing in parallel won't delete each other's
   // key, at DeleteKeys().
-  key_name_ = base::ASCIIToWide(base::StringPrintf(
-        "SOFTWARE\\chromium unittest %d",
-        base::Process::Current().pid()));
+  key_name_ = base::ASCIIToUTF16(base::StringPrintf(
+        "SOFTWARE\\chromium unittest %d", base::GetCurrentProcId()));
   std::wstring hklm_key_name = key_name_ + L"\\HKLM";
   std::wstring hkcu_key_name = key_name_ + L"\\HKCU";
 
@@ -346,6 +346,7 @@ void RegistryTestHarness::SetUp() {}
 ConfigurationPolicyProvider* RegistryTestHarness::CreateProvider(
     SchemaRegistry* registry,
     scoped_refptr<base::SequencedTaskRunner> task_runner) {
+  base::win::SetDomainStateForTesting(true);
   scoped_ptr<AsyncPolicyLoader> loader(
       new PolicyLoaderWin(task_runner, kTestPolicyKey, this));
   return new AsyncPolicyProvider(registry, loader.Pass());
@@ -463,9 +464,10 @@ PRegTestHarness::PRegTestHarness()
 PRegTestHarness::~PRegTestHarness() {}
 
 void PRegTestHarness::SetUp() {
+  base::win::SetDomainStateForTesting(false);
   ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
   preg_file_path_ = temp_dir_.path().Append(PolicyLoaderWin::kPRegFileName);
-  ASSERT_TRUE(file_util::WriteFile(preg_file_path_,
+  ASSERT_TRUE(base::WriteFile(preg_file_path_,
                                    preg_parser::kPRegFileHeader,
                                    arraysize(preg_parser::kPRegFileHeader)));
 
@@ -585,11 +587,10 @@ void PRegTestHarness::AppendRecordToPRegFile(const base::string16& path,
   buffer.insert(buffer.end(), data, data + size);
   AppendChars(&buffer, L"]");
 
-  ASSERT_EQ(buffer.size(),
-            file_util::AppendToFile(
-                preg_file_path_,
-                reinterpret_cast<const char*>(vector_as_array(&buffer)),
-                buffer.size()));
+  ASSERT_TRUE(base::AppendToFile(
+      preg_file_path_,
+      reinterpret_cast<const char*>(vector_as_array(&buffer)),
+      buffer.size()));
 }
 
 void PRegTestHarness::AppendDWORDToPRegFile(const base::string16& path,
@@ -701,7 +702,8 @@ class PolicyLoaderWinTest : public PolicyTestBase,
         gpo_list_status_(ERROR_ACCESS_DENIED) {}
   virtual ~PolicyLoaderWinTest() {}
 
-  virtual void SetUp() OVERRIDE {
+  virtual void SetUp() override {
+    base::win::SetDomainStateForTesting(false);
     PolicyTestBase::SetUp();
 
     ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir_));
@@ -717,11 +719,11 @@ class PolicyLoaderWinTest : public PolicyTestBase,
                                   LPCTSTR machine_name,
                                   PSID sid_user,
                                   GUID* extension_guid,
-                                  PGROUP_POLICY_OBJECT* gpo_list) OVERRIDE {
+                                  PGROUP_POLICY_OBJECT* gpo_list) override {
     *gpo_list = gpo_list_;
     return gpo_list_status_;
   }
-  virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) OVERRIDE {
+  virtual BOOL FreeGPOList(PGROUP_POLICY_OBJECT gpo_list) override {
     return TRUE;
   }
 
@@ -801,7 +803,8 @@ TEST_F(PolicyLoaderWinTest, HKLMOverHKCU) {
       .Set(test_keys::kKeyString,
            POLICY_LEVEL_MANDATORY,
            POLICY_SCOPE_MACHINE,
-           base::Value::CreateStringValue("hklm"), NULL);
+           new base::StringValue("hklm"),
+           NULL);
   EXPECT_TRUE(Matches(expected));
 }
 
@@ -851,15 +854,26 @@ TEST_F(PolicyLoaderWinTest, Merge3rdPartyPolicies) {
 
   PolicyBundle expected;
   PolicyMap& expected_policy = expected.Get(ns);
-  expected_policy.Set("a", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE,
-                      base::Value::CreateStringValue(kMachineMandatory), NULL);
-  expected_policy.Set("b", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
-                      base::Value::CreateStringValue(kUserMandatory), NULL);
-  expected_policy.Set("c", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_MACHINE,
-                      base::Value::CreateStringValue(kMachineRecommended),
+  expected_policy.Set("a",
+                      POLICY_LEVEL_MANDATORY,
+                      POLICY_SCOPE_MACHINE,
+                      new base::StringValue(kMachineMandatory),
+                      NULL);
+  expected_policy.Set("b",
+                      POLICY_LEVEL_MANDATORY,
+                      POLICY_SCOPE_USER,
+                      new base::StringValue(kUserMandatory),
+                      NULL);
+  expected_policy.Set("c",
+                      POLICY_LEVEL_RECOMMENDED,
+                      POLICY_SCOPE_MACHINE,
+                      new base::StringValue(kMachineRecommended),
+                      NULL);
+  expected_policy.Set("d",
+                      POLICY_LEVEL_RECOMMENDED,
+                      POLICY_SCOPE_USER,
+                      new base::StringValue(kUserRecommended),
                       NULL);
-  expected_policy.Set("d", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
-                      base::Value::CreateStringValue(kUserRecommended), NULL);
   EXPECT_TRUE(Matches(expected));
 }
 
@@ -1024,6 +1038,19 @@ TEST_F(PolicyLoaderWinTest, AppliedPolicyEmpty) {
   EXPECT_TRUE(Matches(empty));
 }
 
+TEST_F(PolicyLoaderWinTest, AppliedPolicyInDomain) {
+  base::win::SetDomainStateForTesting(true);
+  InstallRegistrySentinel();
+  base::FilePath gpo_dir(test_data_dir_.AppendASCII("empty"));
+  GROUP_POLICY_OBJECT gpo;
+  InitGPO(&gpo, 0, gpo_dir, NULL, NULL);
+  gpo_list_ = &gpo;
+  gpo_list_status_ = ERROR_SUCCESS;
+
+  PolicyBundle empty;
+  EXPECT_TRUE(MatchesRegistrySentinel());
+}
+
 TEST_F(PolicyLoaderWinTest, AppliedPolicyNonExistingFile) {
   InstallRegistrySentinel();
   GROUP_POLICY_OBJECT gpo;