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
|
#!/usr/bin/env python
# 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.
import os
import pyauto_functional # must come before pyauto.
import policy_base
import pyauto
class ChromeosONC(policy_base.PolicyTestBase):
"""
Tests for Open Network Configuration (ONC).
Open Network Configuration (ONC) files is a json dictionary
that contains network configurations and is pulled via policies.
These tests verify that ONC files that are formatted correctly
add the network/certificate to the device.
"""
ONC_PATH = os.path.join(pyauto.PyUITest.ChromeOSDataDir(), 'network')
def setUp(self):
self.CleanupFlimflamDirsOnChromeOS()
policy_base.PolicyTestBase.setUp(self)
self.LoginWithTestAccount()
def _ReadONCFileAndSet(self, filename):
"""Reads the specified ONC file and sends it as a policy.
Inputs:
filename: The filename of the ONC file. ONC files should
all be stored in the path defined by ONC_PATH.
"""
with open(os.path.join(self.ONC_PATH, filename)) as fp:
self.SetUserPolicy({'OpenNetworkConfiguration': fp.read()})
def _VerifyRememberedWifiNetworks(self, wifi_expect):
"""Verify the list of remembered networks contains those in wifi_expect.
Inputs:
wifi_expect: A dictionary of wifi networks where the key is the ssid
and the value is the encryption type of the network.
"""
# Sometimes there is a race condition where upon restarting chrome
# NetworkScan has not populated the network lists yet. We should
# scan until the device is online.
self.WaitUntil(lambda: not self.NetworkScan().get('offline_mode', True))
networks = self.NetworkScan()
# Temprorary dictionary to keep track of which wifi networks
# have been visited by removing them as we see them.
wifi_expect_temp = dict(wifi_expect)
for service, wifi_dict in networks['remembered_wifi'].iteritems():
if isinstance(wifi_dict, dict) and \
'encryption' in wifi_dict and \
'name' in wifi_dict:
msg = ('Wifi network %s was in the remembered_network list but '
'shouldn\'t be.' % wifi_dict['name'])
# wifi_dict['encryption'] will always be a string and not None.
self.assertTrue(wifi_expect.get(wifi_dict['name'], None) ==
wifi_dict['encryption'], msg)
del wifi_expect_temp[wifi_dict['name']]
# Error if wifi_expect_temp is not empty.
self.assertFalse(wifi_expect_temp, 'The following networks '
'were not remembered: %s' % self.pformat(wifi_expect_temp))
def testONCAddOpenWifi(self):
"""Test adding open network."""
wifi_networks = {
'ssid-none': '',
}
self._ReadONCFileAndSet('toplevel_wifi_open.onc')
self._VerifyRememberedWifiNetworks(wifi_networks)
def testONCAddWEPWifi(self):
"""Test adding WEP network."""
wifi_networks = {
'ssid-wep': 'WEP',
}
self._ReadONCFileAndSet('toplevel_wifi_wep_proxy.onc')
self._VerifyRememberedWifiNetworks(wifi_networks)
def testONCAddPSKWifi(self):
"""Test adding WPA network."""
wifi_networks = {
'ssid-wpa': 'WPA',
}
self._ReadONCFileAndSet('toplevel_wifi_wpa_psk.onc')
self._VerifyRememberedWifiNetworks(wifi_networks)
def testAddBacktoBackONC(self):
"""Test adding three different ONC files one after the other."""
test_dict = {
'toplevel_wifi_open.onc': { 'ssid-none': '' },
'toplevel_wifi_wep_proxy.onc': { 'ssid-wep': 'WEP' },
'toplevel_wifi_wpa_psk.onc': { 'ssid-wpa': 'WPA' },
}
for onc, wifi_networks in test_dict.iteritems():
self._ReadONCFileAndSet(onc)
self._VerifyRememberedWifiNetworks(wifi_networks)
def testAddBacktoBackONC2(self):
"""Test adding three different ONC files one after the other.
Due to inconsistent behaviors as addressed in crosbug.com/27862
this test does not perform a network scan/verification between
the setting of policies.
"""
wifi_networks = {
'ssid-wpa': 'WPA',
}
self._ReadONCFileAndSet('toplevel_wifi_open.onc')
self._ReadONCFileAndSet('toplevel_wifi_wep_proxy.onc')
self._ReadONCFileAndSet('toplevel_wifi_wpa_psk.onc')
# Verify that only the most recent onc is updated.
self._VerifyRememberedWifiNetworks(wifi_networks)
def testAddONCWithUnknownFields(self):
"""Test adding an ONC file with unknown fields."""
wifi_networks = {
'ssid-none': '',
'ssid-wpa': 'WPA'
}
self._ReadONCFileAndSet('toplevel_with_unknown_fields.onc')
self._VerifyRememberedWifiNetworks(wifi_networks)
if __name__ == '__main__':
pyauto_functional.Main()
|