File: test-checksum.py

package info (click to toggle)
python-libtrace 1.6%2Bgit20161027-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,124 kB
  • ctags: 1,357
  • sloc: ansic: 6,890; python: 3,228; makefile: 70; sh: 49
file content (61 lines) | stat: -rwxr-xr-x 2,045 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python

# Thu, 13 Mar 14 (PDT)
# ip.py:  Demonstrate IP objects
# Copyright (C) 2015, Nevil Brownlee, U Auckland | WAND

from plt_testing import *

t = get_example_trace('anon-v4.pcap')

n = 0
for pkt in t:
    n += 1  # Wireshark uses 1-org packet numbers
    ip = pkt.ip
    if not ip:
        continue

    test_println("   %03d: checksum=%04x, OK=%s" % (n, ip.checksum, ip.test_l3_cksm()), get_tag())
    if not ip.test_l3_cksm():
        ip.set_l3_cksm()
    else:
        ip.checksum = 0xCDEF
    test_println("     checksum=%04x, OK=%s" % (ip.checksum, ip.test_l3_cksm()), get_tag("n:"+str(n)))

    tcp = ip.tcp
    if tcp:
        try:
            test_println("        tcp checksum=%04x, ok=%s" % (
                tcp.checksum, tcp.test_trans_cksm()), get_tag("n:"+str(n)))
            if not tcp.test_trans_cksm():
                tcp.set_trans_cksm()
            else:
                tcp.checksum = 0x1234
            test_println("        tcp checksum=%04x, ok=%s" % (
                tcp.checksum, tcp.test_trans_cksm()), get_tag("n:"+str(n)))
        except ValueError as e:
            test_println("        .tcp. %s" % e, get_tag("n:"+str(n)))

    udp = ip.udp
    if udp:
        #continue
        try:
            # Only packet 236 is short enough for plt to check it's correct
            test_println("        udp checksum=%04x, ok=%s" % (
                udp.checksum, udp.test_trans_cksm()), get_tag("n:"+str(n)))
            if not udp.test_trans_cksm():
                udp.set_trans_cksm()
            else:
                udp.checksum = 0x5678
            test_println("        udp checksum=%04x, ok=%s" % (
                udp.checksum, udp.test_trans_cksm()), get_tag("n:"+str(n)))
        except ValueError as e:
            test_println("        .udp. %s" % e, get_tag("n:"+str(n)))

    #if n == 20:
    #    break

test_println("%5d packets accepted" % (t.pkt_accepts()), get_tag())
test_println("%5d packets dropped" % (t.pkt_drops()), get_tag())

t.close()  # Don't do this inside the loop!