-
Notifications
You must be signed in to change notification settings - Fork 534
/
unit_options_test.go
130 lines (105 loc) · 2.83 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
/*
* Copyright (c) 2021 IBM Corp and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Seth Hoenig
* Allan Stockdill-Mander
* Mike Robertson
*/
package mqtt
import (
"crypto/tls"
"crypto/x509"
"testing"
"time"
)
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.KeepAlive != 30 {
t.Fatalf("bad default timeout")
}
}
func Test_NewClientOptions_mix(t *testing.T) {
o := NewClientOptions()
o.AddBroker("tcp://192.168.1.2:9999")
o.SetClientID("myclientid")
o.SetUsername("myuser")
o.SetPassword("mypassword")
o.SetKeepAlive(88 * time.Second)
if o.Servers[0].Scheme != "tcp" {
t.Fatalf("bad scheme")
}
if o.Servers[0].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.KeepAlive != 88 {
t.Fatalf("bad set timeout: %d", o.KeepAlive)
}
}
func Test_ModifyOptions(t *testing.T) {
o := NewClientOptions()
o.AddBroker("tcp://3.3.3.3:12345")
c := NewClient(o).(*client)
o.AddBroker("ws://2.2.2.2:9999")
o.SetOrderMatters(false)
if c.options.Servers[0].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).(*client)
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 Client, err error) {
panic(err)
}
o := NewClientOptions().SetConnectionLostHandler(onconnlost)
c := NewClient(o).(*client)
if c.options.OnConnectionLost == nil {
t.Fatalf("client options.onconnlost was nil")
}
}