conf_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 conf
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestSourceConfValidate(t *testing.T) {
  21. var tests = []struct {
  22. s *SourceConf
  23. e *SourceConf
  24. err string
  25. }{
  26. {
  27. s: &SourceConf{},
  28. e: &SourceConf{
  29. HttpServerIp: "0.0.0.0",
  30. HttpServerPort: 10081,
  31. },
  32. err: "invalidHttpServerPort:httpServerPort must between 0 and 65535",
  33. }, {
  34. s: &SourceConf{
  35. HttpServerIp: "192.168.0.1",
  36. },
  37. e: &SourceConf{
  38. HttpServerIp: "192.168.0.1",
  39. HttpServerPort: 10081,
  40. },
  41. err: "invalidHttpServerPort:httpServerPort must between 0 and 65535",
  42. }, {
  43. s: &SourceConf{
  44. HttpServerPort: 99999,
  45. },
  46. e: &SourceConf{
  47. HttpServerIp: "0.0.0.0",
  48. HttpServerPort: 10081,
  49. },
  50. err: "invalidHttpServerPort:httpServerPort must between 0 and 65535",
  51. }, {
  52. s: &SourceConf{
  53. HttpServerPort: 9090,
  54. HttpServerTls: &tlsConf{
  55. Certfile: "certfile",
  56. Keyfile: "keyfile",
  57. },
  58. },
  59. e: &SourceConf{
  60. HttpServerIp: "0.0.0.0",
  61. HttpServerPort: 9090,
  62. HttpServerTls: &tlsConf{
  63. Certfile: "certfile",
  64. Keyfile: "keyfile",
  65. },
  66. },
  67. },
  68. }
  69. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  70. for i, tt := range tests {
  71. err := tt.s.Validate()
  72. if err != nil && tt.err != err.Error() {
  73. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.err, err)
  74. }
  75. if !reflect.DeepEqual(tt.s, tt.e) {
  76. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.e)
  77. }
  78. }
  79. }