-
Notifications
You must be signed in to change notification settings - Fork 9
/
conn_test.go
158 lines (120 loc) · 3.53 KB
/
conn_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package bifrost_test
import (
"testing"
"time"
"sync"
"os"
"github.com/DE-labtory/bifrost"
"github.com/DE-labtory/bifrost/mocks"
"github.com/DE-labtory/bifrost/pb"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
func TestNewStreamHandler(t *testing.T) {
// given
wg := sync.WaitGroup{}
wg.Add(1)
var connectionHandler = func(stream pb.StreamService_BifrostStreamServer) {
//result
t.Log("connected")
wg.Done()
}
serverIP := "127.0.0.1:9999"
mockServer := &mocks.MockServer{Ch: connectionHandler}
server1, listener1 := mocks.ListenMockServer(mockServer, serverIP)
assert.NotNil(t, server1)
assert.NotNil(t, listener1)
time.Sleep(3 * time.Second)
var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
opts = append(opts, grpc.WithTimeout(3*time.Second))
grpc_conn, err := grpc.Dial(serverIP, opts...)
assert.NoError(t, err)
ctx, _ := context.WithCancel(context.Background())
streamServiceClient := pb.NewStreamServiceClient(grpc_conn)
_, err = streamServiceClient.BifrostStream(ctx)
assert.NoError(t, err)
defer func() {
grpc_conn.Close()
server1.Stop()
listener1.Close()
}()
wg.Wait()
}
func TestGrpcConnection_Send(t *testing.T) {
keyOpts := mocks.NewMockKeyOpts()
mockStreamWrapper := mocks.MockStreamWrapper{SendCallBack: func(envelope *pb.Envelope) {
}}
err := mocks.MockStoreKey(keyOpts.PriKey, "./.test_private_key")
assert.NoError(t, err)
defer os.RemoveAll("./.test_private_key")
crypto := mocks.NewMockCrypto()
crypto.Signer.(*mocks.MockECDSASigner).KeyID = keyOpts.PubKey.ID()
crypto.Signer.(*mocks.MockECDSASigner).KeyDirPath = "./.test_private_key"
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)
mockStreamWrapper.SendCallBack = func(envelope *pb.Envelope) {
//then
assert.Equal(t, envelope.Protocol, "test1")
assert.Equal(t, envelope.Payload, []byte("jun"))
assert.True(t, conn.(*bifrost.GrpcConnection).Verify(envelope))
}
assert.NoError(t, err)
go func() {
if err := conn.Start(); err != nil {
conn.Close()
}
}()
//when
conn.Send([]byte("jun"), "test1", nil, nil)
}
func TestGrpcConnection_GetPeerKey(t *testing.T) {
//given
keyOpts := mocks.NewMockKeyOpts()
mockStreamWrapper := mocks.MockStreamWrapper{}
crypto := mocks.NewMockCrypto()
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)
go func() {
if err := conn.Start(); err != nil {
conn.Close()
}
}()
//when
k := conn.GetPeerKey()
//then
assert.Equal(t, k, keyOpts.PubKey)
}
func TestGrpcConnection_Close(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
//given
keyOpts := mocks.NewMockKeyOpts()
mockStreamWrapper := mocks.MockStreamWrapper{}
mockStreamWrapper.CloseCallBack = func() {
// then
assert.True(t, true)
wg.Done()
}
crypto := mocks.NewMockCrypto()
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)
go func() {
if err := conn.Start(); err != nil {
assert.NotNil(t, err)
}
}()
// when
conn.Close()
wg.Wait()
}
func TestGrpcConnection_GetIP(t *testing.T) {
keyOpts := mocks.NewMockKeyOpts()
mockStreamWrapper := mocks.MockStreamWrapper{}
crypto := mocks.NewMockCrypto()
conn, err := bifrost.NewConnection("127.0.0.1:1234", nil, keyOpts.PubKey, mockStreamWrapper, crypto)
assert.NoError(t, err)
ipAddr := conn.GetIP()
assert.Equal(t, bifrost.Address{IP: "127.0.0.1:1234"}, ipAddr)
}