Пример #1
0
def LegalCombosTest():
    r = list(
        autochannel.LegalCombos(allowed_freqs=[],
                                candidates=autochannel.C_ALL))
    wvtest.WVPASSEQ(r, [])
    r = list(
        autochannel.LegalCombos(allowed_freqs=[2432],
                                candidates=autochannel.C_ALL))
    wvtest.WVPASSEQ(r, [(2432, )])
    r = list(
        autochannel.LegalCombos(allowed_freqs=[2412, 2432, 2452],
                                candidates=autochannel.C_ALL))
    wvtest.WVPASSEQ(r, [(2412, 2432), (2412, ), (2432, ), (
        2432,
        2452,
    ), (2452, )])
    r = list(
        autochannel.LegalCombos(allowed_freqs=[2412, 2432, 2452],
                                candidates=autochannel.C_24MAIN))
    wvtest.WVPASSEQ(r, [(2412, 2432), (2412, ), (2432, )])
    r = list(
        autochannel.LegalCombos(allowed_freqs=range(5745, 5826, 20),
                                candidates=autochannel.C_ALL))
    wvtest.WVPASSEQ(r, [(5745, 5765, 5785, 5805), (5745, 5765), (5785, 5805),
                        (5745, ), (5765, ), (5785, ), (5805, ), (5825, )])
def ReadParameterTest():
  d = tempfile.mkdtemp()
  try:
    wc = waveguide.WifiblasterController([], d)
    testfile = os.path.join(d, 'wifiblaster.name')

    # Result should be equal in value and type (unless None).
    testcases = [('1\n', int, 1),
                 ('1.5\n', float, 1.5),
                 ('True\n', str, 'True\n'),
                 ('x\n', int, None)]
    for (read_data, typecast, expected) in testcases:
      open(testfile, 'w').write(read_data)
      result = wc._ReadParameter('name', typecast)
      wvtest.WVPASSEQ(result, expected)
      if result:
        wvtest.WVPASS(isinstance(result, typecast))

    # Make sure it works if the file isn't readable
    os.chmod(testfile, 0)
    result = wc._ReadParameter('name', int)
    wvtest.WVPASSEQ(result, None)

    # and if it generally doesn't exist
    os.unlink(testfile)
    result = wc._ReadParameter('name', str)
    wvtest.WVPASSEQ(result, None)
  finally:
    os.rmdir(d)
def LogWifiblasterResultsTest():
  old_wifiblaster_dir = waveguide.WIFIBLASTER_DIR
  waveguide.WIFIBLASTER_DIR = tempfile.mkdtemp()
  try:
    wc = waveguide.WifiblasterController([], '')

    # Set the consensus key to a known value.
    waveguide.consensus_key = 16 * 'x'

    stdout = ('version=1 mac=11:11:11:11:11:11 throughput=10000000 '
              'samples=5000000,15000000\n'
              'malformed 11:11:11:11:11:11 but has macs 11:11:11:11:11:11\n')
    expected = [('version=1 mac=11:11:11:11:11:11 throughput=10000000 '
                 'samples=5000000,15000000'),
                'malformed 11:11:11:11:11:11 but has macs 11:11:11:11:11:11']
    filename = os.path.join(waveguide.WIFIBLASTER_DIR, '11:11:11:11:11:11')
    # Should save only the second line, with leading timestamp
    wc._HandleResults(2, stdout, 'error')
    wvtest.WVPASSEQ(open(filename).read().split(' ', 1)[1], expected[1])
    # Should save the first line, with leading timestamp
    wc._HandleResults(0, stdout.split('\n')[0], 'error 2')
    wvtest.WVPASSEQ(open(filename).read().split(' ', 1)[1], expected[0])

  finally:
    shutil.rmtree(waveguide.WIFIBLASTER_DIR)
    waveguide.WIFIBLASTER_DIR = old_wifiblaster_dir
Пример #4
0
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)
Пример #5
0
def find_width_and_channel_test():
    wvtest.WVPASSEQ(('20', '6'), iw.find_width_and_channel('wcli0'))

    # Now, test the 'iw link' fallback.
    global INTERFACE_INFO_OUTPUT
    hold = INTERFACE_INFO_OUTPUT
    INTERFACE_INFO_OUTPUT = ''
    wvtest.WVPASSEQ(('20', '6'), iw.find_width_and_channel('wcli0'))
    INTERFACE_INFO_OUTPUT = hold
