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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
package xdelta
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"sync/atomic"
"sync"
"golang.org/x/sys/unix"
)
var (
tmpDir = "/tmp"
srcSeq int64
)
type Program struct {
Path string
}
type Runner struct {
Testdir string
}
type TestGroup struct {
sync.WaitGroup
sync.Mutex
errs []error
}
type Run struct {
Cmd exec.Cmd
Srcfile string
Stdin io.WriteCloser
Srcin io.WriteCloser
Stdout io.ReadCloser
Stderr io.ReadCloser
}
func (t *TestGroup) Panic(err error) {
t.Lock()
t.errs = append(t.errs, err)
t.Unlock()
t.Done() // For the caller
t.Wait()
for _, e := range t.errs {
fmt.Println(e)
}
panic(fmt.Sprintf("%d errors", len(t.errs)))
}
func NewTestGroup() *TestGroup {
return &TestGroup{}
}
func (t *TestGroup) Drain(f io.ReadCloser, desc string) <-chan []byte {
c := make(chan []byte)
go func() {
t.Add(1)
//fmt.Println("Draining", desc)
if b, err := ioutil.ReadAll(f); err != nil {
t.Panic(err)
} else {
//fmt.Println("Draining", desc, "--got it")
c <- b
}
t.Done()
}()
return c
}
func (t *TestGroup) Empty(f io.ReadCloser, desc string) {
go func() {
t.Add(1)
s := bufio.NewScanner(f)
for s.Scan() {
os.Stderr.Write([]byte(fmt.Sprint(desc, ": ", s.Text(), "\n")))
}
if err := s.Err(); err != nil {
t.Panic(err)
}
t.Done()
}()
}
func (t *TestGroup) Write(what string, f io.WriteCloser, b []byte) {
//fmt.Println("Write (", what, ") ", len(b), "bytes")
if _, err := f.Write(b); err != nil {
t.Panic(errors.New(fmt.Sprint(what, ":", err)))
}
//fmt.Println("Write (", what, ") closing")
if err := f.Close(); err != nil {
t.Panic(errors.New(fmt.Sprint(what, ":", err)))
}
}
func (t *TestGroup) CopyStreams(r io.ReadCloser, w io.WriteCloser) {
go func() {
t.Add(1)
_, err := io.Copy(w, r)
if err != nil {
t.Panic(err)
}
err = r.Close()
if err != nil {
t.Panic(err)
}
err = w.Close()
if err != nil {
t.Panic(err)
}
t.Done()
}()
}
func (t *TestGroup) CompareStreams(r1 io.ReadCloser, r2 io.ReadCloser, length int64) {
go func() {
t.Add(1)
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 {
t.Panic(err)
}
if _, err := io.ReadFull(r2, b2[0:c]); err != nil {
t.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]))
t.Panic(errors.New(fmt.Sprint("Bytes do not compare at ", idx)))
}
length -= int64(c)
idx += int64(c)
}
t.Done()
}()
}
func NewRunner() (*Runner, error) {
if dir, err := ioutil.TempDir(tmpDir, "xrt"); err != nil {
return nil, err
} else {
return &Runner{dir}, nil
}
}
func (r *Runner) Cleanup() {
os.RemoveAll(r.Testdir)
}
func (r *Runner) Exec(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(r.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 = r.Testdir
return run, run.Cmd.Start()
}
func writeFifo(srcfile string, read io.Reader) error {
fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600)
if err != nil {
return err
}
if _, err := io.Copy(fifo, read); err != nil {
return err
}
if err := fifo.Close(); err != nil {
return err
}
return nil
}
|