-
Notifications
You must be signed in to change notification settings - Fork 11
/
msg_test.go
99 lines (82 loc) · 1.97 KB
/
msg_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
package nymo
import (
"bytes"
"crypto/tls"
"math/rand"
"testing"
"github.com/nymo-net/nymo/pb"
"google.golang.org/protobuf/proto"
)
func TestMsgEncDec(t *testing.T) {
key, err := GenerateUser()
if err != nil {
t.Fatal(err)
}
var db testDB
u := OpenUser(&db, key, tls.Certificate{Certificate: [][]byte{{}}}, nil)
buf := make([]byte, 2048)
rand.Read(buf)
err = u.NewMessage(u.Address(), buf)
if err != nil {
t.Fatal(err)
}
message := u.decryptMessage(db.last)
if message == nil || !bytes.Equal(message.Content, buf) {
t.Fatal("decrypt error")
}
}
func TestMsgEncNoDec(t *testing.T) {
key, err := GenerateUser()
if err != nil {
t.Fatal(err)
}
key2, err := GenerateUser()
if err != nil {
t.Fatal(err)
}
var db testDB
u := OpenUser(&db, key, tls.Certificate{Certificate: [][]byte{{}}}, nil)
u2 := OpenUser(new(testDB), key2, tls.Certificate{Certificate: [][]byte{{}}}, nil)
buf := make([]byte, 2048)
rand.Read(buf)
err = u.NewMessage(u.Address(), buf)
if err != nil {
t.Fatal(err)
}
message := u2.decryptMessage(db.last)
if message != nil {
t.Fatal("decrypt success")
}
}
type testDB struct {
last *pb.Message
}
func (t *testDB) ClientHandle(id [hashTruncate]byte) PeerHandle {
panic("implement me")
}
func (t *testDB) AddPeer(url string, digest *pb.Digest) {
panic("implement me")
}
func (t *testDB) EnumeratePeers() PeerEnumerate {
panic("implement me")
}
func (t *testDB) GetUrlByHash(urlHash [hashTruncate]byte) (url string) {
panic("implement me")
}
func (t *testDB) GetMessage(hash [hashSize]byte) (msg []byte, pow uint64) {
panic("implement me")
}
func (t *testDB) IgnoreMessage(digest *pb.Digest) {
panic("implement me")
}
func (t *testDB) StoreMessage(hash [hashSize]byte, c *pb.MsgContainer, f func() (cohort uint32, err error)) error {
_, err := f()
if err == nil {
t.last = new(pb.Message)
err = proto.Unmarshal(c.Msg, t.last)
}
return err
}
func (t *testDB) StoreDecryptedMessage(message *Message) {
panic("implement me")
}