def test_replace_ipv4_host_where_name_differs(tmpdir): """ Test replacement of an ipv4 entry where just the name differs """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['example2.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.132') assert hosts_entries.exists(names=['example2.com', 'example'])
def test_addition_of_ipv4_entry_where_matching_exists_and_force_true(tmpdir): """ Test replacement of an ipv4 entry where just the address differs """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.132') assert hosts_entries.exists(names=['something.com', 'example'])
def test_addition_of_ipv4_entry_where_matching_exists_and_force_true(tmpdir): """ Test replacement of an ipv4 entry where just the address differs """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.132') assert hosts_entries.exists(names=['something.com', 'example'])
def test_replace_ipv4_host_where_name_differs(tmpdir): """ Test replacement of an ipv4 entry where just the name differs """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['example2.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.132') assert hosts_entries.exists(names=['example2.com', 'example'])
def test_remove_existing_entry_using_address_only(tmpdir): """ Test removal of an existing entry using ip4 address only """ entries = '1.2.3.4 example.com example\n# this is a comment' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4') assert not hosts_entries.exists(address='1.2.3.4')
def test_remove_existing_entry_using_address_only(tmpdir): """ Test removal of an existing entry using ip4 address only """ entries = '1.2.3.4 example.com example\n# this is a comment' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4') assert not hosts_entries.exists(address='1.2.3.4')
def test_remove_existing_ipv4_address_using_hostsentry(tmpdir): """ Test removal of an existing ip4 address """ ipv4_line = '1.2.3.4 example.com example' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4', name='example.com') assert not hosts_entries.exists(address='1.2.3.4') assert not hosts_entries.exists(names=['example.com'])
def test_add_adblock_entry_with_force_with_target_having_multiple_names(tmpdir): """ Test that an addition of an adblock entry replaces one with a matching name if force is True (multiple names) """ ipv4_line = '0.0.0.0 example.com example2.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 example2.com') hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(names=['example2.com'])
def test_remove_existing_ipv4_address_using_hostsentry(tmpdir): """ Test removal of an existing ip4 address """ ipv4_line = '1.2.3.4 example.com example' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(address='1.2.3.4', name='example.com') assert not hosts_entries.exists(address='1.2.3.4') assert not hosts_entries.exists(names=['example.com'])
def test_add_adblock_entry_with_force_with_target_having_multiple_names(tmpdir): """ Test that an addition of an adblock entry replaces one with a matching name if force is True (multiple names) """ ipv4_line = '0.0.0.0 example.com example2.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 example2.com') hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(names=['example2.com'])
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'])
def test_addition_of_ipv6_entry_where_matching_name_exists_and_force_false(tmpdir): """ Test no replacement of an ipv6 entry where the address is different but there is a matching name and force is false """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("fe80::200:f8ff:fe21:67cf\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv6', address='2001:db8:a0b:12f0::1', names=['example.com', 'example']) hosts_entries.add(entries=[new_entry], force=False) assert not hosts_entries.exists(address='2001:db8:a0b:12f0::1') assert hosts_entries.exists(address='fe80::200:f8ff:fe21:67cf') assert hosts_entries.exists(names=new_entry.names)
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'])
def test_addition_of_ipv6_entry_where_matching_name_exists_and_force_false(tmpdir): """ Test no replacement of an ipv6 entry where the address is different but there is a matching name and force is false """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("fe80::200:f8ff:fe21:67cf\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv6', address='2001:db8:a0b:12f0::1', names=['example.com', 'example']) hosts_entries.add(entries=[new_entry], force=False) assert not hosts_entries.exists(address='2001:db8:a0b:12f0::1') assert hosts_entries.exists(address='fe80::200:f8ff:fe21:67cf') assert hosts_entries.exists(names=new_entry.names)
def test_remove_existing_entry_using_name_only(tmpdir): """ Test removal of an existing entry using name only """ entries = '1.2.3.4 example.com example\n# this is a comment\n\n3.4.5.6 random.com' # two newlines intentionally follow, see issue #11 hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(name='example.com') assert not hosts_entries.exists(names=['example.com']) hosts_entries.write() assert '# this is a comment\n' in open(hosts_file.strpath).read() assert '3.4.5.6\trandom.com' in open(hosts_file.strpath).read()
def test_remove_existing_entry_using_name_only(tmpdir): """ Test removal of an existing entry using name only """ entries = '1.2.3.4 example.com example\n# this is a comment\n\n3.4.5.6 random.com' # two newlines intentionally follow, see issue #11 hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(entries) hosts_entries = Hosts(path=hosts_file.strpath) assert hosts_entries.exists(address='1.2.3.4') assert hosts_entries.exists(names=['example.com']) hosts_entries.remove_all_matching(name='example.com') assert not hosts_entries.exists(names=['example.com']) hosts_entries.write() assert '# this is a comment\n' in open(hosts_file.strpath).read() assert '3.4.5.6\trandom.com' in open(hosts_file.strpath).read()
def test_replacement_of_ipv4_entry_where_address_differs(tmpdir): """ Test replacement of an ipv4 entry where just the address differs Add: 82.132.132.132 example.com example Then add (with force): 82.132.132.133 example.com example The second addition should replace the former as there is an address match """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.133', names=['example.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.133') assert hosts_entries.exists(names=['example.com', 'example'])
def test_replacement_of_ipv4_entry_where_address_differs(tmpdir): """ Test replacement of an ipv4 entry where just the address differs Add: 82.132.132.132 example.com example Then add (with force): 82.132.132.133 example.com example The second addition should replace the former as there is an address match """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("82.132.132.132\texample.com\texample\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.133', names=['example.com', 'example']) hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(address='82.132.132.133') assert hosts_entries.exists(names=['example.com', 'example'])
def test_import_from_url_with_force(tmpdir): """ Test that a bare import from URL does replace names in existing entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\texample1.com example2.com example3.com\n") hosts = Hosts(path=hosts_file.strpath) import_url = "https://raw.githubusercontent.com/jonhadfield/python-hosts/devel/test_files/hosts_win3" hosts.import_url(url=import_url, force=True) assert not hosts.exists(names=['example3.com'])
def test_add_single_ipv6_host(tmpdir): """ Test addition of an ipv6 entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv6', address='::1', names=['localhost6.localdomain6', 'localhost6']) hosts_entries.add(entries=[new_entry], force=False) assert hosts_entries.exists(address='::1')
def test_add_single_ipv4_host(tmpdir): """ Test the addition of an ipv4 host succeeds """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com']) hosts.add(entries=[new_entry]) assert hosts.exists(address='123.123.123.123')
def test_add_single_ipv4_host(tmpdir): """ Test the addition of an ipv4 host succeeds """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com']) hosts.add(entries=[new_entry]) assert hosts.exists(address='123.123.123.123')
def test_add_single_ipv6_host(tmpdir): """ Test addition of an ipv6 entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("127.0.0.1\tlocalhost\n") hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry(entry_type='ipv6', address='::1', names=['localhost6.localdomain6', 'localhost6']) hosts_entries.add(entries=[new_entry], force=False) assert hosts_entries.exists(address='::1')
def test_import_from_url_without_force(tmpdir): """ Test that a bare import from URL does not replace names in existing entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\texample1.com example2.com example3.com\n") hosts = Hosts(path=hosts_file.strpath) import_url = "https://raw.githubusercontent.com/jonhadfield/python-hosts/devel/test_files/hosts_win3" hosts.import_url(url=import_url) assert hosts.exists(names=['example3.com'])
def test_import_from_url_with_force(tmpdir): """ Test that a bare import from URL does replace names in existing entry """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("1.2.3.4\texample1.com example2.com example3.com\n") hosts = Hosts(path=hosts_file.strpath) import_url = "https://dl.dropboxusercontent.com/u/167103/hosts_win3" hosts.import_url(url=import_url, force=True) assert not hosts.exists(names=['example3.com'])
def test_hosts_write_to_custom_path(tmpdir): """ Test that the hosts file can be written to a different path to the one it was read from """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("6.6.6.6\texample.com\n") hosts = Hosts(path=hosts_file.strpath) alternate_hosts_file = tmpdir.mkdir("tmp").join("hosts2") hosts.write(path=alternate_hosts_file.strpath) alternate_hosts = Hosts(path=alternate_hosts_file.strpath) assert alternate_hosts.count() == 1 assert hosts.exists(address='6.6.6.6', names=['example.com'])
def test_hosts_write_to_custom_path(tmpdir): """ Test that the hosts file can be written to a different path to the one it was read from """ hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write("6.6.6.6\texample.com\n") hosts = Hosts(path=hosts_file.strpath) alternate_hosts_file = tmpdir.mkdir("tmp").join("hosts2") hosts.write(path=alternate_hosts_file.strpath) alternate_hosts = Hosts(path=alternate_hosts_file.strpath) assert alternate_hosts.count() == 1 assert hosts.exists(address='6.6.6.6', names=['example.com'])
def test_add_comments(tmpdir): """ Test adding a comment """ entries = '127.0.0.1 example.com example2.com\n#existing comment' hosts_path = tmpdir.mkdir("etc").join("hosts") hosts_path.write(entries) hosts_entries = Hosts(path=hosts_path.strpath) assert hosts_entries.count() == 2 # 1 address and 1 comment new_entry_1 = HostsEntry(entry_type='comment', comment='# an example comment') new_entry_2 = HostsEntry(entry_type='comment', comment='another example comment') hosts_entries.add(entries=[new_entry_1, new_entry_2], force=True) assert hosts_entries.count() == 4 # 1 address and 3 comments assert not hosts_entries.exists(address='3.4.5.6') assert hosts_entries.exists(comment='# an example comment') assert hosts_entries.exists(comment='# another example comment') assert hosts_entries.exists(names=['example.com']) # check the entries can be written and then read correctly hosts_entries.write() hosts_entries_2 = Hosts(path=hosts_path.strpath) assert hosts_entries_2.count() == 4 # 1 address and 3 comments
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'])
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])
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')
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')