-
Notifications
You must be signed in to change notification settings - Fork 13
/
iot_test.go
341 lines (286 loc) · 9.18 KB
/
iot_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2018, Andrew C. Young
// License: MIT
package iot_test
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"github.com/vaelen/iot"
)
const RSACertificatePath = "test_keys/rsa_cert.pem"
const RSAPrivateKeyPath = "test_keys/rsa_private.pem"
const ECCertificatePath = "test_keys/ec_cert.pem"
const ECPrivateKeyPath = "test_keys/ec_private.pem"
var TestID = &iot.ID{
ProjectID: "test-project",
Location: "test-location",
Registry: "test-registry",
DeviceID: "test-device",
}
var ClientID = "projects/test-project/locations/test-location/registries/test-registry/devices/test-device"
var ConfigTopic = "/devices/test-device/config"
var StateTopic = "/devices/test-device/state"
var EventsTopic = "/devices/test-device/events"
var mockClient *iot.MockMQTTClient
func TestLoadRSACredentials(t *testing.T) {
credentials, err := iot.LoadRSACredentials(RSACertificatePath, RSAPrivateKeyPath)
if err != nil {
t.Fatalf("Couldn't load credentials: %v", err)
}
if credentials == nil {
t.Fatal("Credentials not loaded.")
}
if credentials.PrivateKey == nil {
t.Fatal("Private key not loaded.")
}
}
func TestLoadECCredentials(t *testing.T) {
credentials, err := iot.LoadECCredentials(ECCertificatePath, ECPrivateKeyPath)
if err != nil {
t.Fatalf("Couldn't load credentials: %v", err)
}
if credentials == nil {
t.Fatal("Credentials not loaded.")
}
if credentials.PrivateKey == nil {
t.Fatal("Private key not loaded.")
}
}
func TestDefaultOptions(t *testing.T) {
credentials, err := iot.LoadRSACredentials(RSACertificatePath, RSAPrivateKeyPath)
if err != nil {
t.Fatalf("Couldn't load credentials: %v", err)
}
options := iot.DefaultOptions(TestID, credentials)
if options == nil {
t.Fatal("Options structure wasn't returned")
}
if options.ID != TestID {
t.Fatal("Incorrect ID")
}
if options.Credentials != credentials {
t.Fatal("Incorrect credentials")
}
if options.EventQOS != 1 {
t.Fatalf("Incorrect event QoS: %v", options.EventQOS)
}
if options.StateQOS != 1 {
t.Fatalf("Incorrect state QoS: %v", options.StateQOS)
}
if options.ConfigQOS != 2 {
t.Fatalf("Incorrect config QoS: %v", options.ConfigQOS)
}
if options.AuthTokenExpiration != iot.DefaultAuthTokenExpiration {
t.Fatalf("Incorrect auth token expiration: %v", options.AuthTokenExpiration)
}
}
func TestThingWithBadOptions(t *testing.T) {
ctx := context.Background()
var mockClient *iot.MockMQTTClient
iot.NewClient = func(t iot.Thing, o *iot.ThingOptions) iot.MQTTClient {
mockClient = iot.NewMockClient(t, o)
return mockClient
}
options := &iot.ThingOptions{}
thing := iot.New(options)
if thing == nil {
t.Fatal("Thing was not returned from New() with bad options")
}
err := thing.Connect(ctx, "bad options")
if err != iot.ErrConfigurationError {
t.Fatalf("Wrong error returned from Connect() with invalid options: %v", err)
}
}
func TestRSAThingFull(t *testing.T) {
initMockClient()
credentials := getCredentials(t, iot.CredentialTypeRSA)
options, configReceived := getOptions(t, credentials)
thing := getThing(t, options)
serverAddress := "ssl://mqtt.example.com:443"
doConnectionTest(t, thing, serverAddress)
doAlreadyConnectedTest(t, thing, serverAddress)
checkClientValues(t, options)
doConfigTest(t, configReceived)
doEventTest(t, thing)
doDisconnectTest(t, thing)
}
func TestECThingConnectOnly(t *testing.T) {
initMockClient()
credentials := getCredentials(t, iot.CredentialTypeEC)
options, _ := getOptions(t, credentials)
thing := getThing(t, options)
serverAddress := "ssl://mqtt.example.com:443"
doConnectionTest(t, thing, serverAddress)
checkClientValues(t, options)
doDisconnectTest(t, thing)
}
func initMockClient() {
iot.NewClient = func(t iot.Thing, o *iot.ThingOptions) iot.MQTTClient {
mockClient = iot.NewMockClient(t, o)
return mockClient
}
}
func getThing(t *testing.T, options *iot.ThingOptions) iot.Thing {
thing := iot.New(options)
if thing == nil {
t.Fatal("Thing wasn't returned from New()")
}
if thing.IsConnected() {
t.Fatal("Thing thinks it is connected when it really is not")
}
return thing
}
func getCredentials(t *testing.T, credentialType iot.CredentialType) *iot.Credentials {
var credentials *iot.Credentials
var err error
switch credentialType {
case iot.CredentialTypeEC:
credentials, err = iot.LoadECCredentials(ECCertificatePath, ECPrivateKeyPath)
case iot.CredentialTypeRSA:
fallthrough
default:
credentials, err = iot.LoadRSACredentials(RSACertificatePath, RSAPrivateKeyPath)
}
if err != nil {
t.Fatalf("Couldn't load credentials: %v", err)
}
return credentials
}
func getOptions(t *testing.T, credentials *iot.Credentials) (*iot.ThingOptions, *bytes.Buffer) {
options := iot.DefaultOptions(TestID, credentials)
if options == nil {
t.Fatal("Options structure wasn't returned")
}
debugWriter := &bytes.Buffer{}
infoWriter := &bytes.Buffer{}
errorWriter := &bytes.Buffer{}
configReceived := &bytes.Buffer{}
options.AuthTokenExpiration = 0
options.DebugLogger = func(a ...interface{}) { fmt.Fprint(debugWriter, a...) }
options.InfoLogger = func(a ...interface{}) { fmt.Fprint(infoWriter, a...) }
options.ErrorLogger = func(a ...interface{}) { fmt.Fprint(errorWriter, a...) }
options.LogMQTT = true
options.ConfigHandler = func(thing iot.Thing, config []byte) {
ctx := context.Background()
configReceived.Truncate(0)
configReceived.Write(config)
state := []byte("ok")
thing.PublishState(ctx, state)
}
return options, configReceived
}
func doConnectionTest(t *testing.T, thing iot.Thing, serverAddress string) {
ctx := context.Background()
err := thing.Connect(ctx, serverAddress)
if err != nil {
t.Fatalf("Couldn't connect. Error: %v", err)
}
if !mockClient.Connected {
t.Fatalf("Client not connected")
}
if len(mockClient.ConnectedTo) < 1 || mockClient.ConnectedTo[0] != serverAddress {
t.Fatalf("Client connected to wrong server: %v", mockClient.ConnectedTo)
}
if !thing.IsConnected() {
t.Fatal("Thing thinks it is not connected when it really is")
}
}
func doAlreadyConnectedTest(t *testing.T, thing iot.Thing, serverAddress string) {
ctx := context.Background()
err := thing.Connect(ctx, "already connected")
if err != nil {
t.Fatalf("Calling Connect() while already connected returned an error: %v", err)
}
if len(mockClient.ConnectedTo) < 1 || mockClient.ConnectedTo[0] != serverAddress {
t.Fatalf("Calling Connect() while already connected caused client to reconnect: %v", mockClient.ConnectedTo)
}
if mockClient.CredentialsProvider == nil {
t.Fatal("Credentials provider not set")
}
}
func checkClientValues(t *testing.T, options *iot.ThingOptions) {
options.AuthTokenExpiration = 0
username, password := mockClient.CredentialsProvider()
if username == "" || password == "" {
t.Fatalf("Bad username and/or password returned. Username: %v, Password: %v", username, password)
}
if mockClient.DebugLogger == nil {
t.Fatal("Debug logger not set")
}
if mockClient.InfoLogger == nil {
t.Fatal("Info logger not set")
}
if mockClient.ErrorLogger == nil {
t.Fatal("Error logger not set")
}
if mockClient.ClientID != ClientID {
t.Fatalf("Client ID not set properly: %v", mockClient.ClientID)
}
if len(mockClient.Subscriptions) != 1 {
t.Fatalf("Wrong number of subscriptions: %v", len(mockClient.Subscriptions))
}
if mockClient.OnConnectHandler == nil {
t.Fatalf("OnConnectHandler not set")
}
}
func doConfigTest(t *testing.T, configReceived *bytes.Buffer) {
mockClient.Receive(ConfigTopic, []byte("test config"))
if configReceived.String() != "test config" {
t.Fatalf("Wrong configuration received: %v", configReceived.String())
}
l, ok := mockClient.Messages[StateTopic]
if !ok || l == nil || len(l) == 0 {
t.Fatalf("State not published")
}
if string(l[0].([]byte)) != "ok" {
t.Fatalf("Wrong state published: %v", string(l[0].([]byte)))
}
}
func doEventTest(t *testing.T, thing iot.Thing) {
ctx := context.Background()
topLevelMessage := "Top"
events := make(map[string]string)
events["a"] = "A"
events["a/b"] = "B"
err := thing.PublishEvent(ctx, []byte(topLevelMessage))
if err != nil {
t.Fatalf("Couldn't publish. Error: %v", err)
}
for k, v := range events {
err = thing.PublishEvent(ctx, []byte(v), strings.Split(k, "/")...)
if err != nil {
t.Fatalf("Couldn't publish. Error: %v", err)
}
}
l, ok := mockClient.Messages[EventsTopic]
if !ok || l == nil || len(l) == 0 {
t.Fatalf("Message not published. Topic: %v", EventsTopic)
}
if len(l) > 1 {
t.Fatalf("Too many messages published. Topic: %v, Count; %v", EventsTopic, len(l))
}
if string(l[0].([]byte)) != topLevelMessage {
t.Fatalf("Wrong message published. Topic: %v, Message: %v", EventsTopic, string(l[0].([]byte)))
}
for k, v := range events {
topic := EventsTopic + "/" + k
l, ok = mockClient.Messages[topic]
if !ok || l == nil || len(l) == 0 {
t.Fatalf("Message not published. Topic: %v", topic)
}
if len(l) > 1 {
t.Fatalf("Too many messages published. Topic: %v, Count; %v", topic, len(l))
}
if string(l[0].([]byte)) != v {
t.Fatalf("Wrong message published. Topic: %v, Message: %v", topic, string(l[0].([]byte)))
}
}
}
func doDisconnectTest(t *testing.T, thing iot.Thing) {
thing.Disconnect(context.Background())
if mockClient.Connected {
t.Fatal("Didn't disconnect")
}
}