1#!/usr/bin/env python 2# Copyright (c) 2012 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import os 7 8import pyauto_functional # must come before pyauto. 9import policy_base 10import pyauto 11 12 13class ChromeosONC(policy_base.PolicyTestBase): 14 """ 15 Tests for Open Network Configuration (ONC). 16 17 Open Network Configuration (ONC) files is a json dictionary 18 that contains network configurations and is pulled via policies. 19 These tests verify that ONC files that are formatted correctly 20 add the network/certificate to the device. 21 """ 22 23 ONC_PATH = os.path.join(pyauto.PyUITest.ChromeOSDataDir(), 'network') 24 25 def setUp(self): 26 self.CleanupFlimflamDirsOnChromeOS() 27 policy_base.PolicyTestBase.setUp(self) 28 self.LoginWithTestAccount() 29 30 def _ReadONCFileAndSet(self, filename): 31 """Reads the specified ONC file and sends it as a policy. 32 33 Inputs: 34 filename: The filename of the ONC file. ONC files should 35 all be stored in the path defined by ONC_PATH. 36 """ 37 with open(os.path.join(self.ONC_PATH, filename)) as fp: 38 self.SetUserPolicy({'OpenNetworkConfiguration': fp.read()}) 39 40 def _VerifyRememberedWifiNetworks(self, wifi_expect): 41 """Verify the list of remembered networks contains those in wifi_expect. 42 43 Inputs: 44 wifi_expect: A dictionary of wifi networks where the key is the ssid 45 and the value is the encryption type of the network. 46 """ 47 # Sometimes there is a race condition where upon restarting chrome 48 # NetworkScan has not populated the network lists yet. We should 49 # scan until the device is online. 50 self.WaitUntil(lambda: not self.NetworkScan().get('offline_mode', True)) 51 networks = self.NetworkScan() 52 53 # Temprorary dictionary to keep track of which wifi networks 54 # have been visited by removing them as we see them. 55 wifi_expect_temp = dict(wifi_expect) 56 57 for service, wifi_dict in networks['remembered_wifi'].iteritems(): 58 if isinstance(wifi_dict, dict) and \ 59 'encryption' in wifi_dict and \ 60 'name' in wifi_dict: 61 62 msg = ('Wifi network %s was in the remembered_network list but ' 63 'shouldn\'t be.' % wifi_dict['name']) 64 65 # wifi_dict['encryption'] will always be a string and not None. 66 self.assertTrue(wifi_expect.get(wifi_dict['name'], None) == 67 wifi_dict['encryption'], msg) 68 69 del wifi_expect_temp[wifi_dict['name']] 70 71 # Error if wifi_expect_temp is not empty. 72 self.assertFalse(wifi_expect_temp, 'The following networks ' 73 'were not remembered: %s' % self.pformat(wifi_expect_temp)) 74 75 def testONCAddOpenWifi(self): 76 """Test adding open network.""" 77 wifi_networks = { 78 'ssid-none': '', 79 } 80 81 self._ReadONCFileAndSet('toplevel_wifi_open.onc') 82 self._VerifyRememberedWifiNetworks(wifi_networks) 83 84 def testONCAddWEPWifi(self): 85 """Test adding WEP network.""" 86 wifi_networks = { 87 'ssid-wep': 'WEP', 88 } 89 90 self._ReadONCFileAndSet('toplevel_wifi_wep_proxy.onc') 91 self._VerifyRememberedWifiNetworks(wifi_networks) 92 93 def testONCAddPSKWifi(self): 94 """Test adding WPA network.""" 95 wifi_networks = { 96 'ssid-wpa': 'WPA', 97 } 98 self._ReadONCFileAndSet('toplevel_wifi_wpa_psk.onc') 99 self._VerifyRememberedWifiNetworks(wifi_networks) 100 101 def testAddBacktoBackONC(self): 102 """Test adding three different ONC files one after the other.""" 103 test_dict = { 104 'toplevel_wifi_open.onc': { 'ssid-none': '' }, 105 'toplevel_wifi_wep_proxy.onc': { 'ssid-wep': 'WEP' }, 106 'toplevel_wifi_wpa_psk.onc': { 'ssid-wpa': 'WPA' }, 107 } 108 109 for onc, wifi_networks in test_dict.iteritems(): 110 self._ReadONCFileAndSet(onc) 111 self._VerifyRememberedWifiNetworks(wifi_networks) 112 113 def testAddBacktoBackONC2(self): 114 """Test adding three different ONC files one after the other. 115 116 Due to inconsistent behaviors as addressed in crosbug.com/27862 117 this test does not perform a network scan/verification between 118 the setting of policies. 119 """ 120 121 wifi_networks = { 122 'ssid-wpa': 'WPA', 123 } 124 125 self._ReadONCFileAndSet('toplevel_wifi_open.onc') 126 self._ReadONCFileAndSet('toplevel_wifi_wep_proxy.onc') 127 self._ReadONCFileAndSet('toplevel_wifi_wpa_psk.onc') 128 129 # Verify that only the most recent onc is updated. 130 self._VerifyRememberedWifiNetworks(wifi_networks) 131 132 def testAddONCWithUnknownFields(self): 133 """Test adding an ONC file with unknown fields.""" 134 wifi_networks = { 135 'ssid-none': '', 136 'ssid-wpa': 'WPA' 137 } 138 139 self._ReadONCFileAndSet('toplevel_with_unknown_fields.onc') 140 self._VerifyRememberedWifiNetworks(wifi_networks) 141 142 143if __name__ == '__main__': 144 pyauto_functional.Main() 145