-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_adapter_test.go
151 lines (117 loc) · 3.29 KB
/
handler_adapter_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
package main_test
import (
"bytes"
"crypto/aes"
"encoding/hex"
"net/http"
"net/http/httptest"
"strings"
"testing"
"piot-server"
)
func getAdapter(t *testing.T) *main.Adapter {
log := GetLogger(t)
db := GetDb(t)
things := GetThings(t, log, db)
mqtt := GetMqtt(t, log)
pdevices := GetPiotDevices(t, log, things, mqtt)
return main.NewAdapter(log, pdevices, "1234567890123456")
}
/* GET method is not supported */
func TestForbiddenGet(t *testing.T) {
req, err := http.NewRequest("GET", "/", strings.NewReader(""))
Ok(t, err)
rr := httptest.NewRecorder()
adapter := getAdapter(t)
adapter.ServeHTTP(rr, req)
CheckStatusCode(t, rr, 405)
}
/* Post data for device that is not registered */
func TestPacketForUnknownThing(t *testing.T) {
db := GetDb(t)
CleanDb(t, db)
deviceData := `
{
"device": "Device123",
"readings": [
{
"address": "SensorXYZ",
"t": 23
}
]
}`
req, err := http.NewRequest("POST", "/", strings.NewReader(deviceData))
Ok(t, err)
rr := httptest.NewRecorder()
adapter := getAdapter(t)
adapter.ServeHTTP(rr, req)
CheckStatusCode(t, rr, 200)
// TODO: Check if device is registered
}
/* Post data in short notation for device that is not registered */
func TestPacketShortNotationForUnknownThing(t *testing.T) {
db := GetDb(t)
CleanDb(t, db)
deviceData := `
{
"d": "Device123",
"r": [
{
"a": "SensorXYZ",
"t": 23
}
]
}`
req, err := http.NewRequest("POST", "/", strings.NewReader(deviceData))
Ok(t, err)
rr := httptest.NewRecorder()
adapter := getAdapter(t)
adapter.ServeHTTP(rr, req)
CheckStatusCode(t, rr, 200)
// TODO: Check if device is registered
}
/* Post encrypted data */
func TestPacketEncrypted(t *testing.T) {
db := GetDb(t)
CleanDb(t, db)
raw := `
{
"device": "Device123",
"readings": [
{
"address": "SensorXYZ",
"t": 23
}
]
}`
/*
* key The key used for encryption. The key length can be any of 128bit, 192bit, and 256bit.
* 16-bit key corresponds to 128bit
*/
key := "1234567890123456"
size := 16
padding := size - len(raw) % size
t.Logf("raw text size: %d, right padding with %d PKCS#7 bytes", len(raw), padding)
// padding of block for pkcs#7 padding
if padding > 0 {
raw = raw + string(bytes.Repeat([]byte{byte(padding)}, padding))
// add empty block for pkcs#7 padding
} else {
raw = raw + string(bytes.Repeat([]byte{byte(size)}, size))
}
t.Logf("%s", hex.Dump([]byte(raw)))
cipher, err := aes.NewCipher([]byte(key))
Ok(t, err)
encrypted := make([]byte, len(raw))
open := []byte(raw)
for bs, be := 0, size; bs < len(open); bs, be = bs + size, be + size {
cipher.Encrypt(encrypted[bs:be], open[bs:be])
}
req, err := http.NewRequest("POST", "/", strings.NewReader(string(encrypted)))
Ok(t, err)
rr := httptest.NewRecorder()
adapter := getAdapter(t)
adapter.ServeHTTP(rr, req)
CheckStatusCode(t, rr, 200)
// TODO: Check if device is registered
}