Пример #6
0
def get_all_frequencies_test():
    wvtest.WVPASSEQ([
        '2412', '2417', '2422', '2427', '2432', '2437', '2442', '2447', '2452',
        '2457', '2462'
    ], autochannel.get_all_frequencies('2.4'))

    wvtest.WVPASSEQ([
        '5180', '5200', '5220', '5240', '5745', '5765', '5785', '5805', '5825',
        '5260', '5280', '5300', '5320', '5500', '5520', '5540', '5560', '5580',
        '5660', '5680', '5700'
    ], autochannel.get_all_frequencies('5'))
Пример #7
0
def find_interface_from_band_test():
    wvtest.WVPASSEQ(
        'wlan0', iw.find_interface_from_band('2.4', iw.INTERFACE_TYPE.ap, ''))
    wvtest.WVPASSEQ('wlan1',
                    iw.find_interface_from_band('5', iw.INTERFACE_TYPE.ap, ''))
    wvtest.WVPASSEQ(
        'wcli0',
        iw.find_interface_from_band('2.4', iw.INTERFACE_TYPE.client, ''))
    # The output is from a device with no client interface on phy1.
    wvtest.WVPASSEQ(
        None, iw.find_interface_from_band('5', iw.INTERFACE_TYPE.client, ''))
Пример #8
0
def get_permitted_frequencies_test():
    wvtest.WVPASSEQ('2412 2432 2462',
                    autochannel.get_permitted_frequencies('2.4', 'ANY', '20'))
    wvtest.WVPASSEQ('5180 5745 5260 5500',
                    autochannel.get_permitted_frequencies('5', 'ANY', '80'))

    for case in ((2.4, 'ANY', '80'), ('2.4', 'LOW', '80'),
                 ('5', 'OVERLAP', '20'), ('60', 'INVALID_AUTOTYPE',
                                          '3.14'), (1, 2, 3)):
        wvtest.WVEXCEPT(ValueError, autochannel.get_permitted_frequencies,
                        *case)
Пример #9
0
def find_all_interfaces_from_band_test():
    wvtest.WVPASSEQ(set(['wlan0', 'wlan0_portal', 'wcli0']),
                    iw.find_all_interfaces_from_band('2.4'))
    wvtest.WVPASSEQ(
        set(['wlan0', 'wlan0_portal']),
        iw.find_all_interfaces_from_band('2.4', iw.INTERFACE_TYPE.ap))
    wvtest.WVPASSEQ(
        set(['wcli0']),
        iw.find_all_interfaces_from_band('2.4', iw.INTERFACE_TYPE.client))
    wvtest.WVPASSEQ(set(['wlan1', 'wlan1_portal']),
                    iw.find_all_interfaces_from_band('5'))
Пример #10
0
def find_all_interfaces_from_phy_test():
    wvtest.WVPASSEQ(set(['wlan0', 'wlan0_portal', 'wcli0']),
                    iw.find_all_interfaces_from_phy('phy0'))
    wvtest.WVPASSEQ(
        set(['wlan0', 'wlan0_portal']),
        iw.find_all_interfaces_from_phy('phy0', iw.INTERFACE_TYPE.ap))
    wvtest.WVPASSEQ(
        set(['wcli0']),
        iw.find_all_interfaces_from_phy('phy0', iw.INTERFACE_TYPE.client))
    wvtest.WVPASSEQ(set(['wlan1', 'wlan1_portal']),
                    iw.find_all_interfaces_from_phy('phy1'))
Пример #11
0
def hostapd_options_logging_test():
    """Test bandsteering.hostapd_options when when logging only."""
    unused_raii = experiment_testutils.MakeExperimentDirs()
    experiment_testutils.enable('WifiHostapdLogging')
    bandsteering_dir = bandsteering._BANDSTEERING_DIR

    wvtest.WVPASSEQ(
        ['-L', os.path.join(bandsteering_dir, '2.4_30abcc9ec8')],
        bandsteering.hostapd_options('2.4', 'my_ssid'))
    wvtest.WVPASSEQ(
        ['-L', os.path.join(bandsteering_dir, '5_30abcc9ec8')],
        bandsteering.hostapd_options('5', 'my_ssid'))
