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_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_merge_names(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=['another.example']) hosts_entries.add(entries=[new_entry], merge_names=True) assert hosts_entries.count() == 1 print(hosts_entries) assert len(hosts_entries.entries[0].names) == 3 assert 'example.com' in hosts_entries.entries[0].names assert 'example' in hosts_entries.entries[0].names assert 'another.example' in hosts_entries.entries[0].names
def test_no_entries_if_hosts_path_does_not_exist(): """ Test that no entries are returned if the hosts path is invalid """ hosts = Hosts(path="invalid") assert hosts.count() == 0