Home | History | Annotate | Download | only in policy_hack
      1 // Copyright (c) 2012 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 "base/basictypes.h"
      6 #include "base/bind.h"
      7 #include "base/message_loop/message_loop.h"
      8 #include "base/synchronization/waitable_event.h"
      9 #include "remoting/host/dns_blackhole_checker.h"
     10 #include "remoting/host/policy_hack/fake_policy_watcher.h"
     11 #include "remoting/host/policy_hack/mock_policy_callback.h"
     12 #include "remoting/host/policy_hack/policy_watcher.h"
     13 #include "testing/gmock/include/gmock/gmock.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 namespace remoting {
     17 namespace policy_hack {
     18 
     19 class PolicyWatcherTest : public testing::Test {
     20  public:
     21   PolicyWatcherTest() {
     22   }
     23 
     24   virtual void SetUp() OVERRIDE {
     25     message_loop_proxy_ = base::MessageLoopProxy::current();
     26     policy_callback_ = base::Bind(&MockPolicyCallback::OnPolicyUpdate,
     27                                   base::Unretained(&mock_policy_callback_));
     28     policy_watcher_.reset(new FakePolicyWatcher(message_loop_proxy_));
     29     nat_true_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
     30     nat_false_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
     31     nat_one_.SetInteger(PolicyWatcher::kNatPolicyName, 1);
     32     domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
     33                             std::string());
     34     domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName, kHostDomain);
     35     SetDefaults(nat_true_others_default_);
     36     nat_true_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
     37     SetDefaults(nat_false_others_default_);
     38     nat_false_others_default_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
     39     SetDefaults(domain_empty_others_default_);
     40     domain_empty_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
     41                                            std::string());
     42     SetDefaults(domain_full_others_default_);
     43     domain_full_others_default_.SetString(PolicyWatcher::kHostDomainPolicyName,
     44                                           kHostDomain);
     45     nat_true_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
     46     nat_true_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
     47                                      std::string());
     48     nat_true_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
     49     nat_true_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
     50                                    kHostDomain);
     51     nat_false_domain_empty_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
     52     nat_false_domain_empty_.SetString(PolicyWatcher::kHostDomainPolicyName,
     53                                       std::string());
     54     nat_false_domain_full_.SetBoolean(PolicyWatcher::kNatPolicyName, false);
     55     nat_false_domain_full_.SetString(PolicyWatcher::kHostDomainPolicyName,
     56                                     kHostDomain);
     57     SetDefaults(nat_true_domain_empty_others_default_);
     58     nat_true_domain_empty_others_default_.SetBoolean(
     59         PolicyWatcher::kNatPolicyName, true);
     60     nat_true_domain_empty_others_default_.SetString(
     61         PolicyWatcher::kHostDomainPolicyName, std::string());
     62     unknown_policies_.SetString("UnknownPolicyOne", std::string());
     63     unknown_policies_.SetString("UnknownPolicyTwo", std::string());
     64 
     65     const char kOverrideNatTraversalToFalse[] =
     66       "{ \"RemoteAccessHostFirewallTraversal\": false }";
     67     nat_true_and_overridden_.SetBoolean(PolicyWatcher::kNatPolicyName, true);
     68     nat_true_and_overridden_.SetString(
     69         PolicyWatcher::kHostDebugOverridePoliciesName,
     70         kOverrideNatTraversalToFalse);
     71     pairing_true_.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
     72     pairing_false_.SetBoolean(PolicyWatcher::kHostAllowClientPairing, false);
     73 #if !defined(NDEBUG)
     74     SetDefaults(nat_false_overridden_others_default_);
     75     nat_false_overridden_others_default_.SetBoolean(
     76         PolicyWatcher::kNatPolicyName, false);
     77     nat_false_overridden_others_default_.SetString(
     78         PolicyWatcher::kHostDebugOverridePoliciesName,
     79         kOverrideNatTraversalToFalse);
     80 #endif
     81   }
     82 
     83  protected:
     84   void StartWatching() {
     85     policy_watcher_->StartWatching(policy_callback_);
     86     message_loop_.RunUntilIdle();
     87   }
     88 
     89   void StopWatching() {
     90     base::WaitableEvent stop_event(false, false);
     91     policy_watcher_->StopWatching(&stop_event);
     92     message_loop_.RunUntilIdle();
     93     EXPECT_EQ(true, stop_event.IsSignaled());
     94   }
     95 
     96   static const char* kHostDomain;
     97   base::MessageLoop message_loop_;
     98   scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
     99   MockPolicyCallback mock_policy_callback_;
    100   PolicyWatcher::PolicyCallback policy_callback_;
    101   scoped_ptr<FakePolicyWatcher> policy_watcher_;
    102   base::DictionaryValue empty_;
    103   base::DictionaryValue nat_true_;
    104   base::DictionaryValue nat_false_;
    105   base::DictionaryValue nat_one_;
    106   base::DictionaryValue domain_empty_;
    107   base::DictionaryValue domain_full_;
    108   base::DictionaryValue nat_true_others_default_;
    109   base::DictionaryValue nat_false_others_default_;
    110   base::DictionaryValue domain_empty_others_default_;
    111   base::DictionaryValue domain_full_others_default_;
    112   base::DictionaryValue nat_true_domain_empty_;
    113   base::DictionaryValue nat_true_domain_full_;
    114   base::DictionaryValue nat_false_domain_empty_;
    115   base::DictionaryValue nat_false_domain_full_;
    116   base::DictionaryValue nat_true_domain_empty_others_default_;
    117   base::DictionaryValue unknown_policies_;
    118   base::DictionaryValue nat_true_and_overridden_;
    119   base::DictionaryValue nat_false_overridden_others_default_;
    120   base::DictionaryValue pairing_true_;
    121   base::DictionaryValue pairing_false_;
    122 
    123  private:
    124   void SetDefaults(base::DictionaryValue& dict) {
    125     dict.SetBoolean(PolicyWatcher::kNatPolicyName, true);
    126     dict.SetBoolean(PolicyWatcher::kHostRequireTwoFactorPolicyName, false);
    127     dict.SetString(PolicyWatcher::kHostDomainPolicyName, std::string());
    128     dict.SetBoolean(PolicyWatcher::kHostMatchUsernamePolicyName, false);
    129     dict.SetString(PolicyWatcher::kHostTalkGadgetPrefixPolicyName,
    130                    kDefaultHostTalkGadgetPrefix);
    131     dict.SetBoolean(PolicyWatcher::kHostRequireCurtainPolicyName, false);
    132     dict.SetString(PolicyWatcher::kHostTokenUrlPolicyName, std::string());
    133     dict.SetString(PolicyWatcher::kHostTokenValidationUrlPolicyName,
    134                    std::string());
    135     dict.SetBoolean(PolicyWatcher::kHostAllowClientPairing, true);
    136 #if !defined(NDEBUG)
    137     dict.SetString(PolicyWatcher::kHostDebugOverridePoliciesName, "");
    138 #endif
    139   }
    140 };
    141 
    142 const char* PolicyWatcherTest::kHostDomain = "google.com";
    143 
    144 MATCHER_P(IsPolicies, dict, "") {
    145   return arg->Equals(dict);
    146 }
    147 
    148 TEST_F(PolicyWatcherTest, None) {
    149   EXPECT_CALL(mock_policy_callback_,
    150               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    151 
    152   StartWatching();
    153   policy_watcher_->SetPolicies(&empty_);
    154   StopWatching();
    155 }
    156 
    157 TEST_F(PolicyWatcherTest, NatTrue) {
    158   EXPECT_CALL(mock_policy_callback_,
    159               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    160 
    161   StartWatching();
    162   policy_watcher_->SetPolicies(&nat_true_);
    163   StopWatching();
    164 }
    165 
    166 TEST_F(PolicyWatcherTest, NatFalse) {
    167   EXPECT_CALL(mock_policy_callback_,
    168               OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
    169 
    170   StartWatching();
    171   policy_watcher_->SetPolicies(&nat_false_);
    172   StopWatching();
    173 }
    174 
    175 TEST_F(PolicyWatcherTest, NatOne) {
    176   EXPECT_CALL(mock_policy_callback_,
    177               OnPolicyUpdatePtr(IsPolicies(&nat_false_others_default_)));
    178 
    179   StartWatching();
    180   policy_watcher_->SetPolicies(&nat_one_);
    181   StopWatching();
    182 }
    183 
    184 TEST_F(PolicyWatcherTest, DomainEmpty) {
    185   EXPECT_CALL(mock_policy_callback_,
    186               OnPolicyUpdatePtr(IsPolicies(&domain_empty_others_default_)));
    187 
    188   StartWatching();
    189   policy_watcher_->SetPolicies(&domain_empty_);
    190   StopWatching();
    191 }
    192 
    193 TEST_F(PolicyWatcherTest, DomainFull) {
    194   EXPECT_CALL(mock_policy_callback_,
    195               OnPolicyUpdatePtr(IsPolicies(&domain_full_others_default_)));
    196 
    197   StartWatching();
    198   policy_watcher_->SetPolicies(&domain_full_);
    199   StopWatching();
    200 }
    201 
    202 TEST_F(PolicyWatcherTest, NatNoneThenTrue) {
    203   EXPECT_CALL(mock_policy_callback_,
    204               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    205 
    206   StartWatching();
    207   policy_watcher_->SetPolicies(&empty_);
    208   policy_watcher_->SetPolicies(&nat_true_);
    209   StopWatching();
    210 }
    211 
    212 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrue) {
    213   EXPECT_CALL(mock_policy_callback_,
    214               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    215 
    216   StartWatching();
    217   policy_watcher_->SetPolicies(&empty_);
    218   policy_watcher_->SetPolicies(&nat_true_);
    219   policy_watcher_->SetPolicies(&nat_true_);
    220   StopWatching();
    221 }
    222 
    223 TEST_F(PolicyWatcherTest, NatNoneThenTrueThenTrueThenFalse) {
    224   testing::InSequence sequence;
    225   EXPECT_CALL(mock_policy_callback_,
    226               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    227   EXPECT_CALL(mock_policy_callback_,
    228               OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
    229 
    230   StartWatching();
    231   policy_watcher_->SetPolicies(&empty_);
    232   policy_watcher_->SetPolicies(&nat_true_);
    233   policy_watcher_->SetPolicies(&nat_true_);
    234   policy_watcher_->SetPolicies(&nat_false_);
    235   StopWatching();
    236 }
    237 
    238 TEST_F(PolicyWatcherTest, NatNoneThenFalse) {
    239   testing::InSequence sequence;
    240   EXPECT_CALL(mock_policy_callback_,
    241               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    242   EXPECT_CALL(mock_policy_callback_,
    243               OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
    244 
    245   StartWatching();
    246   policy_watcher_->SetPolicies(&empty_);
    247   policy_watcher_->SetPolicies(&nat_false_);
    248   StopWatching();
    249 }
    250 
    251 TEST_F(PolicyWatcherTest, NatNoneThenFalseThenTrue) {
    252   testing::InSequence sequence;
    253   EXPECT_CALL(mock_policy_callback_,
    254               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    255   EXPECT_CALL(mock_policy_callback_,
    256               OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
    257   EXPECT_CALL(mock_policy_callback_,
    258               OnPolicyUpdatePtr(IsPolicies(&nat_true_)));
    259 
    260   StartWatching();
    261   policy_watcher_->SetPolicies(&empty_);
    262   policy_watcher_->SetPolicies(&nat_false_);
    263   policy_watcher_->SetPolicies(&nat_true_);
    264   StopWatching();
    265 }
    266 
    267 TEST_F(PolicyWatcherTest, ChangeOneRepeatedlyThenTwo) {
    268   testing::InSequence sequence;
    269   EXPECT_CALL(mock_policy_callback_,
    270               OnPolicyUpdatePtr(IsPolicies(
    271                   &nat_true_domain_empty_others_default_)));
    272   EXPECT_CALL(mock_policy_callback_,
    273               OnPolicyUpdatePtr(IsPolicies(&domain_full_)));
    274   EXPECT_CALL(mock_policy_callback_,
    275               OnPolicyUpdatePtr(IsPolicies(&nat_false_)));
    276   EXPECT_CALL(mock_policy_callback_,
    277               OnPolicyUpdatePtr(IsPolicies(&domain_empty_)));
    278   EXPECT_CALL(mock_policy_callback_,
    279               OnPolicyUpdatePtr(IsPolicies(&nat_true_domain_full_)));
    280 
    281   StartWatching();
    282   policy_watcher_->SetPolicies(&nat_true_domain_empty_);
    283   policy_watcher_->SetPolicies(&nat_true_domain_full_);
    284   policy_watcher_->SetPolicies(&nat_false_domain_full_);
    285   policy_watcher_->SetPolicies(&nat_false_domain_empty_);
    286   policy_watcher_->SetPolicies(&nat_true_domain_full_);
    287   StopWatching();
    288 }
    289 
    290 TEST_F(PolicyWatcherTest, FilterUnknownPolicies) {
    291   testing::InSequence sequence;
    292   EXPECT_CALL(mock_policy_callback_,
    293               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    294 
    295   StartWatching();
    296   policy_watcher_->SetPolicies(&empty_);
    297   policy_watcher_->SetPolicies(&unknown_policies_);
    298   policy_watcher_->SetPolicies(&empty_);
    299   StopWatching();
    300 }
    301 
    302 TEST_F(PolicyWatcherTest, DebugOverrideNatPolicy) {
    303 #if !defined(NDEBUG)
    304   EXPECT_CALL(mock_policy_callback_,
    305       OnPolicyUpdatePtr(IsPolicies(&nat_false_overridden_others_default_)));
    306 #else
    307   EXPECT_CALL(mock_policy_callback_,
    308       OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    309 #endif
    310 
    311   StartWatching();
    312   policy_watcher_->SetPolicies(&nat_true_and_overridden_);
    313   StopWatching();
    314 }
    315 
    316 TEST_F(PolicyWatcherTest, PairingFalseThenTrue) {
    317   testing::InSequence sequence;
    318   EXPECT_CALL(mock_policy_callback_,
    319               OnPolicyUpdatePtr(IsPolicies(&nat_true_others_default_)));
    320   EXPECT_CALL(mock_policy_callback_,
    321               OnPolicyUpdatePtr(IsPolicies(&pairing_false_)));
    322   EXPECT_CALL(mock_policy_callback_,
    323               OnPolicyUpdatePtr(IsPolicies(&pairing_true_)));
    324 
    325   StartWatching();
    326   policy_watcher_->SetPolicies(&empty_);
    327   policy_watcher_->SetPolicies(&pairing_false_);
    328   policy_watcher_->SetPolicies(&pairing_true_);
    329   StopWatching();
    330 }
    331 
    332 }  // namespace policy_hack
    333 }  // namespace remoting
    334