def condition_test(): """Test basic Condition functionality.""" x = y = 0 callback_sink = [] cx = ratchet.Condition('cx', lambda: x != 0, 0) cy = ratchet.Condition('cx', lambda: y != 0, 0.1, callback=lambda: callback_sink.append([0])) wvtest.WVEXCEPT(ratchet.TimeoutException, cx.check) wvtest.WVFAIL(cy.check()) time.sleep(0.1) wvtest.WVEXCEPT(ratchet.TimeoutException, cy.check) x = 1 wvtest.WVEXCEPT(ratchet.TimeoutException, cx.check) cx.reset() wvtest.WVPASS(cx.check()) y = 1 cy.reset() wvtest.WVPASS(cy.check()) wvtest.WVPASSEQ(len(callback_sink), 1) # Callback shouldn't fire again. wvtest.WVPASS(cy.check()) wvtest.WVPASSEQ(len(callback_sink), 1) cy.reset() wvtest.WVPASS(cy.check()) wvtest.WVPASSEQ(len(callback_sink), 2)
def EaterTest(): e = wgdata.Eater('abcdefg') wvtest.WVPASSEQ(e.Eat(2), 'ab') wvtest.WVEXCEPT(wgdata.DecodeError, e.Eat, 10) wvtest.WVPASSEQ(e.Unpack('!3s'), ('cde', )) wvtest.WVPASSEQ(e.Remainder(), 'fg') wvtest.WVPASSEQ(e.Remainder(), '') e = wgdata.Eater('\x01\x02\x03\x04\x05\x06') wvtest.WVPASSEQ(list(e.Iter('!H', 4)), [(0x0102, ), (0x0304, )]) wvtest.WVPASSEQ(list(e.Iter('!B', 1)), [(0x05, )]) wvtest.WVEXCEPT(wgdata.DecodeError, lambda: list(e.Iter('!B', 2))) wvtest.WVPASSEQ(e.Remainder(), '\x06')
def file_condition_test(): """Test File*Condition functionality.""" _, filename = tempfile.mkstemp() c_exists = ratchet.FileExistsCondition('c exists', filename, 0.1) c_touched = ratchet.FileTouchedCondition('c touched', filename, 0.1) wvtest.WVPASS(c_exists.check()) wvtest.WVFAIL(c_touched.check()) # File mtime resolution isn't fine enough to see the difference between this # write and the previous one, so sleep for a short time before writing to # ensure a different mtime. time.sleep(0.01) open(filename, 'w') wvtest.WVPASS(c_touched.check()) # Test that pre-existing files don't count. c_touched.reset() wvtest.WVFAIL(c_touched.check()) time.sleep(0.1) wvtest.WVEXCEPT(ratchet.TimeoutException, c_touched.check) # Test that deleting files doesn't count. c_touched.reset() wvtest.WVFAIL(c_touched.check()) os.unlink(filename) wvtest.WVFAIL(c_touched.check())
def validate_set_options_test(): """Tests utils.validate_set_options.""" os.environ['WIFI_PSK'] = 'NOT_USED' for case in _VALIDATION_PASS: try: utils.validate_set_wifi_options(make_optdict(**case)) wvtest.WVPASS(True) # Make WvTest count this as a test. except utils.BinWifiException: wvtest.WVFAIL('Test failed.') for case in _VALIDATION_FAIL: wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_set_wifi_options, make_optdict(**case)) # Test failure when WIFI_PSK is missing del os.environ['WIFI_PSK'] wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_set_wifi_options, make_optdict(**_VALIDATION_PASS[0])) wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_set_wifi_options, make_optdict(encryption='WEP'))
def validate_and_sanitize_psk_test(): """Tests utils.validate_and_sanitize_psk.""" # Too short. wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_and_sanitize_psk, 'foo') # Too long. wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_and_sanitize_psk, '0' * 65) # Not ASCII. wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_and_sanitize_psk, 'abcdefgh\xd7\xa0') # Not hex. wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_and_sanitize_psk, 'g' * 64) # Too short after control characters removed. wvtest.WVEXCEPT(utils.BinWifiException, utils.validate_and_sanitize_psk, 'foobar\n\0') wvtest.WVPASSEQ('foobarba', utils.validate_and_sanitize_psk('foobarba\n\0')) wvtest.WVPASSEQ('0' * 64, utils.validate_and_sanitize_psk('0' * 64)) wvtest.WVPASSEQ('g' * 63, utils.validate_and_sanitize_psk('g' * 63))
def ratchet_test(): """Test Ratchet functionality.""" class P(object): X = 'X' Y = 'Y' Z = 'Z' status.P = P status.IMPLICATIONS = {} status_export_path = tempfile.mkdtemp() try: x = y = z = 0 r = ratchet.Ratchet('test ratchet', [ ratchet.Condition('x', lambda: x, 0.1), ratchet.Condition('y', lambda: y, 0.1), ratchet.Condition('z', lambda: z, 0.1), ], status.Status('test ratchet', status_export_path)) x = y = 1 # Test that timeouts are not just summed, but start whenever the previous # step completed. wvtest.WVPASSEQ(r._current_step, 0) # pylint: disable=protected-access wvtest.WVFAIL(os.path.isfile(os.path.join(status_export_path, 'X'))) wvtest.WVFAIL(os.path.isfile(os.path.join(status_export_path, 'Y'))) wvtest.WVFAIL(os.path.isfile(os.path.join(status_export_path, 'Z'))) r.start() wvtest.WVPASS(os.path.isfile(os.path.join(status_export_path, 'X'))) wvtest.WVFAIL(os.path.isfile(os.path.join(status_export_path, 'Y'))) wvtest.WVFAIL(os.path.isfile(os.path.join(status_export_path, 'Z'))) time.sleep(0.05) wvtest.WVFAIL(r.check()) wvtest.WVPASSEQ(r._current_step, 2) # pylint: disable=protected-access wvtest.WVPASS(os.path.isfile(os.path.join(status_export_path, 'X'))) wvtest.WVPASS(os.path.isfile(os.path.join(status_export_path, 'Y'))) wvtest.WVPASS(os.path.isfile(os.path.join(status_export_path, 'Z'))) wvtest.WVFAIL(r.check()) wvtest.WVPASSEQ(r._current_step, 2) # pylint: disable=protected-access time.sleep(0.1) wvtest.WVEXCEPT(ratchet.TimeoutException, r.check) x = y = z = 1 r.start() wvtest.WVPASS(r.check()) finally: shutil.rmtree(status_export_path)
def generate_wpa_supplicant_config_test(): if subprocess.call(('which', 'wpa_passphrase')) != 0: utils.log( "Can't test generate_wpa_supplicant_config without wpa_passphrase." ) return for band in ('2.4', '5'): opt = FakeOptDict() opt.band = band got = configs.generate_wpa_supplicant_config('some ssid', 'some passphrase', opt) want = _WPA_SUPPLICANT_CONFIG.format(freq_list=_FREQ_LIST[band]) wvtest.WVPASSEQ(want, got) opt.bssid = 'TotallyNotValid' wvtest.WVEXCEPT(utils.BinWifiException, configs.generate_wpa_supplicant_config, 'some ssid', 'some passphrase', opt) opt.bssid = '12:34:56:78:90:Ab' got = configs.generate_wpa_supplicant_config('some ssid', 'some passphrase', opt) want = _WPA_SUPPLICANT_CONFIG_BSSID.format(freq_list=_FREQ_LIST[band]) wvtest.WVPASSEQ(want, got) got = configs.generate_wpa_supplicant_config('some ssid', None, opt) want = _WPA_SUPPLICANT_CONFIG_BSSID_UNSECURED.format( freq_list=_FREQ_LIST[band]) wvtest.WVPASSEQ(want, got) opt.no_band_restriction = True opt.bssid = None got = configs.generate_wpa_supplicant_config('some ssid', 'some passphrase', opt) want = _WPA_SUPPLICANT_CONFIG_NO_FREQ_LIST wvtest.WVPASSEQ(want, got)