gazprea-fuzzer-python/gazprea_fuzzer.py
2023-11-18 20:13:15 -07:00

60 lines
2 KiB
Python

import os
import random
import yaml
import fuzzer as fz
import argparse
import xml.dom.minidom
import xml.etree.ElementTree as ET
class Fuzzer():
def __init__(self, config: str, batch: int, seed: str):
with open(config) as yaml_file:
settings: dict = yaml.safe_load(yaml_file)
self.settings = settings
self.batch = batch
random.seed(seed)
self.fuzzer = fz.GazpreaFuzzer(config)
self.fuzzer.fuzz()
def fuzz(self):
for i in range(self.batch):
self.fuzzer.fuzz()
dom = xml.dom.minidom.parseString(ET.tostring(self.fuzzer.ast).decode('utf-8'))
pretty: str = dom.toprettyxml()
os.system("rm -rf fuzzer")
os.mkdir("fuzzer")
os.mkdir("fuzzer/input")
os.mkdir("fuzzer/debug")
os.mkdir("fuzzer/instream")
os.mkdir("fuzzer/outputs")
with open("fuzzer/input/name{}.in".format(i), 'w') as f:
f.write(self.fuzzer.source)
with open("fuzzer/debug/name{}.out".format(i), 'w') as f:
f.write(pretty)
# with open("fuzzer/instream/{}.in".format(i), 'w') as f:
# f.write(self.fuzzer.source)
# with open("fuzzer/outputs/{}.out".format(i), 'w') as f:
# f.write(self.fuzzer.out)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Procedurally generate a test case for Gazprea'
)
parser.add_argument('-b', '--batch_size', type=int, required=False,
help="generate BATCH cases (fuzzer/input/nameX.in, /instream/..., /outputs/...)")
parser.add_argument('--seed', type=int, required=False, action="store",
help="rng seed")
parser.add_argument('config_file', type=str, action="store",
help="path to your configuration file")
args = parser.parse_args()
fuzzer = Fuzzer(config=args.config_file, batch=args.batch_size, seed=args.seed)
fuzzer.fuzz()