forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccept_test.go
103 lines (90 loc) · 3.18 KB
/
accept_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
/*
* Copyright 2019 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpc
import (
"errors"
"net"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/CovenantSQL/CovenantSQL/crypto/etls"
)
func TestAcceptFunc(t *testing.T) {
Convey("Setup a single server with raw conn accept func", t, func(c C) {
nodes, stop, err := setupEnvironment(1, AcceptRawConn)
So(err, ShouldBeNil)
defer stop()
conn, err := net.Dial("tcp", nodes[0].Addr)
So(err, ShouldBeNil)
cli := NewClient(conn)
defer func() { _ = cli.Close() }()
err = cli.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
So(err, ShouldBeNil)
})
Convey("Setup a single server with crypto conn accept func", t, func(c C) {
secret := `6?$X7$<OmTFR{<!O}`
f := func(conn net.Conn) (*etls.CryptoConn, error) {
return etls.NewConn(conn, etls.NewCipher([]byte(secret))), nil
}
nodes, stop, err := setupEnvironment(1, NewAcceptCryptoConnFunc(f))
So(err, ShouldBeNil)
defer stop()
conn, err := etls.Dial("tcp", nodes[0].Addr, etls.NewCipher([]byte(secret)))
So(err, ShouldBeNil)
cli := NewClient(conn)
defer func() { _ = cli.Close() }()
err = cli.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
So(err, ShouldBeNil)
// Call crypto server with raw TCP conn
rawconn, err := net.Dial("tcp", nodes[0].Addr)
So(err, ShouldBeNil)
rawcli := NewClient(rawconn)
defer func() { _ = rawcli.Close() }()
err = NewClient(rawconn).Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
So(err, ShouldNotBeNil)
})
Convey("Setup a single server with bad crypto conn accept func", t, func(c C) {
f := func(net.Conn) (*etls.CryptoConn, error) {
return nil, errors.New("won't accept")
}
nodes, stop, err := setupEnvironment(1, NewAcceptCryptoConnFunc(f))
So(err, ShouldBeNil)
defer stop()
conn, err := etls.Dial("tcp", nodes[0].Addr, etls.NewCipher([]byte{}))
So(err, ShouldBeNil)
cli := NewClient(conn)
defer func() { _ = cli.Close() }()
err = cli.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
So(err, ShouldNotBeNil)
})
Convey("Setup a single server with naconn accept func", t, func(c C) {
nodes, stop, err := setupEnvironment(1, AcceptNAConn)
So(err, ShouldBeNil)
defer stop()
conn, err := Dial(nodes[0].ID)
So(err, ShouldBeNil)
cli := NewClient(conn)
defer func() { _ = cli.Close() }()
err = cli.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
So(err, ShouldBeNil)
// Call naconn server with raw TCP conn
rawconn, err := net.Dial("tcp", nodes[0].Addr)
So(err, ShouldBeNil)
rawcli := NewClient(rawconn)
defer func() { _ = rawcli.Close() }()
err = rawcli.Call("Count.Add", &AddReq{Delta: 1}, &AddResp{})
So(err, ShouldNotBeNil)
})
}