summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/chromeos_onc.py
blob: ef3387063cf137482bfadb6867a5a6cdbab452c8 (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
#!/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.DataDir(), 'chromeos', 'cros')

  def setUp(self):
    policy_base.PolicyTestBase.setUp(self)
    pyauto.PyUITest.RunSuperuserActionOnChromeOS('CleanFlimflamDir')

  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.SetPolicies({'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():
      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('network-wifi-none.onc')
    self._VerifyRememberedWifiNetworks(wifi_networks)

  def testONCAddWEPWifi(self):
    """Test adding WEP network."""
    wifi_networks = {
        'ssid-wep': 'WEP',
    }

    self._ReadONCFileAndSet('network-wifi-wep.onc')
    self._VerifyRememberedWifiNetworks(wifi_networks)

  def testONCAddPSKWifi(self):
    """Test adding WPA network."""
    wifi_networks = {
        'ssid-wpa': 'PSK',
    }
    self._ReadONCFileAndSet('network-wifi-wpa.onc')
    self._VerifyRememberedWifiNetworks(wifi_networks)

  def testAddBacktoBackONC(self):
    """Test adding three different ONC files one after the other."""
    wifi_networks = {
        'ssid-none': '',
        'ssid-wep': 'WEP',
        'ssid-wpa': 'PSK',
    }

    self._ReadONCFileAndSet('network-wifi-none.onc')
    self._ReadONCFileAndSet('network-wifi-wep.onc')
    self._ReadONCFileAndSet('network-wifi-wpa.onc')
    self._VerifyRememberedWifiNetworks(wifi_networks)

  def testAddONCWithUnknownFields(self):
    """Test adding an ONC file with unknown fields."""
    wifi_networks = {
        'ssid-none': '',
        'ssid-wpa': 'PSK'
    }

    self._ReadONCFileAndSet('network-multiple-unknown.onc')
    self._VerifyRememberedWifiNetworks(wifi_networks)


if __name__ == '__main__':
  pyauto_functional.Main()