summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/network_configuration_updater_unittest.cc
blob: 454d0eb5bcc674ddfbcc135089d28d8483928bf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/network_configuration_updater.h"

#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/cros/mock_network_library.h"
#include "chrome/browser/policy/mock_configuration_policy_provider.h"
#include "chrome/browser/policy/policy_map.h"
#include "chrome/browser/policy/policy_service_impl.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::AtLeast;
using testing::Mock;
using testing::Ne;
using testing::Return;
using testing::_;

namespace policy {

static const char kFakeONC[] = "{ \"GUID\": \"1234\" }";

class NetworkConfigurationUpdaterTest
    : public testing::TestWithParam<const char*>{
 protected:
  virtual void SetUp() OVERRIDE {
    EXPECT_CALL(provider_, IsInitializationComplete())
        .WillRepeatedly(Return(true));
    provider_.Init();
    PolicyServiceImpl::Providers providers;
    providers.push_back(&provider_);
    policy_service_.reset(new PolicyServiceImpl(providers));
  }

  virtual void TearDown() OVERRIDE {
    provider_.Shutdown();
  }

  // Maps configuration policy name to corresponding ONC source.
  static chromeos::NetworkUIData::ONCSource NameToONCSource(
      const std::string& name) {
    if (name == key::kDeviceOpenNetworkConfiguration)
      return chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY;
    if (name == key::kOpenNetworkConfiguration)
      return chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY;
    return chromeos::NetworkUIData::ONC_SOURCE_NONE;
  }

  chromeos::MockNetworkLibrary network_library_;
  MockConfigurationPolicyProvider provider_;
  scoped_ptr<PolicyServiceImpl> policy_service_;
};

TEST_P(NetworkConfigurationUpdaterTest, InitialUpdate) {
  PolicyMap policy;
  policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
             Value::CreateStringValue(kFakeONC));
  provider_.UpdateChromePolicy(policy);

  EXPECT_CALL(network_library_, AddNetworkProfileObserver(_));

  // Initially, both policies are applied.
  EXPECT_CALL(network_library_, LoadOncNetworks(
      kFakeONC, "", NameToONCSource(GetParam()), _, _));
  EXPECT_CALL(network_library_, LoadOncNetworks(
      NetworkConfigurationUpdater::kEmptyConfiguration, "",
      Ne(NameToONCSource(GetParam())), _, _));

  EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_));

  {
    NetworkConfigurationUpdater updater(policy_service_.get(),
                                        &network_library_);
  }
  Mock::VerifyAndClearExpectations(&network_library_);
}

TEST_P(NetworkConfigurationUpdaterTest, AllowWebTrust) {
  {
    EXPECT_CALL(network_library_, AddNetworkProfileObserver(_));

    // Initially web trust is disabled.
    EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, false, _))
        .Times(AtLeast(0));
    NetworkConfigurationUpdater updater(policy_service_.get(),
                                        &network_library_);
    Mock::VerifyAndClearExpectations(&network_library_);

    // Web trust should be forwarded to LoadOncNetworks.
    EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, true, _))
        .Times(AtLeast(0));

    updater.set_allow_web_trust(true);

    PolicyMap policy;
    policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
               Value::CreateStringValue(kFakeONC));
    provider_.UpdateChromePolicy(policy);
    Mock::VerifyAndClearExpectations(&network_library_);

    EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_));
  }
  Mock::VerifyAndClearExpectations(&network_library_);
}

TEST_P(NetworkConfigurationUpdaterTest, PolicyChange) {
  {
    EXPECT_CALL(network_library_, AddNetworkProfileObserver(_));

    // Ignore the initial updates.
    EXPECT_CALL(network_library_, LoadOncNetworks(_, _, _, _, _))
        .Times(AtLeast(0));
    NetworkConfigurationUpdater updater(policy_service_.get(),
                                        &network_library_);
    Mock::VerifyAndClearExpectations(&network_library_);

    // We should update if policy changes.
    EXPECT_CALL(network_library_, LoadOncNetworks(
        kFakeONC, "", NameToONCSource(GetParam()), _, _));

    // In the current implementation, we always apply both policies.
    EXPECT_CALL(network_library_, LoadOncNetworks(
        NetworkConfigurationUpdater::kEmptyConfiguration, "",
        Ne(NameToONCSource(GetParam())), _, _));

    PolicyMap policy;
    policy.Set(GetParam(), POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
               Value::CreateStringValue(kFakeONC));
    provider_.UpdateChromePolicy(policy);
    Mock::VerifyAndClearExpectations(&network_library_);

    // Another update is expected if the policy goes away. In the current
    // implementation, we always apply both policies.
    EXPECT_CALL(network_library_, LoadOncNetworks(
        NetworkConfigurationUpdater::kEmptyConfiguration, "",
        chromeos::NetworkUIData::ONC_SOURCE_DEVICE_POLICY, _, _));

    EXPECT_CALL(network_library_, LoadOncNetworks(
        NetworkConfigurationUpdater::kEmptyConfiguration, "",
        chromeos::NetworkUIData::ONC_SOURCE_USER_POLICY, _, _));

    EXPECT_CALL(network_library_, RemoveNetworkProfileObserver(_));

    policy.Erase(GetParam());
    provider_.UpdateChromePolicy(policy);
  }
  Mock::VerifyAndClearExpectations(&network_library_);
}

INSTANTIATE_TEST_CASE_P(
    NetworkConfigurationUpdaterTestInstance,
    NetworkConfigurationUpdaterTest,
    testing::Values(key::kDeviceOpenNetworkConfiguration,
                    key::kOpenNetworkConfiguration));

}  // namespace policy