server_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 v1
  15. import (
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "github.com/samber/lo"
  20. "github.com/stretchr/testify/require"
  21. )
  22. func TestServerConfigComplete(t *testing.T) {
  23. require := require.New(t)
  24. c := &ServerConfig{}
  25. err := c.Complete()
  26. require.NoError(err)
  27. require.EqualValues("token", c.Auth.Method)
  28. require.Equal(true, lo.FromPtr(c.Transport.TCPMux))
  29. require.Equal(true, lo.FromPtr(c.DetailedErrorsToClient))
  30. }
  31. func TestAuthServerConfig_Complete(t *testing.T) {
  32. // Create a temporary file for testing
  33. tmpDir := t.TempDir()
  34. testFile := filepath.Join(tmpDir, "test_token")
  35. testContent := "file-token-value"
  36. err := os.WriteFile(testFile, []byte(testContent), 0o600)
  37. require.NoError(t, err)
  38. tests := []struct {
  39. name string
  40. config AuthServerConfig
  41. expectToken string
  42. expectPanic bool
  43. }{
  44. {
  45. name: "tokenSource resolved to token",
  46. config: AuthServerConfig{
  47. Method: AuthMethodToken,
  48. TokenSource: &ValueSource{
  49. Type: "file",
  50. File: &FileSource{
  51. Path: testFile,
  52. },
  53. },
  54. },
  55. expectToken: testContent,
  56. expectPanic: false,
  57. },
  58. {
  59. name: "direct token unchanged",
  60. config: AuthServerConfig{
  61. Method: AuthMethodToken,
  62. Token: "direct-token",
  63. },
  64. expectToken: "direct-token",
  65. expectPanic: false,
  66. },
  67. {
  68. name: "invalid tokenSource should panic",
  69. config: AuthServerConfig{
  70. Method: AuthMethodToken,
  71. TokenSource: &ValueSource{
  72. Type: "file",
  73. File: &FileSource{
  74. Path: "/non/existent/file",
  75. },
  76. },
  77. },
  78. expectPanic: true,
  79. },
  80. }
  81. for _, tt := range tests {
  82. t.Run(tt.name, func(t *testing.T) {
  83. if tt.expectPanic {
  84. err := tt.config.Complete()
  85. require.Error(t, err)
  86. } else {
  87. err := tt.config.Complete()
  88. require.NoError(t, err)
  89. require.Equal(t, tt.expectToken, tt.config.Token)
  90. require.Nil(t, tt.config.TokenSource, "TokenSource should be cleared after resolution")
  91. }
  92. })
  93. }
  94. }