| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import collections |
| 4 |
import unittest |
| 5 |
|
| 6 |
from cryptodatahub.ike.algorithm import ( |
| 7 |
Ikev1PayloadType, Ikev1AuthenticationMethod, Ikev1TransformId, Ikev1ProtocolId, Ikev1Doi |
| 8 |
) |
| 9 |
|
| 10 |
from cryptoparser.ike.ikev1 import ( |
| 11 |
Ikev1AttributeAuthenticationMethod, Ikev1PayloadTransform, Ikev1PayloadProposal, |
| 12 |
Ikev1PayloadSecurityAssociation, Ikev1Situation |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
class TestIkev1PayloadProposal(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 17 |
def setUp(self): |
| 18 |
self.simple_transform = Ikev1PayloadTransform( |
| 19 |
transform_id=Ikev1TransformId.KEY_IKE, |
| 20 |
attributes=[] |
| 21 |
) |
| 22 |
|
| 23 |
self.auth_attribute = Ikev1AttributeAuthenticationMethod(value=Ikev1AuthenticationMethod.PRE_SHARED_KEY) |
| 24 |
self.transform_with_attr = Ikev1PayloadTransform( |
| 25 |
transform_id=Ikev1TransformId.KEY_IKE, |
| 26 |
attributes=[self.auth_attribute] |
| 27 |
) |
| 28 |
|
| 29 |
self.test_dict_single_transform = collections.OrderedDict([ |
| 30 |
('next_payload', b'\x00'), # NONE = 0x00 |
| 31 |
('reserved', b'\x00'), |
| 32 |
('payload_length', b'\x00\x10'), # 16 bytes total |
| 33 |
('proposal_number', b'\x01'), |
| 34 |
('protocol_id', b'\x01'), # ISAKMP = 0x01 |
| 35 |
('spi_size', b'\x00'), |
| 36 |
('transform_count', b'\x01'), |
| 37 |
('transform_data', b'\x00\x00\x00\x08\x01\x01\x00\x00'), # 8-byte transform |
| 38 |
]) |
| 39 |
self.test_bytes_single_transform = b''.join(self.test_dict_single_transform.values()) |
| 40 |
|
| 41 |
self.spi_data = b'\x12\x34\x56\x78' |
| 42 |
self.test_dict_with_spi = collections.OrderedDict([ |
| 43 |
('next_payload', b'\x00'), # NONE = 0x00 |
| 44 |
('reserved', b'\x00'), |
| 45 |
('payload_length', b'\x00\x14'), # 20 bytes total |
| 46 |
('proposal_number', b'\x01'), |
| 47 |
('protocol_id', b'\x03'), # IPSEC_ESP = 0x03 |
| 48 |
('spi_size', b'\x04'), # 4-byte SPI |
| 49 |
('transform_count', b'\x01'), |
| 50 |
('spi', self.spi_data), |
| 51 |
('transform_data', b'\x00\x00\x00\x08\x01\x01\x00\x00'), # 8-byte transform |
| 52 |
]) |
| 53 |
self.test_bytes_with_spi = b''.join(self.test_dict_with_spi.values()) |
| 54 |
|
| 55 |
self.test_dict_multi_transforms = collections.OrderedDict([ |
| 56 |
('next_payload', b'\x00'), # NONE = 0x00 |
| 57 |
('reserved', b'\x00'), |
| 58 |
('payload_length', b'\x00\x18'), # 24 bytes total |
| 59 |
('proposal_number', b'\x01'), |
| 60 |
('protocol_id', b'\x01'), # ISAKMP = 0x01 |
| 61 |
('spi_size', b'\x00'), |
| 62 |
('transform_count', b'\x02'), |
| 63 |
('transform1_data', b'\x03\x00\x00\x08\x01\x01\x00\x00'), # next=TRANSFORM |
| 64 |
('transform2_data', b'\x00\x00\x00\x08\x02\x01\x00\x00'), # next=NONE |
| 65 |
]) |
| 66 |
self.test_bytes_multi_transforms = b''.join(self.test_dict_multi_transforms.values()) |
| 67 |
|
| 68 |
self.proposal_single = Ikev1PayloadProposal( |
| 69 |
protocol_id=Ikev1ProtocolId.ISAKMP, |
| 70 |
transforms=[self.simple_transform], |
| 71 |
spi=b'' |
| 72 |
) |
| 73 |
self.proposal_single.proposal_number = 1 |
| 74 |
self.proposal_single.next_payload = Ikev1PayloadType.NONE |
| 75 |
|
| 76 |
self.proposal_with_spi = Ikev1PayloadProposal( |
| 77 |
protocol_id=Ikev1ProtocolId.IPSEC_ESP, |
| 78 |
transforms=[self.simple_transform], |
| 79 |
spi=self.spi_data |
| 80 |
) |
| 81 |
self.proposal_with_spi.proposal_number = 1 |
| 82 |
self.proposal_with_spi.next_payload = Ikev1PayloadType.NONE |
| 83 |
|
| 84 |
self.second_transform = Ikev1PayloadTransform( |
| 85 |
transform_id=Ikev1TransformId.KEY_IKE, |
| 86 |
attributes=[] |
| 87 |
) |
| 88 |
|
| 89 |
self.proposal_multi = Ikev1PayloadProposal( |
| 90 |
protocol_id=Ikev1ProtocolId.ISAKMP, |
| 91 |
transforms=[self.simple_transform, self.second_transform], |
| 92 |
spi=b'' |
| 93 |
) |
| 94 |
self.proposal_multi.proposal_number = 1 |
| 95 |
self.proposal_multi.next_payload = Ikev1PayloadType.NONE |
| 96 |
|
| 97 |
def test_get_payload_type(self): |
| 98 |
self.assertEqual(Ikev1PayloadProposal.get_payload_type(), Ikev1PayloadType.PROPOSAL) |
| 99 |
|
| 100 |
def test_constructor_with_single_transform(self): |
| 101 |
proposal = Ikev1PayloadProposal( |
| 102 |
protocol_id=Ikev1ProtocolId.ISAKMP, |
| 103 |
transforms=[self.simple_transform], |
| 104 |
spi=b'' |
| 105 |
) |
| 106 |
self.assertEqual(proposal.protocol_id, Ikev1ProtocolId.ISAKMP) |
| 107 |
self.assertEqual(len(proposal.transforms), 1) |
| 108 |
self.assertEqual(proposal.transforms[0], self.simple_transform) |
| 109 |
self.assertEqual(proposal.spi, b'') |
| 110 |
|
| 111 |
def test_constructor_with_spi(self): |
| 112 |
proposal = Ikev1PayloadProposal( |
| 113 |
protocol_id=Ikev1ProtocolId.IPSEC_ESP, |
| 114 |
transforms=[self.simple_transform], |
| 115 |
spi=self.spi_data |
| 116 |
) |
| 117 |
self.assertEqual(proposal.protocol_id, Ikev1ProtocolId.IPSEC_ESP) |
| 118 |
self.assertEqual(proposal.spi, self.spi_data) |
| 119 |
|
| 120 |
def test_constructor_with_multiple_transforms(self): |
| 121 |
proposal = Ikev1PayloadProposal( |
| 122 |
protocol_id=Ikev1ProtocolId.ISAKMP, |
| 123 |
transforms=[self.simple_transform, self.second_transform], |
| 124 |
spi=b'' |
| 125 |
) |
| 126 |
self.assertEqual(len(proposal.transforms), 2) |
| 127 |
self.assertEqual(proposal.transforms[0], self.simple_transform) |
| 128 |
self.assertEqual(proposal.transforms[1], self.second_transform) |
| 129 |
|
| 130 |
def test_parse_single_transform(self): |
| 131 |
parsed: Ikev1PayloadProposal = Ikev1PayloadProposal.parse_exact_size(self.test_bytes_single_transform) |
| 132 |
|
| 133 |
self.assertEqual(parsed.next_payload, Ikev1PayloadType.NONE) |
| 134 |
self.assertEqual(parsed.proposal_number, 1) |
| 135 |
self.assertEqual(parsed.protocol_id, Ikev1ProtocolId.ISAKMP) |
| 136 |
self.assertEqual(parsed.spi, b'') |
| 137 |
self.assertEqual(len(parsed.transforms), 1) |
| 138 |
self.assertEqual(parsed.transforms[0].transform_id, Ikev1TransformId.KEY_IKE) |
| 139 |
|
| 140 |
def test_parse_with_spi(self): |
| 141 |
parsed: Ikev1PayloadProposal = Ikev1PayloadProposal.parse_exact_size(self.test_bytes_with_spi) |
| 142 |
|
| 143 |
self.assertEqual(parsed.protocol_id, Ikev1ProtocolId.IPSEC_ESP) |
| 144 |
self.assertEqual(parsed.spi, self.spi_data) |
| 145 |
self.assertEqual(len(parsed.transforms), 1) |
| 146 |
|
| 147 |
def test_parse_multiple_transforms(self): |
| 148 |
parsed: Ikev1PayloadProposal = Ikev1PayloadProposal.parse_exact_size(self.test_bytes_multi_transforms) |
| 149 |
|
| 150 |
self.assertEqual(parsed.proposal_number, 1) |
| 151 |
self.assertEqual(parsed.protocol_id, Ikev1ProtocolId.ISAKMP) |
| 152 |
self.assertEqual(len(parsed.transforms), 2) |
| 153 |
self.assertEqual(parsed.transforms[0].transform_id, Ikev1TransformId.KEY_IKE) |
| 154 |
self.assertEqual(parsed.transforms[1].transform_id, Ikev1TransformId.KEY_IKE) |
| 155 |
|
| 156 |
def test_compose_single_transform(self): |
| 157 |
composed_bytes = self.proposal_single.compose() |
| 158 |
self.assertEqual(composed_bytes, self.test_bytes_single_transform) |
| 159 |
|
| 160 |
def test_compose_with_spi(self): |
| 161 |
composed_bytes = self.proposal_with_spi.compose() |
| 162 |
self.assertEqual(composed_bytes, self.test_bytes_with_spi) |
| 163 |
|
| 164 |
def test_compose_multiple_transforms(self): |
| 165 |
composed_bytes = self.proposal_multi.compose() |
| 166 |
self.assertEqual(composed_bytes, self.test_bytes_multi_transforms) |
| 167 |
|
| 168 |
def test_round_trip_single_transform(self): |
| 169 |
composed_bytes = self.proposal_single.compose() |
| 170 |
parsed: Ikev1PayloadProposal = Ikev1PayloadProposal.parse_exact_size(composed_bytes) |
| 171 |
|
| 172 |
self.assertEqual(parsed.protocol_id, self.proposal_single.protocol_id) |
| 173 |
self.assertEqual(parsed.spi, self.proposal_single.spi) |
| 174 |
self.assertEqual(len(parsed.transforms), len(self.proposal_single.transforms)) |
| 175 |
self.assertEqual(parsed.proposal_number, self.proposal_single.proposal_number) |
| 176 |
self.assertEqual(parsed.next_payload, self.proposal_single.next_payload) |
| 177 |
|
| 178 |
def test_round_trip_with_spi(self): |
| 179 |
composed_bytes = self.proposal_with_spi.compose() |
| 180 |
parsed: Ikev1PayloadProposal = Ikev1PayloadProposal.parse_exact_size(composed_bytes) |
| 181 |
|
| 182 |
self.assertEqual(parsed.protocol_id, self.proposal_with_spi.protocol_id) |
| 183 |
self.assertEqual(parsed.spi, self.proposal_with_spi.spi) |
| 184 |
self.assertEqual(len(parsed.transforms), len(self.proposal_with_spi.transforms)) |
| 185 |
|
| 186 |
def test_round_trip_multiple_transforms(self): |
| 187 |
composed_bytes = self.proposal_multi.compose() |
| 188 |
parsed: Ikev1PayloadProposal = Ikev1PayloadProposal.parse_exact_size(composed_bytes) |
| 189 |
|
| 190 |
self.assertEqual(parsed.protocol_id, self.proposal_multi.protocol_id) |
| 191 |
self.assertEqual(len(parsed.transforms), len(self.proposal_multi.transforms)) |
| 192 |
self.assertEqual(parsed.transforms[0].next_payload, Ikev1PayloadType.TRANSFORM) |
| 193 |
self.assertEqual(parsed.transforms[1].next_payload, Ikev1PayloadType.NONE) |
| 194 |
|
| 195 |
def test_transform_numbering_in_compose(self): |
| 196 |
composed_bytes = self.proposal_multi.compose() |
| 197 |
|
| 198 |
self.assertEqual(composed_bytes[12], 1) # First transform number at byte 12 |
| 199 |
self.assertEqual(composed_bytes[20], 2) # Second transform number at byte 20 |
| 200 |
|
| 201 |
def test_spi_size_calculation(self): |
| 202 |
different_spi = b'\xaa\xbb\xcc\xdd\xee\xff' # 6 bytes |
| 203 |
proposal = Ikev1PayloadProposal( |
| 204 |
protocol_id=Ikev1ProtocolId.IPSEC_AH, |
| 205 |
transforms=[self.simple_transform], |
| 206 |
spi=different_spi |
| 207 |
) |
| 208 |
proposal.proposal_number = 1 |
| 209 |
proposal.next_payload = Ikev1PayloadType.NONE |
| 210 |
|
| 211 |
composed_bytes = proposal.compose() |
| 212 |
self.assertEqual(composed_bytes[6], 6) # SPI size field at byte 6 |
| 213 |
|
| 214 |
|
| 215 |
class TestIkev1PayloadSecurityAssociation(unittest.TestCase): # pylint: disable=too-many-instance-attributes |
| 216 |
def setUp(self): |
| 217 |
# Simple transform for proposals |
| 218 |
self.simple_transform = Ikev1PayloadTransform( |
| 219 |
transform_id=Ikev1TransformId.KEY_IKE, |
| 220 |
attributes=[] |
| 221 |
) |
| 222 |
|
| 223 |
# Simple proposal for SA |
| 224 |
self.simple_proposal = Ikev1PayloadProposal( |
| 225 |
protocol_id=Ikev1ProtocolId.ISAKMP, |
| 226 |
transforms=[self.simple_transform], |
| 227 |
spi=b'' |
| 228 |
) |
| 229 |
|
| 230 |
# Second proposal for multi-proposal tests |
| 231 |
self.second_proposal = Ikev1PayloadProposal( |
| 232 |
protocol_id=Ikev1ProtocolId.IPSEC_ESP, |
| 233 |
transforms=[self.simple_transform], |
| 234 |
spi=b'\x12\x34\x56\x78' |
| 235 |
) |
| 236 |
|
| 237 |
# Test data for SA with single proposal |
| 238 |
self.test_dict_single_proposal = collections.OrderedDict([ |
| 239 |
('next_payload', b'\x00'), # NONE = 0x00 |
| 240 |
('reserved', b'\x00'), |
| 241 |
('payload_length', b'\x00\x1c'), # 28 bytes total |
| 242 |
('doi', b'\x00\x00\x00\x01'), # IPSEC = 0x00000001 |
| 243 |
('situation', b'\x00\x00\x00\x01'), # SIT_IDENTITY_ONLY = 0x00000001 |
| 244 |
# Proposal: next=NONE, length=16, prop_num=1, protocol=ISAKMP, spi_size=0, transform_count=1 |
| 245 |
('proposal_data', b'\x00\x00\x00\x10\x01\x01\x00\x01'), |
| 246 |
# Transform: next=NONE, length=8, transform_num=1, transform_id=KEY_IKE |
| 247 |
('transform_data', b'\x00\x00\x00\x08\x01\x01\x00\x00'), |
| 248 |
]) |
| 249 |
self.test_bytes_single_proposal = b''.join(self.test_dict_single_proposal.values()) |
| 250 |
|
| 251 |
# Test data for SA with multiple proposals |
| 252 |
self.test_dict_multi_proposals = collections.OrderedDict([ |
| 253 |
('next_payload', b'\x00'), # NONE = 0x00 |
| 254 |
('reserved', b'\x00'), |
| 255 |
('payload_length', b'\x00\x30'), # 48 bytes total |
| 256 |
('doi', b'\x00\x00\x00\x01'), # IPSEC = 0x00000001 |
| 257 |
('situation', b'\x00\x00\x00\x01'), # SIT_IDENTITY_ONLY = 0x00000001 |
| 258 |
# First proposal: next=PROPOSAL, length=16, prop_num=1 |
| 259 |
('proposal1_data', b'\x02\x00\x00\x10\x01\x01\x00\x01'), |
| 260 |
('transform1_data', b'\x00\x00\x00\x08\x01\x01\x00\x00'), |
| 261 |
# Second proposal: next=NONE, length=20, prop_num=2, protocol=ESP, 4-byte SPI |
| 262 |
('proposal2_data', b'\x00\x00\x00\x14\x02\x03\x04\x01'), |
| 263 |
('spi_data', b'\x12\x34\x56\x78'), |
| 264 |
('transform2_data', b'\x00\x00\x00\x08\x01\x01\x00\x00'), |
| 265 |
]) |
| 266 |
self.test_bytes_multi_proposals = b''.join(self.test_dict_multi_proposals.values()) |
| 267 |
|
| 268 |
# Test data for different situation flags |
| 269 |
self.test_dict_secrecy_integrity = collections.OrderedDict([ |
| 270 |
('next_payload', b'\x00'), # NONE = 0x00 |
| 271 |
('reserved', b'\x00'), |
| 272 |
('payload_length', b'\x00\x1c'), # 28 bytes total |
| 273 |
('doi', b'\x00\x00\x00\x01'), # IPSEC = 0x00000001 |
| 274 |
('situation', b'\x00\x00\x00\x06'), # SIT_SECRECY | SIT_INTEGRITY = 0x02 | 0x04 = 0x06 |
| 275 |
('proposal_data', b'\x00\x00\x00\x10\x01\x01\x00\x01'), |
| 276 |
('transform_data', b'\x00\x00\x00\x08\x01\x01\x00\x00'), |
| 277 |
]) |
| 278 |
self.test_bytes_secrecy_integrity = b''.join(self.test_dict_secrecy_integrity.values()) |
| 279 |
|
| 280 |
# SA objects for compose tests |
| 281 |
self.sa_single = Ikev1PayloadSecurityAssociation( |
| 282 |
doi=Ikev1Doi.IPSEC, |
| 283 |
situation={Ikev1Situation.SIT_IDENTITY_ONLY}, |
| 284 |
proposals=[self.simple_proposal] |
| 285 |
) |
| 286 |
self.sa_single.next_payload = Ikev1PayloadType.NONE |
| 287 |
|
| 288 |
self.sa_multi = Ikev1PayloadSecurityAssociation( |
| 289 |
doi=Ikev1Doi.IPSEC, |
| 290 |
situation={Ikev1Situation.SIT_IDENTITY_ONLY}, |
| 291 |
proposals=[self.simple_proposal, self.second_proposal] |
| 292 |
) |
| 293 |
self.sa_multi.next_payload = Ikev1PayloadType.NONE |
| 294 |
|
| 295 |
self.sa_flags = Ikev1PayloadSecurityAssociation( |
| 296 |
doi=Ikev1Doi.IPSEC, |
| 297 |
situation={Ikev1Situation.SIT_SECRECY, Ikev1Situation.SIT_INTEGRITY}, |
| 298 |
proposals=[self.simple_proposal] |
| 299 |
) |
| 300 |
self.sa_flags.next_payload = Ikev1PayloadType.NONE |
| 301 |
|
| 302 |
def test_get_payload_type(self): |
| 303 |
self.assertEqual(Ikev1PayloadSecurityAssociation.get_payload_type(), Ikev1PayloadType.SECURITY_ASSOCIATION) |
| 304 |
|
| 305 |
def test_constructor_with_single_proposal(self): |
| 306 |
sa = Ikev1PayloadSecurityAssociation( |
| 307 |
doi=Ikev1Doi.IPSEC, |
| 308 |
situation={Ikev1Situation.SIT_IDENTITY_ONLY}, |
| 309 |
proposals=[self.simple_proposal] |
| 310 |
) |
| 311 |
self.assertEqual(sa.doi, Ikev1Doi.IPSEC) |
| 312 |
self.assertEqual(sa.situation, {Ikev1Situation.SIT_IDENTITY_ONLY}) |
| 313 |
self.assertEqual(len(sa.proposals), 1) |
| 314 |
self.assertEqual(sa.proposals[0], self.simple_proposal) |
| 315 |
|
| 316 |
def test_constructor_with_multiple_proposals(self): |
| 317 |
sa = Ikev1PayloadSecurityAssociation( |
| 318 |
doi=Ikev1Doi.IPSEC, |
| 319 |
situation={Ikev1Situation.SIT_IDENTITY_ONLY}, |
| 320 |
proposals=[self.simple_proposal, self.second_proposal] |
| 321 |
) |
| 322 |
self.assertEqual(len(sa.proposals), 2) |
| 323 |
self.assertEqual(sa.proposals[0], self.simple_proposal) |
| 324 |
self.assertEqual(sa.proposals[1], self.second_proposal) |
| 325 |
|
| 326 |
def test_constructor_with_situation_flags(self): |
| 327 |
sa = Ikev1PayloadSecurityAssociation( |
| 328 |
doi=Ikev1Doi.IPSEC, |
| 329 |
situation={Ikev1Situation.SIT_SECRECY, Ikev1Situation.SIT_INTEGRITY}, |
| 330 |
proposals=[self.simple_proposal] |
| 331 |
) |
| 332 |
self.assertEqual(sa.situation, {Ikev1Situation.SIT_SECRECY, Ikev1Situation.SIT_INTEGRITY}) |
| 333 |
|
| 334 |
def test_doi_storage(self): |
| 335 |
sa = Ikev1PayloadSecurityAssociation( |
| 336 |
doi=Ikev1Doi.GDOI, |
| 337 |
situation={Ikev1Situation.SIT_IDENTITY_ONLY}, |
| 338 |
proposals=[self.simple_proposal] |
| 339 |
) |
| 340 |
self.assertEqual(sa.doi, Ikev1Doi.GDOI) |
| 341 |
|
| 342 |
def test_parse_single_proposal(self): |
| 343 |
parsed: Ikev1PayloadSecurityAssociation = Ikev1PayloadSecurityAssociation.parse_exact_size( |
| 344 |
self.test_bytes_single_proposal |
| 345 |
) |
| 346 |
|
| 347 |
self.assertEqual(parsed.next_payload, Ikev1PayloadType.NONE) |
| 348 |
self.assertEqual(parsed.doi, Ikev1Doi.IPSEC) |
| 349 |
self.assertEqual(parsed.situation, {Ikev1Situation.SIT_IDENTITY_ONLY}) |
| 350 |
self.assertEqual(len(parsed.proposals), 1) |
| 351 |
self.assertEqual(parsed.proposals[0].protocol_id, Ikev1ProtocolId.ISAKMP) |
| 352 |
|
| 353 |
def test_parse_multiple_proposals(self): |
| 354 |
parsed: Ikev1PayloadSecurityAssociation = Ikev1PayloadSecurityAssociation.parse_exact_size( |
| 355 |
self.test_bytes_multi_proposals |
| 356 |
) |
| 357 |
|
| 358 |
self.assertEqual(parsed.doi, Ikev1Doi.IPSEC) |
| 359 |
self.assertEqual(len(parsed.proposals), 2) |
| 360 |
self.assertEqual(parsed.proposals[0].protocol_id, Ikev1ProtocolId.ISAKMP) |
| 361 |
self.assertEqual(parsed.proposals[1].protocol_id, Ikev1ProtocolId.IPSEC_ESP) |
| 362 |
|
| 363 |
def test_parse_situation_flags(self): |
| 364 |
parsed: Ikev1PayloadSecurityAssociation = Ikev1PayloadSecurityAssociation.parse_exact_size( |
| 365 |
self.test_bytes_secrecy_integrity |
| 366 |
) |
| 367 |
|
| 368 |
self.assertEqual(parsed.situation, {Ikev1Situation.SIT_SECRECY, Ikev1Situation.SIT_INTEGRITY}) |
| 369 |
|
| 370 |
def test_compose_single_proposal(self): |
| 371 |
composed_bytes = self.sa_single.compose() |
| 372 |
self.assertEqual(composed_bytes, self.test_bytes_single_proposal) |
| 373 |
|
| 374 |
def test_compose_multiple_proposals(self): |
| 375 |
composed_bytes = self.sa_multi.compose() |
| 376 |
self.assertEqual(composed_bytes, self.test_bytes_multi_proposals) |
| 377 |
|
| 378 |
def test_compose_situation_flags(self): |
| 379 |
composed_bytes = self.sa_flags.compose() |
| 380 |
self.assertEqual(composed_bytes, self.test_bytes_secrecy_integrity) |
| 381 |
|
| 382 |
def test_round_trip_single_proposal(self): |
| 383 |
composed_bytes = self.sa_single.compose() |
| 384 |
parsed: Ikev1PayloadSecurityAssociation = Ikev1PayloadSecurityAssociation.parse_exact_size(composed_bytes) |
| 385 |
|
| 386 |
self.assertEqual(parsed.doi, self.sa_single.doi) |
| 387 |
self.assertEqual(parsed.situation, self.sa_single.situation) |
| 388 |
self.assertEqual(len(parsed.proposals), len(self.sa_single.proposals)) |
| 389 |
self.assertEqual(parsed.next_payload, self.sa_single.next_payload) |
| 390 |
|
| 391 |
def test_round_trip_multiple_proposals(self): |
| 392 |
composed_bytes = self.sa_multi.compose() |
| 393 |
parsed: Ikev1PayloadSecurityAssociation = Ikev1PayloadSecurityAssociation.parse_exact_size(composed_bytes) |
| 394 |
|
| 395 |
self.assertEqual(parsed.doi, self.sa_multi.doi) |
| 396 |
self.assertEqual(len(parsed.proposals), len(self.sa_multi.proposals)) |
| 397 |
# Verify proposal chaining (first proposal points to next, last to NONE) |
| 398 |
self.assertEqual(parsed.proposals[0].next_payload, Ikev1PayloadType.PROPOSAL) |
| 399 |
self.assertEqual(parsed.proposals[1].next_payload, Ikev1PayloadType.NONE) |
| 400 |
|
| 401 |
def test_round_trip_situation_flags(self): |
| 402 |
composed_bytes = self.sa_flags.compose() |
| 403 |
parsed: Ikev1PayloadSecurityAssociation = Ikev1PayloadSecurityAssociation.parse_exact_size(composed_bytes) |
| 404 |
|
| 405 |
self.assertEqual(parsed.situation, self.sa_flags.situation) |
| 406 |
|
| 407 |
def test_proposal_numbering_in_compose(self): |
| 408 |
composed_bytes = self.sa_multi.compose() |
| 409 |
|
| 410 |
# Verify proposal numbers are written correctly (1-based indexing) |
| 411 |
self.assertEqual(composed_bytes[16], 1) # First proposal number at byte 16 |
| 412 |
self.assertEqual(composed_bytes[32], 2) # Second proposal number at byte 32 |
| 413 |
|
| 414 |
|
| 415 |
if __name__ == '__main__': |
| 416 |
unittest.main() |