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 // Checks if two schemas are the same or not. Note that this function doesn't
     24 // consider restrictions on integers and strings nor pattern properties.
     25 bool IsSameSchema(Schema a, Schema b) {
     26   if (a.valid() != b.valid())
     27     return false;
     28   if (!a.valid())
     29     return true;
     30   if (a.type() != b.type())
     31     return false;
     32   if (a.type() == base::Value::TYPE_LIST)
     33     return IsSameSchema(a.GetItems(), b.GetItems());
     34   if (a.type() != base::Value::TYPE_DICTIONARY)
     35     return true;
     36   Schema::Iterator a_it = a.GetPropertiesIterator();
     37   Schema::Iterator b_it = b.GetPropertiesIterator();
     38   while (!a_it.IsAtEnd()) {
     39     if (b_it.IsAtEnd())
     40       return false;
     41     if (strcmp(a_it.key(), b_it.key()) != 0)
     42       return false;
     43     if (!IsSameSchema(a_it.schema(), b_it.schema()))
     44       return false;
     45     a_it.Advance();
     46     b_it.Advance();
     47   }
     48   if (!b_it.IsAtEnd())
     49     return false;
     50   return IsSameSchema(a.GetAdditionalProperties(), b.GetAdditionalProperties());
     51 }
     52 
     53 }  // namespace
     54 
     55 TEST(GeneratePolicySource, ChromeSchemaData) {
     56   Schema schema = Schema::Wrap(GetChromeSchemaData());
     57   ASSERT_TRUE(schema.valid());
     58   EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
     59 
     60   Schema subschema = schema.GetAdditionalProperties();
     61   EXPECT_FALSE(subschema.valid());
     62 
     63   subschema = schema.GetProperty("no such policy exists");
     64   EXPECT_FALSE(subschema.valid());
     65 
     66   subschema = schema.GetProperty(key::kSearchSuggestEnabled);
     67   ASSERT_TRUE(subschema.valid());
     68   EXPECT_EQ(base::Value::TYPE_BOOLEAN, subschema.type());
     69 
     70   subschema = schema.GetProperty(key::kDefaultCookiesSetting);
     71   ASSERT_TRUE(subschema.valid());
     72   EXPECT_EQ(base::Value::TYPE_INTEGER, subschema.type());
     73 
     74   subschema = schema.GetProperty(key::kProxyMode);
     75   ASSERT_TRUE(subschema.valid());
     76   EXPECT_EQ(base::Value::TYPE_STRING, subschema.type());
     77 
     78   subschema = schema.GetProperty(key::kURLBlacklist);
     79   ASSERT_TRUE(subschema.valid());
     80   EXPECT_EQ(base::Value::TYPE_LIST, subschema.type());
     81   ASSERT_TRUE(subschema.GetItems().valid());
     82   EXPECT_EQ(base::Value::TYPE_STRING, subschema.GetItems().type());
     83 
     84   subschema = schema.GetProperty(key::kProxySettings);
     85   ASSERT_TRUE(subschema.valid());
     86   EXPECT_EQ(base::Value::TYPE_DICTIONARY, subschema.type());
     87   EXPECT_FALSE(subschema.GetAdditionalProperties().valid());
     88   EXPECT_FALSE(subschema.GetProperty("no such proxy key exists").valid());
     89   ASSERT_TRUE(subschema.GetProperty(key::kProxyMode).valid());
     90   ASSERT_TRUE(subschema.GetProperty(key::kProxyServer).valid());
     91   ASSERT_TRUE(subschema.GetProperty(key::kProxyServerMode).valid());
     92   ASSERT_TRUE(subschema.GetProperty(key::kProxyPacUrl).valid());
     93   ASSERT_TRUE(subschema.GetProperty(key::kProxyBypassList).valid());
     94 
     95   // Verify that all the Chrome policies are there.
     96   for (Schema::Iterator it = schema.GetPropertiesIterator();
     97        !it.IsAtEnd(); it.Advance()) {
     98     EXPECT_TRUE(it.key());
     99     EXPECT_FALSE(std::string(it.key()).empty());
    100     EXPECT_TRUE(GetChromePolicyDetails(it.key()));
    101   }
    102 
    103   // The properties are iterated in order.
    104   const char* kExpectedProperties[] = {
    105     key::kProxyBypassList,
    106     key::kProxyMode,
    107     key::kProxyPacUrl,
    108     key::kProxyServer,
    109     key::kProxyServerMode,
    110     NULL,
    111   };
    112   const char** next = kExpectedProperties;
    113   for (Schema::Iterator it(subschema.GetPropertiesIterator());
    114        !it.IsAtEnd(); it.Advance(), ++next) {
    115     ASSERT_TRUE(*next != NULL);
    116     EXPECT_STREQ(*next, it.key());
    117     ASSERT_TRUE(it.schema().valid());
    118     EXPECT_EQ(base::Value::TYPE_STRING, it.schema().type());
    119   }
    120   EXPECT_TRUE(*next == NULL);
    121 
    122 #if defined(OS_CHROMEOS)
    123   subschema = schema.GetKnownProperty(key::kPowerManagementIdleSettings);
    124   ASSERT_TRUE(subschema.valid());
    125 
    126   EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"),
    127                            subschema.GetKnownProperty("Battery")));
    128 
    129   subschema = schema.GetKnownProperty(key::kDeviceLoginScreenPowerManagement);
    130   ASSERT_TRUE(subschema.valid());
    131 
    132   EXPECT_TRUE(IsSameSchema(subschema.GetKnownProperty("AC"),
    133                            subschema.GetKnownProperty("Battery")));
    134 #endif
    135 }
    136 
    137 TEST(GeneratePolicySource, PolicyDetails) {
    138   EXPECT_FALSE(GetChromePolicyDetails(""));
    139   EXPECT_FALSE(GetChromePolicyDetails("no such policy"));
    140   EXPECT_FALSE(GetChromePolicyDetails("SearchSuggestEnable"));
    141   EXPECT_FALSE(GetChromePolicyDetails("searchSuggestEnabled"));
    142   EXPECT_FALSE(GetChromePolicyDetails("SSearchSuggestEnabled"));
    143 
    144   const PolicyDetails* details =
    145       GetChromePolicyDetails(key::kSearchSuggestEnabled);
    146   ASSERT_TRUE(details);
    147   EXPECT_FALSE(details->is_deprecated);
    148   EXPECT_FALSE(details->is_device_policy);
    149   EXPECT_EQ(6, details->id);
    150   EXPECT_EQ(0u, details->max_external_data_size);
    151 
    152 #if !defined(OS_IOS)
    153   details = GetChromePolicyDetails(key::kJavascriptEnabled);
    154   ASSERT_TRUE(details);
    155   EXPECT_TRUE(details->is_deprecated);
    156   EXPECT_FALSE(details->is_device_policy);
    157   EXPECT_EQ(9, details->id);
    158   EXPECT_EQ(0u, details->max_external_data_size);
    159 #endif
    160 
    161 #if defined(OS_CHROMEOS)
    162   details = GetChromePolicyDetails(key::kDevicePolicyRefreshRate);
    163   ASSERT_TRUE(details);
    164   EXPECT_FALSE(details->is_deprecated);
    165   EXPECT_TRUE(details->is_device_policy);
    166   EXPECT_EQ(90, details->id);
    167   EXPECT_EQ(0u, details->max_external_data_size);
    168 #endif
    169 
    170   // TODO(bartfab): add a test that verifies a max_external_data_size larger
    171   // than 0, once a type 'external' policy is added.
    172 }
    173 
    174 }  // namespace policy
    175