client_mqtt_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mqtt
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestMQTTClient_CfgValidate(t *testing.T) {
  20. type args struct {
  21. props map[string]interface{}
  22. }
  23. tests := []struct {
  24. name string
  25. args args
  26. wantErr bool
  27. }{
  28. {
  29. name: "config pass",
  30. args: args{
  31. props: map[string]interface{}{
  32. "server": "tcp:127.0.0.1",
  33. },
  34. },
  35. wantErr: false,
  36. },
  37. {
  38. name: "config are not case sensitive",
  39. args: args{
  40. props: map[string]interface{}{
  41. "SERVER": "tcp:127.0.0.1",
  42. },
  43. },
  44. wantErr: false,
  45. },
  46. {
  47. name: "config server addr key error",
  48. args: args{
  49. props: map[string]interface{}{
  50. "server": "tcp:127.0.0.1",
  51. },
  52. },
  53. wantErr: false,
  54. },
  55. {
  56. name: "config have unwanted topic fields",
  57. args: args{
  58. props: map[string]interface{}{
  59. "server": "tcp:127.0.0.1",
  60. "topic": "demo",
  61. },
  62. },
  63. wantErr: false,
  64. },
  65. {
  66. name: "config no server addr",
  67. args: args{
  68. props: map[string]interface{}{
  69. "username": "user1",
  70. },
  71. },
  72. wantErr: true,
  73. },
  74. {
  75. name: "config no server addr",
  76. args: args{
  77. props: map[string]interface{}{
  78. "server": "",
  79. },
  80. },
  81. wantErr: true,
  82. },
  83. {
  84. name: "config miss cert key file",
  85. args: args{
  86. props: map[string]interface{}{
  87. "server": "tcp:127.0.0.1",
  88. "certificationPath": "./not_exist.crt",
  89. "privateKeyPath": "./not_exist.key",
  90. },
  91. },
  92. wantErr: true,
  93. },
  94. }
  95. for _, tt := range tests {
  96. t.Run(tt.name, func(t *testing.T) {
  97. ms := &MQTTClient{}
  98. err := ms.CfgValidate(tt.args.props)
  99. if (err != nil) != tt.wantErr {
  100. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  101. }
  102. })
  103. }
  104. }
  105. func TestMQTTClient_CfgResult(t *testing.T) {
  106. props := map[string]interface{}{
  107. "server": "tcp:127.0.0.1:1883",
  108. "USERNAME": "demo",
  109. "Password": "password",
  110. "clientID": "clientid",
  111. }
  112. ms := &MQTTClient{}
  113. _ = ms.CfgValidate(props)
  114. if !reflect.DeepEqual("tcp:127.0.0.1:1883", ms.srv) {
  115. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.srv)
  116. }
  117. if !reflect.DeepEqual("demo", ms.uName) {
  118. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.uName)
  119. }
  120. if !reflect.DeepEqual("password", ms.password) {
  121. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.password)
  122. }
  123. if !reflect.DeepEqual("clientid", ms.clientid) {
  124. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.clientid)
  125. }
  126. if !reflect.DeepEqual(uint(3), ms.pVersion) {
  127. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.pVersion)
  128. }
  129. }