#!/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)