fuzz: structured helpers for creating mpatch seed corpus entries
authorAugie Fackler <augie@google.com>
Sun, 29 Apr 2018 00:04:16 -0400
changeset 38247 06e2fc08f6f0
parent 38246 46dcb9f14900
child 38248 99ab72b867b7
fuzz: structured helpers for creating mpatch seed corpus entries Maybe this is better. I'm not sold, honestly. Differential Revision: https://phab.mercurial-scm.org/D3696
contrib/fuzz/mpatch_corpus.py
--- a/contrib/fuzz/mpatch_corpus.py	Sat Apr 28 22:35:14 2018 -0400
+++ b/contrib/fuzz/mpatch_corpus.py	Sun Apr 29 00:04:16 2018 -0400
@@ -8,11 +8,55 @@
 ap.add_argument("out", metavar="some.zip", type=str, nargs=1)
 args = ap.parse_args()
 
+class deltafrag(object):
+    def __init__(self, start, end, data):
+        self.start = start
+        self.end = end
+        self.data = data
+
+    def __str__(self):
+        return struct.pack(
+            ">lll", self.start, self.end, len(self.data)) + self.data
+
+class delta(object):
+    def __init__(self, frags):
+        self.frags = frags
+
+    def __str__(self):
+        return ''.join(str(f) for f in self.frags)
+
+class corpus(object):
+
+    def __init__(self, base, deltas):
+        self.base = base
+        self.deltas = deltas
+
+    def __str__(self):
+        deltas = [str(d) for d in self.deltas]
+        parts = (
+            [
+                struct.pack(">B", len(deltas) + 1),
+                struct.pack(">H", len(self.base)),
+            ]
+            + [struct.pack(">H", len(d)) for d in deltas]
+            + [self.base]
+            + deltas
+        )
+        return "".join(parts)
+
 with zipfile.ZipFile(args.out[0], "w", zipfile.ZIP_STORED) as zf:
     # Manually constructed entries
     zf.writestr(
         "one_delta_applies",
-        "\x02\x00\x01\x00\ra\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01b",
+        str(corpus('a', [delta([deltafrag(0, 1, 'b')])]))
+    )
+    zf.writestr(
+        "one_delta_starts_late",
+        str(corpus('a', [delta([deltafrag(3, 1, 'b')])]))
+    )
+    zf.writestr(
+        "one_delta_ends_late",
+        str(corpus('a', [delta([deltafrag(0, 20, 'b')])]))
     )
 
     # Automatically discovered by running the fuzzer