-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdump_server_test.go
155 lines (126 loc) · 4.67 KB
/
dump_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
package server
import (
"context"
"testing"
"time"
"github.com/regnull/easyecc/v2"
bcmocks "github.com/regnull/ubikom/bc/mocks"
"github.com/regnull/ubikom/pb"
"github.com/regnull/ubikom/protoutil"
"github.com/regnull/ubikom/store"
"github.com/stretchr/testify/assert"
)
func Test_DumpServer_SendReceive(t *testing.T) {
assert := assert.New(t)
dumpStore := store.NewMemory()
bchain := new(bcmocks.MockBlockchain)
ctx := context.Background()
dumpServer := NewDumpServer(dumpStore, bchain)
aliceKey, err := easyecc.NewPrivateKey(easyecc.P256)
assert.NoError(err)
bobKey, err := easyecc.NewPrivateKey(easyecc.P256)
assert.NoError(err)
bchain.EXPECT().PublicKeyByCurve(ctx, "alice",
easyecc.P256).Return(aliceKey.PublicKey(), nil)
bchain.EXPECT().PublicKeyByCurve(ctx, "bob",
easyecc.P256).Return(bobKey.PublicKey(), nil)
msg, err := protoutil.CreateMessage(aliceKey, []byte("hi bob"), "alice", "bob", bobKey.PublicKey())
assert.NoError(err)
sendRes, err := dumpServer.Send(ctx, &pb.SendRequest{Message: msg})
assert.NoError(err)
assert.NotNil(sendRes)
identityProof, err := protoutil.IdentityProof(bobKey, time.Now())
assert.NoError(err)
req := &pb.ReceiveRequest{
IdentityProof: identityProof,
CryptoContext: &pb.CryptoContext{
EllipticCurve: pb.EllipticCurve(easyecc.P256),
EcdhVersion: 2,
EcdsaVersion: 1,
},
}
receiveRes, err := dumpServer.Receive(ctx, req)
assert.NoError(err)
assert.NotNil(receiveRes)
assert.Equal("alice", receiveRes.GetMessage().GetSender())
assert.Equal("bob", receiveRes.GetMessage().GetReceiver())
content, err := protoutil.DecryptMessage(ctx, bchain, bobKey, receiveRes.GetMessage())
assert.NoError(err)
assert.Equal("hi bob", content)
bchain.AssertExpectations(t)
}
func Test_DumpServer_SendReceiveLegacy(t *testing.T) {
assert := assert.New(t)
dumpStore := store.NewMemory()
bchain := new(bcmocks.MockBlockchain)
ctx := context.Background()
dumpServer := NewDumpServer(dumpStore, bchain)
aliceKey, err := easyecc.NewPrivateKey(easyecc.SECP256K1)
assert.NoError(err)
bobKey, err := easyecc.NewPrivateKey(easyecc.SECP256K1)
assert.NoError(err)
bchain.EXPECT().PublicKeyByCurve(ctx, "alice",
easyecc.SECP256K1).Return(aliceKey.PublicKey(), nil)
bchain.EXPECT().PublicKeyByCurve(ctx, "bob",
easyecc.SECP256K1).Return(bobKey.PublicKey(), nil)
msg, err := protoutil.CreateLegacyMessage(aliceKey, []byte("hi bob"), "alice", "bob", bobKey.PublicKey())
assert.NoError(err)
sendRes, err := dumpServer.Send(ctx, &pb.SendRequest{Message: msg})
assert.NoError(err)
assert.NotNil(sendRes)
identityProof, err := protoutil.IdentityProof(bobKey, time.Now())
assert.NoError(err)
req := &pb.ReceiveRequest{
IdentityProof: identityProof,
}
receiveRes, err := dumpServer.Receive(ctx, req)
assert.NoError(err)
assert.NotNil(receiveRes)
assert.Equal("alice", receiveRes.GetMessage().GetSender())
assert.Equal("bob", receiveRes.GetMessage().GetReceiver())
content, err := protoutil.DecryptLegacyMessage(ctx, bchain, bobKey, receiveRes.GetMessage())
assert.NoError(err)
assert.Equal("hi bob", content)
bchain.AssertExpectations(t)
}
func Test_DumpServer_IdentityFallback(t *testing.T) {
// This test should go away after the identity proof is working as expected.
assert := assert.New(t)
dumpStore := store.NewMemory()
bchain := new(bcmocks.MockBlockchain)
ctx := context.Background()
dumpServer := NewDumpServer(dumpStore, bchain)
aliceKey, err := easyecc.NewPrivateKey(easyecc.P256)
assert.NoError(err)
bobKey, err := easyecc.NewPrivateKey(easyecc.P256)
assert.NoError(err)
bchain.EXPECT().PublicKeyByCurve(ctx, "alice",
easyecc.P256).Return(aliceKey.PublicKey(), nil)
bchain.EXPECT().PublicKeyByCurve(ctx, "bob",
easyecc.P256).Return(bobKey.PublicKey(), nil)
msg, err := protoutil.CreateMessage(aliceKey, []byte("hi bob"), "alice", "bob", bobKey.PublicKey())
assert.NoError(err)
sendRes, err := dumpServer.Send(ctx, &pb.SendRequest{Message: msg})
assert.NoError(err)
assert.NotNil(sendRes)
// Intentionally send old proof of identity.
identityProof, err := protoutil.IdentityProof(bobKey, time.Now().Add(-time.Second*100))
assert.NoError(err)
req := &pb.ReceiveRequest{
IdentityProof: identityProof,
CryptoContext: &pb.CryptoContext{
EllipticCurve: pb.EllipticCurve(easyecc.P256),
EcdhVersion: 2,
EcdsaVersion: 1,
},
}
receiveRes, err := dumpServer.Receive(ctx, req)
assert.NoError(err)
assert.NotNil(receiveRes)
assert.Equal("alice", receiveRes.GetMessage().GetSender())
assert.Equal("bob", receiveRes.GetMessage().GetReceiver())
content, err := protoutil.DecryptMessage(ctx, bchain, bobKey, receiveRes.GetMessage())
assert.NoError(err)
assert.Equal("hi bob", content)
bchain.AssertExpectations(t)
}