1# Copyright (c) 2013 The Chromium OS 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"""Unit tests for server/cros/ap_configurators/ap_spec.py.
6"""
7
8import unittest
9
10from autotest_lib.server.cros.ap_configurators import \
11    ap_spec
12
13class APSpecTest(unittest.TestCase):
14    """Unit test for the ap_spec object."""
15
16    def test_default_creation(self):
17        """Test building a default ap_spec object."""
18        spec = ap_spec.APSpec()
19        self.assertEquals(spec.visible, True)
20        self.assertEquals(spec.security, ap_spec.DEFAULT_SECURITY_TYPE)
21        self.assertEquals(spec.band, ap_spec.DEFAULT_BAND)
22        self.assertEquals(spec.mode, ap_spec.DEFAULT_2GHZ_MODE)
23        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
24        self.assertIsNone(spec.password)
25
26
27    def test_only_set_band_2ghz(self):
28        """Test setting only the band to 2GHz."""
29        spec = ap_spec.APSpec(band=ap_spec.BAND_2GHZ)
30        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
31        self.assertEquals(spec.mode, ap_spec.DEFAULT_2GHZ_MODE)
32
33
34    def test_only_set_band_5ghz(self):
35        """Test setting only the band to 5GHz."""
36        spec = ap_spec.APSpec(band=ap_spec.BAND_5GHZ)
37        self.assertEquals(spec.channel, ap_spec.DEFAULT_5GHZ_CHANNEL)
38        self.assertEquals(spec.mode, ap_spec.DEFAULT_5GHZ_MODE)
39
40
41    def test_only_set_mode_2ghz(self):
42        """Test setting only a 2GHz mode."""
43        spec = ap_spec.APSpec(mode=ap_spec.MODE_B)
44        self.assertEquals(spec.band, ap_spec.DEFAULT_BAND)
45        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
46
47
48    def test_only_set_mode_5ghz(self):
49        """Test setting only a 5GHz mode."""
50        spec = ap_spec.APSpec(mode=ap_spec.MODE_A)
51        self.assertEquals(spec.band, ap_spec.BAND_5GHZ)
52        self.assertEquals(spec.channel, ap_spec.DEFAULT_5GHZ_CHANNEL)
53
54
55    def test_only_set_mode_n(self):
56        """Test setting the mode to N."""
57        spec = ap_spec.APSpec(mode=ap_spec.MODE_N)
58        self.assertEquals(spec.band, ap_spec.DEFAULT_BAND)
59        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
60
61
62    def test_only_set_channel_2ghz(self):
63        """Test setting only a 2GHz channel."""
64        spec = ap_spec.APSpec(channel=ap_spec.DEFAULT_2GHZ_CHANNEL)
65        self.assertEquals(spec.band, ap_spec.BAND_2GHZ)
66        self.assertEquals(spec.mode, ap_spec.DEFAULT_2GHZ_MODE)
67
68
69    def test_only_set_channel_5ghz(self):
70        """Test setting only a 5GHz channel."""
71        spec = ap_spec.APSpec(channel=ap_spec.DEFAULT_5GHZ_CHANNEL)
72        self.assertEquals(spec.band, ap_spec.BAND_5GHZ)
73        self.assertEquals(spec.mode, ap_spec.DEFAULT_5GHZ_MODE)
74
75
76    def test_set_band_and_mode_2ghz(self):
77        """Test setting the band and mode to valid 2GHz values."""
78        spec = ap_spec.APSpec(band=ap_spec.BAND_2GHZ, mode=ap_spec.MODE_G)
79        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
80
81
82    def test_set_band_and_mode_5ghz(self):
83        """Test setting the band and mode to valid 5GHz values."""
84        spec = ap_spec.APSpec(band=ap_spec.BAND_5GHZ, mode=ap_spec.MODE_A)
85        self.assertEquals(spec.channel, ap_spec.DEFAULT_5GHZ_CHANNEL)
86
87
88    def test_set_band_mode_and_channel_2ghz(self):
89        """Test setting the band and channel to valid 2GHz values."""
90        spec = ap_spec.APSpec(band=ap_spec.BAND_2GHZ, mode=ap_spec.MODE_N,
91                              channel=ap_spec.DEFAULT_2GHZ_CHANNEL)
92        self.assertNotEquals(spec.mode, ap_spec.DEFAULT_5GHZ_MODE)
93
94
95    def test_set_band_mode_and_channel_5ghz(self):
96        """Test setting the band and channel to valid 5GHz value."""
97        spec = ap_spec.APSpec(band=ap_spec.BAND_5GHZ, mode=ap_spec.MODE_N,
98                              channel=ap_spec.DEFAULT_5GHZ_CHANNEL)
99        self.assertNotEquals(spec.mode, ap_spec.DEFAULT_2GHZ_MODE)
100
101
102    def test_set_security_psk_default(self):
103        """Test setting security to WPAPSK."""
104        spec = ap_spec.APSpec(security=ap_spec.SECURITY_TYPE_WPAPSK)
105        self.assertEquals(spec.visible, True)
106        self.assertEquals(spec.security, ap_spec.SECURITY_TYPE_WPAPSK)
107        self.assertEquals(spec.band, ap_spec.DEFAULT_BAND)
108        self.assertEquals(spec.mode, ap_spec.DEFAULT_2GHZ_MODE)
109        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
110
111
112    def test_set_security_and_visibility(self):
113        """Test setting visibility to hidden and security to WPAPSK."""
114        spec = ap_spec.APSpec(visible=False,
115                              security=ap_spec.SECURITY_TYPE_WPAPSK)
116        self.assertEquals(spec.visible, False)
117        self.assertEquals(spec.security, ap_spec.SECURITY_TYPE_WPAPSK)
118        self.assertEquals(spec.band, ap_spec.DEFAULT_BAND)
119        self.assertEquals(spec.mode, ap_spec.DEFAULT_2GHZ_MODE)
120        self.assertEquals(spec.channel, ap_spec.DEFAULT_2GHZ_CHANNEL)
121        self.assertIsNotNone(spec.password)
122
123
124    def test_invalid_mode_and_band(self):
125        """Test setting mode and band to non-compatible settings."""
126        self.assertRaises(ValueError, ap_spec.APSpec,
127                          band=ap_spec.BAND_2GHZ, mode=ap_spec.MODE_A)
128
129
130    def test_invalid_channel_and_band(self):
131        """Test setting channel and band to non-compatible settings."""
132        self.assertRaises(ValueError, ap_spec.APSpec,
133                          band=ap_spec.BAND_5GHZ, channel=1)
134
135
136    def test_invalid_mode_and_channel(self):
137        """Test setting mode and channel to non-compatible settings."""
138        self.assertRaises(ValueError, ap_spec.APSpec,
139                          mode=ap_spec.MODE_G, channel=153)
140
141
142    def test_invalid_values(self):
143        """Test passing invalid values to an ap_spec object."""
144        self.assertRaises(ValueError, ap_spec.APSpec, band='foo')
145
146        self.assertRaises(ValueError, ap_spec.APSpec, mode=0x3)
147
148        self.assertRaises(ValueError, ap_spec.APSpec, channel=84)
149
150        self.assertRaises(ValueError, ap_spec.APSpec, security='foo')
151
152
153    def test_mode_string_generation(self):
154        """Test a set of mode constants a generates a human readable string."""
155        mode = ap_spec.mode_string_for_mode(ap_spec.MODE_B | ap_spec.MODE_G)
156        self.assertEquals('b/g', mode)
157
158        mode = ap_spec.mode_string_for_mode(ap_spec.MODE_B | ap_spec.MODE_G |
159                                            ap_spec.MODE_N)
160        self.assertEquals('b/g/n', mode)
161
162        mode = ap_spec.mode_string_for_mode(ap_spec.MODE_A)
163        self.assertEquals('a', mode)
164
165
166    def test_mode_n_on_both_bands(self):
167        """Test that band is maintained when setting a mode N spec."""
168        spec = ap_spec.APSpec(band=ap_spec.BAND_5GHZ, mode=ap_spec.MODE_N)
169        self.assertEquals(spec.band, ap_spec.BAND_5GHZ)
170        self.assertEquals(spec.mode, ap_spec.MODE_N)
171        spec = ap_spec.APSpec(band=ap_spec.BAND_2GHZ, mode=ap_spec.MODE_N)
172        self.assertEquals(spec.band, ap_spec.BAND_2GHZ)
173        self.assertEquals(spec.mode, ap_spec.MODE_N)
174
175
176if __name__ == '__main__':
177    unittest.main()
178