| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from cryptodatahub.common.exception import InvalidValue |
| 6 |
|
| 7 |
from cryptoparser.common.exception import NotEnoughData |
| 8 |
|
| 9 |
from cryptoparser.tls.mysql import ( |
| 10 |
MySQLRecord, |
| 11 |
MySQLCapability, |
| 12 |
MySQLCharacterSet, |
| 13 |
MySQLStatusFlag, |
| 14 |
MySQLHandshakeSslRequest, |
| 15 |
MySQLHandshakeV10, |
| 16 |
MySQLVersion, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class TestMySQLRecord(unittest.TestCase): |
| 21 |
def setUp(self): |
| 22 |
self.test_record = MySQLRecord( |
| 23 |
packet_number=1, |
| 24 |
packet_bytes=b'\x01\x02\x03\x04' |
| 25 |
) |
| 26 |
self.test_record_bytes = bytes( |
| 27 |
b'\x04\x00\x00' + # packet_length |
| 28 |
b'\x01' + # packet_number |
| 29 |
b'\x01\x02\x03\x04' |
| 30 |
) |
| 31 |
|
| 32 |
def test_error(self): |
| 33 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 34 |
MySQLRecord.parse_exact_size(b'\x00') |
| 35 |
self.assertEqual(context_manager.exception.bytes_needed, MySQLRecord.HEADER_SIZE - 1) |
| 36 |
|
| 37 |
def test_parse(self): |
| 38 |
MySQLRecord.parse_exact_size(self.test_record_bytes) |
| 39 |
|
| 40 |
def test_compose(self): |
| 41 |
self.assertEqual(self.test_record.compose(), self.test_record_bytes) |
| 42 |
|
| 43 |
|
| 44 |
class TestMySQLHandshake10(unittest.TestCase): |
| 45 |
def setUp(self): |
| 46 |
self.handshake_minimal = MySQLHandshakeV10( |
| 47 |
protocol_version=MySQLVersion.MYSQL_9, |
| 48 |
server_version='1.2.3.4', |
| 49 |
connection_id=0x01020304, |
| 50 |
auth_plugin_data=b'\x01\x02\x03\x04\x05\x06\x07\x08', |
| 51 |
capabilities={ |
| 52 |
MySQLCapability.CLIENT_SSL, |
| 53 |
MySQLCapability.CLIENT_MULTI_STATEMENTS, |
| 54 |
}, |
| 55 |
character_set=MySQLCharacterSet.UTF8, |
| 56 |
states={MySQLStatusFlag.SERVER_STATUS_IN_TRANS, }, |
| 57 |
) |
| 58 |
self.handshake_full = MySQLHandshakeV10( |
| 59 |
protocol_version=MySQLVersion.MYSQL_9, |
| 60 |
server_version='1.2.3.4', |
| 61 |
connection_id=0x01020304, |
| 62 |
auth_plugin_data=b'\x01\x02\x03\x04\x05\x06\x07\x08', |
| 63 |
capabilities={ |
| 64 |
MySQLCapability.CLIENT_SSL, |
| 65 |
MySQLCapability.CLIENT_MULTI_STATEMENTS, |
| 66 |
MySQLCapability.CLIENT_PLUGIN_AUTH, |
| 67 |
}, |
| 68 |
character_set=MySQLCharacterSet.UTF8, |
| 69 |
states={MySQLStatusFlag.SERVER_STATUS_IN_TRANS, }, |
| 70 |
auth_plugin_name='auth_plugin_name', |
| 71 |
auth_plugin_data_2=b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d' |
| 72 |
) |
| 73 |
|
| 74 |
self.handshake_bytes_base = bytes( |
| 75 |
b'\x09' + # protocol_version |
| 76 |
b'1.2.3.4\x00' + # server_version |
| 77 |
b'\x04\x03\x02\x01' + # connection_id |
| 78 |
b'\x01\x02\x03\x04\x05\x06\x07\x08' + # auth_plugin_data |
| 79 |
b'\x00' + # filler |
| 80 |
b'\x00\x08' + # capabilities |
| 81 |
b'\x21' + # character_set |
| 82 |
b'\x01\x00' + # states |
| 83 |
b'' |
| 84 |
) |
| 85 |
self.handshake_bytes_minimal = self.handshake_bytes_base + bytes( |
| 86 |
b'\x01\x00' + # capabilities_2 |
| 87 |
b'\x00' + # auth_plugin_data_len |
| 88 |
10 * b'\x00' + # reserved |
| 89 |
b'' |
| 90 |
) |
| 91 |
self.handshake_bytes_full = self.handshake_bytes_base + bytes( |
| 92 |
b'\x09\x00' + # capabilities_2 |
| 93 |
b'\x15' + # auth_plugin_data_len |
| 94 |
10 * b'\x00' + # reserved |
| 95 |
b'\x01\x02\x03\x04\x05\x06\x07\x08' + # auth_plugin_data_2 |
| 96 |
b'\x09\x0a\x0b\x0c\x0d' + |
| 97 |
b'auth_plugin_name\x00' + # auth_plugin_name |
| 98 |
b'' |
| 99 |
) |
| 100 |
|
| 101 |
def test_error_not_enough_data(self): |
| 102 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 103 |
MySQLHandshakeV10.parse_exact_size(b'\x00') |
| 104 |
self.assertEqual(context_manager.exception.bytes_needed, MySQLHandshakeV10.MINIMUM_SIZE - 1) |
| 105 |
|
| 106 |
def test_error_no_auth_plugin_data(self): |
| 107 |
handshake_bytes_no_auth_plugin_data = self.handshake_bytes_base + bytes( |
| 108 |
b'\x09\x00' + # capabilities_2 |
| 109 |
b'\x00' + # auth_plugin_data_len |
| 110 |
10 * b'\x00' + # reserved |
| 111 |
b'' |
| 112 |
) |
| 113 |
with self.assertRaises(InvalidValue) as context_manager: |
| 114 |
MySQLHandshakeV10.parse_exact_size(handshake_bytes_no_auth_plugin_data) |
| 115 |
self.assertEqual(context_manager.exception.value, 0) |
| 116 |
|
| 117 |
def test_parse(self): |
| 118 |
handshake_minimal = MySQLHandshakeV10.parse_exact_size(self.handshake_bytes_minimal) |
| 119 |
self.assertEqual(handshake_minimal, self.handshake_minimal) |
| 120 |
|
| 121 |
handshake_full = MySQLHandshakeV10.parse_exact_size(self.handshake_bytes_full) |
| 122 |
self.assertEqual(handshake_full, self.handshake_full) |
| 123 |
|
| 124 |
def test_compose(self): |
| 125 |
self.assertEqual(self.handshake_minimal.compose(), self.handshake_bytes_minimal) |
| 126 |
self.assertEqual(self.handshake_full.compose(), self.handshake_bytes_full) |
| 127 |
|
| 128 |
|
| 129 |
class TestMySQLHandshakeSslRequest(unittest.TestCase): |
| 130 |
def setUp(self): |
| 131 |
self.handshake_bytes_with_client_41_capability = bytes( |
| 132 |
b'\x00\x02\x00\x00' + # capabilities |
| 133 |
b'\x04\x03\x02\x01' + # max_packet_size |
| 134 |
b'\x21' + # character_set |
| 135 |
23 * b'\x00' + # filler |
| 136 |
b'' |
| 137 |
) |
| 138 |
self.handshake_with_client_41_capability = MySQLHandshakeSslRequest( |
| 139 |
capabilities={MySQLCapability.CLIENT_PROTOCOL_41}, |
| 140 |
max_packet_size=0x01020304, |
| 141 |
character_set=MySQLCharacterSet.UTF8 |
| 142 |
) |
| 143 |
self.handshake_bytes_without_client_41_capability = bytes( |
| 144 |
b'\x00\x00' + # capabilities |
| 145 |
b'\x03\x02\x01' + # max_packet_size |
| 146 |
b'' |
| 147 |
) |
| 148 |
self.handshake_without_client_41_capability = MySQLHandshakeSslRequest( |
| 149 |
capabilities=set(), |
| 150 |
max_packet_size=0x010203, |
| 151 |
) |
| 152 |
|
| 153 |
def test_error_not_enough_data(self): |
| 154 |
with self.assertRaises(NotEnoughData) as context_manager: |
| 155 |
MySQLHandshakeSslRequest.parse_exact_size(b'\x00') |
| 156 |
self.assertEqual(context_manager.exception.bytes_needed, MySQLHandshakeSslRequest.MINIMUM_SIZE - 1) |
| 157 |
|
| 158 |
def test_with_client_41_character_set_is_none(self): |
| 159 |
ssl_request = MySQLHandshakeSslRequest( |
| 160 |
capabilities=set([MySQLCapability.CLIENT_PROTOCOL_41]), max_packet_size=1, character_set=None |
| 161 |
) |
| 162 |
self.assertEqual(ssl_request.character_set, MySQLCharacterSet.UTF8) |
| 163 |
|
| 164 |
def test_error_without_client_41_capability_too_large(self): |
| 165 |
with self.assertRaises(ValueError) as context_manager: |
| 166 |
MySQLHandshakeSslRequest(capabilities=set([MySQLCapability.CLIENT_MULTI_STATEMENTS]), max_packet_size=1) |
| 167 |
self.assertEqual(context_manager.exception.args[0], 1) |
| 168 |
|
| 169 |
def test_error_without_client_41_max_packet_size_too_large(self): # pylint: disable=invalid-name |
| 170 |
with self.assertRaises(ValueError) as context_manager: |
| 171 |
MySQLHandshakeSslRequest(capabilities=set(), max_packet_size=2 ** 24) |
| 172 |
self.assertEqual(context_manager.exception.args[0], 2 ** 24) |
| 173 |
|
| 174 |
def test_with_client_41_capability(self): |
| 175 |
self.assertEqual( |
| 176 |
MySQLHandshakeSslRequest.parse_exact_size(self.handshake_bytes_with_client_41_capability), |
| 177 |
self.handshake_with_client_41_capability, |
| 178 |
) |
| 179 |
self.assertEqual( |
| 180 |
self.handshake_with_client_41_capability.compose(), |
| 181 |
self.handshake_bytes_with_client_41_capability, |
| 182 |
) |
| 183 |
|
| 184 |
def test_without_client_41_capability(self): |
| 185 |
self.assertEqual( |
| 186 |
MySQLHandshakeSslRequest.parse_exact_size(self.handshake_bytes_without_client_41_capability), |
| 187 |
self.handshake_without_client_41_capability, |
| 188 |
) |
| 189 |
self.assertEqual( |
| 190 |
self.handshake_without_client_41_capability.compose(), |
| 191 |
self.handshake_bytes_without_client_41_capability, |
| 192 |
) |