-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_test.go
55 lines (46 loc) · 1.12 KB
/
common_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
// 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 (
"crypto/sha1"
"fmt"
"hash"
"io"
"math/rand"
"net"
"testing"
"github.com/hdonnay/venti"
"github.com/hdonnay/venti/ventitest"
)
// This file has bits common to the normal tests and the compatability tests.
const (
errStr = "could not"
)
var (
errs = ventitest.NewErrFS(fmt.Errorf(errStr))
)
func randomBlock() (int64, io.Reader, hash.Hash) {
sz := rand.Int63n(1 << 15)
h := sha1.New()
r := rand.New(rand.NewSource(rand.Int63()))
return sz, io.TeeReader(io.LimitReader(r, sz), h), h
}
// Start a server on a random port, connect to it with a client, and return the
// client and a cleanup function.
func startServer(t *testing.T, h venti.Handshake) (*venti.Client, func()) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
go venti.Serve(l, h)
t.Log("serving on", l.Addr())
c, err := venti.Dial(l.Addr().String())
if err != nil {
t.Fatal(err)
}
return c, func() {
l.Close()
c.Close()
}
}