| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from cryptoparser.common.utils import bytes_from_hex_string, bytes_to_hex_string |
| 6 |
|
| 7 |
|
| 8 |
class TestBytesToHexString(unittest.TestCase): |
| 9 |
def test_separator(self): |
| 10 |
self.assertEqual(bytes_to_hex_string(b''), '') |
| 11 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef'), 'DEADBEEF') |
| 12 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef', separator=':'), 'DE:AD:BE:EF') |
| 13 |
|
| 14 |
def test_lowercase(self): |
| 15 |
self.assertEqual(bytes_to_hex_string(b''), '') |
| 16 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef'), 'DEADBEEF') |
| 17 |
self.assertEqual(bytes_to_hex_string(b'\xde\xad\xbe\xef', lowercase=True), 'deadbeef') |
| 18 |
|
| 19 |
|
| 20 |
class TestBytesFromHexString(unittest.TestCase): |
| 21 |
def test_error_odd_length_string(self): |
| 22 |
with self.assertRaises(ValueError) as context_manager: |
| 23 |
bytes_from_hex_string('0d:d') |
| 24 |
self.assertEqual(type(context_manager.exception), ValueError) |
| 25 |
|
| 26 |
def test_error_non_hex_string(self): |
| 27 |
with self.assertRaises(ValueError) as context_manager: |
| 28 |
bytes_from_hex_string('no:th:ex') |
| 29 |
self.assertEqual(type(context_manager.exception), ValueError) |
| 30 |
|
| 31 |
def test_separator(self): |
| 32 |
self.assertEqual(bytes_from_hex_string(''), b'') |
| 33 |
self.assertEqual(bytes_from_hex_string('DEADBEEF'), b'\xde\xad\xbe\xef') |
| 34 |
self.assertEqual(bytes_from_hex_string('DE:AD:BE:EF', separator=':'), b'\xde\xad\xbe\xef') |