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