client_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TestClientConfigComplete(t *testing.T) {
  23. require := require.New(t)
  24. c := &ClientConfig{}
  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.LoginFailExit))
  30. require.Equal(true, lo.FromPtr(c.Transport.TLS.Enable))
  31. require.Equal(true, lo.FromPtr(c.Transport.TLS.DisableCustomTLSFirstByte))
  32. require.NotEmpty(c.NatHoleSTUNServer)
  33. }
  34. func TestAuthClientConfig_Complete(t *testing.T) {
  35. // Create a temporary file for testing
  36. tmpDir := t.TempDir()
  37. testFile := filepath.Join(tmpDir, "test_token")
  38. testContent := "client-token-value"
  39. err := os.WriteFile(testFile, []byte(testContent), 0o600)
  40. require.NoError(t, err)
  41. tests := []struct {
  42. name string
  43. config AuthClientConfig
  44. expectToken string
  45. expectPanic bool
  46. }{
  47. {
  48. name: "tokenSource resolved to token",
  49. config: AuthClientConfig{
  50. Method: AuthMethodToken,
  51. TokenSource: &ValueSource{
  52. Type: "file",
  53. File: &FileSource{
  54. Path: testFile,
  55. },
  56. },
  57. },
  58. expectToken: testContent,
  59. expectPanic: false,
  60. },
  61. {
  62. name: "direct token unchanged",
  63. config: AuthClientConfig{
  64. Method: AuthMethodToken,
  65. Token: "direct-token",
  66. },
  67. expectToken: "direct-token",
  68. expectPanic: false,
  69. },
  70. {
  71. name: "invalid tokenSource should panic",
  72. config: AuthClientConfig{
  73. Method: AuthMethodToken,
  74. TokenSource: &ValueSource{
  75. Type: "file",
  76. File: &FileSource{
  77. Path: "/non/existent/file",
  78. },
  79. },
  80. },
  81. expectPanic: true,
  82. },
  83. }
  84. for _, tt := range tests {
  85. t.Run(tt.name, func(t *testing.T) {
  86. if tt.expectPanic {
  87. err := tt.config.Complete()
  88. require.Error(t, err)
  89. } else {
  90. err := tt.config.Complete()
  91. require.NoError(t, err)
  92. require.Equal(t, tt.expectToken, tt.config.Token)
  93. require.Nil(t, tt.config.TokenSource, "TokenSource should be cleared after resolution")
  94. }
  95. })
  96. }
  97. }