github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/impacket/tests/ImpactPacket/test_IP6_Extension_Headers.py (about) 1 #!/usr/bin/env python 2 from __future__ import division 3 from __future__ import print_function 4 import sys 5 from six import PY2 6 sys.path.insert(0,"../..") 7 8 #Impact test version 9 try: 10 from impacket import IP6_Address, IP6, ImpactDecoder, IP6_Extension_Headers 11 except: 12 pass 13 14 #Standalone test version 15 try: 16 import sys 17 sys.path.insert(0,"../..") 18 import IP6_Address, IP6, ImpactDecoder, IP6_Extension_Headers 19 except: 20 pass 21 22 import unittest 23 24 class TestIP6(unittest.TestCase): 25 def string_to_list(self, bytes): 26 if PY2: 27 return list(map(ord, list(bytes))) 28 else: 29 return list(bytes) 30 31 def test_create_simple_hop_by_hop(self): 32 hop_by_hop_binary_packet = [0x3a, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00] 33 34 hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() 35 hop_by_hop.set_next_header(58) 36 37 self.assertEqual( 38 self.string_to_list(hop_by_hop.get_packet()), hop_by_hop_binary_packet, 39 "Simple Hop By Hop Header creation - Buffer mismatch") 40 41 self.assertEqual( 42 hop_by_hop.get_size(), len(hop_by_hop_binary_packet), 43 "Simple Hop By Hop Header creation - Size mismatch") 44 45 def test_simple_hop_by_hop_contained_in_ipv6(self): 46 ipv6_binary_packet = [ 47 0x64, 0x82, 0x46, 0x05, 48 0x05, 0xdc, 0x00, 0x01, 49 0xfe, 0x80, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 51 0x78, 0xf8, 0x89, 0xd1, 52 0x30, 0xff, 0x25, 0x6b, 53 0xff, 0x02, 0x00, 0x00, 54 0x00, 0x00, 0x00, 0x00, 55 0x00, 0x00, 0x00, 0x00, 56 0x00, 0x01, 0x00, 0x03] 57 58 hop_by_hop_binary_packet = [ 59 0x3a, 0x00, 0x01, 0x04, 60 0x00, 0x00, 0x00, 0x00] 61 62 binary_packet = ipv6_binary_packet + hop_by_hop_binary_packet 63 64 ip6_packet = IP6.IP6() 65 ip6_packet.set_traffic_class(72) 66 ip6_packet.set_flow_label(148997) 67 ip6_packet.set_payload_length(1500) 68 ip6_packet.set_next_header(17) 69 ip6_packet.set_hop_limit(1) 70 ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") 71 ip6_packet.set_ip_dst("FF02::1:3") 72 73 hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() 74 hop_by_hop.set_next_header(58) 75 76 ip6_packet.contains(hop_by_hop) 77 78 self.assertEqual( 79 self.string_to_list(ip6_packet.get_packet()), binary_packet, 80 "IP6 Hop By Hop Header contained in IPv6 Header - Buffer mismatch") 81 82 self.assertEqual( 83 ip6_packet.get_size(), len(binary_packet), 84 "IP6 Hop By Hop Header contained in IPv6 Header - Size mismatch") 85 86 def test_add_option_to_hop_by_hop(self): 87 hop_by_hop_binary_packet = [ 88 0x3a, 0x01, 0x01, 0x0C, 89 0x00, 0x00, 0x00, 0x00, 90 0x00, 0x00, 0x00, 0x00, 91 0x00, 0x00, 0x00, 0x00] 92 93 hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() 94 hop_by_hop.set_next_header(58) 95 hop_by_hop.add_option(IP6_Extension_Headers.Option_PADN(14)) 96 97 self.assertEqual( 98 self.string_to_list(hop_by_hop.get_packet()), hop_by_hop_binary_packet, 99 "Add Option to Hop By Hop Header - Buffer mismatch") 100 101 self.assertEqual( 102 hop_by_hop.get_size(), len(hop_by_hop_binary_packet), 103 "Add Option to Hop By Hop Header - Size mismatch") 104 105 def test_pad_hop_by_hop_when_adding_option(self): 106 hop_by_hop_binary_packet = [ 107 0x3a, 0x00, 0x00, 0x01, 108 0x03, 0x00, 0x00, 0x00] 109 110 hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() 111 hop_by_hop.set_next_header(58) 112 hop_by_hop.add_option(IP6_Extension_Headers.Option_PAD1()) 113 114 self.assertEqual( 115 self.string_to_list(hop_by_hop.get_packet()), hop_by_hop_binary_packet, 116 "Pad Hop By Hop Header when adding option - Buffer mismatch") 117 118 self.assertEqual( 119 hop_by_hop.get_size(), len(hop_by_hop_binary_packet), 120 "Pad Hop By Hop Header when adding option - Size mismatch") 121 122 def test_create_simple_dest_opts(self): 123 dest_opts_binary_packet = [0x3a, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00] 124 125 dest_opts = IP6_Extension_Headers.Destination_Options() 126 dest_opts.set_next_header(58) 127 128 self.assertEqual( 129 self.string_to_list(dest_opts.get_packet()), dest_opts_binary_packet, 130 "Simple Destination Options Header creation - Buffer mismatch") 131 132 self.assertEqual( 133 dest_opts.get_size(), len(dest_opts_binary_packet), 134 "Simple Destination Options Header creation - Size mismatch") 135 136 def test_simple_dest_opts_contained_in_ipv6(self): 137 ipv6_binary_packet = [ 138 0x64, 0x82, 0x46, 0x05, 139 0x05, 0xdc, 0x3c, 0x01, 140 0xfe, 0x80, 0x00, 0x00, 141 0x00, 0x00, 0x00, 0x00, 142 0x78, 0xf8, 0x89, 0xd1, 143 0x30, 0xff, 0x25, 0x6b, 144 0xff, 0x02, 0x00, 0x00, 145 0x00, 0x00, 0x00, 0x00, 146 0x00, 0x00, 0x00, 0x00, 147 0x00, 0x01, 0x00, 0x03] 148 149 dest_opts_binary_packet = [ 150 0x3a, 0x00, 0x01, 0x04, 151 0x00, 0x00, 0x00, 0x00] 152 153 binary_packet = ipv6_binary_packet + dest_opts_binary_packet 154 155 ip6_packet = IP6.IP6() 156 ip6_packet.set_traffic_class(72) 157 ip6_packet.set_flow_label(148997) 158 ip6_packet.set_payload_length(1500) 159 ip6_packet.set_next_header(17) 160 ip6_packet.set_hop_limit(1) 161 ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") 162 ip6_packet.set_ip_dst("FF02::1:3") 163 164 dest_opts = IP6_Extension_Headers.Destination_Options() 165 dest_opts.set_next_header(58) 166 167 ip6_packet.contains(dest_opts) 168 169 self.assertEqual( 170 self.string_to_list(ip6_packet.get_packet()), binary_packet, 171 "IP6 Destination Options Header contained in IPv6 Header - Buffer mismatch") 172 173 self.assertEqual( 174 ip6_packet.get_size(), len(binary_packet), 175 "IP6 Destination Options Header contained in IPv6 Header - Size mismatch") 176 177 def test_add_option_to_dest_opts(self): 178 dest_opts_binary_packet = [ 179 0x3a, 0x01, 0x01, 0x0C, 180 0x00, 0x00, 0x00, 0x00, 181 0x00, 0x00, 0x00, 0x00, 182 0x00, 0x00, 0x00, 0x00] 183 184 dest_opts = IP6_Extension_Headers.Destination_Options() 185 dest_opts.set_next_header(58) 186 dest_opts.add_option(IP6_Extension_Headers.Option_PADN(14)) 187 188 self.assertEqual( 189 self.string_to_list(dest_opts.get_packet()), dest_opts_binary_packet, 190 "Add Option to Destination Options Header - Buffer mismatch") 191 192 self.assertEqual( 193 dest_opts.get_size(), len(dest_opts_binary_packet), 194 "Add Option to Destination Options Header - Size mismatch") 195 196 def test_pad_dest_opts_when_adding_option(self): 197 dest_opts_binary_packet = [ 198 0x3a, 0x00, 0x00, 0x01, 199 0x03, 0x00, 0x00, 0x00] 200 201 dest_opts = IP6_Extension_Headers.Destination_Options() 202 dest_opts.set_next_header(58) 203 dest_opts.add_option(IP6_Extension_Headers.Option_PAD1()) 204 205 self.assertEqual( 206 self.string_to_list(dest_opts.get_packet()), dest_opts_binary_packet, 207 "Pad Destination Options Header when adding option - Buffer mismatch") 208 209 self.assertEqual( 210 dest_opts.get_size(), len(dest_opts_binary_packet), 211 "Pad Destination Options Header when adding option - Size mismatch") 212 213 def test_create_simple_routing_options(self): 214 routing_options_binary_packet = [0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] 215 216 routing_options = IP6_Extension_Headers.Routing_Options() 217 routing_options.set_next_header(58) 218 219 self.assertEqual( 220 self.string_to_list(routing_options.get_packet()), routing_options_binary_packet, 221 "Simple Routing Options Header creation - Buffer mismatch") 222 223 self.assertEqual( 224 routing_options.get_size(), len(routing_options_binary_packet), 225 "Simple Routing Options Header creation - Size mismatch") 226 227 def test_simple_routing_options_contained_in_ipv6(self): 228 ipv6_binary_packet = [ 229 0x64, 0x82, 0x46, 0x05, 230 0x05, 0xdc, 0x2b, 0x01, 231 0xfe, 0x80, 0x00, 0x00, 232 0x00, 0x00, 0x00, 0x00, 233 0x78, 0xf8, 0x89, 0xd1, 234 0x30, 0xff, 0x25, 0x6b, 235 0xff, 0x02, 0x00, 0x00, 236 0x00, 0x00, 0x00, 0x00, 237 0x00, 0x00, 0x00, 0x00, 238 0x00, 0x01, 0x00, 0x03] 239 240 routing_options_binary_packet = [ 241 0x3a, 0x00, 0x00, 0x0a, 242 0x00, 0x00, 0x00, 0x00] 243 244 binary_packet = ipv6_binary_packet + routing_options_binary_packet 245 246 ip6_packet = IP6.IP6() 247 ip6_packet.set_traffic_class(72) 248 ip6_packet.set_flow_label(148997) 249 ip6_packet.set_payload_length(1500) 250 ip6_packet.set_next_header(17) 251 ip6_packet.set_hop_limit(1) 252 ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") 253 ip6_packet.set_ip_dst("FF02::1:3") 254 255 routing_options = IP6_Extension_Headers.Routing_Options() 256 routing_options.set_next_header(58) 257 routing_options.set_routing_type(0) 258 routing_options.set_segments_left(10) 259 260 ip6_packet.contains(routing_options) 261 262 self.assertEqual( 263 self.string_to_list(ip6_packet.get_packet()), binary_packet, 264 "IP6 Hop By Hop Header contained in IPv6 Header - Buffer mismatch") 265 266 self.assertEqual( 267 ip6_packet.get_size(), len(binary_packet), 268 "IP6 Hop By Hop Header contained in IPv6 Header - Size mismatch") 269 270 def test_chained_basic_options(self): 271 dest_opts_binary_packet = [ 272 0x2b, 0x00, 0x00, 0x01, 273 0x03, 0x00, 0x00, 0x00] 274 275 routing_options_binary_packet = [ 276 0x00, 0x00, 0x00, 0x0a, 277 0x00, 0x00, 0x00, 0x00] 278 279 hop_by_hop_binary_packet = [ 280 0x3a, 0x01, 0x01, 0x0C, 281 0x00, 0x00, 0x00, 0x00, 282 0x00, 0x00, 0x00, 0x00, 283 0x00, 0x00, 0x00, 0x00] 284 285 binary_packet = dest_opts_binary_packet + routing_options_binary_packet + hop_by_hop_binary_packet 286 287 dest_opts = IP6_Extension_Headers.Destination_Options() 288 dest_opts.add_option(IP6_Extension_Headers.Option_PAD1()) 289 290 routing_options = IP6_Extension_Headers.Routing_Options() 291 routing_options.set_next_header(58) 292 routing_options.set_routing_type(0) 293 routing_options.set_segments_left(10) 294 295 hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() 296 hop_by_hop.add_option(IP6_Extension_Headers.Option_PADN(14)) 297 298 dest_opts.contains(routing_options) 299 routing_options.contains(hop_by_hop) 300 hop_by_hop.set_next_header(58) 301 302 self.assertEqual( 303 self.string_to_list(dest_opts.get_packet()), binary_packet, 304 "Chained options - Buffer mismatch") 305 306 self.assertEqual( 307 dest_opts.get_size(), len(binary_packet), 308 "Chained options - Size mismatch") 309 310 def test_chained_basic_options_inside_ipv6_packet(self): 311 ipv6_binary_packet = [ 312 0x64, 0x82, 0x46, 0x05, 313 0x05, 0xdc, 0x00, 0x01, 314 0xfe, 0x80, 0x00, 0x00, 315 0x00, 0x00, 0x00, 0x00, 316 0x78, 0xf8, 0x89, 0xd1, 317 0x30, 0xff, 0x25, 0x6b, 318 0xff, 0x02, 0x00, 0x00, 319 0x00, 0x00, 0x00, 0x00, 320 0x00, 0x00, 0x00, 0x00, 321 0x00, 0x01, 0x00, 0x03] 322 323 hop_by_hop_binary_packet = [ 324 0x2b, 0x01, 0x01, 0x0C, 325 0x00, 0x00, 0x00, 0x00, 326 0x00, 0x00, 0x00, 0x00, 327 0x00, 0x00, 0x00, 0x00] 328 329 routing_options_binary_packet = [ 330 0x3c, 0x00, 0x00, 0x0a, 331 0x00, 0x00, 0x00, 0x00] 332 333 dest_opts_binary_packet = [ 334 0x3a, 0x00, 0x00, 0x01, 335 0x03, 0x00, 0x00, 0x00] 336 337 binary_packet = ipv6_binary_packet + hop_by_hop_binary_packet + routing_options_binary_packet + dest_opts_binary_packet 338 339 ip6_packet = IP6.IP6() 340 ip6_packet.set_traffic_class(72) 341 ip6_packet.set_flow_label(148997) 342 ip6_packet.set_payload_length(1500) 343 ip6_packet.set_next_header(17) 344 ip6_packet.set_hop_limit(1) 345 ip6_packet.set_ip_src("FE80::78F8:89D1:30FF:256B") 346 ip6_packet.set_ip_dst("FF02::1:3") 347 348 hop_by_hop = IP6_Extension_Headers.Hop_By_Hop() 349 hop_by_hop.add_option(IP6_Extension_Headers.Option_PADN(14)) 350 351 routing_options = IP6_Extension_Headers.Routing_Options() 352 routing_options.set_next_header(58) 353 routing_options.set_routing_type(0) 354 routing_options.set_segments_left(10) 355 356 dest_opts = IP6_Extension_Headers.Destination_Options() 357 dest_opts.add_option(IP6_Extension_Headers.Option_PAD1()) 358 359 ip6_packet.contains(hop_by_hop) 360 hop_by_hop.contains(routing_options) 361 routing_options.contains(dest_opts) 362 dest_opts.set_next_header(58) 363 364 self.assertEqual( 365 self.string_to_list(ip6_packet.get_packet()), binary_packet, 366 "Chained options inside an IPv6 packet - Buffer mismatch") 367 368 self.assertEqual( 369 ip6_packet.get_size(), len(binary_packet), 370 "Chained options inside an IPv6 packet - Size mismatch") 371 372 def test_decoding_simple_hop_by_hop(self): 373 hop_by_hop_binary_packet = [ 374 0x2b, 0x01, 0x01, 0x0C, 375 0x00, 0x00, 0x00, 0x00, 376 0x00, 0x00, 0x00, 0x00, 377 0x00, 0x00, 0x00, 0x00] 378 379 d = ImpactDecoder.HopByHopDecoder() 380 parsed_packet = d.decode(hop_by_hop_binary_packet) 381 382 next_header = parsed_packet.get_next_header() 383 header_extension_length = parsed_packet.get_header_extension_length() 384 options = parsed_packet.get_options() 385 386 self.assertEqual(1, len(options), "Simple Hop By Hop Parsing - Wrong Quantity of Options") 387 388 padn_option = options[0] 389 padn_option_type = padn_option.get_option_type() 390 padn_option_length = padn_option.get_option_length() 391 392 self.assertEqual(parsed_packet.get_header_type(), 0, "Simple Hop By Hop Parsing - Incorrect packet") 393 self.assertEqual(next_header, 43, "Simple Hop By Hop Parsing - Incorrect next header value") 394 self.assertEqual(header_extension_length, 1, "Simple Hop By Hop Parsing - Incorrect size") 395 self.assertEqual(padn_option_type, 1, "Simple Hop By Hop Parsing - Incorrect option type") 396 self.assertEqual(padn_option_length, 12, "Simple Hop By Hop Parsing - Incorrect option size") 397 398 def test_decoding_multi_option_hop_by_hop(self): 399 hop_by_hop_binary_packet = [ 400 0x3a, 0x00, 0x00, 0x01, 401 0x03, 0x00, 0x00, 0x00] 402 403 d = ImpactDecoder.HopByHopDecoder() 404 parsed_packet = d.decode(hop_by_hop_binary_packet) 405 406 next_header = parsed_packet.get_next_header() 407 header_extension_length = parsed_packet.get_header_extension_length() 408 options = parsed_packet.get_options() 409 410 self.assertEqual(2, len(options), "Simple Hop By Hop Parsing - Wrong Quantity of Options") 411 412 pad1_option = options[0] 413 pad1_option_type = pad1_option.get_option_type() 414 415 padn_option = options[1] 416 padn_option_type = padn_option.get_option_type() 417 padn_option_length = padn_option.get_option_length() 418 419 self.assertEqual(parsed_packet.get_header_type(), 0, "Hop By Hop with multiple options parsing - Incorrect packet") 420 self.assertEqual(next_header, 58, "Hop By Hop with multiple options parsing - Incorrect next header value") 421 self.assertEqual(header_extension_length, 0, "Hop By Hop with multiple options parsing - Incorrect size") 422 self.assertEqual(pad1_option_type, 0, "Hop By Hop with multiple options parsing - Incorrect option type") 423 self.assertEqual(padn_option_type, 1, "Hop By Hop with multiple options parsing - Incorrect option type") 424 self.assertEqual(padn_option_length, 3, "Hop By Hop with multiple options parsing - Incorrect option size") 425 426 def test_decoding_simple_destination_options(self): 427 destination_options_binary_packet = [ 428 0x2b, 0x01, 0x01, 0x0C, 429 0x00, 0x00, 0x00, 0x00, 430 0x00, 0x00, 0x00, 0x00, 431 0x00, 0x00, 0x00, 0x00] 432 433 d = ImpactDecoder.DestinationOptionsDecoder() 434 parsed_packet = d.decode(destination_options_binary_packet) 435 436 next_header = parsed_packet.get_next_header() 437 header_extension_length = parsed_packet.get_header_extension_length() 438 options = parsed_packet.get_options() 439 440 self.assertEqual(1, len(options), "Simple Destination Options Parsing - Wrong Quantity of Options") 441 442 padn_option = options[0] 443 padn_option_type = padn_option.get_option_type() 444 padn_option_length = padn_option.get_option_length() 445 446 self.assertEqual(parsed_packet.get_header_type(), 60, "Simple Destination Options Parsing - Incorrect packet") 447 self.assertEqual(next_header, 43, "Simple Destination Options Parsing - Incorrect next header value") 448 self.assertEqual(header_extension_length, 1, "Simple Destination Options Parsing - Incorrect size") 449 self.assertEqual(padn_option_type, 1, "Simple Destination Options Parsing - Incorrect option type") 450 self.assertEqual(padn_option_length, 12, "Simple Destination Options Parsing - Incorrect option size") 451 452 def test_decoding_multi_option_destination_options(self): 453 destination_options_binary_packet = [ 454 0x3a, 0x00, 0x00, 0x01, 455 0x03, 0x00, 0x00, 0x00] 456 457 d = ImpactDecoder.DestinationOptionsDecoder() 458 parsed_packet = d.decode(destination_options_binary_packet) 459 460 next_header = parsed_packet.get_next_header() 461 header_extension_length = parsed_packet.get_header_extension_length() 462 options = parsed_packet.get_options() 463 464 self.assertEqual(2, len(options), "Destination Options with multiple options parsing - Wrong Quantity of Options") 465 466 pad1_option = options[0] 467 pad1_option_type = pad1_option.get_option_type() 468 469 padn_option = options[1] 470 padn_option_type = padn_option.get_option_type() 471 padn_option_length = padn_option.get_option_length() 472 473 self.assertEqual(parsed_packet.get_header_type(), 60, "Destination Options with multiple options parsing - Incorrect packet") 474 self.assertEqual(next_header, 58, "Destination Options with multiple options parsing - Incorrect next header value") 475 self.assertEqual(header_extension_length, 0, "Destination Options with multiple options parsing - Incorrect size") 476 self.assertEqual(pad1_option_type, 0, "Destination Options with multiple options parsing - Incorrect option type") 477 self.assertEqual(padn_option_type, 1, "Destination Options with multiple options parsing - Incorrect option type") 478 self.assertEqual(padn_option_length, 3, "Destination Options with multiple options parsing - Incorrect option size") 479 480 def test_decoding_simple_routing_options(self): 481 routing_options_binary_packet = [0x3a, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00] 482 483 d = ImpactDecoder.RoutingOptionsDecoder() 484 parsed_packet = d.decode(routing_options_binary_packet) 485 486 next_header = parsed_packet.get_next_header() 487 header_extension_length = parsed_packet.get_header_extension_length() 488 routing_type = parsed_packet.get_routing_type() 489 segments_left = parsed_packet.get_segments_left() 490 options = parsed_packet.get_options() 491 492 self.assertEqual(parsed_packet.get_header_type(), 43, "Simple Routing Options Parsing - Incorrect packet") 493 self.assertEqual(next_header, 58, "Simple Routing Options Parsing - Incorrect next header value") 494 self.assertEqual(header_extension_length, 0, "Simple Routing Options Parsing - Incorrect size") 495 self.assertEqual(routing_type, 0, "Simple Routing Options Parsing - Incorrect routing type") 496 self.assertEqual(segments_left, 10, "Simple Routing Options Parsing - Incorrect quantity of segments left size") 497 self.assertEqual(0, len(options), "Simple Routing Options Parsing - Wrong Quantity of Options") 498 499 def test_decoding_chained_basic_options_inside_ipv6_packet(self): 500 ipv6_binary_packet = [ 501 0x64, 0x82, 0x46, 0x05, 502 0x05, 0xdc, 0x00, 0x01, 503 0xfe, 0x80, 0x00, 0x00, 504 0x00, 0x00, 0x00, 0x00, 505 0x78, 0xf8, 0x89, 0xd1, 506 0x30, 0xff, 0x25, 0x6b, 507 0xff, 0x02, 0x00, 0x00, 508 0x00, 0x00, 0x00, 0x00, 509 0x00, 0x00, 0x00, 0x00, 510 0x00, 0x01, 0x00, 0x03] 511 512 hop_by_hop_binary_packet = [ 513 0x2b, 0x01, 0x01, 0x0C, 514 0x00, 0x00, 0x00, 0x00, 515 0x00, 0x00, 0x00, 0x00, 516 0x00, 0x00, 0x00, 0x00] 517 518 routing_options_binary_packet = [ 519 0x3c, 0x00, 0x00, 0x0a, 520 0x00, 0x00, 0x00, 0x00] 521 522 dest_opts_binary_packet = [ 523 0x3a, 0x00, 0x00, 0x01, 524 0x03, 0x00, 0x00, 0x00] 525 526 binary_packet = ipv6_binary_packet + hop_by_hop_binary_packet + routing_options_binary_packet + dest_opts_binary_packet 527 528 d = ImpactDecoder.IP6Decoder() 529 parsed_ipv6_packet = d.decode(binary_packet) 530 531 # IPv6 Parsing 532 ipv6_protocol_version = parsed_ipv6_packet.get_ip_v() 533 ipv6_traffic_class = parsed_ipv6_packet.get_traffic_class() 534 ipv6_flow_label = parsed_ipv6_packet.get_flow_label() 535 ipv6_payload_length = parsed_ipv6_packet.get_payload_length() 536 ipv6_next_header = parsed_ipv6_packet.get_next_header() 537 ipv6_hop_limit = parsed_ipv6_packet.get_hop_limit() 538 ipv6_source_address = parsed_ipv6_packet.get_ip_src() 539 ipv6_destination_address = parsed_ipv6_packet.get_ip_dst() 540 541 # Hop By Hop Parsing 542 hop_by_hop_parsed_packet = parsed_ipv6_packet.child() 543 hop_by_hop_next_header = hop_by_hop_parsed_packet.get_next_header() 544 hop_by_hop_header_extension_length = hop_by_hop_parsed_packet.get_header_extension_length() 545 hop_by_hop_options = hop_by_hop_parsed_packet.get_options() 546 self.assertEqual(1, len(hop_by_hop_options), "Hop By Hop Parsing - Wrong Quantity of Options") 547 hop_by_hop_padn_option = hop_by_hop_options[0] 548 hop_by_hop_padn_option_type = hop_by_hop_padn_option.get_option_type() 549 hop_by_hop_padn_option_length = hop_by_hop_padn_option.get_option_length() 550 551 # Routing Options Tests 552 routing_options_parsed_packet = hop_by_hop_parsed_packet.child() 553 routing_options_next_header = routing_options_parsed_packet.get_next_header() 554 routing_options_header_extension_length = routing_options_parsed_packet.get_header_extension_length() 555 routing_options_routing_type = routing_options_parsed_packet.get_routing_type() 556 routing_options_segments_left = routing_options_parsed_packet.get_segments_left() 557 routing_options_options = routing_options_parsed_packet.get_options() 558 559 # Destination Options Parsing 560 destination_options_parsed_packet = routing_options_parsed_packet.child() 561 destination_options_next_header = destination_options_parsed_packet.get_next_header() 562 destination_options_header_extension_length = destination_options_parsed_packet.get_header_extension_length() 563 destination_options_options = destination_options_parsed_packet.get_options() 564 self.assertEqual(2, len(destination_options_options), "Destination Options Parsing - Wrong Quantity of Options") 565 destination_options_pad1_option = destination_options_options[0] 566 destination_options_pad1_option_type = destination_options_pad1_option.get_option_type() 567 destination_options_padn_option = destination_options_options[1] 568 destination_options_padn_option_type = destination_options_padn_option.get_option_type() 569 destination_options_padn_option_length = destination_options_padn_option.get_option_length() 570 571 self.assertEqual(ipv6_protocol_version, 6, "IP6 parsing - Incorrect protocol version") 572 self.assertEqual(ipv6_traffic_class, 72, "IP6 parsing - Incorrect traffic class") 573 self.assertEqual(ipv6_flow_label, 148997, "IP6 parsing - Incorrect flow label") 574 self.assertEqual(ipv6_payload_length, 1500, "IP6 parsing - Incorrect payload length") 575 self.assertEqual(ipv6_next_header, 0, "IP6 parsing - Incorrect next header") 576 self.assertEqual(ipv6_hop_limit, 1, "IP6 parsing - Incorrect hop limit") 577 self.assertEqual(ipv6_source_address.as_string(), "FE80::78F8:89D1:30FF:256B", "IP6 parsing - Incorrect source address") 578 self.assertEqual(ipv6_destination_address.as_string(), "FF02::1:3", "IP6 parsing - Incorrect destination address") 579 self.assertEqual(hop_by_hop_parsed_packet.get_header_type(), 0, "Hop By Hop Parsing - Incorrect packet") 580 self.assertEqual(hop_by_hop_next_header, 43, "Hop By Hop Parsing - Incorrect next header value") 581 self.assertEqual(hop_by_hop_header_extension_length, 1, "Hop By Hop Parsing - Incorrect size") 582 self.assertEqual(hop_by_hop_padn_option_type, 1, "Hop By Hop Parsing - Incorrect option type") 583 self.assertEqual(hop_by_hop_padn_option_length, 12, "Hop By Hop Parsing - Incorrect option size") 584 self.assertEqual(routing_options_parsed_packet.get_header_type(), 43, "Routing Options Parsing - Incorrect packet") 585 self.assertEqual(routing_options_next_header, 60, "Routing Options Parsing - Incorrect next header value") 586 self.assertEqual(routing_options_header_extension_length, 0, "Routing Options Parsing - Incorrect size") 587 self.assertEqual(routing_options_routing_type, 0, "Routing Options Parsing - Incorrect routing type") 588 self.assertEqual(routing_options_segments_left, 10, "Routing Options Parsing - Incorrect quantity of segments left size") 589 self.assertEqual(0, len(routing_options_options), "Routing Options Parsing - Wrong Quantity of Options") 590 self.assertEqual(destination_options_parsed_packet.get_header_type(), 60, "Destination Options Parsing - Incorrect packet") 591 self.assertEqual(destination_options_next_header, 58, "Destination Options Parsing - Incorrect next header value") 592 self.assertEqual(destination_options_header_extension_length, 0, "Destination Options Parsing - Incorrect size") 593 self.assertEqual(destination_options_pad1_option_type, 0, "Destination Options Parsing - Incorrect option type") 594 self.assertEqual(destination_options_padn_option_type, 1, "Destination Options Parsing - Incorrect option type") 595 self.assertEqual(destination_options_padn_option_length, 3, "Destination Options Parsing - Incorrect option size") 596 597 def test_decoding_extension_header_from_string(self): 598 hop_by_hop_binary_packet = b'\x2b\x01\x01\x0C\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 599 600 d = ImpactDecoder.HopByHopDecoder() 601 parsed_packet = d.decode(hop_by_hop_binary_packet) 602 603 next_header = parsed_packet.get_next_header() 604 header_extension_length = parsed_packet.get_header_extension_length() 605 options = parsed_packet.get_options() 606 607 self.assertEqual(1, len(options), "Simple Hop By Hop Parsing - Wrong Quantity of Options") 608 609 padn_option = options[0] 610 padn_option_type = padn_option.get_option_type() 611 padn_option_length = padn_option.get_option_length() 612 613 self.assertEqual(parsed_packet.get_header_type(), 0, "Simple Hop By Hop Parsing - Incorrect packet") 614 self.assertEqual(next_header, 43, "Simple Hop By Hop Parsing - Incorrect next header value") 615 self.assertEqual(header_extension_length, 1, "Simple Hop By Hop Parsing - Incorrect size") 616 self.assertEqual(padn_option_type, 1, "Simple Hop By Hop Parsing - Incorrect option type") 617 self.assertEqual(padn_option_length, 12, "Simple Hop By Hop Parsing - Incorrect option size") 618 619 suite = unittest.TestLoader().loadTestsFromTestCase(TestIP6) 620 unittest.TextTestRunner(verbosity=1).run(suite)