-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_test.go
74 lines (63 loc) · 1.35 KB
/
stream_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
package secretstream
import (
"bytes"
"crypto/rand"
"testing"
)
func TestEncodeDecode(t *testing.T) {
common_test(t, NewEncryptor, NewDecryptor)
}
func common_test(t *testing.T,
makeEnc func([]byte) (Encryptor, []byte, error),
makeDec func(k, h []byte) (Decryptor, error)) {
key, err := NewStreamKey()
if err != nil {
t.Fatal(err)
}
sender, hdr, err := makeEnc(key)
if err != nil {
t.Fatal(err)
}
plain_text_messages := [][]byte{
[]byte("Hello world"),
randomData(100),
randomData(1000),
randomData(10000),
[]byte("This is good-bye!"),
}
var coded_msgs [][]byte
for i, m := range plain_text_messages {
coded, err := sender.Push(m, byte(i%2))
if err != nil {
t.Error(err)
}
coded_msgs = append(coded_msgs, coded)
}
var decoded_msgs [][]byte
receiver, err := makeDec(key, hdr)
if err != nil {
t.Fatal(err)
}
for i, m := range coded_msgs {
decoded, tag, err := receiver.Pull(m)
if err != nil {
t.Error("decoding error", err)
}
if tag != byte(i%2) {
t.Errorf("unexpected tag received")
}
decoded_msgs = append(decoded_msgs, decoded)
}
for i := range plain_text_messages {
if !bytes.Equal(plain_text_messages[i], decoded_msgs[i]) {
t.Error("failed to decode")
}
}
}
func randomData(c int) []byte {
out := make([]byte, c)
if _, err := rand.Read(out); err != nil {
panic(err)
}
return out
}