load_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "strings"
  17. "testing"
  18. "github.com/stretchr/testify/require"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. )
  21. const tomlServerContent = `
  22. bindAddr = "127.0.0.1"
  23. kcpBindPort = 7000
  24. quicBindPort = 7001
  25. tcpmuxHTTPConnectPort = 7005
  26. custom404Page = "/abc.html"
  27. transport.tcpKeepalive = 10
  28. `
  29. const yamlServerContent = `
  30. bindAddr: 127.0.0.1
  31. kcpBindPort: 7000
  32. quicBindPort: 7001
  33. tcpmuxHTTPConnectPort: 7005
  34. custom404Page: /abc.html
  35. transport:
  36. tcpKeepalive: 10
  37. `
  38. const jsonServerContent = `
  39. {
  40. "bindAddr": "127.0.0.1",
  41. "kcpBindPort": 7000,
  42. "quicBindPort": 7001,
  43. "tcpmuxHTTPConnectPort": 7005,
  44. "custom404Page": "/abc.html",
  45. "transport": {
  46. "tcpKeepalive": 10
  47. }
  48. }
  49. `
  50. func TestLoadServerConfig(t *testing.T) {
  51. for _, content := range []string{tomlServerContent, yamlServerContent, jsonServerContent} {
  52. svrCfg := v1.ServerConfig{}
  53. err := LoadConfigure([]byte(content), &svrCfg, true)
  54. require := require.New(t)
  55. require.NoError(err)
  56. require.EqualValues("127.0.0.1", svrCfg.BindAddr)
  57. require.EqualValues(7000, svrCfg.KCPBindPort)
  58. require.EqualValues(7001, svrCfg.QUICBindPort)
  59. require.EqualValues(7005, svrCfg.TCPMuxHTTPConnectPort)
  60. require.EqualValues("/abc.html", svrCfg.Custom404Page)
  61. require.EqualValues(10, svrCfg.Transport.TCPKeepAlive)
  62. }
  63. }
  64. // Test that loading in strict mode fails when the config is invalid.
  65. func TestLoadServerConfigErrorMode(t *testing.T) {
  66. for strict := range []bool{false, true} {
  67. for _, content := range []string{tomlServerContent, yamlServerContent, jsonServerContent} {
  68. // Break the content with an innocent typo
  69. brokenContent := strings.Replace(content, "bindAddr", "bindAdur", 1)
  70. svrCfg := v1.ServerConfig{}
  71. err := LoadConfigure([]byte(brokenContent), &svrCfg, strict == 1)
  72. require := require.New(t)
  73. if strict == 1 {
  74. require.ErrorContains(err, "bindAdur")
  75. } else {
  76. require.NoError(err)
  77. // BindAddr didn't get parsed because of the typo.
  78. require.EqualValues("", svrCfg.BindAddr)
  79. }
  80. }
  81. }
  82. }