client_mqtt_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package connection
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestMQTTClient_CfgValidate(t *testing.T) {
  7. type args struct {
  8. props map[string]interface{}
  9. }
  10. tests := []struct {
  11. name string
  12. args args
  13. wantErr bool
  14. }{
  15. {
  16. name: "config pass",
  17. args: args{
  18. props: map[string]interface{}{
  19. "servers": []string{"tcp:127.0.0.1"},
  20. },
  21. },
  22. wantErr: false,
  23. },
  24. {
  25. name: "config are not case sensitive",
  26. args: args{
  27. props: map[string]interface{}{
  28. "SERVERS": []string{"tcp:127.0.0.1"},
  29. },
  30. },
  31. wantErr: false,
  32. },
  33. {
  34. name: "config server addr key error",
  35. args: args{
  36. props: map[string]interface{}{
  37. "server": []string{"tcp:127.0.0.1"},
  38. },
  39. },
  40. wantErr: true,
  41. },
  42. {
  43. name: "config have unwanted topic fields",
  44. args: args{
  45. props: map[string]interface{}{
  46. "servers": []string{"tcp:127.0.0.1"},
  47. "topic": "demo",
  48. },
  49. },
  50. wantErr: true,
  51. },
  52. {
  53. name: "config no server addr",
  54. args: args{
  55. props: map[string]interface{}{
  56. "username": "user1",
  57. },
  58. },
  59. wantErr: true,
  60. },
  61. {
  62. name: "config no server addr",
  63. args: args{
  64. props: map[string]interface{}{
  65. "servers": []string{},
  66. },
  67. },
  68. wantErr: true,
  69. },
  70. {
  71. name: "config miss cert key file",
  72. args: args{
  73. props: map[string]interface{}{
  74. "servers": []string{"tcp:127.0.0.1"},
  75. "certificationPath": "./not_exist.crt",
  76. "privateKeyPath": "./not_exist.key",
  77. },
  78. },
  79. wantErr: true,
  80. },
  81. }
  82. for _, tt := range tests {
  83. t.Run(tt.name, func(t *testing.T) {
  84. ms := &MQTTClient{
  85. selector: &ConSelector{
  86. ConnSelectorCfg: "testSelector",
  87. },
  88. }
  89. err := ms.CfgValidate(tt.args.props)
  90. if (err != nil) != tt.wantErr {
  91. t.Errorf("CfgValidate() error = %v, wantErr %v", err, tt.wantErr)
  92. }
  93. })
  94. }
  95. }
  96. func TestMQTTClient_CfgResult(t *testing.T) {
  97. props := map[string]interface{}{
  98. "servers": []string{"tcp:127.0.0.1:1883"},
  99. "USERNAME": "demo",
  100. "Password": "password",
  101. "clientID": "clientid",
  102. }
  103. ms := &MQTTClient{
  104. selector: &ConSelector{
  105. ConnSelectorCfg: "testSelector",
  106. },
  107. }
  108. _ = ms.CfgValidate(props)
  109. if !reflect.DeepEqual("tcp:127.0.0.1:1883", ms.srv) {
  110. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.srv)
  111. }
  112. if !reflect.DeepEqual("demo", ms.uName) {
  113. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.uName)
  114. }
  115. if !reflect.DeepEqual("password", ms.password) {
  116. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.password)
  117. }
  118. if !reflect.DeepEqual("clientid", ms.clientid) {
  119. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.clientid)
  120. }
  121. if !reflect.DeepEqual(uint(3), ms.pVersion) {
  122. t.Errorf("result mismatch:\n\n got=%#v\n\n", ms.pVersion)
  123. }
  124. }