-
Notifications
You must be signed in to change notification settings - Fork 9
/
xxd_test.go
93 lines (83 loc) · 1.82 KB
/
xxd_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bytes"
"crypto/rand"
"flag"
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
"testing"
"testing/quick"
)
var xxdFile = flag.String("xxdFile", "", "File to test against.")
func TestXXD(t *testing.T) {
if *xxdFile == "" {
t.Skip("-xxdFile argument not given")
}
data, err := ioutil.ReadFile(*xxdFile)
if err != nil {
t.Fatal(err)
}
test := func(fn func(r io.Reader, w io.Writer, s string) error) func(n uint64) []string {
return func(n uint64) []string {
size := n % uint64(len(data))
fmt.Printf("%d\n", size)
var out bytes.Buffer
if err := fn(&pathologicalReader{data[0:size]}, &out, ""); err != nil {
return []string{err.Error()}
}
return strings.Split(out.String(), "\n")
}
}
if err := quick.CheckEqual(test(xxd), test(xxdNative), nil); err != nil {
cErr := err.(*quick.CheckEqualError)
size := cErr.In[0].(uint64) % uint64(len(data))
for i := range cErr.Out1[0].([]string) {
got := cErr.Out1[0].([]string)[i]
want := cErr.Out2[0].([]string)[i]
if got != want {
t.Errorf("size: %d\n\ngot : %s\nwant: %s\n", size, got, want)
break
}
}
}
}
type pathologicalReader struct {
data []byte
}
func (p *pathologicalReader) Read(b []byte) (int, error) {
n := len(b)
if n > len(p.data) {
n = len(p.data)
}
if n > 1 {
n--
}
copy(b, p.data[0:n])
p.data = p.data[n:]
if len(p.data) == 0 {
return n, io.EOF
}
return n, nil
}
func BenchmarkXXD(b *testing.B) {
b.StopTimer()
data := make([]byte, b.N)
if _, err := io.ReadFull(rand.Reader, data); err != nil {
b.Fatal(err)
}
buf := bytes.NewBuffer(data)
b.StartTimer()
if err := xxd(buf, ioutil.Discard, ""); err != nil {
b.Fatal(err)
}
}
func xxdNative(r io.Reader, w io.Writer, s string) error {
xxd := exec.Command("xxd", "-")
xxd.Stdin = r
xxd.Stdout = w
xxd.Stderr = w
return xxd.Run()
}