api_auth_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "github.com/syncthing/syncthing/lib/config"
  10. )
  11. var guiCfg config.GUIConfiguration
  12. func init() {
  13. guiCfg.User = "user"
  14. guiCfg.SetPassword("pass")
  15. }
  16. func TestStaticAuthOK(t *testing.T) {
  17. t.Parallel()
  18. ok := authStatic("user", "pass", guiCfg)
  19. if !ok {
  20. t.Fatalf("should pass auth")
  21. }
  22. }
  23. func TestSimpleAuthUsernameFail(t *testing.T) {
  24. t.Parallel()
  25. ok := authStatic("userWRONG", "pass", guiCfg)
  26. if ok {
  27. t.Fatalf("should fail auth")
  28. }
  29. }
  30. func TestStaticAuthPasswordFail(t *testing.T) {
  31. t.Parallel()
  32. ok := authStatic("user", "passWRONG", guiCfg)
  33. if ok {
  34. t.Fatalf("should fail auth")
  35. }
  36. }
  37. func TestFormatOptionalPercentS(t *testing.T) {
  38. t.Parallel()
  39. cases := []struct {
  40. template string
  41. username string
  42. expected string
  43. }{
  44. {"cn=%s,dc=some,dc=example,dc=com", "username", "cn=username,dc=some,dc=example,dc=com"},
  45. {"cn=fixedusername,dc=some,dc=example,dc=com", "username", "cn=fixedusername,dc=some,dc=example,dc=com"},
  46. {"cn=%%s,dc=%s,dc=example,dc=com", "username", "cn=%s,dc=username,dc=example,dc=com"},
  47. {"cn=%%s,dc=%%s,dc=example,dc=com", "username", "cn=%s,dc=%s,dc=example,dc=com"},
  48. {"cn=%s,dc=%s,dc=example,dc=com", "username", "cn=username,dc=username,dc=example,dc=com"},
  49. }
  50. for _, c := range cases {
  51. templatedDn := formatOptionalPercentS(c.template, c.username)
  52. if c.expected != templatedDn {
  53. t.Fatalf("result should be %s != %s", c.expected, templatedDn)
  54. }
  55. }
  56. }
  57. func TestEscapeForLDAPFilter(t *testing.T) {
  58. t.Parallel()
  59. cases := []struct {
  60. in string
  61. out string
  62. }{
  63. {"username", `username`},
  64. {"user(name", `user\28name`},
  65. {"user)name", `user\29name`},
  66. {"user\\name", `user\5Cname`},
  67. {"user*name", `user\2Aname`},
  68. {"*,CN=asdf", `\2A,CN=asdf`},
  69. }
  70. for _, c := range cases {
  71. res := escapeForLDAPFilter(c.in)
  72. if c.out != res {
  73. t.Fatalf("result should be %s != %s", c.out, res)
  74. }
  75. }
  76. }
  77. func TestEscapeForLDAPDN(t *testing.T) {
  78. t.Parallel()
  79. cases := []struct {
  80. in string
  81. out string
  82. }{
  83. {"username", `username`},
  84. {"* ,CN=asdf", `*\20\2CCN\3Dasdf`},
  85. }
  86. for _, c := range cases {
  87. res := escapeForLDAPDN(c.in)
  88. if c.out != res {
  89. t.Fatalf("result should be %s != %s", c.out, res)
  90. }
  91. }
  92. }