1 % Regression tests for Scapy regarding fields 2 3 ############ 4 ############ 5 + Tests on basic fields 6 7 #= Field class 8 #~ core field 9 #Field("foo", None, fmt="H").i2m(None,0xabcdef) 10 #assert( _ == b"\xcd\xef" ) 11 #Field("foo", None, fmt="<I").i2m(None,0x12cdef) 12 #assert( _ == b"\xef\xcd\x12\x00" ) 13 #Field("foo", None, fmt="B").addfield(None, "FOO", 0x12) 14 #assert( _ == b"FOO\x12" ) 15 #Field("foo", None, fmt="I").getfield(None, b"\x12\x34\x56\x78ABCD") 16 #assert( _ == ("ABCD",0x12345678) ) 17 # 18 #= ConditionnalField class 19 #~ core field 20 #False 21 22 23 = Simple tests 24 25 assert LELongField("test", None).addfield(None, b"", 0x44434241) == b'ABCD\x00\x00\x00\x00' 26 27 = MACField class 28 ~ core field 29 m = MACField("foo", None) 30 m.i2m(None, None) 31 assert( _ == b"\x00\x00\x00\x00\x00\x00" ) 32 m.getfield(None, b"\xc0\x01\xbe\xef\xba\xbeABCD") 33 assert( _ == (b"ABCD","c0:01:be:ef:ba:be") ) 34 m.addfield(None, b"FOO", "c0:01:be:ef:ba:be") 35 assert( _ == b"FOO\xc0\x01\xbe\xef\xba\xbe" ) 36 37 = SourceMACField, ARPSourceMACField 38 conf.route.add(net="1.2.3.4/32", dev=conf.iface) 39 p = Ether() / ARP(pdst="1.2.3.4") 40 assert p.src == p.hwsrc == p[ARP].hwsrc == get_if_hwaddr(conf.iface) 41 conf.route.delt(net="1.2.3.4/32") 42 43 = IPField class 44 ~ core field 45 46 if WINDOWS: 47 route_add_loopback() 48 49 i = IPField("foo", None) 50 i.i2m(None, "1.2.3.4") 51 assert( _ == b"\x01\x02\x03\x04" ) 52 i.i2m(None, "255.255.255.255") 53 assert( _ == b"\xff\xff\xff\xff" ) 54 i.m2i(None, b"\x01\x02\x03\x04") 55 assert( _ == "1.2.3.4" ) 56 i.getfield(None, b"\x01\x02\x03\x04ABCD") 57 assert( _ == (b"ABCD","1.2.3.4") ) 58 i.addfield(None, b"FOO", "1.2.3.4") 59 assert( _ == b"FOO\x01\x02\x03\x04" ) 60 61 = SourceIPField 62 ~ core field 63 defaddr = conf.route.route('0.0.0.0')[1] 64 class Test(Packet): fields_desc = [SourceIPField("sourceip", None)] 65 66 assert Test().sourceip == defaddr 67 assert Test(raw(Test())).sourceip == defaddr 68 69 assert IP(dst="0.0.0.0").src == defaddr 70 assert IP(raw(IP(dst="0.0.0.0"))).src == defaddr 71 assert IP(dst="0.0.0.0/31").src == defaddr 72 assert IP(raw(IP(dst="0.0.0.0/31"))).src == defaddr 73 74 75 #= ByteField 76 #~ core field 77 #b = ByteField("foo", None) 78 #b.i2m(" 79 #b.getfield 80 81 82 ############ 83 ############ 84 + Tests on ActionField 85 86 = Creation of a layer with ActionField 87 ~ field actionfield 88 89 from __future__ import print_function 90 91 class TestAction(Packet): 92 __slots__ = ["_val", "_fld", "_priv1", "_priv2"] 93 name = "TestAction" 94 fields_desc = [ ActionField(ByteField("tst", 3), "my_action", priv1=1, priv2=2) ] 95 def __init__(self, *args, **kargs): 96 self._val, self._fld, self._priv1, self._priv2 = None, None, None, None 97 super(TestAction, self).__init__(*args, **kargs) 98 def my_action(self, val, fld, priv1, priv2): 99 print("Action (%i)!" % val) 100 self._val, self._fld, self._priv1, self._priv2 = val, fld, priv1, priv2 101 102 = Triggering action 103 ~ field actionfield 104 105 t = TestAction() 106 assert(t._val == t._fld == t._priv1 == t._priv2 == None) 107 t.tst=42 108 assert(t._priv1 == 1) 109 assert(t._priv2 == 2) 110 assert(t._val == 42) 111 112 113 ############ 114 ############ 115 + Tests on FieldLenField 116 117 = Creation of a layer with FieldLenField 118 ~ field 119 class TestFLenF(Packet): 120 fields_desc = [ FieldLenField("len", None, length_of="str", fmt="B", adjust=lambda pkt,x:x+1), 121 StrLenField("str", "default", length_from=lambda pkt:pkt.len-1,) ] 122 123 = Assembly of an empty packet 124 ~ field 125 TestFLenF() 126 raw(_) 127 _ == b"\x08default" 128 129 = Assembly of non empty packet 130 ~ field 131 TestFLenF(str="123") 132 raw(_) 133 _ == b"\x04123" 134 135 = Disassembly 136 ~ field 137 TestFLenF(b"\x04ABCDEFGHIJKL") 138 _ 139 _.len == 4 and _.str == b"ABC" and Raw in _ 140 141 142 = BitFieldLenField test 143 ~ field 144 class TestBFLenF(Packet): 145 fields_desc = [ BitFieldLenField("len", None, 4, length_of="str" , adjust=lambda pkt,x:x+1), 146 BitField("nothing",0xfff, 12), 147 StrLenField("str", "default", length_from=lambda pkt:pkt.len-1, ) ] 148 149 a=TestBFLenF() 150 raw(a) 151 assert( _ == b"\x8f\xffdefault" ) 152 153 a.str="" 154 raw(a) 155 assert( _ == b"\x1f\xff" ) 156 157 TestBFLenF(b"\x1f\xff@@") 158 assert( _.len == 1 and _.str == b"" and Raw in _ and _[Raw].load == b"@@" ) 159 160 TestBFLenF(b"\x6f\xffabcdeFGH") 161 assert( _.len == 6 and _.str == b"abcde" and Raw in _ and _[Raw].load == b"FGH" ) 162 163 164 165 ############ 166 ############ 167 + Tests on FieldListField 168 169 = Creation of a layer 170 ~ field 171 class TestFLF(Packet): 172 name="test" 173 fields_desc = [ FieldLenField("len", None, count_of="lst", fmt="B"), 174 FieldListField("lst", None, IntField("elt",0), count_from=lambda pkt:pkt.len) 175 ] 176 177 = Assembly of an empty packet 178 ~ field 179 a = TestFLF() 180 raw(a) 181 182 = Assembly of a non-empty packet 183 ~ field 184 a = TestFLF() 185 a.lst = [7,65539] 186 ls(a) 187 raw(a) 188 import struct 189 _ == struct.pack("!BII", 2,7,65539) 190 191 = Disassemble 192 ~ field 193 import struct 194 TestFLF(b"\x00\x11\x12") 195 assert(_.len == 0 and Raw in _ and _[Raw].load == b"\x11\x12") 196 TestFLF(struct.pack("!BIII",3,1234,2345,12345678)) 197 assert(_.len == 3 and _.lst == [1234,2345,12345678]) 198 199 = Manipulate 200 ~ field 201 a = TestFLF(lst=[4]) 202 raw(a) 203 assert(_ == b"\x01\x00\x00\x00\x04") 204 a.lst.append(1234) 205 TestFLF(raw(a)) 206 a.show2() 207 a.len=7 208 raw(a) 209 assert(_ == b"\x07\x00\x00\x00\x04\x00\x00\x04\xd2") 210 a.len=2 211 a.lst=[1,2,3,4,5] 212 TestFLF(raw(a)) 213 assert(Raw in _ and _[Raw].load == b'\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05') 214 215 216 ############ 217 ############ 218 + PacketListField 219 220 = Create a layer 221 ~ field lengthfield 222 class TestPLF(Packet): 223 name="test" 224 fields_desc=[ FieldLenField("len", None, count_of="plist"), 225 PacketListField("plist", None, IP, count_from=lambda pkt:pkt.len,) ] 226 227 = Test the PacketListField assembly 228 ~ field lengthfield 229 x=TestPLF() 230 raw(x) 231 _ == b"\x00\x00" 232 233 = Test the PacketListField assembly 2 234 ~ field lengthfield 235 x=TestPLF() 236 x.plist=[IP()/TCP(), IP()/UDP()] 237 raw(x) 238 _.startswith(b'\x00\x02E') 239 240 = Test disassembly 241 ~ field lengthfield 242 x=TestPLF(plist=[IP()/TCP(seq=1234567), IP()/UDP()]) 243 TestPLF(raw(x)) 244 _.show() 245 IP in _ and TCP in _ and UDP in _ and _[TCP].seq == 1234567 246 247 = Nested PacketListField 248 ~ field lengthfield 249 y=IP()/TCP(seq=111111)/TestPLF(plist=[IP()/TCP(seq=222222),IP()/UDP()]) 250 TestPLF(plist=[y,IP()/TCP(seq=333333)]) 251 _.show() 252 IP in _ and TCP in _ and UDP in _ and _[TCP].seq == 111111 and _[TCP:2].seq==222222 and _[TCP:3].seq == 333333 253 254 ############ 255 ############ 256 + PacketListField tests 257 258 = Create a layer 259 ~ field lengthfield 260 class TestPLF(Packet): 261 name="test" 262 fields_desc=[ FieldLenField("len", None, count_of="plist"), 263 PacketListField("plist", None, IP, count_from=lambda pkt:pkt.len) ] 264 265 = Test the PacketListField assembly 266 ~ field lengthfield 267 x=TestPLF() 268 raw(x) 269 _ == b"\x00\x00" 270 271 = Test the PacketListField assembly 2 272 ~ field lengthfield 273 x=TestPLF() 274 x.plist=[IP()/TCP(), IP()/UDP()] 275 raw(x) 276 _.startswith(b'\x00\x02E') 277 278 = Test disassembly 279 ~ field lengthfield 280 x=TestPLF(plist=[IP()/TCP(seq=1234567), IP()/UDP()]) 281 TestPLF(raw(x)) 282 _.show() 283 IP in _ and TCP in _ and UDP in _ and _[TCP].seq == 1234567 284 285 = Nested PacketListField 286 ~ field lengthfield 287 y=IP()/TCP(seq=111111)/TestPLF(plist=[IP()/TCP(seq=222222),IP()/UDP()]) 288 TestPLF(plist=[y,IP()/TCP(seq=333333)]) 289 _.show() 290 IP in _ and TCP in _ and UDP in _ and _[TCP].seq == 111111 and _[TCP:2].seq==222222 and _[TCP:3].seq == 333333 291 292 = Complex packet 293 ~ field lengthfield ccc 294 class TestPkt(Packet): 295 fields_desc = [ ByteField("f1",65), 296 ShortField("f2",0x4244) ] 297 def extract_padding(self, p): 298 return "", p 299 300 class TestPLF2(Packet): 301 fields_desc = [ FieldLenField("len1", None, count_of="plist", fmt="H", 302 adjust=lambda pkt, x: x + 2), 303 FieldLenField("len2", None, length_of="plist", fmt="I", 304 adjust=lambda pkt, x: (x + 1) // 2), 305 PacketListField("plist", None, TestPkt, 306 length_from=lambda x: (x.len2 * 2) // 3 * 3) ] 307 308 a=TestPLF2() 309 raw(a) 310 assert( _ == b"\x00\x02\x00\x00\x00\x00" ) 311 312 a.plist=[TestPkt(),TestPkt(f1=100)] 313 raw(a) 314 assert(_ == b'\x00\x04\x00\x00\x00\x03ABDdBD') 315 316 a /= "123456" 317 b = TestPLF2(raw(a)) 318 b.show() 319 assert(b.len1 == 4 and b.len2 == 3) 320 assert(b[TestPkt].f1 == 65 and b[TestPkt].f2 == 0x4244) 321 assert(b[TestPkt:2].f1 == 100) 322 assert(Raw in b and b[Raw].load == b"123456") 323 324 a.plist.append(TestPkt(f1=200)) 325 b = TestPLF2(raw(a)) 326 b.show() 327 assert(b.len1 == 5 and b.len2 == 5) 328 assert(b[TestPkt].f1 == 65 and b[TestPkt].f2 == 0x4244) 329 assert(b[TestPkt:2].f1 == 100) 330 assert(b[TestPkt:3].f1 == 200) 331 assert(b.getlayer(TestPkt,4) is None) 332 assert(Raw in b and b[Raw].load == b"123456") 333 hexdiff(a,b) 334 assert( raw(a) == raw(b) ) 335 336 ############ 337 ############ 338 + Tests on TCPOptionsField 339 340 = Test calls on TCPOptionsField.getfield 341 342 assert TCPOptionsField("test", "").getfield(TCP(dataofs=0), "") == ('', []) 343 344 345 ############ 346 ############ 347 + PacketListField tests 348 349 = Create a layer 350 ~ field lengthfield 351 class TestPLF(Packet): 352 name="test" 353 fields_desc=[ FieldLenField("len", None, count_of="plist"), 354 PacketListField("plist", None, IP, count_from=lambda pkt:pkt.len) ] 355 356 = Test the PacketListField assembly 357 ~ field lengthfield 358 x=TestPLF() 359 raw(x) 360 _ == b"\x00\x00" 361 362 = Test the PacketListField assembly 2 363 ~ field lengthfield 364 x=TestPLF() 365 x.plist=[IP()/TCP(), IP()/UDP()] 366 raw(x) 367 _.startswith(b'\x00\x02E') 368 369 = Test disassembly 370 ~ field lengthfield 371 x=TestPLF(plist=[IP()/TCP(seq=1234567), IP()/UDP()]) 372 TestPLF(raw(x)) 373 _.show() 374 IP in _ and TCP in _ and UDP in _ and _[TCP].seq == 1234567 375 376 = Nested PacketListField 377 ~ field lengthfield 378 y=IP()/TCP(seq=111111)/TestPLF(plist=[IP()/TCP(seq=222222),IP()/UDP()]) 379 TestPLF(plist=[y,IP()/TCP(seq=333333)]) 380 _.show() 381 IP in _ and TCP in _ and UDP in _ and _[TCP].seq == 111111 and _[TCP:2].seq==222222 and _[TCP:3].seq == 333333 382 383 = Complex packet 384 ~ field lengthfield ccc 385 class TestPkt(Packet): 386 fields_desc = [ ByteField("f1",65), 387 ShortField("f2",0x4244) ] 388 def extract_padding(self, p): 389 return "", p 390 391 class TestPLF2(Packet): 392 fields_desc = [ FieldLenField("len1", None, count_of="plist",fmt="H", 393 adjust=lambda pkt,x: x + 2), 394 FieldLenField("len2", None, length_of="plist", fmt="I", 395 adjust=lambda pkt, x: (x + 1) // 2), 396 PacketListField("plist", None, TestPkt, 397 length_from=lambda x: (x.len2 * 2) // 3 *3) ] 398 399 a=TestPLF2() 400 raw(a) 401 assert( _ == b"\x00\x02\x00\x00\x00\x00" ) 402 403 a.plist=[TestPkt(),TestPkt(f1=100)] 404 raw(a) 405 assert(_ == b'\x00\x04\x00\x00\x00\x03ABDdBD') 406 407 a /= "123456" 408 b = TestPLF2(raw(a)) 409 b.show() 410 assert(b.len1 == 4 and b.len2 == 3) 411 assert(b[TestPkt].f1 == 65 and b[TestPkt].f2 == 0x4244) 412 assert(b[TestPkt:2].f1 == 100) 413 assert(Raw in b and b[Raw].load == b"123456") 414 415 a.plist.append(TestPkt(f1=200)) 416 b = TestPLF2(raw(a)) 417 b.show() 418 assert(b.len1 == 5 and b.len2 == 5) 419 assert(b[TestPkt].f1 == 65 and b[TestPkt].f2 == 0x4244) 420 assert(b[TestPkt:2].f1 == 100) 421 assert(b[TestPkt:3].f1 == 200) 422 assert(b.getlayer(TestPkt,4) is None) 423 assert(Raw in b and b[Raw].load == b"123456") 424 hexdiff(a,b) 425 assert( raw(a) == raw(b) ) 426 427 = Create layers for heterogeneous PacketListField 428 ~ field lengthfield 429 TestPLFH1 = type('TestPLFH1', (Packet,), { 430 'name': 'test1', 431 'fields_desc': [ByteField('data', 0)], 432 'guess_payload_class': lambda self, p: conf.padding_layer, 433 } 434 ) 435 TestPLFH2 = type('TestPLFH2', (Packet,), { 436 'name': 'test2', 437 'fields_desc': [ShortField('data', 0)], 438 'guess_payload_class': lambda self, p: conf.padding_layer, 439 } 440 ) 441 class TestPLFH3(Packet): 442 name = 'test3' 443 fields_desc = [ 444 PacketListField( 445 'data', [], 446 next_cls_cb=lambda pkt, lst, p, remain: pkt.detect_next_packet(lst, p, remain) 447 ) 448 ] 449 def detect_next_packet(self, lst, p, remain): 450 if len(remain) < 3: 451 return None 452 if isinstance(p, type(None)): 453 return TestPLFH1 454 if p.data & 3 == 1: 455 return TestPLFH1 456 if p.data & 3 == 2: 457 return TestPLFH2 458 return None 459 460 = Test heterogeneous PacketListField 461 ~ field lengthfield 462 463 p = TestPLFH3(b'\x02\x01\x01\xc1\x02\x80\x04toto') 464 assert(isinstance(p.data[0], TestPLFH1)) 465 assert(p.data[0].data == 0x2) 466 assert(isinstance(p.data[1], TestPLFH2)) 467 assert(p.data[1].data == 0x101) 468 assert(isinstance(p.data[2], TestPLFH1)) 469 assert(p.data[2].data == 0xc1) 470 assert(isinstance(p.data[3], TestPLFH1)) 471 assert(p.data[3].data == 0x2) 472 assert(isinstance(p.data[4], TestPLFH2)) 473 assert(p.data[4].data == 0x8004) 474 assert(isinstance(p.payload, conf.raw_layer)) 475 assert(p.payload.load == b'toto') 476 477 p = TestPLFH3(b'\x02\x01\x01\xc1\x02\x80\x02to') 478 assert(isinstance(p.data[0], TestPLFH1)) 479 assert(p.data[0].data == 0x2) 480 assert(isinstance(p.data[1], TestPLFH2)) 481 assert(p.data[1].data == 0x101) 482 assert(isinstance(p.data[2], TestPLFH1)) 483 assert(p.data[2].data == 0xc1) 484 assert(isinstance(p.data[3], TestPLFH1)) 485 assert(p.data[3].data == 0x2) 486 assert(isinstance(p.data[4], TestPLFH2)) 487 assert(p.data[4].data == 0x8002) 488 assert(isinstance(p.payload, conf.raw_layer)) 489 assert(p.payload.load == b'to') 490 491 = Create layers for heterogeneous PacketListField with memory 492 ~ field lengthfield 493 TestPLFH4 = type('TestPLFH4', (Packet,), { 494 'name': 'test4', 495 'fields_desc': [ByteField('data', 0)], 496 'guess_payload_class': lambda self, p: conf.padding_layer, 497 } 498 ) 499 TestPLFH5 = type('TestPLFH5', (Packet,), { 500 'name': 'test5', 501 'fields_desc': [ShortField('data', 0)], 502 'guess_payload_class': lambda self, p: conf.padding_layer, 503 } 504 ) 505 class TestPLFH6(Packet): 506 __slots__ = ['_memory'] 507 name = 'test6' 508 fields_desc = [ 509 PacketListField( 510 'data', [], 511 next_cls_cb=lambda pkt, lst, p, remain: pkt.detect_next_packet(lst, p, remain) 512 ) 513 ] 514 def detect_next_packet(self, lst, p, remain): 515 if isinstance(p, type(None)): 516 self._memory = [TestPLFH4] * 3 + [TestPLFH5] 517 try: 518 return self._memory.pop(0) 519 except IndexError: 520 return None 521 522 = Test heterogeneous PacketListField with memory 523 ~ field lengthfield 524 525 p = TestPLFH6(b'\x01\x02\x03\xc1\x02toto') 526 assert(isinstance(p.data[0], TestPLFH4)) 527 assert(p.data[0].data == 0x1) 528 assert(isinstance(p.data[1], TestPLFH4)) 529 assert(p.data[1].data == 0x2) 530 assert(isinstance(p.data[2], TestPLFH4)) 531 assert(p.data[2].data == 0x3) 532 assert(isinstance(p.data[3], TestPLFH5)) 533 assert(p.data[3].data == 0xc102) 534 assert(isinstance(p.payload, conf.raw_layer)) 535 assert(p.payload.load == b'toto') 536 537 538 ############ 539 ############ 540 + Tests on MultiFlagsField 541 542 = Test calls on MultiFlagsField.any2i 543 ~ multiflagsfield 544 545 import collections 546 MockPacket = collections.namedtuple('MockPacket', ['type']) 547 548 f = MultiFlagsField('flags', set(), 3, { 549 0: { 550 0: MultiFlagsEntry('A', 'OptionA'), 551 1: MultiFlagsEntry('B', 'OptionB'), 552 }, 553 1: { 554 0: MultiFlagsEntry('+', 'Plus'), 555 1: MultiFlagsEntry('*', 'Star'), 556 }, 557 }, 558 depends_on=lambda x: x.type 559 ) 560 561 mp = MockPacket(0) 562 x = f.any2i(mp, set()) 563 assert(isinstance(x, set)) 564 assert(len(x) == 0) 565 x = f.any2i(mp, {'A'}) 566 assert(isinstance(x, set)) 567 assert(len(x) == 1) 568 assert('A' in x) 569 assert('B' not in x) 570 assert('+' not in x) 571 x = f.any2i(mp, {'A', 'B'}) 572 assert(isinstance(x, set)) 573 assert(len(x) == 2) 574 assert('A' in x) 575 assert('B' in x) 576 assert('+' not in x) 577 assert('*' not in x) 578 x = f.any2i(mp, 3) 579 assert(isinstance(x, set)) 580 assert(len(x) == 2) 581 assert('A' in x) 582 assert('B' in x) 583 assert('+' not in x) 584 assert('*' not in x) 585 x = f.any2i(mp, 7) 586 assert(isinstance(x, set)) 587 assert(len(x) == 3) 588 assert('A' in x) 589 assert('B' in x) 590 assert('bit 2' in x) 591 assert('+' not in x) 592 assert('*' not in x) 593 mp = MockPacket(1) 594 x = f.any2i(mp, {'+', '*'}) 595 assert(isinstance(x, set)) 596 assert(len(x) == 2) 597 assert('+' in x) 598 assert('*' in x) 599 assert('A' not in x) 600 assert('B' not in x) 601 try: 602 x = f.any2i(mp, {'A'}) 603 ret = False 604 except AssertionError: 605 ret = True 606 607 assert(ret) 608 #Following test demonstrate a non-sensical yet acceptable usage :( 609 x = f.any2i(None, {'Toto'}) 610 assert('Toto' in x) 611 612 = Test calls on MultiFlagsField.i2m 613 ~ multiflagsfield 614 615 import collections 616 MockPacket = collections.namedtuple('MockPacket', ['type']) 617 618 f = MultiFlagsField('flags', set(), 3, { 619 0: { 620 0: MultiFlagsEntry('A', 'OptionA'), 621 1: MultiFlagsEntry('B', 'OptionB'), 622 }, 623 1: { 624 0: MultiFlagsEntry('+', 'Plus'), 625 1: MultiFlagsEntry('*', 'Star'), 626 }, 627 }, 628 depends_on=lambda x: x.type 629 ) 630 631 mp = MockPacket(0) 632 x = f.i2m(mp, set()) 633 assert(isinstance(x, six.integer_types)) 634 assert(x == 0) 635 x = f.i2m(mp, {'A'}) 636 assert(isinstance(x, six.integer_types)) 637 assert(x == 1) 638 x = f.i2m(mp, {'A', 'B'}) 639 assert(isinstance(x, six.integer_types)) 640 assert(x == 3) 641 x = f.i2m(mp, {'A', 'B', 'bit 2'}) 642 assert(isinstance(x, six.integer_types)) 643 assert(x == 7) 644 try: 645 x = f.i2m(mp, {'+'}) 646 ret = False 647 except: 648 ret = True 649 650 assert(ret) 651 652 = Test calls on MultiFlagsField.m2i 653 ~ multiflagsfield 654 655 import collections 656 MockPacket = collections.namedtuple('MockPacket', ['type']) 657 658 f = MultiFlagsField('flags', set(), 3, { 659 0: { 660 0: MultiFlagsEntry('A', 'OptionA'), 661 1: MultiFlagsEntry('B', 'OptionB'), 662 }, 663 1: { 664 0: MultiFlagsEntry('+', 'Plus'), 665 1: MultiFlagsEntry('*', 'Star'), 666 }, 667 }, 668 depends_on=lambda x: x.type 669 ) 670 671 mp = MockPacket(0) 672 x = f.m2i(mp, 2) 673 assert(isinstance(x, set)) 674 assert(len(x) == 1) 675 assert('B' in x) 676 assert('A' not in x) 677 assert('*' not in x) 678 679 x = f.m2i(mp, 7) 680 assert(isinstance(x, set)) 681 assert('B' in x) 682 assert('A' in x) 683 assert('bit 2' in x) 684 assert('*' not in x) 685 assert('+' not in x) 686 x = f.m2i(mp, 0) 687 assert(len(x) == 0) 688 mp = MockPacket(1) 689 x = f.m2i(mp, 2) 690 assert(isinstance(x, set)) 691 assert(len(x) == 1) 692 assert('*' in x) 693 assert('+' not in x) 694 assert('B' not in x) 695 696 = Test calls on MultiFlagsField.i2repr 697 ~ multiflagsfield 698 699 import collections, re 700 MockPacket = collections.namedtuple('MockPacket', ['type']) 701 702 f = MultiFlagsField('flags', set(), 3, { 703 0: { 704 0: MultiFlagsEntry('A', 'OptionA'), 705 1: MultiFlagsEntry('B', 'OptionB'), 706 }, 707 1: { 708 0: MultiFlagsEntry('+', 'Plus'), 709 1: MultiFlagsEntry('*', 'Star'), 710 }, 711 }, 712 depends_on=lambda x: x.type 713 ) 714 715 mp = MockPacket(0) 716 x = f.i2repr(mp, {'A', 'B'}) 717 assert(re.match(r'^.*OptionA \(A\).*$', x) is not None) 718 assert(re.match(r'^.*OptionB \(B\).*$', x) is not None) 719 mp = MockPacket(1) 720 x = f.i2repr(mp, {'*', '+', 'bit 2'}) 721 assert(re.match(r'^.*Star \(\*\).*$', x) is not None) 722 assert(re.match(r'^.*Plus \(\+\).*$', x) is not None) 723 assert(re.match(r'^.*bit 2.*$', x) is not None) 724 725 ############ 726 ############ 727 + EnumField tests 728 729 = EnumField tests initialization 730 731 # Basic EnumField 732 f = EnumField('test', 0, {0: 'Foo', 1: 'Bar'}) 733 # Reverse i2s/s2i 734 rf = EnumField('test', 0, {'Foo': 0, 'Bar': 1}) 735 # EnumField initialized with a list 736 lf = EnumField('test', 0, ['Foo', 'Bar']) 737 # EnumField with i2s_cb/s2i_cb 738 fcb = EnumField('test', 0, ( 739 lambda x: 'Foo' if x == 0 else 'Bar' if 1 <= x <= 10 else repr(x), 740 lambda x: 0 if x == 'Foo' else 1 if x == 'Bar' else int(x), 741 ) 742 ) 743 744 def expect_exception(e, c): 745 try: 746 eval(c) 747 return False 748 except e: 749 return True 750 751 752 = EnumField.any2i_one 753 ~ field enumfield 754 755 assert(f.any2i_one(None, 'Foo') == 0) 756 assert(f.any2i_one(None, 'Bar') == 1) 757 assert(f.any2i_one(None, 2) == 2) 758 expect_exception(KeyError, 'f.any2i_one(None, "Baz")') 759 760 assert(rf.any2i_one(None, 'Foo') == 0) 761 assert(rf.any2i_one(None, 'Bar') == 1) 762 assert(rf.any2i_one(None, 2) == 2) 763 expect_exception(KeyError, 'rf.any2i_one(None, "Baz")') 764 765 assert(lf.any2i_one(None, 'Foo') == 0) 766 assert(lf.any2i_one(None, 'Bar') == 1) 767 assert(lf.any2i_one(None, 2) == 2) 768 expect_exception(KeyError, 'lf.any2i_one(None, "Baz")') 769 770 assert(fcb.any2i_one(None, 'Foo') == 0) 771 assert(fcb.any2i_one(None, 'Bar') == 1) 772 assert(fcb.any2i_one(None, 5) == 5) 773 expect_exception(ValueError, 'fcb.any2i_one(None, "Baz")') 774 775 True 776 777 = EnumField.any2i 778 ~ field enumfield 779 780 assert(f.any2i(None, 'Foo') == 0) 781 assert(f.any2i(None, 'Bar') == 1) 782 assert(f.any2i(None, 2) == 2) 783 expect_exception(KeyError, 'f.any2i(None, "Baz")') 784 assert(f.any2i(None, ['Foo', 'Bar', 2]) == [0, 1, 2]) 785 786 assert(rf.any2i(None, 'Foo') == 0) 787 assert(rf.any2i(None, 'Bar') == 1) 788 assert(rf.any2i(None, 2) == 2) 789 expect_exception(KeyError, 'rf.any2i(None, "Baz")') 790 assert(rf.any2i(None, ['Foo', 'Bar', 2]) == [0, 1, 2]) 791 792 assert(lf.any2i(None, 'Foo') == 0) 793 assert(lf.any2i(None, 'Bar') == 1) 794 assert(lf.any2i(None, 2) == 2) 795 expect_exception(KeyError, 'lf.any2i(None, "Baz")') 796 assert(lf.any2i(None, ['Foo', 'Bar', 2]) == [0, 1, 2]) 797 798 assert(fcb.any2i(None, 'Foo') == 0) 799 assert(fcb.any2i(None, 'Bar') == 1) 800 assert(fcb.any2i(None, 5) == 5) 801 expect_exception(ValueError, 'fcb.any2i(None, "Baz")') 802 assert(f.any2i(None, ['Foo', 'Bar', 5]) == [0, 1, 5]) 803 804 True 805 806 = EnumField.i2repr_one 807 ~ field enumfield 808 809 assert(f.i2repr_one(None, 0) == 'Foo') 810 assert(f.i2repr_one(None, 1) == 'Bar') 811 expect_exception(KeyError, 'f.i2repr_one(None, 2)') 812 813 assert(rf.i2repr_one(None, 0) == 'Foo') 814 assert(rf.i2repr_one(None, 1) == 'Bar') 815 expect_exception(KeyError, 'rf.i2repr_one(None, 2)') 816 817 assert(lf.i2repr_one(None, 0) == 'Foo') 818 assert(lf.i2repr_one(None, 1) == 'Bar') 819 expect_exception(KeyError, 'lf.i2repr_one(None, 2)') 820 821 assert(fcb.i2repr_one(None, 0) == 'Foo') 822 assert(fcb.i2repr_one(None, 1) == 'Bar') 823 assert(fcb.i2repr_one(None, 5) == 'Bar') 824 assert(fcb.i2repr_one(None, 11) == repr(11)) 825 826 conf.noenum.add(f, rf, lf, fcb) 827 828 assert(f.i2repr_one(None, 0) == repr(0)) 829 assert(f.i2repr_one(None, 1) == repr(1)) 830 assert(f.i2repr_one(None, 2) == repr(2)) 831 832 assert(rf.i2repr_one(None, 0) == repr(0)) 833 assert(rf.i2repr_one(None, 1) == repr(1)) 834 assert(rf.i2repr_one(None, 2) == repr(2)) 835 836 assert(lf.i2repr_one(None, 0) == repr(0)) 837 assert(lf.i2repr_one(None, 1) == repr(1)) 838 assert(lf.i2repr_one(None, 2) == repr(2)) 839 840 assert(fcb.i2repr_one(None, 0) == repr(0)) 841 assert(fcb.i2repr_one(None, 1) == repr(1)) 842 assert(fcb.i2repr_one(None, 5) == repr(5)) 843 assert(fcb.i2repr_one(None, 11) == repr(11)) 844 845 conf.noenum.remove(f, rf, lf, fcb) 846 847 assert(f.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 848 assert(rf.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 849 assert(lf.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 850 assert(fcb.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 851 852 True 853 854 = EnumField.i2repr 855 ~ field enumfield 856 857 assert(f.i2repr(None, 0) == 'Foo') 858 assert(f.i2repr(None, 1) == 'Bar') 859 expect_exception(KeyError, 'f.i2repr(None, 2)') 860 assert(f.i2repr(None, [0, 1]) == ['Foo', 'Bar']) 861 862 assert(rf.i2repr(None, 0) == 'Foo') 863 assert(rf.i2repr(None, 1) == 'Bar') 864 expect_exception(KeyError, 'rf.i2repr(None, 2)') 865 assert(rf.i2repr(None, [0, 1]) == ['Foo', 'Bar']) 866 867 assert(lf.i2repr(None, 0) == 'Foo') 868 assert(lf.i2repr(None, 1) == 'Bar') 869 expect_exception(KeyError, 'lf.i2repr(None, 2)') 870 assert(lf.i2repr(None, [0, 1]) == ['Foo', 'Bar']) 871 872 assert(fcb.i2repr(None, 0) == 'Foo') 873 assert(fcb.i2repr(None, 1) == 'Bar') 874 assert(fcb.i2repr(None, 5) == 'Bar') 875 assert(fcb.i2repr(None, 11) == repr(11)) 876 assert(fcb.i2repr(None, [0, 1, 5, 11]) == ['Foo', 'Bar', 'Bar', repr(11)]) 877 878 conf.noenum.add(f, rf, lf, fcb) 879 880 assert(f.i2repr(None, 0) == repr(0)) 881 assert(f.i2repr(None, 1) == repr(1)) 882 assert(f.i2repr(None, 2) == repr(2)) 883 assert(f.i2repr(None, [0, 1, 2]) == [repr(0), repr(1), repr(2)]) 884 885 assert(rf.i2repr(None, 0) == repr(0)) 886 assert(rf.i2repr(None, 1) == repr(1)) 887 assert(rf.i2repr(None, 2) == repr(2)) 888 assert(rf.i2repr(None, [0, 1, 2]) == [repr(0), repr(1), repr(2)]) 889 890 assert(lf.i2repr(None, 0) == repr(0)) 891 assert(lf.i2repr(None, 1) == repr(1)) 892 assert(lf.i2repr(None, 2) == repr(2)) 893 assert(lf.i2repr(None, [0, 1, 2]) == [repr(0), repr(1), repr(2)]) 894 895 assert(fcb.i2repr(None, 0) == repr(0)) 896 assert(fcb.i2repr(None, 1) == repr(1)) 897 assert(fcb.i2repr(None, 5) == repr(5)) 898 assert(fcb.i2repr(None, 11) == repr(11)) 899 assert(fcb.i2repr(None, [0, 1, 5, 11]) == [repr(0), repr(1), repr(5), repr(11)]) 900 901 conf.noenum.remove(f, rf, lf, fcb) 902 903 assert(f.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 904 assert(rf.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 905 assert(lf.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 906 assert(fcb.i2repr_one(None, RandNum(0, 10)) == '<RandNum>') 907 908 True 909 910 ############ 911 ############ 912 + CharEnumField tests 913 914 = Building expect_exception handler 915 ~ field charenumfield 916 917 def expect_exception(e, c): 918 try: 919 eval(c) 920 return False 921 except e: 922 return True 923 924 925 = CharEnumField tests initialization 926 ~ field charenumfield 927 928 fc = CharEnumField('test', 'f', {'f': 'Foo', 'b': 'Bar'}) 929 fcb = CharEnumField('test', 'a', ( 930 lambda x: 'Foo' if x == 'a' else 'Bar' if x == 'b' else 'Baz', 931 lambda x: 'a' if x == 'Foo' else 'b' if x == 'Bar' else '' 932 )) 933 934 True 935 936 = CharEnumField.any2i_one 937 ~ field charenumfield 938 939 assert(fc.any2i_one(None, 'Foo') == 'f') 940 assert(fc.any2i_one(None, 'Bar') == 'b') 941 expect_exception(KeyError, 'fc.any2i_one(None, "Baz")') 942 943 assert(fcb.any2i_one(None, 'Foo') == 'a') 944 assert(fcb.any2i_one(None, 'Bar') == 'b') 945 assert(fcb.any2i_one(None, 'Baz') == '') 946 947 True 948 949 ############ 950 ############ 951 + XShortEnumField tests 952 953 = Building expect_exception handler 954 ~ field xshortenumfield 955 956 def expect_exception(e, c): 957 try: 958 eval(c) 959 return False 960 except e: 961 return True 962 963 964 = XShortEnumField tests initialization 965 ~ field xshortenumfield 966 967 f = XShortEnumField('test', 0, {0: 'Foo', 1: 'Bar'}) 968 fcb = XShortEnumField('test', 0, ( 969 lambda x: 'Foo' if x == 0 else 'Bar' if x == 1 else lhex(x), 970 lambda x: x 971 )) 972 973 True 974 975 = XShortEnumField.i2repr_one 976 ~ field xshortenumfield 977 978 assert(f.i2repr_one(None, 0) == 'Foo') 979 assert(f.i2repr_one(None, 1) == 'Bar') 980 assert(f.i2repr_one(None, 0xff) == '0xff') 981 982 assert(f.i2repr_one(None, 0) == 'Foo') 983 assert(f.i2repr_one(None, 1) == 'Bar') 984 assert(f.i2repr_one(None, 0xff) == '0xff') 985 986 True 987 988 ############ 989 ############ 990 + DNSStrField tests 991 992 = Raise exception - test data 993 994 dnsf = DNSStrField("test", "") 995 assert(dnsf.getfield("", b"\x01x\x00") == (b"", b"x.")) 996 997 try: 998 dnsf.getfield("", b"\xff") 999 assert(False) 1000 except (Scapy_Exception, IndexError): 1001 pass 1002