summaryrefslogtreecommitdiffstats
path: root/src/plugins/nat/pnat/tests/test_genpackets.py
blob: 40867317078703893f04c4883eeae0d20fcd4452 (plain)
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
#!/usr/bin/env python3

import sys
import os
from importlib.machinery import SourceFileLoader
from scapy.all import *
from scapy.contrib.geneve import GENEVE


def hexstring(p):
    s = bytes(p.__class__(p))
    return ",".join("0x{:02x}".format(c) for c in s)


def output_test(filename, tests):
    (name, ext) = os.path.basename(filename).split(".")
    print("/* DO NOT EDIT: automatically generated by test_genpackets.py */")
    print("/* clang-format off */")
    print("test_t tests_{}[] = {{".format(name))

    for t in tests:
        print("  {")
        print('    .name = "{}",'.format(t[0]))
        print("    .nsend = {},".format(len(t[1])))
        print("    .send = (char []){{{}}},".format(hexstring(t[1])))
        print("    .nexpect = {},".format(len(t[2])))
        print("    .expect = (char []){{{}}},".format(hexstring(t[2])))
        print("    .expect_next_index = {}".format(t[3]))
        print("  },")
    print("};")
    print("/* clang-format on */")


# Read tests from file
for filename in sys.argv[1:]:
    with open(filename) as f:
        content = f.read().replace("\n", "")

    tests = eval(content)
    output_test(filename, tests)