forked from eclipse-paho/paho.mqtt.golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_options_test.go
178 lines (141 loc) · 3.6 KB
/
unit_options_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
/*
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Seth Hoenig
* Allan Stockdill-Mander
* Mike Robertson
*/
package mqtt
import "os"
import "crypto/tls"
import "crypto/x509"
import "testing"
func Test_NewClientOptions_default(t *testing.T) {
o := NewClientOptions()
if o.clientId != "" {
t.Fatalf("bad default client id")
}
if o.username != "" {
t.Fatalf("bad default username")
}
if o.password != "" {
t.Fatalf("bad default password")
}
if o.tlsconfig != nil {
t.Fatalf("bad default tlsconfig")
}
if o.timeout != 30 {
t.Fatalf("bad default timeout")
}
if o.tracefile != os.Stdout {
t.Fatalf("bad default tracefile")
}
if o.msgRouter == nil {
t.Fatalf("bad default msgRouter")
}
if o.stopRouter == nil {
t.Fatalf("bad stopRouter")
}
if o.pubChanZero != nil {
t.Fatalf("bad default pubChanZero")
}
if o.pubChanOne != nil {
t.Fatalf("bad default pubChanOne")
}
if o.pubChanTwo != nil {
t.Fatalf("bad default pubChanTwo")
}
}
func Test_NewClientOptions_mix(t *testing.T) {
o := NewClientOptions()
o.SetBroker("tcp://192.168.1.2:9999")
o.SetClientId("myclientid")
o.SetUsername("myuser")
o.SetPassword("mypassword")
o.SetTimeout(88)
o.SetTracefile(os.Stderr)
o.SetTraceLevel(Warn)
if o.server.Scheme != "tcp" {
t.Fatalf("bad scheme")
}
if o.server.Host != "192.168.1.2:9999" {
t.Fatalf("bad host")
}
if o.clientId != "myclientid" {
t.Fatalf("bad set clientid")
}
if o.username != "myuser" {
t.Fatalf("bad set username")
}
if o.password != "mypassword" {
t.Fatalf("bad set password")
}
if o.timeout != 88 {
t.Fatalf("bad set timeout")
}
if o.tracefile != os.Stderr {
t.Fatalf("bad set tracefile")
}
if o.tracelevel != Warn {
t.Fatalf("bad set tracelevel")
}
}
func Test_ModifyOptions(t *testing.T) {
o := NewClientOptions()
o.SetBroker("tcp://3.3.3.3:12345")
c := NewClient(o)
o.SetBroker("ws://2.2.2.2:9999")
o.SetOrderMatters(false)
if c.options.server.Scheme != "tcp" {
t.Fatalf("client options.server.Scheme was modified")
}
// if c.options.server.Host != "2.2.2.2:9999" {
// t.Fatalf("client options.server.Host was modified")
// }
if o.order != false {
t.Fatalf("options.order was not modified")
}
}
func Test_TlsConfig(t *testing.T) {
o := NewClientOptions().SetTlsConfig(&tls.Config{
RootCAs: x509.NewCertPool(),
ClientAuth: tls.NoClientCert,
ClientCAs: x509.NewCertPool(),
InsecureSkipVerify: true})
c := NewClient(o)
if c.options.tlsconfig == nil {
t.Fatalf("client options.tlsconfig was nil")
}
if c.options.tlsconfig.ClientAuth != tls.NoClientCert {
t.Fatalf("client options.tlsconfig ClientAuth incorrect")
}
if c.options.tlsconfig.InsecureSkipVerify != true {
t.Fatalf("client options.tlsconfig InsecureSkipVerify incorrect")
}
}
func Test_OnConnectionLost(t *testing.T) {
onconnlost := func(client *MqttClient, err error) {
panic(err)
}
o := NewClientOptions().SetOnConnectionLost(onconnlost)
c := NewClient(o)
if c.options.onconnlost == nil {
t.Fatalf("client options.onconnlost was nil")
}
}
// func Test_MaxInFlight(t *testing.T) {
// dflt := NewClientOptions()
// if dflt.maxinflight != 10 {
// t.Fatalf("options.maxinflight was not defaulted to 10")
// }
// set := NewClientOptions().SetMaxInFlight(1)
// if set.maxinflight != 1 {
// t.Fatalf("options.maxinflight was not set to 1")
// }
// }