123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package connection
- import (
- "reflect"
- "testing"
- )
- func TestMQTTClient_CfgValidate(t *testing.T) {
- type args struct {
- props map[string]interface{}
- }
- tests := []struct {
- name string
- args args
- wantErr bool
- }{
- {
- name: "config pass",
- args: args{
- props: map[string]interface{}{
- "servers": []string{"tcp:127.0.0.1"},
- },
- },
- wantErr: false,
- },
- {
- name: "config are not case sensitive",
- args: args{
- props: map[string]interface{}{
- "SERVERS": []string{"tcp:127.0.0.1"},
- },
- },
- wantErr: false,
- },
- {
- name: "config server addr key error",
- args: args{
- props: map[string]interface{}{
- "server": []string{"tcp:127.0.0.1"},
- },
- },
- wantErr: true,
- },
- {
- name: "config have unwanted topic fields",
- args: args{
- props: map[string]interface{}{
- "servers": []string{"tcp:127.0.0.1"},
- "topic": "demo",
- },
- },
- wantErr: true,
- },
- {
- name: "config no server addr",
- args: args{
- props: map[string]interface{}{
- "username": "user1",
- },
- },
- wantErr: true,
- },
- {
- name: "config no server addr",
- args: args{
- props: map[string]interface{}{
- "servers": []string{},
- },
- },
- wantErr: true,
- },
- {
- name: "config miss cert key file",
- args: args{
- props: map[string]interface{}{
- "servers": []string{"tcp:127.0.0.1"},
- "certificationPath": "./not_exist.crt",
- "privateKeyPath": "./not_exist.key",
- },
- },
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- ms := &MQTTClient{
- selector: &ConSelector{
- ConnSelectorCfg: "testSelector",
- },
- }
- err := ms.CfgValidate(tt.args.props)
- if (err != nil) != tt.wantErr {
- t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
- }
- })
- }
- }
- func TestMQTTClient_CfgResult(t *testing.T) {
- props := map[string]interface{}{
- "servers": []string{"tcp:127.0.0.1:1883"},
- "USERNAME": "demo",
- "Password": "password",
- "clientID": "clientid",
- }
- ms := &MQTTClient{
- selector: &ConSelector{
- ConnSelectorCfg: "testSelector",
- },
- }
- _ = ms.CfgValidate(props)
- if !reflect.DeepEqual("tcp:127.0.0.1:1883", ms.srv) {
- t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.srv)
- }
- if !reflect.DeepEqual("demo", ms.uName) {
- t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.uName)
- }
- if !reflect.DeepEqual("password", ms.password) {
- t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.password)
- }
- if !reflect.DeepEqual("clientid", ms.clientid) {
- t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.clientid)
- }
- if !reflect.DeepEqual(uint(3), ms.pVersion) {
- t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.pVersion)
- }
- }
|