| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import unittest |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from test.common.classes import TEST_URL_PREFIX |
| 7 |
|
| 8 |
import urllib3 |
| 9 |
|
| 10 |
from cryptodatahub.common.algorithm import Hash |
| 11 |
from cryptodatahub.common.utils import ( |
| 12 |
HttpFetcher, |
| 13 |
bytes_from_hex_string, |
| 14 |
bytes_to_hex_string, |
| 15 |
hash_bytes, |
| 16 |
name_to_enum_item_name, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class TestBytesToHexString(unittest.TestCase): |
| 21 |
def test_separator(self): |
| 22 |
self.assertEqual(bytes_to_hex_string(b''), '') |
| 23 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef'), 'DEADBEEF') |
| 24 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef', separator=':'), 'DE:AD:BE:EF') |
| 25 |
|
| 26 |
def test_lowercase(self): |
| 27 |
self.assertEqual(bytes_to_hex_string(b''), '') |
| 28 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef'), 'DEADBEEF') |
| 29 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef', lowercase=True), 'deadbeef') |
| 30 |
|
| 31 |
|
| 32 |
class TestBytesFromHexString(unittest.TestCase): |
| 33 |
def test_error_odd_length_string(self): |
| 34 |
with self.assertRaises(ValueError) as context_manager: |
| 35 |
bytes_from_hex_string('0d:d') |
| 36 |
self.assertEqual(type(context_manager.exception), ValueError) |
| 37 |
|
| 38 |
def test_error_non_hex_string(self): |
| 39 |
with self.assertRaises(ValueError) as context_manager: |
| 40 |
bytes_from_hex_string('no:th:ex') |
| 41 |
self.assertEqual(type(context_manager.exception), ValueError) |
| 42 |
|
| 43 |
def test_separator(self): |
| 44 |
self.assertEqual(bytes_from_hex_string(''), b'') |
| 45 |
self.assertEqual(bytes_from_hex_string('DEADBEEF'), b'\xde\xad\xbe\xef') |
| 46 |
self.assertEqual(bytes_from_hex_string('DE:AD:BE:EF', separator=':'), b'\xde\xad\xbe\xef') |
| 47 |
|
| 48 |
|
| 49 |
class TestHashBytes(unittest.TestCase): |
| 50 |
def test_error_unknown_hash_algorithm(self): |
| 51 |
with self.assertRaises(NotImplementedError) as context_manager: |
| 52 |
hash_bytes(Hash.SHA3_512, b'') |
| 53 |
self.assertEqual(context_manager.exception.args, (Hash.SHA3_512, )) |
| 54 |
|
| 55 |
def test_hash(self): |
| 56 |
self.assertEqual( |
| 57 |
hash_bytes(Hash.SHA1, b'abc'), |
| 58 |
b'\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D' |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
class TestNameToEnumItemName(unittest.TestCase): |
| 63 |
def test_convert_simple_name(self): |
| 64 |
self.assertEqual(name_to_enum_item_name('lower'), 'LOWER') |
| 65 |
|
| 66 |
def test_convert_multipart_name(self): |
| 67 |
self.assertEqual(name_to_enum_item_name('multiple part'), 'MULTIPLE_PART') |
| 68 |
self.assertEqual(name_to_enum_item_name('aplha 123'), 'APLHA_123') |
| 69 |
self.assertEqual(name_to_enum_item_name('m u l t i s p a c e'), 'M_U_L_T_I_S_P_A_C_E') |
| 70 |
self.assertEqual(name_to_enum_item_name('trailing space '), 'TRAILING_SPACE') |
| 71 |
self.assertEqual(name_to_enum_item_name('apo\'s trophe'), 'APOS_TROPHE') |
| 72 |
|
| 73 |
def test_convert_i18n_name(self): |
| 74 |
self.assertEqual(name_to_enum_item_name('αβγ'), 'ΑΒΓ') |
| 75 |
|
| 76 |
|
| 77 |
class TestHttpFetcher(unittest.TestCase): |
| 78 |
@mock.patch.object(urllib3.poolmanager.PoolManager, 'request', side_effect=NotImplementedError) |
| 79 |
def test_error_unhandaled_exception(self, _): |
| 80 |
with self.assertRaises(NotImplementedError): |
| 81 |
HttpFetcher()('http://example.com') |
| 82 |
|
| 83 |
def test_error_fetch_timeout(self): |
| 84 |
http_fetcher = HttpFetcher( |
| 85 |
connect_timeout=0.000001, read_timeout=0.000001, retry=0 |
| 86 |
) |
| 87 |
with self.assertRaises(AttributeError): |
| 88 |
http_fetcher(TEST_URL_PREFIX + 'test.csv') |
| 89 |
with self.assertRaises(AttributeError): |
| 90 |
http_fetcher.get_response_header('Server') |
| 91 |
with self.assertRaises(AttributeError): |
| 92 |
_ = http_fetcher.response_data |
| 93 |
|
| •94 |
@unittest.skip("no network") |
| 95 |
def test_fetch(self): |
| 96 |
http_fetcher = HttpFetcher() |
| 97 |
http_fetcher.fetch(TEST_URL_PREFIX + 'test.html') |
| 98 |
self.assertEqual(http_fetcher.get_response_header('Content-Type'), 'text/plain; charset=utf-8') |
| 99 |
self.assertEqual(http_fetcher.response_data, b'\n'.join([ |
| 100 |
b'<!DOCTYPE html>', |
| 101 |
b'<html>', |
| 102 |
b' <body>', |
| 103 |
b' Page content', |
| 104 |
b' </body>', |
| 105 |
b'</html>', |
| 106 |
])) |
| 107 |
|
| 108 |
def test_user_agent_default_omits_header(self): |
| 109 |
with mock.patch.object(urllib3.poolmanager.PoolManager, 'request') as mock_request: |
| 110 |
HttpFetcher().fetch('http://example.com') |
| 111 |
self.assertNotIn('headers', mock_request.call_args.kwargs) |
| 112 |
|
| 113 |
def test_user_agent_custom_passed_as_header(self): |
| 114 |
with mock.patch.object(urllib3.poolmanager.PoolManager, 'request') as mock_request: |
| 115 |
HttpFetcher(user_agent='Mozilla/5.0').fetch('http://example.com') |
| 116 |
self.assertEqual(mock_request.call_args.kwargs['headers'], {'User-Agent': 'Mozilla/5.0'}) |
| 117 |
|
| 118 |
def test_user_agent_invalid_type_rejected(self): |
| 119 |
with self.assertRaises(TypeError): |
| 120 |
HttpFetcher(user_agent=42) |