From e35b4b66cc57e2551f52df155cf3d3f4e2c67280 Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Sun, 3 Jul 2011 10:51:07 -0600 Subject: [PATCH] bounds checking stress tests --- block_test.go | 23 ++++++++++++++++++++-- inline_test.go | 23 ++++++++++++++++++++-- markdown.go | 6 ++++++ upskirtref_test.go | 49 ++++++++++++++++++++++++++++++++-------------- 4 files changed, 82 insertions(+), 19 deletions(-) diff --git a/block_test.go b/block_test.go index 888841e..dac78da 100644 --- a/block_test.go +++ b/block_test.go @@ -27,13 +27,32 @@ func runMarkdownBlock(input string, extensions int) string { } func doTestsBlock(t *testing.T, tests []string, extensions int) { + // catch and report panics + var candidate string + defer func() { + if err := recover(); err != nil { + t.Errorf("\npanic while processing [%#v]\n", candidate) + } + }() + for i := 0; i+1 < len(tests); i += 2 { input := tests[i] + candidate = input expected := tests[i+1] - actual := runMarkdownBlock(input, extensions) + actual := runMarkdownBlock(candidate, extensions) if actual != expected { t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", - input, expected, actual) + candidate, expected, actual) + } + + // now test every substring to stress test bounds checking + if !testing.Short() { + for start := 0; start < len(input); start++ { + for end := start + 1; end <= len(input); end++ { + candidate = input[start:end] + _ = runMarkdownBlock(candidate, extensions) + } + } } } } diff --git a/inline_test.go b/inline_test.go index 7995794..0ddc0a7 100644 --- a/inline_test.go +++ b/inline_test.go @@ -31,13 +31,32 @@ func runMarkdownInline(input string) string { } func doTestsInline(t *testing.T, tests []string) { + // catch and report panics + var candidate string + defer func() { + if err := recover(); err != nil { + t.Errorf("\npanic while processing [%#v]\n", candidate) + } + }() + for i := 0; i+1 < len(tests); i += 2 { input := tests[i] + candidate = input expected := tests[i+1] - actual := runMarkdownInline(input) + actual := runMarkdownInline(candidate) if actual != expected { t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", - input, expected, actual) + candidate, expected, actual) + } + + // now test every substring to stress test bounds checking + if !testing.Short() { + for start := 0; start < len(input); start++ { + for end := start + 1; end <= len(input); end++ { + candidate = input[start:end] + _ = runMarkdownInline(candidate) + } + } } } } diff --git a/markdown.go b/markdown.go index 0bccdd8..46d60db 100644 --- a/markdown.go +++ b/markdown.go @@ -270,6 +270,12 @@ func firstPass(parser *Parser, input []byte) []byte { beg = end } } + + // empty input? + if out.Len() == 0 { + out.WriteByte('\n') + } + return out.Bytes() } diff --git a/upskirtref_test.go b/upskirtref_test.go index 2dedc66..47e6c75 100644 --- a/upskirtref_test.go +++ b/upskirtref_test.go @@ -25,27 +25,46 @@ func runMarkdownReference(input string) string { } func doTestsReference(t *testing.T, files []string) { - for _, basename := range files { - fn := filepath.Join("upskirtref", basename+".text") - actualdata, err := ioutil.ReadFile(fn) - if err != nil { - t.Errorf("Couldn't open '%s', error: %v\n", fn, err) - continue - } - fn = filepath.Join("upskirtref", basename+".html") - expecteddata, err := ioutil.ReadFile(fn) - if err != nil { - t.Errorf("Couldn't open '%s', error: %v\n", fn, err) - continue + // catch and report panics + var candidate string + defer func() { + if err := recover(); err != nil { + t.Errorf("\npanic while processing [%#v]\n", candidate) } + }() - actual := string(actualdata) - actual = string(runMarkdownReference(actual)) - expected := string(expecteddata) + for _, basename := range files { + filename := filepath.Join("upskirtref", basename+".text") + inputBytes, err := ioutil.ReadFile(filename) + if err != nil { + t.Errorf("Couldn't open '%s', error: %v\n", filename, err) + continue + } + input := string(inputBytes) + + filename = filepath.Join("upskirtref", basename+".html") + expectedBytes, err := ioutil.ReadFile(filename) + if err != nil { + t.Errorf("Couldn't open '%s', error: %v\n", filename, err) + continue + } + expected := string(expectedBytes) + + actual := string(runMarkdownReference(input)) if actual != expected { t.Errorf("\n [%#v]\nExpected[%#v]\nActual [%#v]", basename+".text", expected, actual) } + + // now test every substring of every input to check for + // bounds checking + if !testing.Short() { + start := 0 + for end := start + 1; end <= len(input); end++ { + candidate = input[start:end] + _ = runMarkdownReference(candidate) + } + } } }