Introduction of domain_to_idna() tests.

This commit is contained in:
funilrys 2018-03-02 22:23:21 +01:00
parent 780e47ffe5
commit 4798710029
No known key found for this signature in database
GPG Key ID: 0D8BFEF5515C00C6

View File

@ -5,25 +5,29 @@
#
# Python script for testing updateHostFiles.py
from updateHostsFile import (
Colors, PY3, colorize, display_exclusion_options, exclude_domain,
flush_dns_cache, gather_custom_exclusions, get_defaults, get_file_by_url,
is_valid_domain_format, matches_exclusions, move_hosts_file_into_place,
normalize_rule, path_join_robust, print_failure, print_success,
prompt_for_exclusions, prompt_for_move, prompt_for_flush_dns_cache,
prompt_for_update, query_yes_no, recursive_glob, remove_old_hosts_file,
supports_color, strip_rule, update_all_sources, update_readme_data,
update_sources_data, write_data, write_opening_header)
import updateHostsFile
import unittest
import tempfile
import locale
import shutil
import json
import sys
import locale
import os
import re
import shutil
import sys
import tempfile
import unittest
import updateHostsFile
from updateHostsFile import (PY3, Colors, colorize, display_exclusion_options,
domain_to_idna, exclude_domain, flush_dns_cache,
gather_custom_exclusions, get_defaults,
get_file_by_url, is_valid_domain_format,
matches_exclusions, move_hosts_file_into_place,
normalize_rule, path_join_robust, print_failure,
print_success, prompt_for_exclusions,
prompt_for_flush_dns_cache, prompt_for_move,
prompt_for_update, query_yes_no, recursive_glob,
remove_old_hosts_file, strip_rule, supports_color,
update_all_sources, update_readme_data,
update_sources_data, write_data,
write_opening_header)
if PY3:
from io import BytesIO, StringIO
@ -1360,6 +1364,81 @@ def mock_url_open_decode_fail(_):
return m
class DomainToIDNA(Base):
def __init__(self, *args, **kwargs):
super(DomainToIDNA, self).__init__(*args, **kwargs)
self.domains = [b'\xc9\xa2oogle.com', b'www.huala\xc3\xb1e.cl']
self.expected_domains = ['xn--oogle-wmc.com', 'www.xn--hualae-0wa.cl']
def test_empty_line(self):
data = ["", "\r", "\n"]
for empty in data:
expected = empty
actual = domain_to_idna(empty)
self.assertEqual(actual, expected)
def test_commented_line(self):
data = "# Hello World"
expected = data
actual = domain_to_idna(data)
self.assertEqual(actual, expected)
def test_simple_line(self):
# Test with a space as separator.
for i in range(len(self.domains)):
data = (b"0.0.0.0 " + self.domains[i]).decode('utf-8')
expected = "0.0.0.0 " + self.expected_domains[i]
actual = domain_to_idna(data)
self.assertEqual(actual, expected)
# Test with a tabulation as separator.
for i in range(len(self.domains)):
data = (b"0.0.0.0\t" + self.domains[i]).decode('utf-8')
expected = "0.0.0.0\t" + self.expected_domains[i]
actual = domain_to_idna(data)
self.assertEqual(actual, expected)
def test_single_line_with_comment_at_the_end(self):
# Test with a space as separator.
for i in range(len(self.domains)):
data = (b"0.0.0.0 " + self.domains[i] + b" # Hello World") \
.decode('utf-8')
expected = "0.0.0.0 " + self.expected_domains[i] + " # Hello World"
actual = domain_to_idna(data)
self.assertEqual(actual, expected)
# Test with a tabulation as separator.
for i in range(len(self.domains)):
data = (b"0.0.0.0\t" + self.domains[i] + b" # Hello World") \
.decode('utf-8')
expected = "0.0.0.0\t" + self.expected_domains[i] + \
" # Hello World"
actual = domain_to_idna(data)
self.assertEqual(actual, expected)
def test_single_line_without_prefix(self):
for i in range(len(self.domains)):
data = self.domains[i].decode('utf-8')
expected = self.expected_domains[i]
actual = domain_to_idna(data)
self.assertEqual(actual, expected)
class GetFileByUrl(BaseStdout):
@mock.patch("updateHostsFile.urlopen",