Пример #12
0
def phy_bands_test():
    # phy0 claims to support 5 GHz, but phy1 only supports 5 GHz and so it
    # supercedes it.
    wvtest.WVPASSEQ(set(['2.4']), iw.phy_bands('phy0'))
    wvtest.WVPASSEQ(set(['5']), iw.phy_bands('phy1'))

    # Now remove phy1 from the 'iw phy' output and see that phy0 gets both bands.
    global PHY_OUTPUT
    hold = PHY_OUTPUT
    PHY_OUTPUT = PHY_OUTPUT[PHY_OUTPUT.find('Wiphy phy0'):]
    wvtest.WVPASSEQ(set(['2.4', '5']), iw.phy_bands('phy0'))
    PHY_OUTPUT = hold
Пример #13
0
def sanitize_ssid_test():
    """Tests utils.sanitize_ssid."""
    wvtest.WVPASSEQ('foo', utils.sanitize_ssid('foo'))
    wvtest.WVPASSEQ('foo', utils.sanitize_ssid('foo\n\0'))
    hebrew = (
        '\xd7\xa0\xd6\xb0\xd7\xa7\xd6\xbb\xd7\x93\xd6\xbc\xd7\x95\xd6\xb9'
        '\xd7\xaa')
    unicode_control_char = u'\u200e'.encode('utf-8')
    non_utf8 = '\x97'
    wvtest.WVPASSEQ(
        hebrew,
        utils.sanitize_ssid(''.join((hebrew, unicode_control_char, non_utf8))))
Пример #14
0
def get_interface_test():
  old_get_quantenna_interfaces = quantenna._get_quantenna_interfaces
  old_get_external_mac = quantenna._get_external_mac
  quantenna._get_quantenna_interfaces = lambda: 'wlan0 3\nwlan0_portal 4\n'
  quantenna._get_external_mac = lambda _: '00:00:00:00:00:00'
  wvtest.WVPASSEQ(quantenna._get_interface('ap', ''),
                  ('wlan0', 'wifi1', '00:00:00:00:00:00', 3))
  wvtest.WVPASSEQ(quantenna._get_interface('ap', '_portal'),
                  ('wlan0_portal', 'wifi2', '00:00:00:00:00:00', 4))
  wvtest.WVPASSEQ(quantenna._get_interface('sta', ''),
                  (None, None, None, None))
  quantenna._get_external_mac = old_get_external_mac
  quantenna._get_quantenna_interfaces = old_get_quantenna_interfaces
Пример #15
0
def increment_mac_address_test():
    """Test increment_mac_address."""

    wvtest.WVPASSEQ('12:34:56:78:90:13',
                    utils.increment_mac_address('12:34:56:78:90:12'))
    wvtest.WVPASSEQ('12:34:56:78:91:00',
                    utils.increment_mac_address('12:34:56:78:90:FF'))

    # b/34050122
    wvtest.WVPASSEQ('00:0b:6b:ed:eb:ad',
                    utils.increment_mac_address('00:0b:6b:ed:eb:ac'))

    # b/34050122 (initial misunderstanding of bug, but still worth testing)
    wvtest.WVPASSEQ('00:00:00:00:00:00',
                    utils.increment_mac_address('ff:ff:ff:ff:ff:ff'))
Пример #16
0
def client_interface_name_test():
    wvtest.WVPASSEQ('wcli0', wifi.client_interface_name('phy0', ''))

    try:
        hold = iw_test.DEV_OUTPUT
        iw_test.DEV_OUTPUT = """phy#1
      Interface wlan0
        ifindex 5
        wdev 0x1
        addr 88:dc:96:08:60:2c
        type AP
    """
        wvtest.WVPASSEQ('wcli0', wifi.client_interface_name('phy1', ''))
    finally:
        iw_test.DEV_OUTPUT = hold
def test_process_query_update_recent_hit_time():
    qry = '123456789 www.yahoo.com'
    cache_warming.hit_log = {'www.yahoo.com': (1, '987654321')}
    cache_warming.process_query(qry)
    expected = '123456789'
    actual = cache_warming.hit_log['www.yahoo.com'][1]
    wvtest.WVPASSEQ(actual, expected)
def test_process_query_update_hit_count():
    qry = '123456789 www.yahoo.com'
    cache_warming.hit_log = {'www.yahoo.com': (1, '123456789')}
    cache_warming.process_query(qry)
    expected = 2
    actual = cache_warming.hit_log['www.yahoo.com'][0]
    wvtest.WVPASSEQ(actual, expected)
def test_process_query_first_hit():
    qry = '123456789 www.yahoo.com'
    expected = {'www.yahoo.com': (1, '123456789')}
    cache_warming.hit_log = {}
    cache_warming.process_query(qry)
    actual = cache_warming.hit_log
    wvtest.WVPASSEQ(actual, expected)
