-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
120 lines (101 loc) · 2.29 KB
/
client_test.go
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
// Copyright 2016 The Venti Authors. All rights reserved.
// Use of this source code is governed by an ISC-style
// license that can be found in the LICENSE file.
package venti_test
import (
"bytes"
"crypto/sha1"
"io"
"testing"
"github.com/hdonnay/venti"
"github.com/hdonnay/venti/ventitest"
)
func TestWrite(t *testing.T) {
memfs := ventitest.NewMemFS()
c, done := startServer(t, memfs.Handshake)
defer done()
if err := c.Ping(); err != nil {
t.Fatal(err)
}
for i := 0; i < 50; i++ {
_, r, h := randomBlock()
sc, err := c.Write(venti.VtData, r)
if err != nil {
t.Fatal(err)
}
if exp, got := h.Sum(nil), sc; !bytes.Equal(exp, got) {
t.Fatalf("exp: %020x, got: %020x", exp, got)
}
t.Logf("wrote %v\n", sc)
}
if err := c.Sync(); err != nil {
t.Fatal(err)
}
}
func TestReadWrite(t *testing.T) {
memfs := ventitest.NewMemFS()
var scs []venti.Score
c, done := startServer(t, memfs.Handshake)
defer done()
if err := c.Ping(); err != nil {
t.Fatal(err)
}
for i := 0; i < 50; i++ {
_, r, h := randomBlock()
sc, err := c.Write(venti.VtData, r)
if err != nil {
t.Fatal(err)
}
if exp, got := h.Sum(nil), sc; !bytes.Equal(exp, got) {
t.Fatalf("exp: %020x, got: %020x", exp, got)
}
t.Logf("wrote %v\n", sc)
scs = append(scs, sc)
}
if err := c.Sync(); err != nil {
t.Fatal(err)
}
h := sha1.New()
for _, sc := range scs {
r, err := c.Read(venti.VtData, sc, 4096)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(h, r); err != nil {
t.Fatal(err)
}
if exp, got := sc, h.Sum(nil); !bytes.Equal(exp, got) {
t.Fatalf("exp: %020x, got: %020x", exp, got)
}
t.Logf("read %v\n", sc)
r.Close()
h.Reset()
}
}
func TestWriteFail(t *testing.T) {
c, done := startServer(t, errs.Handshake)
defer done()
_, err := c.Write(venti.VtData, &bytes.Buffer{})
if err == nil {
t.Fatal("wanted an error, didn't get one")
}
t.Log(err)
}
func TestReadFail(t *testing.T) {
c, done := startServer(t, errs.Handshake)
defer done()
_, err := c.Read(venti.VtData, venti.Score(nil), 0)
if err == nil {
t.Fatal("wanted an error, didn't get one")
}
t.Log(err)
}
func TestSyncFail(t *testing.T) {
c, done := startServer(t, errs.Handshake)
defer done()
err := c.Sync()
if err == nil {
t.Fatal("wanted an error, didn't get one")
}
t.Log(err)
}