summaryrefslogtreecommitdiff
path: root/xdelta3/go/src/xdelta/test.go
diff options
context:
space:
mode:
Diffstat (limited to 'xdelta3/go/src/xdelta/test.go')
-rw-r--r--xdelta3/go/src/xdelta/test.go164
1 files changed, 164 insertions, 0 deletions
diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go
new file mode 100644
index 0000000..7210698
--- /dev/null
+++ b/xdelta3/go/src/xdelta/test.go
@@ -0,0 +1,164 @@
1package xdelta
2
3import (
4 "bufio"
5 "bytes"
6 "errors"
7 "fmt"
8 "io"
9 "io/ioutil"
10 "os"
11 "path"
12 "sync/atomic"
13
14 "golang.org/x/sys/unix"
15)
16
17var (
18 tmpDir = "/tmp"
19 srcSeq int64
20)
21
22func (t *TestGroup) Drain(f io.ReadCloser, desc string) <-chan []byte {
23 c := make(chan []byte)
24 t.Go(desc, func(g *Goroutine) {
25 if b, err := ioutil.ReadAll(f); err != nil {
26 g.Panic(err)
27 } else {
28 c <- b
29 }
30 g.OK()
31 })
32 return c
33}
34
35func (t *TestGroup) Empty(f io.ReadCloser, desc string) *Goroutine {
36 return t.Go("empty:"+desc, func (g *Goroutine) {
37 s := bufio.NewScanner(f)
38 for s.Scan() {
39 os.Stderr.Write([]byte(fmt.Sprint(desc, ": ", s.Text(), "\n")))
40 }
41 err := s.Err()
42 f.Close()
43 if err != nil {
44 g.Panic(err)
45 }
46 g.OK()
47 })
48}
49
50func (t *TestGroup) TestWrite(what string, f io.WriteCloser, b []byte) *Goroutine {
51 return t.Go("write", func(g *Goroutine) {
52 if _, err := f.Write(b); err != nil {
53 g.Panic(err)
54 }
55 if err := f.Close(); err != nil {
56 g.Panic(err)
57 }
58 g.OK()
59 })
60}
61
62func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser, written *int64) *Goroutine {
63 return t.Go("copy", func(g *Goroutine) {
64 nwrite, err := io.Copy(w, r)
65 if err != nil {
66 g.Panic(err)
67 }
68 err = r.Close()
69 if err != nil {
70 g.Panic(err)
71 }
72 err = w.Close()
73 if err != nil {
74 g.Panic(err)
75 }
76 g.OK()
77 *written = nwrite
78 })
79}
80
81func (t *TestGroup) CompareStreams(r1 io.ReadCloser, r2 io.ReadCloser, length int64) *Goroutine {
82 return t.Go("compare", func(g *Goroutine) {
83 b1 := make([]byte, blocksize)
84 b2 := make([]byte, blocksize)
85 var idx int64
86 for length > 0 {
87 c := blocksize
88 if length < blocksize {
89 c = int(length)
90 }
91 if _, err := io.ReadFull(r1, b1[0:c]); err != nil {
92 g.Panic(err)
93 }
94 if _, err := io.ReadFull(r2, b2[0:c]); err != nil {
95 g.Panic(err)
96 }
97 if bytes.Compare(b1[0:c], b2[0:c]) != 0 {
98 fmt.Println("B1 is", string(b1[0:c]))
99 fmt.Println("B2 is", string(b2[0:c]))
100 g.Panic(errors.New(fmt.Sprint("Bytes do not compare at ", idx)))
101 }
102 length -= int64(c)
103 idx += int64(c)
104 }
105 g.OK()
106 })
107}
108
109func (t *TestGroup) Exec(desc string, p Program, srcfifo bool, flags []string) (*Run, error) {
110 var err error
111 run := &Run{}
112 args := []string{p.Path}
113 if srcfifo {
114 num := atomic.AddInt64(&srcSeq, 1)
115 run.Srcfile = path.Join(t.Runner.Testdir, fmt.Sprint("source", num))
116 if err = unix.Mkfifo(run.Srcfile, 0600); err != nil {
117 return nil, err
118 }
119 read, write := io.Pipe()
120 t.writeFifo(run.Srcfile, read)
121 run.Srcin = write
122 args = append(args, "-s")
123 args = append(args, run.Srcfile)
124 }
125 if run.Stdin, err = run.Cmd.StdinPipe(); err != nil {
126 return nil, err
127 }
128 if run.Stdout, err = run.Cmd.StdoutPipe(); err != nil {
129 return nil, err
130 }
131 if run.Stderr, err = run.Cmd.StderrPipe(); err != nil {
132 return nil, err
133 }
134
135 run.Cmd.Path = p.Path
136 run.Cmd.Args = append(args, flags...)
137 run.Cmd.Dir = t.Runner.Testdir
138 if serr := run.Cmd.Start(); serr != nil {
139 return nil, serr
140 }
141 return run, nil
142}
143
144func (t *TestGroup) Fail(v ...interface{}) {
145 panic(fmt.Sprintln(v...))
146}
147
148func (t *TestGroup) writeFifo(srcfile string, read io.Reader) *Goroutine {
149 return t.Go("compare", func(g *Goroutine) {
150 fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600)
151 if err != nil {
152 fifo.Close()
153 g.Panic(err)
154 }
155 if _, err := io.Copy(fifo, read); err != nil {
156 fifo.Close()
157 g.Panic(err)
158 }
159 if err := fifo.Close(); err != nil {
160 g.Panic(err)
161 }
162 g.OK()
163 })
164}