-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaflfix_test.go
79 lines (65 loc) · 1.46 KB
/
aflfix_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
// Fixers must define the "tests" map, the "bench" string varible as well as
// the Fix() method etc. See fix_simple.go for an example.
import (
"bufio"
"fmt"
"net"
"os"
"path"
"testing"
"time"
)
func sleepyConnect(dest string) (s net.Conn, err error) {
zzz := 1 * time.Millisecond
for {
if zzz > 1*time.Second {
err = fmt.Errorf("Failed to connect to fix server")
return
}
time.Sleep(zzz)
s, err = net.Dial("unix", dest)
if err == nil {
return
}
zzz *= 2
}
}
func BenchmarkFixup(b *testing.B) {
sock := path.Join(os.TempDir(), "aflfix.sock")
srv := server{NewFixer()}
go srv.Run(sock)
s, err := sleepyConnect(sock)
if err != nil {
b.Fatalf("Failed to connect to fix server")
}
defer s.Close()
r := bufio.NewReader(s)
out := []byte(fmt.Sprintf("%d:%s,", len(srv.BenchString()), srv.BenchString()))
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Write(out)
readNetString(r)
}
}
func TestRoundTrip(t *testing.T) {
sock := path.Join(os.TempDir(), "aflfix.sock")
srv := server{NewFixer()}
go srv.Run(sock)
s, err := sleepyConnect(sock)
if err != nil {
t.Fatalf("Failed to connect to fix server")
}
defer s.Close()
r := bufio.NewReader(s)
for k, want := range srv.TestMap() {
s.Write([]byte(fmt.Sprintf("%d:%s,", len(k), k)))
in, err := readNetString(r)
if string(in) != want {
t.Fatalf("Sent %s, got %s, want %s", k, string(in), want)
}
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
}
}