From 9d2dc5c06f2c33eeaf618a8d9759c8cc05c04743 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Mon, 26 Apr 2021 02:05:27 -0500 Subject: [PATCH] fuzz opentype --- tests/common.nim | 7 +++++++ tests/fuzz_opentype.nim | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/common.nim create mode 100644 tests/fuzz_opentype.nim diff --git a/tests/common.nim b/tests/common.nim new file mode 100644 index 0000000..697c081 --- /dev/null +++ b/tests/common.nim @@ -0,0 +1,7 @@ +import algorithm, os + +proc findAllFonts*(rootPath: string): seq[string] = + for fontPath in walkDirRec(rootPath): + if splitFile(fontPath).ext in [".ttf", ".otf"]: + result.add(fontPath) + result.sort() diff --git a/tests/fuzz_opentype.nim b/tests/fuzz_opentype.nim new file mode 100644 index 0000000..a02bb09 --- /dev/null +++ b/tests/fuzz_opentype.nim @@ -0,0 +1,32 @@ +import common, random, pixie, strformat, unicode + +randomize() + +let fontPaths = findAllFonts("tests/fonts") + +doAssert fontPaths.len > 0 + +for i in 0 ..< 10000: + var + file = fontPaths[rand(fontPaths.len - 1)] + data = readFile(file) + pos = rand(data.len) + value = rand(255).char + data[pos] = value + echo &"{i} {file} {pos} {value.uint8}" + try: + let font = parseOtf(data) + doAssert font != nil + for i in 0.uint16 ..< uint16.high: + discard font.getGlyphPath(Rune(i.int)) + except PixieError: + discard + + data = data[0 ..< pos] + try: + let font = parseOtf(data) + doAssert font != nil + for i in 0.uint16 ..< uint16.high: + discard font.getGlyphPath(Rune(i.int)) + except PixieError: + discard