summaryrefslogtreecommitdiff
path: root/xdelta3/go/src/xdelta
diff options
context:
space:
mode:
authorJoshua MacDonald <josh.macdonald@gmail.com>2015-01-25 23:17:17 -0800
committerJoshua MacDonald <josh.macdonald@gmail.com>2015-01-25 23:17:17 -0800
commitb55fcf3dc4c775ef35dce116ab23d35a5f9ff326 (patch)
tree47a160fb2855434f17689a9028081b29bff8f1d8 /xdelta3/go/src/xdelta
parent57c7df665788bbb524c544ce5dea66b1aade63d3 (diff)
regtest.go tests a random stream and an offset copy
Diffstat (limited to 'xdelta3/go/src/xdelta')
-rw-r--r--xdelta3/go/src/xdelta/rstream.go56
-rw-r--r--xdelta3/go/src/xdelta/test.go63
2 files changed, 97 insertions, 22 deletions
diff --git a/xdelta3/go/src/xdelta/rstream.go b/xdelta3/go/src/xdelta/rstream.go
new file mode 100644
index 0000000..1666934
--- /dev/null
+++ b/xdelta3/go/src/xdelta/rstream.go
@@ -0,0 +1,56 @@
1package xdelta
2
3
4import (
5 "io"
6 "math/rand"
7)
8
9const (
10 blocksize = 16380 // A factor of 7
11)
12
13func WriteRstreams(seed, offset, len int64,
14 first, second io.WriteCloser) {
15 go writeOne(seed, 0, len, first)
16 go writeOne(seed, offset, len, second)
17}
18
19func writeOne(seed, offset, len int64, stream io.WriteCloser) {
20 if offset != 0 {
21 // Fill with other random data until the offset
22 writeRand(rand.New(rand.NewSource(^seed)),
23 offset, stream)
24 }
25 writeRand(rand.New(rand.NewSource(seed)),
26 len - offset, stream)
27 if err := stream.Close(); err != nil {
28 panic(err)
29 }
30}
31
32func writeRand(r *rand.Rand, len int64, s io.Writer) {
33 blk := make([]byte, blocksize)
34 for len > 0 {
35 fillRand(r, blk)
36 c := blocksize
37 if len < blocksize {
38 c = int(len)
39 }
40 if _, err := s.Write(blk[0:c]); err != nil {
41 panic(err)
42 }
43 len -= int64(c)
44 }
45}
46
47func fillRand(r *rand.Rand, blk []byte) {
48 for p := 0; p < blocksize; {
49 v := r.Int63()
50 for i := 7; i != 0; i-- {
51 blk[p] = byte(v)
52 p++
53 v >>= 8
54 }
55 }
56}
diff --git a/xdelta3/go/src/xdelta/test.go b/xdelta3/go/src/xdelta/test.go
index d586538..292f133 100644
--- a/xdelta3/go/src/xdelta/test.go
+++ b/xdelta3/go/src/xdelta/test.go
@@ -1,41 +1,58 @@
1package xdelta 1package xdelta
2 2
3import ( 3import (
4 "fmt"
4 "io" 5 "io"
5 "io/ioutil" 6 "io/ioutil"
6 "os" 7 "os"
7 "os/exec" 8 "os/exec"
8 "path" 9 "path"
10 "sync/atomic"
11
9 "golang.org/x/sys/unix" 12 "golang.org/x/sys/unix"
10) 13)
11 14
12var ( 15var (
13 tmpDir = "/tmp" 16 tmpDir = "/tmp"
17 srcSeq int64
14) 18)
15 19
16type Program struct { 20type Program struct {
17 Path string 21 Path string
18} 22}
19 23
24type Runner struct {
25 Testdir string
26}
27
20type Run struct { 28type Run struct {
21 Cmd exec.Cmd 29 Cmd exec.Cmd
22 Testdir string
23 Srcfile string 30 Srcfile string
24 Stdin io.WriteCloser 31 Stdin io.WriteCloser
25 Srcin io.WriteCloser 32 Srcin io.WriteCloser
26 Stdout io.ReadCloser 33 Stdout io.ReadCloser
27 Stderr io.ReadCloser 34 Stderr io.ReadCloser
28} 35}
29 36
30func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) { 37func NewRunner() (*Runner, error) {
31 var err error 38 if dir, err := ioutil.TempDir(tmpDir, "xrt"); err != nil {
32 run := &Run{}
33 if run.Testdir, err = ioutil.TempDir(tmpDir, "xrt"); err != nil {
34 return nil, err 39 return nil, err
40 } else {
41 return &Runner{dir}, nil
35 } 42 }
36 args := []string{b.Path} 43}
44
45func (r *Runner) Cleanup() {
46 os.RemoveAll(r.Testdir)
47}
48
49func (r *Runner) Exec(p *Program, srcfifo bool, flags []string) (*Run, error) {
50 var err error
51 run := &Run{}
52 args := []string{p.Path}
37 if srcfifo { 53 if srcfifo {
38 run.Srcfile = path.Join(run.Testdir, "source") 54 num := atomic.AddInt64(&srcSeq, 1)
55 run.Srcfile = path.Join(r.Testdir, fmt.Sprint("source", num))
39 if err = unix.Mkfifo(run.Srcfile, 0600); err != nil { 56 if err = unix.Mkfifo(run.Srcfile, 0600); err != nil {
40 return nil, err 57 return nil, err
41 } 58 }
@@ -44,18 +61,7 @@ func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) {
44 read, write := io.Pipe() 61 read, write := io.Pipe()
45 run.Srcin = write 62 run.Srcin = write
46 63
47 go func() { 64 go writeFifo(run.Srcfile, read)
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") 65 args = append(args, "-s")
60 args = append(args, run.Srcfile) 66 args = append(args, run.Srcfile)
61 } 67 }
@@ -69,10 +75,23 @@ func (b *Program) Exec(srcfifo bool, flags []string) (*Run, error) {
69 return nil, err 75 return nil, err
70 } 76 }
71 77
72 run.Cmd.Path = b.Path 78 run.Cmd.Path = p.Path
73 run.Cmd.Args = append(args, flags...) 79 run.Cmd.Args = append(args, flags...)
74 run.Cmd.Dir = run.Testdir 80 run.Cmd.Dir = r.Testdir
75 run.Cmd.Start() 81 run.Cmd.Start()
76 82
77 return run, nil 83 return run, nil
78} 84}
85
86func writeFifo(srcfile string, read io.Reader) {
87 fifo, err := os.OpenFile(srcfile, os.O_WRONLY, 0600)
88 if err != nil {
89 panic(err)
90 }
91 if _, err := io.Copy(fifo, read); err != nil {
92 panic(err)
93 }
94 if err := fifo.Close(); err != nil {
95 panic(err)
96 }
97}