Home | History | Annotate | Download | only in common
      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 <cstring>
      6 #include <string>
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/values.h"
     10 #include "build/build_config.h"
     11 #include "components/policy/core/common/policy_details.h"
     12 #include "components/policy/core/common/schema.h"
     13 #include "policy/policy_constants.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 // This unittest tests the code generated by
     17 // chrome/tools/build/generate_policy_source.py.
     18 
     19 namespace policy {
     20 
     21 namespace {
     22 
     23 #if defined(OS_CHROMEOS)
     24 // Checks if two schemas are the same or not. Note that this function doesn't
     25 // consider restrictions on integers and strings nor pattern properties.
     26 bool IsSameSchema(Schema a, Schema b) {
     27   if (a.valid() != b.valid())
     28     return false;
     29   if (!a.valid())
     30     return true;
     31   if (a.type() != b.type())
     32     return false;
     33   if (a.type() == base::Value::TYPE_LIST)
     34     return IsSameSchema(a.GetItems(), b.GetItems());
     35   if (a.type() != base::Value::TYPE_DICTIONARY)
     36     return true;
     37   Schema::Iterator a_it = a.GetPropertiesIterator();
     38   Schema::Iterator b_it = b.GetPropertiesIterator();
     39   while (!a_it.IsAtEnd()) {
     40     if (b_it.IsAtEnd())
     41       return false;
     42     if (strcmp(a_it.key(), b_it.key()) != 0)
     43       return false;
     44     if (!IsSameSchema(a_it.schema(), b_it.schema()))
     45       return false;
     46     a_it.Advance();
     47     b_it.Advance();
     48   }
     49   if (!b_it.IsAtEnd())
     50     return false;
     51   return IsSameSchema(a.GetAdditionalProperties(), b.GetAdditionalProperties());
     52 }
     53 #endif
     54 
     55 }  // namespace
     56 
     57 TEST(GeneratePolicySource, ChromeSchemaData) {
     58   Schema schema = Schema::Wrap(GetChromeSchemaData());
     59   ASSERT_TRUE(schema.valid());
     60   EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
     61 
     62   Schema subschema = schema.GetAdditionalProperties();
     63   EXPECT_FALSE(subschema.valid());
     64 
     65   subschema = schema.GetProperty("no such policy exists");
     66   EXPECT_FALSE(subschema.valid());
     67 
     68   subschema = schema.GetProperty(key::kSearchSuggestEnabled);
     69   ASSERT_TRUE(subschema.valid());
     70   EXPECT_EQ(base::Value::TYPE_BOOLEAN, subschema.type());
     71 
     72   subschema = schema.GetProperty(key::kDefaultCookiesSetting);
     73   ASSERT_TRUE(subschema.valid());
     74   EXPECT_EQ(base::Value::TYPE_INTEGER, subschema.type());
     75 
     76   subschema = schema.GetProperty(key::kProxyMode);
     77   ASSERT_TRUE(subschema.valid());
     78   EXPECT_EQ(base::Value::TYPE_STRING, subschema.type());
     79 
     80   subschema = schema.GetProperty(key::kURLBlacklist);
     81   ASSERT_TRUE(subschema.valid());
     82   EXPECT_EQ(base::Value::TYPE_LIST, subschema.type());
     83   ASSERT_TRUE(subschema.GetItems().valid());
     84   EXPECT_EQ(base::Value::TYPE_STRING, subschema.GetItems().type());
     85 
     86   subschema = schema.GetProperty(key::kProxySettings);
     87   ASSERT_TRUE(subschema.valid());
     88   EXPECT_EQ(base::Value::TYPE_DICTIONARY, subschema.type());
     89   EXPECT_FALSE(subschema.GetAdditionalProperties().valid());
     90   EXPECT_FALSE(subschema.GetProperty("no such proxy key exists").valid());
     91   ASSERT_TRUE(subschema.GetProperty(key::kProxyMode).valid());
     92   ASSERT_TRUE(subschema.GetProperty(key::kProxyServer).valid());
     93   ASSERT_TRUE(subschema.GetProperty(key::kProxyServerMode).valid());
     94   ASSERT_TRUE(subschema.GetProperty(key::kProxyPacUrl).valid());
     95   ASSERT_TRUE(subschema.GetProperty(key::kProxyBypassList).valid());
     96 
     97   // Verify that all the Chrome policies are there.
     98   for (Schema::Iterator it = schema.GetPropertiesIterator();
     99        !it.IsAtEnd(); it.Advance()) {
    100     EXPECT_TRUE(it.key());
    101     EXPECT_FALSE(std::string(it.key()).empty());
    102     EXPECT_TRUE(GetChromePolicyDetails(it.key()));
    103   }
    104 
    105   // The properties are iterated in order.
    106   const char* kExpectedProperties[] = {
    107     key::kProxyBypassList,
    108     key::kProxyMode,
    109     key::kProxyPacUrl,
    110     key::kProxyServer,
    111     key::kProxyServerMode,
    112     NULL,
    113   };
    114   const char** next = kExpectedProperties;
    115   for (Schema::Iterator it(subschema.GetPropertiesIterator());
    116        !it.IsAtEnd(); it.Advance(), ++next) {
    117     ASSERT_TRUE(*next != NULL);
    118     EXPECT_STREQ(*next, it.key());
    119     ASSERT_TRUE(it.schema().valid());
    120     EXPECT_EQ(base::Value::TYPE_STRING, it.schema().type());
    121   }
    122   EXPECT_TRUE(*next == NULL);
    123 
    124 #if defined(OS_CHROMEOS)
    125   subschema = schema.GetKnownProperty(key::kPowerManagementIdleSettings);
    126   ASSERT_TRUE(subschema.valid());
    127 
    128   EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"),
    129                            subschema.GetKnownProperty("Battery")));
    130 
    131   subschema = schema.GetKnownProperty(key::kDeviceLoginScreenPowerManagement);
    132   ASSERT_TRUE(subschema.valid());
    133 
    134   EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"),
    135                            subschema.GetKnownProperty("Battery")));
    136 #endif
    137 }
    138 
    139 TEST(GeneratePolicySource, PolicyDetails) {
    140   EXPECT_FALSE(GetChromePolicyDetails(""));
    141   EXPECT_FALSE(GetChromePolicyDetails("no such policy"));
    142   EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable"));
    143   EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled"));
    144   EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled"));
    145 
    146   const PolicyDetails* details =
    147       GetChromePolicyDetails(key::kSearchSuggestEnabled);
    148   ASSERT_TRUE(details);
    149   EXPECT_FALSE(details->is_deprecated);
    150   EXPECT_FALSE(details->is_device_policy);
    151   EXPECT_EQ(6, details->id);
    152   EXPECT_EQ(0u, details->max_external_data_size);
    153 
    154 #if !defined(OS_IOS)
    155   details = GetChromePolicyDetails(key::kJavascriptEnabled);
    156   ASSERT_TRUE(details);
    157   EXPECT_TRUE(details->is_deprecated);
    158   EXPECT_FALSE(details->is_device_policy);
    159   EXPECT_EQ(9, details->id);
    160   EXPECT_EQ(0u, details->max_external_data_size);
    161 #endif
    162 
    163 #if defined(OS_CHROMEOS)
    164   details = GetChromePolicyDetails(key::kDevicePolicyRefreshRate);
    165   ASSERT_TRUE(details);
    166   EXPECT_FALSE(details->is_deprecated);
    167   EXPECT_TRUE(details->is_device_policy);
    168   EXPECT_EQ(90, details->id);
    169   EXPECT_EQ(0u, details->max_external_data_size);
    170 #endif
    171 
    172   // TODO(bartfab): add a test that verifies a max_external_data_size larger
    173   // than 0, once a type 'external' policy is added.
    174 }
    175 
    176 #if defined(OS_CHROMEOS)
    177 TEST(GeneratePolicySource, SetEnterpriseDefaults) {
    178   PolicyMap policy_map;
    179 
    180   // If policy not configured yet, set the enterprise default.
    181   SetEnterpriseUsersDefaults(&policy_map);
    182 
    183   const base::Value* multiprof_behavior =
    184       policy_map.GetValue(key::kChromeOsMultiProfileUserBehavior);
    185   base::StringValue expected("primary-only");
    186   EXPECT_TRUE(expected.Equals(multiprof_behavior));
    187 
    188   // If policy already configured, it's not changed to enterprise defaults.
    189   policy_map.Set(key::kChromeOsMultiProfileUserBehavior,
    190                  POLICY_LEVEL_MANDATORY,
    191                  POLICY_SCOPE_USER,
    192                  new base::StringValue("test_value"),
    193                  NULL);
    194   SetEnterpriseUsersDefaults(&policy_map);
    195   multiprof_behavior =
    196       policy_map.GetValue(key::kChromeOsMultiProfileUserBehavior);
    197   expected = base::StringValue("test_value");
    198   EXPECT_TRUE(expected.Equals(multiprof_behavior));
    199 }
    200 #endif
    201 
    202 }  // namespace policy
    203