-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_server_test.go
264 lines (238 loc) · 7.22 KB
/
sync_server_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"os"
"testing"
"time"
"github.com/breez/data-sync/config"
"github.com/breez/data-sync/middleware"
"github.com/breez/data-sync/proto"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/reflect/protoreflect"
)
type testCase struct {
name string
request protoreflect.ProtoMessage
reply protoreflect.ProtoMessage
}
func TestSyncService(t *testing.T) {
config, privateKey := testParameters(t)
client, closer := server(context.Background(), config)
defer closer()
defer func() {
os.RemoveAll(config.SQLiteDirPath)
}()
for _, testCase := range testCases() {
if setRecordRequest, ok := testCase.request.(*proto.SetRecordRequest); ok {
testSetRecord(t, privateKey, client, setRecordRequest, testCase)
}
if listChangesRequest, ok := testCase.request.(*proto.ListChangesRequest); ok {
testListChanges(t, privateKey, client, listChangesRequest, testCase)
}
}
}
func TestTrackChanges(t *testing.T) {
config, privateKey := testParameters(t)
client, closer := server(context.Background(), config)
defer closer()
defer func() {
os.RemoveAll(config.SQLiteDirPath)
}()
requestTime := uint32(time.Now().Unix())
toSign := fmt.Sprintf("%v", requestTime)
signature, err := middleware.SignMessage(privateKey, []byte(toSign))
require.NoError(t, err, "failed to sign message")
request := &proto.TrackChangesRequest{
RequestTime: requestTime,
Signature: signature,
}
request.RequestTime = requestTime
request.Signature = signature
stream, err := client.TrackChanges(context.Background(), request)
require.NoError(t, err, "failed to call TrackChanges")
// check realtime changes
client.SetRecord(context.Background(), createSetRecordRequest(t, privateKey, &proto.Record{
Id: "1",
Revision: 0,
Data: []byte("revision1"),
}))
record, err := stream.Recv()
require.NoError(t, err, "failed to receive record")
require.Equal(t, record.Data, []byte("revision1"))
}
func createSetRecordRequest(t *testing.T, privateKey *btcec.PrivateKey, record *proto.Record) *proto.SetRecordRequest {
requestTime := uint32(time.Now().Unix())
toSign := middleware.SignSetRecord(record, requestTime)
signature, err := middleware.SignMessage(privateKey, []byte(toSign))
require.NoError(t, err, "failed to sign message")
return &proto.SetRecordRequest{
Record: record,
RequestTime: requestTime,
Signature: signature,
}
}
func testParameters(t *testing.T) (*config.Config, *btcec.PrivateKey) {
config, err := config.NewConfig()
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
privateKey, err := btcec.NewPrivateKey()
require.NoError(t, err, "failed to create private key")
os.RemoveAll(config.SQLiteDirPath)
return config, privateKey
}
func testCases() []testCase {
return []testCase{
// empty db, no changes.
{
name: "empty db, no changes",
request: &proto.ListChangesRequest{
SinceRevision: 0,
},
reply: &proto.ListChangesReply{
Changes: []*proto.Record{},
},
},
// set first record
{
name: "initial record insert",
request: &proto.SetRecordRequest{
Record: &proto.Record{
Id: "1",
Revision: 0,
Data: []byte("revision1"),
},
},
reply: &proto.SetRecordReply{
Status: proto.SetRecordStatus_SUCCESS,
NewRevision: 1,
},
},
// update record
{
name: "initial record update",
request: &proto.SetRecordRequest{
Record: &proto.Record{
Id: "1",
Revision: 1,
Data: []byte("revision2"),
},
},
reply: &proto.SetRecordReply{
Status: proto.SetRecordStatus_SUCCESS,
NewRevision: 2,
},
},
// test conflict
{
name: "test conflict",
request: &proto.SetRecordRequest{
Record: &proto.Record{
Id: "1",
Revision: 1,
Data: []byte("revision3"),
},
},
reply: &proto.SetRecordReply{
Status: proto.SetRecordStatus_CONFLICT,
},
},
// no changes since revision 5
{
name: "empty changes",
request: &proto.ListChangesRequest{
SinceRevision: 5,
},
reply: &proto.ListChangesReply{
Changes: []*proto.Record{},
},
},
// 1 record changes
{
name: "list changes returns 1 record",
request: &proto.ListChangesRequest{
SinceRevision: 0,
},
reply: &proto.ListChangesReply{
Changes: []*proto.Record{
{
Id: "1",
Revision: 2,
Data: []byte("revision2"),
},
},
},
},
}
}
func testSetRecord(t *testing.T, privateKey *btcec.PrivateKey, client proto.SyncerClient, request *proto.SetRecordRequest, test testCase) {
requestTime := uint32(time.Now().Unix())
toSign := middleware.SignSetRecord(request.Record, requestTime)
signature, err := middleware.SignMessage(privateKey, []byte(toSign))
require.NoError(t, err, "failed to sign message")
request.RequestTime = requestTime
request.Signature = signature
response, err := client.SetRecord(context.Background(), request)
require.NoError(t, err, "failed to call SetRecord")
res, err := json.Marshal(response)
require.NoError(t, err, "failed to marshal response")
expected, err := json.Marshal(test.reply)
require.NoError(t, err, "failed to marshal expected response")
require.Equal(t, res, expected, fmt.Sprintf("failed to compare test results for %v", test.name))
}
func testListChanges(t *testing.T, privateKey *btcec.PrivateKey, client proto.SyncerClient, request *proto.ListChangesRequest, test testCase) {
requestTime := uint32(time.Now().Unix())
toSign := fmt.Sprintf("%v-%v", request.SinceRevision, requestTime)
signature, err := middleware.SignMessage(privateKey, []byte(toSign))
require.NoError(t, err, "failed to sign message")
request.RequestTime = requestTime
request.Signature = signature
response, err := client.ListChanges(context.Background(), request)
require.NoError(t, err, "failed to call ListChanges")
res, err := json.Marshal(response)
require.NoError(t, err, "failed to marshal response")
expected, err := json.Marshal(test.reply)
require.NoError(t, err, "failed to marshal expected response")
require.Equal(t, res, expected, fmt.Sprintf("failed to compare test results for %v", test.name))
}
func server(ctx context.Context, config *config.Config) (proto.SyncerClient, func()) {
buffer := 101024 * 1024
lis := bufconn.Listen(buffer)
quitChan := make(chan struct{})
syncServer, err := NewPersistentSyncerServer(config)
if err != nil {
log.Fatalf("failed to create sync server: %v", err)
}
syncServer.Start(quitChan)
baseServer := CreateServer(config, lis, syncServer)
go func() {
if err := baseServer.Serve(lis); err != nil {
log.Printf("error serving server: %v", err)
}
}()
conn, err := grpc.DialContext(ctx, "",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Printf("error connecting to server: %v", err)
}
closer := func() {
err := lis.Close()
if err != nil {
log.Printf("error closing listener: %v", err)
}
baseServer.Stop()
quitChan <- struct{}{}
}
client := proto.NewSyncerClient(conn)
return client, closer
}