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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package xdelta
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"sync/atomic"
"golang.org/x/sys/unix"
)
var (
tmpDir = "/tmp"
srcSeq int64
)
type Program struct {
Path string
}
type Run struct {
Cmd exec.Cmd
Srcfile string
Stdin io.WriteCloser
Srcin io.WriteCloser
Stdout io.ReadCloser
Stderr io.ReadCloser
}
func (t *TestGroup) Drain(f io.ReadCloser, desc string) <-chan []byte {
c := make(chan []byte)
t.Go(desc, func(g Goroutine) {
if b, err := ioutil.ReadAll(f); err != nil {
fmt.Println("Drain", err)
g.Panic(err)
} else {
c <- b
}
g.OK()
})
return c
}
func (t *TestGroup) Empty(f io.ReadCloser, desc string) Goroutine {
return t.Go("empty:"+desc, func (g Goroutine) {
s := bufio.NewScanner(f)
for s.Scan() {
os.Stderr.Write([]byte(fmt.Sprint(desc, ": ", s.Text(), "\n")))
}
err := s.Err()
f.Close()
if err != nil {
g.Panic(err)
}
g.OK()
})
}
func TestWrite(what string, f io.WriteCloser, b []byte) error {
if _, err := f.Write(b); err != nil {
fmt.Println("Write", err)
return errors.New(fmt.Sprint(what, ":", err))
}
if err := f.Close(); err != nil {
fmt.Println("Close", err)
return errors.New(fmt.Sprint(what, ":", err))
}
return nil
}
func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser) Goroutine {
return t.Go("copy", func(g Goroutine) {
_, err := io.Copy(w, r)
if err != nil {
g.Panic(err)
}
err = r.Close()
if err != nil {
g.Panic(err)
}
err = w.Close()
if err != nil {
g.Panic(err)
}
g.OK()
})
}
func (t *TestGroup) CompareStreams(r1 io.ReadCloser, r2 io.ReadCloser, length int64) Goroutine {
return t.Go("compare", func(g Goroutine) {
b1 := make([]byte, blocksize)
b2 := make([]byte, blocksize)
var idx int64
for length > 0 {
c := blocksize
if length < blocksize {
c = int(length)
}
if _, err := io.ReadFull(r1, b1[0:c]); err != nil {
fmt.Println("ReadFull1", err)
g.Panic(err)
}
if _, err := io.ReadFull(r2, b2[0:c]); err != nil {
fmt.Println("ReadFull2", err)
g.Panic(err)
}
if bytes.Compare(b1[0:c], b2[0:c]) != 0 {
fmt.Println("B1 is", string(b1[0:c]))
fmt.Println("B2 is", string(b2[0:c]))
g.Panic(errors.New(fmt.Sprint("Bytes do not compare at ", idx)))
}
length -= int64(c)
idx += int64(c)
}
g.OK()
})
}
func (t *TestGroup) Exec(desc string, p *Program, srcfifo bool, flags []string) (*Run, error) {
var err error
run := &Run{}
args := []string{p.Path}
if srcfifo {
num := atomic.AddInt64(&srcSeq, 1)
run.Srcfile = path.Join(t.Runner.Testdir, fmt.Sprint("source", num))
if err = unix.Mkfifo(run.Srcfile, 0600); err != nil {
return nil, err
}
// Because OpenFile blocks on the Fifo until the reader
// arrives, a pipe to defer open
read, write := io.Pipe()
run.Srcin = write
go writeFifo(run.Srcfile, read)
args = append(args, "-s")
args = append(args, run.Srcfile)
}
if run.Stdin, err = run.Cmd.StdinPipe(); err != nil {
return nil, err
}
if run.Stdout, err = run.Cmd.StdoutPipe(); err != nil {
return nil, err
}
if run.Stderr, err = run.Cmd.StderrPipe(); err != nil {
return nil, err
}
run.Cmd.Path = p.Path
run.Cmd.Args = append(args, flags...)
run.Cmd.Dir = t.Runner.Testdir
fmt.Println("Start command", run.Cmd.Args)
if serr := run.Cmd.Start(); serr != nil {
return nil, serr
}
return run, nil
}
func (r *Run) Wait() error {
return r.Cmd.Wait()
}
func writeFifo(srcfile string, read io.Reader) error {
fmt.Println("About to open", srcfile)
fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600)
fmt.Println("Opened!!!", srcfile)
if err != nil {
fifo.Close()
return err
}
if _, err := io.Copy(fifo, read); err != nil {
fifo.Close()
return err
}
return fifo.Close()
}
|