git » chasquid » commit e543a03

aliases: Fuzz testing

author Alberto Bertogli
2017-04-22 13:50:39 UTC
committer Alberto Bertogli
2018-03-30 21:33:53 UTC
parent f73b889016d4e50718d698b30d05c1029f03f622

aliases: Fuzz testing

internal/aliases/aliases.go +11 -5
internal/aliases/fuzz.go +22 -0
internal/aliases/testdata/fuzz/corpus/t-001 +0 -0
internal/aliases/testdata/fuzz/corpus/t-002 +14 -0
internal/aliases/testdata/fuzz/corpus/t-003 +12 -0
internal/aliases/testdata/fuzz/corpus/t-004 +3 -0
internal/aliases/testdata/fuzz/corpus/t-005 +2 -0

diff --git a/internal/aliases/aliases.go b/internal/aliases/aliases.go
index 6635e72..2670999 100644
--- a/internal/aliases/aliases.go
+++ b/internal/aliases/aliases.go
@@ -56,6 +56,7 @@ package aliases
 import (
 	"bufio"
 	"fmt"
+	"io"
 	"os"
 	"strings"
 	"sync"
@@ -269,9 +270,17 @@ func parseFile(domain, path string) (map[string][]Recipient, error) {
 	}
 	defer f.Close()
 
+	aliases, err := parseReader(domain, f)
+	if err != nil {
+		return nil, fmt.Errorf("reading %q: %v", path, err)
+	}
+	return aliases, nil
+}
+
+func parseReader(domain string, r io.Reader) (map[string][]Recipient, error) {
 	aliases := map[string][]Recipient{}
 
-	scanner := bufio.NewScanner(f)
+	scanner := bufio.NewScanner(r)
 	for i := 1; scanner.Scan(); i++ {
 		line := strings.TrimSpace(scanner.Text())
 		if strings.HasPrefix(line, "#") {
@@ -317,11 +326,8 @@ func parseFile(domain, path string) (map[string][]Recipient, error) {
 			aliases[addr] = rs
 		}
 	}
-	if err := scanner.Err(); err != nil {
-		return nil, fmt.Errorf("reading %q: %v", path, err)
-	}
 
-	return aliases, nil
+	return aliases, scanner.Err()
 }
 
 // removeAllAfter removes everything from s that comes after the separators,
diff --git a/internal/aliases/fuzz.go b/internal/aliases/fuzz.go
new file mode 100644
index 0000000..5c8b793
--- /dev/null
+++ b/internal/aliases/fuzz.go
@@ -0,0 +1,22 @@
+// Fuzz testing for package aliases.
+
+// +build gofuzz
+
+package aliases
+
+import "bytes"
+
+func Fuzz(data []byte) int {
+	interesting := 0
+	aliases, _ := parseReader("domain", bytes.NewReader(data))
+
+	// Mark cases with actual aliases as more interesting.
+	for _, rcpts := range aliases {
+		if len(rcpts) > 0 {
+			interesting = 1
+			break
+		}
+	}
+
+	return interesting
+}
diff --git a/internal/aliases/testdata/fuzz/corpus/t-001 b/internal/aliases/testdata/fuzz/corpus/t-001
new file mode 100644
index 0000000..e69de29
diff --git a/internal/aliases/testdata/fuzz/corpus/t-002 b/internal/aliases/testdata/fuzz/corpus/t-002
new file mode 100644
index 0000000..d056f62
--- /dev/null
+++ b/internal/aliases/testdata/fuzz/corpus/t-002
@@ -0,0 +1,14 @@
+# First some valid cases.
+a: b
+c: d@e, f,
+x: | command
+
+# The following is invalid, should be ignored.
+a@dom: x@dom
+
+# Overrides.
+o1: a
+o1: b
+
+# Finally one to make the file NOT end in \n:
+y: z
diff --git a/internal/aliases/testdata/fuzz/corpus/t-003 b/internal/aliases/testdata/fuzz/corpus/t-003
new file mode 100644
index 0000000..0ab5f52
--- /dev/null
+++ b/internal/aliases/testdata/fuzz/corpus/t-003
@@ -0,0 +1,12 @@
+
+# Easy aliases.
+pepe: jose
+joan: juan
+
+# UTF-8 aliases.
+pitanga: ñangapirí
+añil: azul, índigo
+
+# Pipe aliases.
+tubo: | writemailto ../.data/pipe_alias_worked
+
diff --git a/internal/aliases/testdata/fuzz/corpus/t-004 b/internal/aliases/testdata/fuzz/corpus/t-004
new file mode 100644
index 0000000..0c85b2c
--- /dev/null
+++ b/internal/aliases/testdata/fuzz/corpus/t-004
@@ -0,0 +1,3 @@
+
+fail: | false
+
diff --git a/internal/aliases/testdata/fuzz/corpus/t-005 b/internal/aliases/testdata/fuzz/corpus/t-005
new file mode 100644
index 0000000..69c81a5
--- /dev/null
+++ b/internal/aliases/testdata/fuzz/corpus/t-005
@@ -0,0 +1,2 @@
+
+aliasA: aliasB@srv-B