load_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2023 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. "fmt"
  17. "strings"
  18. "testing"
  19. "github.com/stretchr/testify/require"
  20. v1 "github.com/fatedier/frp/pkg/config/v1"
  21. )
  22. const tomlServerContent = `
  23. bindAddr = "127.0.0.1"
  24. kcpBindPort = 7000
  25. quicBindPort = 7001
  26. tcpmuxHTTPConnectPort = 7005
  27. custom404Page = "/abc.html"
  28. transport.tcpKeepalive = 10
  29. `
  30. const yamlServerContent = `
  31. bindAddr: 127.0.0.1
  32. kcpBindPort: 7000
  33. quicBindPort: 7001
  34. tcpmuxHTTPConnectPort: 7005
  35. custom404Page: /abc.html
  36. transport:
  37. tcpKeepalive: 10
  38. `
  39. const jsonServerContent = `
  40. {
  41. "bindAddr": "127.0.0.1",
  42. "kcpBindPort": 7000,
  43. "quicBindPort": 7001,
  44. "tcpmuxHTTPConnectPort": 7005,
  45. "custom404Page": "/abc.html",
  46. "transport": {
  47. "tcpKeepalive": 10
  48. }
  49. }
  50. `
  51. func TestLoadServerConfig(t *testing.T) {
  52. tests := []struct {
  53. name string
  54. content string
  55. }{
  56. {"toml", tomlServerContent},
  57. {"yaml", yamlServerContent},
  58. {"json", jsonServerContent},
  59. }
  60. for _, test := range tests {
  61. t.Run(test.name, func(t *testing.T) {
  62. require := require.New(t)
  63. svrCfg := v1.ServerConfig{}
  64. err := LoadConfigure([]byte(test.content), &svrCfg, true)
  65. require.NoError(err)
  66. require.EqualValues("127.0.0.1", svrCfg.BindAddr)
  67. require.EqualValues(7000, svrCfg.KCPBindPort)
  68. require.EqualValues(7001, svrCfg.QUICBindPort)
  69. require.EqualValues(7005, svrCfg.TCPMuxHTTPConnectPort)
  70. require.EqualValues("/abc.html", svrCfg.Custom404Page)
  71. require.EqualValues(10, svrCfg.Transport.TCPKeepAlive)
  72. })
  73. }
  74. }
  75. // Test that loading in strict mode fails when the config is invalid.
  76. func TestLoadServerConfigStrictMode(t *testing.T) {
  77. tests := []struct {
  78. name string
  79. content string
  80. }{
  81. {"toml", tomlServerContent},
  82. {"yaml", yamlServerContent},
  83. {"json", jsonServerContent},
  84. }
  85. for _, strict := range []bool{false, true} {
  86. for _, test := range tests {
  87. t.Run(fmt.Sprintf("%s-strict-%t", test.name, strict), func(t *testing.T) {
  88. require := require.New(t)
  89. // Break the content with an innocent typo
  90. brokenContent := strings.Replace(test.content, "bindAddr", "bindAdur", 1)
  91. svrCfg := v1.ServerConfig{}
  92. err := LoadConfigure([]byte(brokenContent), &svrCfg, strict)
  93. if strict {
  94. require.ErrorContains(err, "bindAdur")
  95. } else {
  96. require.NoError(err)
  97. // BindAddr didn't get parsed because of the typo.
  98. require.EqualValues("", svrCfg.BindAddr)
  99. }
  100. })
  101. }
  102. }
  103. }
  104. func TestCustomStructStrictMode(t *testing.T) {
  105. require := require.New(t)
  106. proxyStr := `
  107. serverPort = 7000
  108. [[proxies]]
  109. name = "test"
  110. type = "tcp"
  111. remotePort = 6000
  112. `
  113. clientCfg := v1.ClientConfig{}
  114. err := LoadConfigure([]byte(proxyStr), &clientCfg, true)
  115. require.NoError(err)
  116. proxyStr += `unknown = "unknown"`
  117. err = LoadConfigure([]byte(proxyStr), &clientCfg, true)
  118. require.Error(err)
  119. visitorStr := `
  120. serverPort = 7000
  121. [[visitors]]
  122. name = "test"
  123. type = "stcp"
  124. bindPort = 6000
  125. serverName = "server"
  126. `
  127. err = LoadConfigure([]byte(visitorStr), &clientCfg, true)
  128. require.NoError(err)
  129. visitorStr += `unknown = "unknown"`
  130. err = LoadConfigure([]byte(visitorStr), &clientCfg, true)
  131. require.Error(err)
  132. pluginStr := `
  133. serverPort = 7000
  134. [[proxies]]
  135. name = "test"
  136. type = "tcp"
  137. remotePort = 6000
  138. [proxies.plugin]
  139. type = "unix_domain_socket"
  140. unixPath = "/tmp/uds.sock"
  141. `
  142. err = LoadConfigure([]byte(pluginStr), &clientCfg, true)
  143. require.NoError(err)
  144. pluginStr += `unknown = "unknown"`
  145. err = LoadConfigure([]byte(pluginStr), &clientCfg, true)
  146. require.Error(err)
  147. }