-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
100 lines (78 loc) · 3.13 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
package main
import (
"encoding/json"
"testing"
"github.com/direktoren/go-nats-go/pkg/easycrypt"
"github.com/stretchr/testify/assert"
)
func TestByteMessageFunc(t *testing.T) {
data := []byte("This is the test string that is the bulk of our message")
generateMessage := byteMessageFunc(data)
var total uint64 = 10
var count uint64
for ; count < total; count++ {
testRawMessage := generateMessage(count, total)
assert.Equal(t, string([]byte{0, 0, 0, 0}), testRawMessage.messageType())
assert.Equal(t, string([]byte{0, 0, 0, 0}), testRawMessage.format())
message := byteMessage(testRawMessage.message())
assert.Equal(t, count, message.count())
assert.Equal(t, total, message.total())
assert.Equal(t, data, message.data())
}
}
func TestStructMessageFunc(t *testing.T) {
data := struct{ MyData string }{"This is the test string that is the bulk of our message"}
generateMessage := structMessageFunc(&data)
var total uint64 = 10
var count uint64
for ; count < total; count++ {
testRawMessage := generateMessage(count, total)
assert.Equal(t, string([]byte{0, 0, 0, 0}), testRawMessage.messageType())
assert.Equal(t, string([]byte{0, 0, 0, 0}), testRawMessage.format())
message := testRawMessage.message()
copyStruct := structMessage{Data: &struct{ MyData string }{}}
err := json.Unmarshal(message, ©Struct)
assert.Equal(t, err, nil, "json.Unmarshal failed")
assert.Equal(t, count, copyStruct.count())
assert.Equal(t, total, copyStruct.total())
assert.Equal(t, &data, copyStruct.Data)
notTheData := struct{ MyData string }{"Just another message"}
assert.NotEqual(t, ¬TheData, copyStruct.Data)
}
}
func TestEncryptedMessageFunc(t *testing.T) {
data := []byte("This is the test string that is the bulk of our message")
key := "ThisIsMy32BytesKeyForTestingFine"
generateMessage := encryptedMessageFunc(byteMessageFunc(data), key)
var total uint64 = 10
var count uint64
for ; count < total; count++ {
testEncryptedRawMessage := generateMessage(count, total)
assert.Equal(t, string([]byte{0, 0, 0, 0}), testEncryptedRawMessage.messageType())
assert.Equal(t, string([]byte{0, 0, 0, 0}), testEncryptedRawMessage.format())
message := testEncryptedRawMessage.message()
tmpDecrypted, err := easycrypt.Decrypt(message, key)
assert.Equal(t, err, nil, "Decrypt failed")
decryptedMessage := byteMessage(tmpDecrypted)
assert.Equal(t, count, decryptedMessage.count())
assert.Equal(t, total, decryptedMessage.total())
assert.Equal(t, data, decryptedMessage.data())
}
}
func TestRawMessageFunc(t *testing.T) {
data := []byte("This is the test string that is the bulk of our message")
msgType := "test"
format := "byte"
generateMessage := rawMessageFunc([]byte(msgType), []byte(format), byteMessageFunc(data))
var total uint64 = 10
var count uint64
for ; count < total; count++ {
testRawMessage := generateMessage(count, total)
assert.Equal(t, msgType, testRawMessage.messageType())
assert.Equal(t, format, testRawMessage.format())
message := byteMessage(testRawMessage.message())
assert.Equal(t, count, message.count())
assert.Equal(t, total, message.total())
assert.Equal(t, data, message.data())
}
}