1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-18 14:47:03 +00:00

aliases: Fuzz testing

This commit is contained in:
Alberto Bertogli
2017-04-22 14:50:39 +01:00
parent f73b889016
commit e543a03feb
7 changed files with 64 additions and 5 deletions

View File

@@ -56,6 +56,7 @@ package aliases
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
"sync" "sync"
@@ -269,9 +270,17 @@ func parseFile(domain, path string) (map[string][]Recipient, error) {
} }
defer f.Close() 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{} aliases := map[string][]Recipient{}
scanner := bufio.NewScanner(f) scanner := bufio.NewScanner(r)
for i := 1; scanner.Scan(); i++ { for i := 1; scanner.Scan(); i++ {
line := strings.TrimSpace(scanner.Text()) line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") { if strings.HasPrefix(line, "#") {
@@ -317,11 +326,8 @@ func parseFile(domain, path string) (map[string][]Recipient, error) {
aliases[addr] = rs 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, // removeAllAfter removes everything from s that comes after the separators,

22
internal/aliases/fuzz.go Normal file
View File

@@ -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
}

View File

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,3 @@
fail: | false

View File

@@ -0,0 +1,2 @@
aliasA: aliasB@srv-B