From 2c054e0e4dbc94ecff95d62fd8a06b98405ff4d4 Mon Sep 17 00:00:00 2001 From: Evgeny Kuznetsov Date: Wed, 5 Feb 2020 18:48:03 +0300 Subject: parse regexes safely --- main_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'main_test.go') diff --git a/main_test.go b/main_test.go index 73dc0c8..2d520cb 100644 --- a/main_test.go +++ b/main_test.go @@ -26,6 +26,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "sync" "testing" @@ -301,3 +302,41 @@ func TestParseDate(t *testing.T) { } } } + +func TestParseErrors(t *testing.T) { + type testval struct { + src []byte + re *regexp.Regexp + n int + err error + } + + var tests = []testval{ + { + []byte("

Аэростат

"), + programNameRe, + 1, + nil, + }, { + []byte("

Аэростат

foo

"), + programNameRe, + 1, + nil, + }, { + []byte{}, + programNameRe, + 1, + errCantParse, + }, + } + + for _, test := range tests { + res, got := parse(test.src, test.re, test.n) + if test.err != got { + t.Error("for", test.src, test.re, test.n, "\nwant:", test.err, "got:", got) + } + if test.n != len(res) { + t.Error("for", test.src, test.re, test.n, "\nwant length:", test.n, "got:", len(res)) + } + } +} -- cgit v1.2.3