-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
client_test.go
106 lines (95 loc) · 2.65 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"testing"
"time"
"github.com/hyperledger/fabric/core/comm"
"github.com/stretchr/testify/assert"
)
func TestTLSClient(t *testing.T) {
srv, err := comm.NewGRPCServer("127.0.0.1:", comm.ServerConfig{
SecOpts: &comm.SecureOptions{
UseTLS: true,
Key: loadFileOrDie(filepath.Join("testdata", "server", "key.pem")),
Certificate: loadFileOrDie(filepath.Join("testdata", "server", "cert.pem")),
},
})
assert.NoError(t, err)
go srv.Start()
defer srv.Stop()
conf := Config{}
cl, err := NewClient(conf)
assert.NoError(t, err)
_, port, _ := net.SplitHostPort(srv.Address())
dial := cl.NewDialer(net.JoinHostPort("localhost", port))
conn, err := dial()
assert.NoError(t, err)
conn.Close()
}
func TestDialBadEndpoint(t *testing.T) {
conf := Config{
PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
Timeout: 100 * time.Millisecond,
}
cl, err := NewClient(conf)
assert.NoError(t, err)
dial := cl.NewDialer("non_existent_host.xyz.blabla:9999")
_, err = dial()
assert.Error(t, err)
}
func TestNonTLSClient(t *testing.T) {
srv, err := comm.NewGRPCServer("127.0.0.1:", comm.ServerConfig{
SecOpts: &comm.SecureOptions{},
})
assert.NoError(t, err)
go srv.Start()
defer srv.Stop()
conf := Config{}
cl, err := NewClient(conf)
assert.NoError(t, err)
_, port, _ := net.SplitHostPort(srv.Address())
dial := cl.NewDialer(fmt.Sprintf("localhost:%s", port))
conn, err := dial()
assert.NoError(t, err)
conn.Close()
}
func TestClientBadConfig(t *testing.T) {
conf := Config{
PeerCACertPath: filepath.Join("testdata", "server", "non_existent_file"),
}
cl, err := NewClient(conf)
assert.Nil(t, cl)
assert.Contains(t, err.Error(), "open testdata/server/non_existent_file: no such file or directory")
conf = Config{
PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
KeyPath: "non_existent_file",
CertPath: "non_existent_file",
}
cl, err = NewClient(conf)
assert.Nil(t, cl)
assert.Contains(t, err.Error(), "open non_existent_file: no such file or directory")
conf = Config{
PeerCACertPath: filepath.Join("testdata", "server", "ca.pem"),
KeyPath: filepath.Join("testdata", "client", "key.pem"),
CertPath: "non_existent_file",
}
cl, err = NewClient(conf)
assert.Nil(t, cl)
assert.Contains(t, err.Error(), "open non_existent_file: no such file or directory")
}
func loadFileOrDie(path string) []byte {
b, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("Failed opening file", path, ":", err)
os.Exit(1)
}
return b
}