visitor_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2020 The frp Authors
  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 config
  15. import (
  16. "testing"
  17. "github.com/fatedier/frp/pkg/consts"
  18. "github.com/stretchr/testify/assert"
  19. "gopkg.in/ini.v1"
  20. )
  21. const testVisitorPrefix = "test."
  22. func Test_Visitor_Interface(t *testing.T) {
  23. for name := range visitorConfTypeMap {
  24. DefaultVisitorConf(name)
  25. }
  26. }
  27. func Test_Visitor_UnmarshalFromIni(t *testing.T) {
  28. assert := assert.New(t)
  29. testcases := []struct {
  30. sname string
  31. source []byte
  32. expected VisitorConf
  33. }{
  34. {
  35. sname: "secret_tcp_visitor",
  36. source: []byte(`
  37. [secret_tcp_visitor]
  38. role = visitor
  39. type = stcp
  40. server_name = secret_tcp
  41. sk = abcdefg
  42. bind_addr = 127.0.0.1
  43. bind_port = 9000
  44. use_encryption = false
  45. use_compression = false
  46. `),
  47. expected: &STCPVisitorConf{
  48. BaseVisitorConf: BaseVisitorConf{
  49. ProxyName: testVisitorPrefix + "secret_tcp_visitor",
  50. ProxyType: consts.STCPProxy,
  51. Role: "visitor",
  52. Sk: "abcdefg",
  53. ServerName: testVisitorPrefix + "secret_tcp",
  54. BindAddr: "127.0.0.1",
  55. BindPort: 9000,
  56. },
  57. },
  58. },
  59. {
  60. sname: "p2p_tcp_visitor",
  61. source: []byte(`
  62. [p2p_tcp_visitor]
  63. role = visitor
  64. type = xtcp
  65. server_name = p2p_tcp
  66. sk = abcdefg
  67. bind_addr = 127.0.0.1
  68. bind_port = 9001
  69. use_encryption = false
  70. use_compression = false
  71. `),
  72. expected: &XTCPVisitorConf{
  73. BaseVisitorConf: BaseVisitorConf{
  74. ProxyName: testVisitorPrefix + "p2p_tcp_visitor",
  75. ProxyType: consts.XTCPProxy,
  76. Role: "visitor",
  77. Sk: "abcdefg",
  78. ServerName: testProxyPrefix + "p2p_tcp",
  79. BindAddr: "127.0.0.1",
  80. BindPort: 9001,
  81. },
  82. },
  83. },
  84. }
  85. for _, c := range testcases {
  86. f, err := ini.LoadSources(testLoadOptions, c.source)
  87. assert.NoError(err)
  88. visitorType := f.Section(c.sname).Key("type").String()
  89. assert.NotEmpty(visitorType)
  90. actual := DefaultVisitorConf(visitorType)
  91. assert.NotNil(actual)
  92. err = actual.UnmarshalFromIni(testVisitorPrefix, c.sname, f.Section(c.sname))
  93. assert.NoError(err)
  94. assert.Equal(c.expected, actual)
  95. }
  96. }