Пример #20
0
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)
Пример #21
0
def TaxonomyTest():
    taxonomy.dhcp.DHCP_LEASES_FILE = 'fake/dhcp.leases'
    clientinfo.FINGERPRINTS_DIR = 'fake/taxonomy'
    wvtest.WVPASS('Nexus 6' in clientinfo.taxonomize('00:00:01:00:00:01'))
    wvtest.WVPASS('Nexus 6' in clientinfo.taxonomize('00:00:01:00:00:01\n'))
    v = 'Moto G or Moto X'
    wvtest.WVPASS(v in clientinfo.taxonomize('9c:d9:17:00:00:02'))
    wvtest.WVPASSEQ(clientinfo.taxonomize('00:00:22:00:00:22'), None)
def test_sort_hit_log_non_empty():
    cache_warming.hit_log = {
        'www.google.com': (2, '123456789'),
        'www.yahoo.com': (1, '987654321'),
        'www.espn.com': (3, '135792468')
    }
    expected = ['www.espn.com', 'www.google.com', 'www.yahoo.com']
    actual = cache_warming.sort_hit_log()
    wvtest.WVPASSEQ(actual, expected)
Пример #23
0
def wpa_passphrase_test():
    """Make sure the configs we generate are the same as wpa_passphrase."""
    for testdata in (('some ssid', 'some passphrase'),
                     (r'some\ssid', 'some passphrase'), ('some ssid',
                                                         r'some\passphrase'),
                     ('some"ssid', 'some passphrase'), ('some ssid',
                                                        'some"passphrase')):
        got, want = wpa_passphrase(*testdata), wpa_passphrase_subprocess(
            *testdata)
        wvtest.WVPASSEQ(got, want)
Пример #24
0
 def lock_until_qi_nonempty(qi, qo, timeout_sec):
     try:
         utils.lock(lockfile, timeout_sec)
     except utils.BinWifiException:
         qo.put('timed out')
         return
     qo.put('acquired')
     wvtest.WVPASSEQ(qi.get(), 'release')
     qo.put('released')
     sys.exit(0)
Пример #25
0
def IwTimeoutTest():
    old_timeout = waveguide.IW_TIMEOUT_SECS
    waveguide.IW_TIMEOUT_SECS = 1
    old_path = os.environ['PATH']
    os.environ['PATH'] = 'fake:' + os.environ['PATH']
    waveguide.RunProc(
        lambda e, so, se: wvtest.WVPASSEQ(e, -9),
        ['iw', 'sleepn', str(waveguide.IW_TIMEOUT_SECS + 1)])
    os.environ['PATH'] = old_path
    waveguide.IW_TIMEOUT_SECS = old_timeout
Пример #26
0
def info_parsed_test():
    wvtest.WVPASSEQ(
        {
            'wdev': '0x3',
            'wiphy': '0',
            'addr': '88:dc:96:08:60:2d',
            'width': '20',
            'ifindex': '20',
            'type': 'managed',
            'channel': '6'
        }, iw.info_parsed('wcli0'))
Пример #27
0
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))
Пример #28
0
def wpa_raw_psk_test():
    """Make sure we do the right thing when we get a raw PSK too."""
    ssid = 'some ssid'
    psk = '41821f7ca3ea5d85beea7644ed7e0fefebd654177fa06c26fbdfdc3c599a317f'

    got = wpa_passphrase(ssid, psk)
    # hard code since `wpa_passphrase` dies if given a 64-character hex psk
    want = """network={
\tssid="some ssid"
\tpsk=41821f7ca3ea5d85beea7644ed7e0fefebd654177fa06c26fbdfdc3c599a317f
}
"""
    wvtest.WVPASSEQ(got, want)
def test_hit_log_subset():
    hosts = ['www.google.com', 'www.yahoo.com']
    cache_warming.hit_log = cache_warming.hit_log = {
        'www.youtube.com': (4, '987654321'),
        'www.google.com': (1, '987654321'),
        'www.espn.com': (3, '123456789'),
        'www.yahoo.com': (2, '135792468')
    }
    expected = {
        'www.yahoo.com': (2, '135792468'),
        'www.google.com': (1, '987654321')
    }
    actual = cache_warming.hit_log_subset(hosts)
    wvtest.WVPASSEQ(actual, expected)
Пример #30
0
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)