api_auth_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package api
  7. import (
  8. "testing"
  9. "time"
  10. "github.com/syncthing/syncthing/internal/db"
  11. "github.com/syncthing/syncthing/internal/db/sqlite"
  12. "github.com/syncthing/syncthing/lib/config"
  13. )
  14. var guiCfg config.GUIConfiguration
  15. func init() {
  16. guiCfg.User = "user"
  17. guiCfg.SetPassword("pass")
  18. }
  19. func TestStaticAuthOK(t *testing.T) {
  20. t.Parallel()
  21. ok := authStatic("user", "pass", guiCfg)
  22. if !ok {
  23. t.Fatalf("should pass auth")
  24. }
  25. }
  26. func TestSimpleAuthUsernameFail(t *testing.T) {
  27. t.Parallel()
  28. ok := authStatic("userWRONG", "pass", guiCfg)
  29. if ok {
  30. t.Fatalf("should fail auth")
  31. }
  32. }
  33. func TestStaticAuthPasswordFail(t *testing.T) {
  34. t.Parallel()
  35. ok := authStatic("user", "passWRONG", guiCfg)
  36. if ok {
  37. t.Fatalf("should fail auth")
  38. }
  39. }
  40. func TestFormatOptionalPercentS(t *testing.T) {
  41. t.Parallel()
  42. cases := []struct {
  43. template string
  44. username string
  45. expected string
  46. }{
  47. {"cn=%s,dc=some,dc=example,dc=com", "username", "cn=username,dc=some,dc=example,dc=com"},
  48. {"cn=fixedusername,dc=some,dc=example,dc=com", "username", "cn=fixedusername,dc=some,dc=example,dc=com"},
  49. {"cn=%%s,dc=%s,dc=example,dc=com", "username", "cn=%s,dc=username,dc=example,dc=com"},
  50. {"cn=%%s,dc=%%s,dc=example,dc=com", "username", "cn=%s,dc=%s,dc=example,dc=com"},
  51. {"cn=%s,dc=%s,dc=example,dc=com", "username", "cn=username,dc=username,dc=example,dc=com"},
  52. }
  53. for _, c := range cases {
  54. templatedDn := formatOptionalPercentS(c.template, c.username)
  55. if c.expected != templatedDn {
  56. t.Fatalf("result should be %s != %s", c.expected, templatedDn)
  57. }
  58. }
  59. }
  60. func TestEscapeForLDAPFilter(t *testing.T) {
  61. t.Parallel()
  62. cases := []struct {
  63. in string
  64. out string
  65. }{
  66. {"username", `username`},
  67. {"user(name", `user\28name`},
  68. {"user)name", `user\29name`},
  69. {"user\\name", `user\5Cname`},
  70. {"user*name", `user\2Aname`},
  71. {"*,CN=asdf", `\2A,CN=asdf`},
  72. }
  73. for _, c := range cases {
  74. res := escapeForLDAPFilter(c.in)
  75. if c.out != res {
  76. t.Fatalf("result should be %s != %s", c.out, res)
  77. }
  78. }
  79. }
  80. func TestEscapeForLDAPDN(t *testing.T) {
  81. t.Parallel()
  82. cases := []struct {
  83. in string
  84. out string
  85. }{
  86. {"username", `username`},
  87. {"* ,CN=asdf", `*\20\2CCN\3Dasdf`},
  88. }
  89. for _, c := range cases {
  90. res := escapeForLDAPDN(c.in)
  91. if c.out != res {
  92. t.Fatalf("result should be %s != %s", c.out, res)
  93. }
  94. }
  95. }
  96. type mockClock struct {
  97. now time.Time
  98. }
  99. func (c *mockClock) Now() time.Time {
  100. c.now = c.now.Add(1) // time always ticks by at least 1 ns
  101. return c.now
  102. }
  103. func (c *mockClock) wind(t time.Duration) {
  104. c.now = c.now.Add(t)
  105. }
  106. func TestTokenManager(t *testing.T) {
  107. t.Parallel()
  108. mdb, err := sqlite.OpenTemp()
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. t.Cleanup(func() {
  113. mdb.Close()
  114. })
  115. kdb := db.NewMiscDB(mdb)
  116. clock := &mockClock{now: time.Now()}
  117. // Token manager keeps up to three tokens with a validity time of 24 hours.
  118. tm := newTokenManager("testTokens", kdb, 24*time.Hour, 3)
  119. tm.timeNow = clock.Now
  120. // Create three tokens
  121. t0 := tm.New()
  122. t1 := tm.New()
  123. t2 := tm.New()
  124. // Check that the tokens are valid
  125. if !tm.Check(t0) {
  126. t.Errorf("token %q should be valid", t0)
  127. }
  128. if !tm.Check(t1) {
  129. t.Errorf("token %q should be valid", t1)
  130. }
  131. if !tm.Check(t2) {
  132. t.Errorf("token %q should be valid", t2)
  133. }
  134. // Create a fourth token
  135. t3 := tm.New()
  136. // It should be valid
  137. if !tm.Check(t3) {
  138. t.Errorf("token %q should be valid", t3)
  139. }
  140. // But the first token should have been removed
  141. if tm.Check(t0) {
  142. t.Errorf("token %q should be invalid", t0)
  143. }
  144. // Wind the clock by 12 hours
  145. clock.wind(12 * time.Hour)
  146. // The second token should still be valid (and checking it will give it more life)
  147. if !tm.Check(t1) {
  148. t.Errorf("token %q should be valid", t1)
  149. }
  150. // Wind the clock by 12 hours
  151. clock.wind(12 * time.Hour)
  152. // The second token should still be valid
  153. if !tm.Check(t1) {
  154. t.Errorf("token %q should be valid", t1)
  155. }
  156. // But the third and fourth tokens should have expired
  157. if tm.Check(t2) {
  158. t.Errorf("token %q should be invalid", t2)
  159. }
  160. if tm.Check(t3) {
  161. t.Errorf("token %q should be invalid", t3)
  162. }
  163. }