Ejemplo n.º 1
0
def test_add_adblock_entry_without_force_multiple_names(tmpdir):
    """
    Test that addition of an adblock entry does not succeed if force is not set
    and there is a matching name
    """
    ipv4_line = '0.0.0.0 example2.com example3.com'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(ipv4_line)
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com')
    hosts_entries.add(entries=[new_entry], force=False)
    assert hosts_entries.exists(names=['example2.com'])
Ejemplo n.º 2
0
def test_add_adblock_entry_with_force_single_name(tmpdir):
    """
    Test that an addition of an adblock entry replaces one with a matching name
    if force is True
    """
    ipv4_line = '0.0.0.0 example2.com example3.com'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(ipv4_line)
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com')
    hosts_entries.add(entries=[new_entry], force=True)
    assert hosts_entries.exists(names=['example.com'])
Ejemplo n.º 3
0
def test_add_adblock_entry_with_force_single_name(tmpdir):
    """
    Test that an addition of an adblock entry replaces one with a matching name
    if force is True
    """
    ipv4_line = '0.0.0.0 example2.com example3.com'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(ipv4_line)
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com')
    hosts_entries.add(entries=[new_entry], force=True)
    assert hosts_entries.exists(names=['example.com'])
Ejemplo n.º 4
0
def test_add_adblock_entry_without_force_with_target_having_multiple_names(tmpdir):
    """
    Test that addition of an adblock entry does not succeed if force is not set
    and there is a matching name (matching names)
    """
    ipv4_line = '0.0.0.0 example.com'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(ipv4_line)
    hosts_entries = Hosts(path=hosts_file.strpath)
    assert hosts_entries.exists(address='0.0.0.0')
    new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com')
    hosts_entries.add(entries=[new_entry])
Ejemplo n.º 5
0
def test_remove_all_matching_with_non_loopback_address_and_multiple_names(tmpdir):
    """
    Test that a forced addition of an adblock entry with multiple names first removes 'all' matching names
    """
    entries = '1.2.1.1 example.com example2.com\n# this is a comment\n\n3.4.5.6 random.com example2.com\n'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(entries)
    hosts_entries = Hosts(path=hosts_file.strpath)
    # assert hosts_entries.exists(address='0.0.0.0')
    new_entry = HostsEntry.str_to_hostentry('8.9.10.11 example.com example2.com')
    hosts_entries.add(entries=[new_entry], force=True)
    assert hosts_entries.count() == 3  # 1 address, 1 comment and 1 blank
    assert not hosts_entries.exists(address='3.4.5.6')
    assert hosts_entries.exists(names=['example.com'])
Ejemplo n.º 6
0
def test_remove_all_matching_with_non_loopback_address_and_multiple_names(tmpdir):
    """
    Test that a forced addition of an adblock entry with multiple names first removes 'all' matching names
    """
    entries = '1.2.1.1 example.com example2.com\n# this is a comment\n\n3.4.5.6 random.com example2.com\n'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(entries)
    hosts_entries = Hosts(path=hosts_file.strpath)
    # assert hosts_entries.exists(address='0.0.0.0')
    new_entry = HostsEntry.str_to_hostentry('8.9.10.11 example.com example2.com')
    hosts_entries.add(entries=[new_entry], force=True)
    assert hosts_entries.count() == 3  # 1 address, 1 comment and 1 blank
    assert not hosts_entries.exists(address='3.4.5.6')
    assert hosts_entries.exists(names=['example.com'])
Ejemplo n.º 7
0
def test_write_will_create_path_if_missing():
    """
    Test that the hosts file declared when constructing a Hosts instance will
    be created if it doesn't exist
    """
    now = datetime.datetime.now()
    timestamp = now.strftime('%Y%m%d%H%M%S')
    hosts_path = '/tmp/testwrite.{0}'.format(timestamp)
    hosts = Hosts(path=hosts_path)
    entry = HostsEntry.str_to_hostentry('1.2.3.4 example.com example.org')
    hosts.add(entries=[entry])
    hosts.write()
    hosts2 = Hosts(path=hosts_path)
    os.remove(hosts_path)
    assert hosts2.exists(address='1.2.3.4')
Ejemplo n.º 8
0
def test_write_will_create_path_if_missing():
    """
    Test that the hosts file declared when constructing a Hosts instance will
    be created if it doesn't exist
    """
    now = datetime.datetime.now()
    timestamp = now.strftime('%Y%m%d%H%M%S')
    hosts_path = '/tmp/testwrite.{0}'.format(timestamp)
    hosts = Hosts(path=hosts_path)
    entry = HostsEntry.str_to_hostentry('1.2.3.4 example.com example.org')
    hosts.add(entries=[entry])
    hosts.write()
    hosts2 = Hosts(path=hosts_path)
    os.remove(hosts_path)
    assert hosts2.exists(address='1.2.3.4')
Ejemplo n.º 9
0
def test_str_to_hostentry_returns_fails_with_false():
    result = HostsEntry.str_to_hostentry('invalid example.com example')
    assert not result
Ejemplo n.º 10
0
def test_str_to_hostentry_ipv6():
    str_entry = HostsEntry.str_to_hostentry('2001:0db8:85a3:0042:1000:8a2e:0370:7334 example.com example')
    assert str_entry.entry_type == 'ipv6'
    assert str_entry.address == '2001:0db8:85a3:0042:1000:8a2e:0370:7334'
    assert str_entry.names == ['example.com', 'example']
Ejemplo n.º 11
0
def test_str_to_hostentry_ipv4():
    str_entry = HostsEntry.str_to_hostentry('10.10.10.10 example.com example.org example')
    assert str_entry.entry_type == 'ipv4'
    assert str_entry.address == '10.10.10.10'
    assert str_entry.names == ['example.com', 'example.org', 'example']