-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecnet_test.go
95 lines (87 loc) · 1.77 KB
/
decnet_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
package decnet
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"time"
"github.com/sirupsen/logrus"
)
func TestEncryption(t *testing.T) {
const message = "this is secret message !"
key, err := GenerateKey()
if err != nil {
t.Error(err)
return
}
encrypted, err := Encrypt(key.publicKey, []byte(message))
if err != nil {
t.Error(err)
return
}
result, err := key.Decrypt(encrypted)
if err != nil {
t.Error(err)
return
}
if string(result) != message {
t.Error(string(result), "not matched with ", message)
}
logrus.Info(string(result))
}
func TestEcho(t *testing.T) {
const (
message = "Hello Sir!"
APort = 8000
BPort = 8001
)
keyA, err := GenerateKey()
if err != nil {
t.Error(err)
return
}
keyB, err := GenerateKey()
if err != nil {
t.Error(err)
return
}
connA, err := New(Options{Port: APort, DBPath: "decnet_test_conn_a.db"}, keyA)
if err != nil {
t.Error(err)
return
}
connA.AddReplayHandler("echo", func(c *Context) error {
data, err := ioutil.ReadAll(c.Body())
if err != nil {
return err
}
if string(data) != message {
t.Errorf("%s not match with > %s", string(data), message)
}
return nil
})
connB, err := New(Options{Port: BPort, DBPath: "decnet_test_conn_b.db"}, keyB)
if err != nil {
t.Error(err)
return
}
connB.AddHandler("echo", func(c *Context) error {
data, err := ioutil.ReadAll(c.Body())
if err != nil {
return err
}
if string(data) != message {
t.Errorf("%s not match with > %s", string(data), message)
}
return c.Replay(string(data))
})
go connA.Start()
go connB.Start()
time.Sleep(time.Second)
reader := bytes.NewReader([]byte(message))
if err := connA.Send(fmt.Sprintf("0.0.0.0:%d", BPort), reader, "echo"); err != nil {
t.Error(err)
return
}
time.Sleep(time.Second)
}