2014-11-03 18:30:56 -08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
2016-11-07 20:44:22 +01:00
|
|
|
"errors"
|
2014-11-04 08:33:05 -08:00
|
|
|
"fmt"
|
2017-04-06 10:55:06 +02:00
|
|
|
"strings"
|
2014-11-03 18:56:33 -08:00
|
|
|
"testing"
|
2014-11-06 12:28:04 -08:00
|
|
|
|
2016-11-07 20:44:22 +01:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-11-03 18:30:56 -08:00
|
|
|
)
|
|
|
|
|
2016-09-26 10:13:37 +02:00
|
|
|
var _ gobot.Adaptor = (*Adaptor)(nil)
|
2016-07-13 11:01:36 -06:00
|
|
|
|
2016-09-26 10:13:37 +02:00
|
|
|
func initTestMqttAdaptor() *Adaptor {
|
2017-04-19 10:46:28 +02:00
|
|
|
return NewAdaptor("tcp://localhost:1883", "client")
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
2017-04-06 10:55:06 +02:00
|
|
|
func TestMqttAdaptorName(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(a.Name(), "MQTT"), true)
|
|
|
|
a.SetName("NewName")
|
|
|
|
gobottest.Assert(t, a.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
2017-04-19 10:30:30 +02:00
|
|
|
func TestMqttAdaptorPort(t *testing.T) {
|
2014-11-03 18:56:33 -08:00
|
|
|
a := initTestMqttAdaptor()
|
2017-04-19 10:46:28 +02:00
|
|
|
gobottest.Assert(t, a.Port(), "tcp://localhost:1883")
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorAutoReconnect(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, a.AutoReconnect(), false)
|
|
|
|
a.SetAutoReconnect(true)
|
|
|
|
gobottest.Assert(t, a.AutoReconnect(), true)
|
|
|
|
}
|
|
|
|
|
2017-09-24 13:45:47 +02:00
|
|
|
func TestMqttAdaptorCleanSession(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, a.CleanSession(), true)
|
|
|
|
a.SetCleanSession(false)
|
|
|
|
gobottest.Assert(t, a.CleanSession(), false)
|
|
|
|
}
|
|
|
|
|
2017-04-19 10:30:30 +02:00
|
|
|
func TestMqttAdaptorUseSSL(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, a.UseSSL(), false)
|
|
|
|
a.SetUseSSL(true)
|
|
|
|
gobottest.Assert(t, a.UseSSL(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseServerCert(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, a.ServerCert(), "")
|
|
|
|
a.SetServerCert("/path/to/server.cert")
|
|
|
|
gobottest.Assert(t, a.ServerCert(), "/path/to/server.cert")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseClientCert(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, a.ClientCert(), "")
|
|
|
|
a.SetClientCert("/path/to/client.cert")
|
|
|
|
gobottest.Assert(t, a.ClientCert(), "/path/to/client.cert")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorUseClientKey(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
gobottest.Assert(t, a.ClientKey(), "")
|
|
|
|
a.SetClientKey("/path/to/client.key")
|
|
|
|
gobottest.Assert(t, a.ClientKey(), "/path/to/client.key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectError(t *testing.T) {
|
2019-05-21 14:29:06 +02:00
|
|
|
a := NewAdaptor("tcp://localhost:1884", "client")
|
2017-04-19 10:46:28 +02:00
|
|
|
|
2017-05-02 20:34:46 +02:00
|
|
|
err := a.Connect()
|
2017-05-02 20:39:49 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "connection refused"), true)
|
2017-04-19 10:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectSSLError(t *testing.T) {
|
2019-05-21 14:29:06 +02:00
|
|
|
a := NewAdaptor("tcp://localhost:1884", "client")
|
2017-04-19 10:46:28 +02:00
|
|
|
a.SetUseSSL(true)
|
2017-05-02 20:34:46 +02:00
|
|
|
err := a.Connect()
|
2017-05-02 20:39:49 +02:00
|
|
|
gobottest.Assert(t, strings.Contains(err.Error(), "connection refused"), true)
|
2017-04-19 10:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorConnectWithAuthError(t *testing.T) {
|
2019-05-21 14:29:06 +02:00
|
|
|
a := NewAdaptorWithAuth("xyz://localhost:1883", "client", "user", "pass")
|
2016-11-07 20:44:22 +01:00
|
|
|
var expected error
|
2022-10-19 19:32:56 +02:00
|
|
|
expected = multierror.Append(expected, errors.New("network Error : unknown protocol"))
|
2016-11-07 20:44:22 +01:00
|
|
|
|
|
|
|
gobottest.Assert(t, a.Connect(), expected)
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorFinalize(t *testing.T) {
|
2014-11-03 18:56:33 -08:00
|
|
|
a := initTestMqttAdaptor()
|
2016-11-07 19:52:28 +01:00
|
|
|
gobottest.Assert(t, a.Finalize(), nil)
|
2014-11-03 18:30:56 -08:00
|
|
|
}
|
2014-11-04 08:33:05 -08:00
|
|
|
|
|
|
|
func TestMqttAdaptorCannotPublishUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
data := []byte("o")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.Publish("test", data), false)
|
2014-11-04 08:33:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorPublishWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.Connect()
|
|
|
|
data := []byte("o")
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, a.Publish("test", data), true)
|
2014-11-04 08:33:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorCannotOnUnlessConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
2017-04-18 19:49:17 +02:00
|
|
|
gobottest.Assert(t, a.On("hola", func(msg Message) {
|
2014-11-04 08:33:05 -08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMqttAdaptorOnWhenConnected(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.Connect()
|
2017-04-18 19:49:17 +02:00
|
|
|
gobottest.Assert(t, a.On("hola", func(msg Message) {
|
2014-11-04 08:33:05 -08:00
|
|
|
fmt.Println("hola")
|
|
|
|
}), true)
|
|
|
|
}
|
2018-12-17 11:06:34 -06:00
|
|
|
|
|
|
|
func TestMqttAdaptorQoS(t *testing.T) {
|
|
|
|
a := initTestMqttAdaptor()
|
|
|
|
a.SetQoS(1)
|
|
|
|
gobottest.Assert(t, 1, a.qos)
|
|
|
|
}
|