This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_test.go
144 lines (109 loc) · 3.39 KB
/
client_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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package ttnsdk
import (
"os"
"strings"
"testing"
"time"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
testlog "github.com/TheThingsNetwork/go-utils/log/test"
"github.com/TheThingsNetwork/go-utils/random"
"github.com/TheThingsNetwork/ttn/core/types"
. "github.com/smartystreets/assertions"
)
func TestClient(t *testing.T) {
a := New(t)
for _, env := range strings.Split("APP_ID APP_ACCESS_KEY", " ") {
if os.Getenv(env) == "" {
t.Skipf("Skipping client test: %s not configured", env)
}
}
log := testlog.NewLogger()
ttnlog.Set(log)
defer log.Print(t)
appID := os.Getenv("APP_ID")
config := NewCommunityConfig("client-test")
client := config.NewClient(appID, os.Getenv("APP_ACCESS_KEY")).(*client)
client.connectHandler()
client.connectMQTT()
time.Sleep(10 * time.Millisecond)
client.Close()
time.Sleep(time.Second)
client.connectHandler()
client.connectMQTT()
time.Sleep(10 * time.Millisecond)
devs, err := client.ManageDevices()
a.So(err, ShouldBeNil)
var appEUI types.AppEUI
var devEUI types.DevEUI
var appKey types.AppKey
random.FillBytes(appEUI[:])
random.FillBytes(devEUI[:])
random.FillBytes(appKey[:])
dev := &Device{
SparseDevice: SparseDevice{
AppID: appID,
DevID: "sdk-test",
AppEUI: appEUI,
DevEUI: devEUI,
Description: "SDK Test Device",
AppKey: &appKey,
},
Uses32BitFCnt: true,
}
err = devs.Set(dev)
a.So(err, ShouldBeNil)
defer devs.Delete("sdk-test")
deviceList, err := devs.List(0, 0)
a.So(err, ShouldBeNil)
a.So(deviceList, ShouldNotBeEmpty)
dev, err = devs.Get("sdk-test")
a.So(err, ShouldBeNil)
err = dev.PersonalizeRandom()
a.So(err, ShouldBeNil)
a.So(dev.DevAddr, ShouldNotBeNil)
pubsub, err := client.PubSub()
a.So(err, ShouldBeNil)
uplink, err := pubsub.AllDevices().SubscribeUplink()
a.So(err, ShouldBeNil)
sim, err := client.Simulate("sdk-test")
a.So(err, ShouldBeNil)
err = sim.Uplink(1, []byte{0xaa, 0xbc})
a.So(err, ShouldBeNil)
select {
case msg := <-uplink:
a.So(msg.AppID, ShouldEqual, appID)
a.So(msg.DevID, ShouldEqual, "sdk-test")
case <-time.After(time.Second):
t.Fatal("Did not receive uplink within a second")
}
err = dev.Delete()
a.So(err, ShouldBeNil)
}
func TestCleanMQTTAddress(t *testing.T) {
a := New(t)
addr, err := cleanMQTTAddress("localhost:1883")
a.So(err, ShouldBeNil)
a.So(addr, ShouldEqual, "tcp://localhost:1883")
// if `host:port` then `mqtt://host:port`
addr, err = cleanMQTTAddress("localhost:1234")
a.So(err, ShouldBeNil)
a.So(addr, ShouldEqual, "tcp://localhost:1234")
// if `host:8883` then `mqtts://host:8883`
addr, err = cleanMQTTAddress("localhost:8883")
a.So(err, ShouldBeNil)
a.So(addr, ShouldEqual, "ssl://localhost:8883")
// if `host` then `mqtt://host:1883` and `mqtts://host:8883` (we choose mqtts)
addr, err = cleanMQTTAddress("localhost")
a.So(err, ShouldBeNil)
a.So(addr, ShouldEqual, "ssl://localhost:8883")
// if `mqtt://host` then `mqtt://host:1883`
addr, err = cleanMQTTAddress("mqtt://localhost")
a.So(err, ShouldBeNil)
a.So(addr, ShouldEqual, "tcp://localhost:1883")
// if `mqtts://host` then `mqtt://host:1883` and `mqtts://host:8883` (we choose mqtts)
addr, err = cleanMQTTAddress("mqtts://localhost")
a.So(err, ShouldBeNil)
a.So(addr, ShouldEqual, "ssl://localhost:8883")
}