summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xdelta3/go/src/regtest.go83
-rw-r--r--xdelta3/go/src/xdelta/test.go78
2 files changed, 161 insertions, 0 deletions
diff --git a/xdelta3/go/src/regtest.go b/xdelta3/go/src/regtest.go
new file mode 100644
index 0000000..aee419d
--- /dev/null
+++ b/xdelta3/go/src/regtest.go
@@ -0,0 +1,83 @@
1package main
2
3import (
4 "io"
5 "io/ioutil"
6 "xdelta"
7)
8
9const (
10 prog = "/Users/jmacd/src/xdelta/xdelta3/xdelta3"
11)
12
13func drain(f io.ReadCloser) <-chan []byte {
14 c := make(chan []byte)
15 go func() {
16 if b, err := ioutil.ReadAll(f); err != nil {
17 panic(err)
18 } else {
19 c <- b
20 }
21 }()
22 return c
23}
24
25func empty(f io.ReadCloser) {
26 go func() {
27 if _, err := ioutil.ReadAll(f); err != nil {
28 panic(err)
29 }
30 }()
31}
32
33func write(f io.WriteCloser, b []byte) {
34 if _, err := f.Write(b); err != nil {
35 panic(err)
36 }
37 if err := f.Close(); err != nil {
38 panic(err)
39 }
40}
41
42func main() {
43 x := xdelta.Program{prog}
44 smokeTest(x)
45}
46
47func smokeTest(p xdelta.Program) {
48 target := "Hello world!"
49 source := "Hello world, nice to meet you!"
50
51 run, err := p.Exec(true, []string{"-e"})
52 if err != nil {
53 panic(err)
54 }
55 encodeout := drain(run.Stdout)
56 empty(run.Stderr)
57
58 write(run.Stdin, []byte(target))
59 write(run.Srcin, []byte(source))
60
61 if err := run.Cmd.Wait(); err != nil {
62 panic(err)
63 }
64
65 run, err = p.Exec(true, []string{"-d"})
66 if err != nil {
67 panic(err)
68 }
69
70 decodeout := drain(run.Stdout)
71 empty(run.Stderr)
72
73 write(run.Stdin, <-encodeout)
74 write(run.Srcin, []byte(source))
75
76 if err := run.Cmd.Wait(); err != nil {
77 panic(err)
78 }
79
80 if string(<-decodeout) != target {
81 panic("It's not working!!!")
82 }
83}
diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go
new file mode 100644
index 0000000..d586538
--- /dev/null
+++ b/xdelta3/go/src/xdelta/test.go
@@ -0,0 +1,78 @@
1package xdelta
2
3import (
4 "io"
5 "io/ioutil"
6 "os"
7 "os/exec"
8 "path"
9 "golang.org/x/sys/unix"
10)
11
12var (
13 tmpDir = "/tmp"
14)
15
16type Program struct {
17 Path string
18}
19
20type Run struct {
21 Cmd exec.Cmd
22 Testdir string
23 Srcfile string
24 Stdin io.WriteCloser
25 Srcin io.WriteCloser
26 Stdout io.ReadCloser
27 Stderr io.ReadCloser
28}
29
30func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) {
31 var err error
32 run := &Run{}
33 if run.Testdir, err = ioutil.TempDir(tmpDir, "xrt"); err != nil {
34 return nil, err
35 }
36 args := []string{b.Path}
37 if srcfifo {
38 run.Srcfile = path.Join(run.Testdir, "source")
39 if err = unix.Mkfifo(run.Srcfile, 0600); err != nil {
40 return nil, err
41 }
42 // Because OpenFile blocks on the Fifo until the reader
43 // arrives, a pipe to defer open
44 read, write := io.Pipe()
45 run.Srcin = write
46
47 go func() {
48 fifo, err := os.OpenFile(run.Srcfile, os.O_WRONLY, 0600)
49 if err != nil {
50 panic(err)
51 }
52 if _, err := io.Copy(fifo, read); err != nil {
53 panic(err)
54 }
55 if err := fifo.Close(); err != nil {
56 panic(err)
57 }
58 }()
59 args = append(args, "-s")
60 args = append(args, run.Srcfile)
61 }
62 if run.Stdin, err = run.Cmd.StdinPipe(); err != nil {
63 return nil, err
64 }
65 if run.Stdout, err = run.Cmd.StdoutPipe(); err != nil {
66 return nil, err
67 }
68 if run.Stderr, err = run.Cmd.StderrPipe(); err != nil {
69 return nil, err
70 }
71
72 run.Cmd.Path = b.Path
73 run.Cmd.Args = append(args, flags...)
74 run.Cmd.Dir = run.Testdir
75 run.Cmd.Start()
76
77 return run, nil
78}