-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathloxone_test.go
232 lines (204 loc) · 5.85 KB
/
loxone_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
package loxone
import (
log "github.com/sirupsen/logrus"
"testing"
"time"
)
func TestEncodeCommand(t *testing.T) {
encrypt := &encrypt{
iv: "a74b457d12e5c00520292ca83b03aac3",
key: "c8afa9a257c1577892d940afa82435550bbcc52bc2ff9d49d1c6aea5a71bf4a8",
salt: "d93b",
}
encoded, _ := encrypt.getEncryptedCmd("jdev/sys/getkey2/xcid", requestResponseVal)
if string(encoded) != "jdev/sys/fenc/FqlXx4NrS7XYxddF8kTP1dadaH9FY%2FMBt9Z1zC%2FANqI%3D" {
print(string(encoded))
t.Errorf("Error, cypher methods does'nt match: result %s", encoded)
}
}
func TestEncryptation_HashUser(t *testing.T) {
encrypt := &encrypt{
iv: "a74b457d12e5c00520292ca83b03aac3",
key: "c8afa9a257c1577892d940afa82435550bbcc52bc2ff9d49d1c6aea5a71bf4a8",
salt: "d93b",
}
encoded := encrypt.hashUser("user", "test", "31333338623266642D303135342D363463392D66666666353034663934313032343764", "46324345453737334642443534343439313744394535313539443642424436373144323532423246", SHA1)
if encoded != "fe64ac92e98486eed980a8a03401ee175bbd51d3" {
t.Errorf("Error, hash user password doest not match: result %s", encoded)
}
}
func TestDeserializeLoxoneResponse(t *testing.T) {
json := []byte(`{"LL": {"value": "ok", "code": "200", "control": "test"}}`)
result := &SimpleValue{}
body, _ := deserializeLoxoneResponse(json, result)
if result.Value != "ok" {
t.Errorf("Error during value deserilization")
}
if body.Code != 200 {
t.Errorf("Error during code deserilization")
}
if body.Control != "test" {
t.Errorf("Error during test deserilization")
}
json = []byte(`{"LL": {"value": {"key": "ontTimeSalt", "Salt": "Salt"}, "code": 200, "control": "test"}}`)
resultSalt := &salt{}
body, _ = deserializeLoxoneResponse(json, resultSalt)
if resultSalt.Salt != "Salt" {
t.Errorf("Error during Salt value deserilization")
}
if resultSalt.OneTimeSalt != "ontTimeSalt" {
t.Errorf("Error during Salt value deserilization")
}
if body.Code != 200 {
t.Errorf("Error during code deserilization")
}
}
func TestConfig_CatName(t *testing.T) {
cfg := CreateConfig()
tests := []struct {
name string
key interface{}
want string
}{
{
name: "Valid category",
key: "test category key",
want: "test category name",
},
{
name: "Non existent category",
key: "non existent key",
want: "",
},
{
name: "Nil category",
key: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cfg.CatName(tt.key); got != tt.want {
t.Errorf("Config.CatName() = %v, want %v", got, tt.want)
}
})
}
}
func TestConfig_RoomName(t *testing.T) {
cfg := CreateConfig()
tests := []struct {
name string
key interface{}
want string
}{
{
name: "Valid room",
key: "test room key",
want: "test room name",
},
{
name: "Non existent room",
key: "non existent key",
want: "",
},
{
name: "Nil room",
key: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := cfg.RoomName(tt.key); got != tt.want {
t.Errorf("Config.RoomName() = %v, want %v", got, tt.want)
}
})
}
}
func CreateConfig() *Config {
cfg := &Config{
LastModified: "not used in tests (yet?)",
MsInfo: map[string]interface{}{
"not used in tests (yet?)": nil,
},
GlobalStates: map[string]string{
"not used in tests (yet?)": "not used in tests (yet?)",
},
OperatingModes: map[string]interface{}{
"not used in tests (yet?)": nil,
},
Rooms: map[string]*Room{
"test room key": &Room{
Name: "test room name",
UUID: "not used in tests (yet?)",
Type: 0, //not used in tests (yet?)
},
},
Cats: map[string]*Category{
"test category key": &Category{
Name: "test category name",
UUID: "not used in tests (yet?)",
Type: "not used in tests (yet?)",
},
},
Controls: map[string]*Control{
"not used in tests (yet?)": &Control{
Name: "not used in tests (yet?)",
Type: "not used in tests (yet?)",
UUIDAction: "not used in tests (yet?)",
Room: "not used in tests (yet?)",
Cat: "not used in tests (yet?)",
States: map[string]interface{}{
"not used in tests (yet?)": "not used in tests (yet?)",
},
},
},
}
return cfg
}
func ExampleNew() {
// create a new connection to 1.2.3.4 using the default port 80 with the username 'admin' and password 'admin'
// auto reconnect will be enabled with a timeout of 30 seconds
lox, err := New(WithHost("1.2.3.4"), WithUsernameAndPassword("admin", "admin"))
if err != nil {
panic(err)
}
// register for events so we get sent updates for values
err = lox.RegisterEvents()
if err != nil {
panic(err)
}
// loop the events channel
for event := range lox.GetEvents() {
log.Infof("%s: %.2f", event.UUID, event.Value)
}
}
func ExampleNew_complex() {
// create a new connection to 1.2.3.4 using a custom port 7777 with the username 'admin' and password 'admin',
// automatically register for events when connected and adjust the reconnect timeout to 15 seconds.
lox, err := New(WithHost("1.2.3.4"), WithPort(7777), WithUsernameAndPassword("admin", "admin"), WithRegisterEvents(), WithReconnectTimeout(15*time.Second))
if err != nil {
panic(err)
}
// loop the events channel
for event := range lox.GetEvents() {
log.Infof("%s: %.2f", event.UUID, event.Value)
}
}
func ExampleNew_cloudDNS() {
// gets the connection details from Loxone Cloud DNS for the specified Miniserver MAC and connects with the
// username 'admin' and password 'admin' auto reconnect will be enabled with a timeout of 30 seconds
lox, err := New(WithCloudDNS("504f94a00000"), WithUsernameAndPassword("admin", "admin"))
if err != nil {
panic(err)
}
// register for events so we get sent updates for values
err = lox.RegisterEvents()
if err != nil {
panic(err)
}
// loop the events channel
for event := range lox.GetEvents() {
log.Infof("%s: %.2f", event.UUID, event.Value)
}
}