-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
188 lines (160 loc) · 3.84 KB
/
main_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
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"testing"
"github.com/reynoldsbd3/golang-challenge2/secnet"
)
func TestReadWriterPing(t *testing.T) {
priv, pub := &[32]byte{'p', 'r', 'i', 'v'}, &[32]byte{'p', 'u', 'b'}
r, w := io.Pipe()
secureR := secnet.NewSecureReader(r, priv, pub)
secureW := secnet.NewSecureWriter(w, priv, pub)
// Encrypt hello world
go func() {
fmt.Fprintf(secureW, "hello world\n")
w.Close()
}()
// Decrypt message
buf := make([]byte, 1024)
n, err := secureR.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
buf = buf[:n]
// Make sure we have hello world back
if res := string(buf); res != "hello world\n" {
t.Fatalf("Unexpected result: %s != %s", res, "hello world")
}
}
func TestSecureWriter(t *testing.T) {
priv, pub := &[32]byte{'p', 'r', 'i', 'v'}, &[32]byte{'p', 'u', 'b'}
r, w := io.Pipe()
secureW := secnet.NewSecureWriter(w, priv, pub)
// Make sure we are secure
// Encrypt hello world
go func() {
fmt.Fprintf(secureW, "hello world\n")
w.Close()
}()
// Read from the underlying transport instead of the decoder
buf, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
// Make sure we dont' read the plain text message.
if res := string(buf); res == "hello world\n" {
t.Fatal("Unexpected result. The message is not encrypted.")
}
r, w = io.Pipe()
secureW = secnet.NewSecureWriter(w, priv, pub)
// Make sure we are unique
// Encrypt hello world
go func() {
fmt.Fprintf(secureW, "hello world\n")
w.Close()
}()
// Read from the underlying transport instead of the decoder
buf2, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}
// Make sure we dont' read the plain text message.
if string(buf) == string(buf2) {
t.Fatal("Unexpected result. The encrypted message is not unique.")
}
}
func TestSecureEchoServer(t *testing.T) {
// Create a random listener
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Start the server
go Serve(l)
conn, err := secnet.Dial(l.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
expected := "hello world\n"
if _, err := fmt.Fprintf(conn, expected); err != nil {
t.Fatal(err)
}
buf := make([]byte, 2048)
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if got := string(buf[:n]); got != expected {
t.Fatalf("Unexpected result:\nGot:\t\t%s\nExpected:\t%s\n", got, expected)
}
}
func TestSecureServe(t *testing.T) {
// Create a random listener
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Start the server
go Serve(l)
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
unexpected := "hello world\n"
if _, err := fmt.Fprintf(conn, unexpected); err != nil {
t.Fatal(err)
}
buf := make([]byte, 2048)
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if got := string(buf[:n]); got == unexpected {
t.Fatalf("Unexpected result:\nGot raw data instead of serialized key")
}
}
func TestSecureDial(t *testing.T) {
// Create a random listener
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Start the server
go func(l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
return
}
go func(c net.Conn) {
defer c.Close()
key := [32]byte{}
c.Write(key[:])
buf := make([]byte, 2048)
n, err := c.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if got := string(buf[:n]); got == "hello world\n" {
t.Fatal("Unexpected result. Got raw data instead of encrypted")
}
}(conn)
}
}(l)
conn, err := secnet.Dial(l.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
expected := "hello world\n"
if _, err := fmt.Fprintf(conn, expected); err != nil {
t.Fatal(err)
}
}