2018-09-11 21:25:24 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
package api
|
2018-09-11 21:25:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-09-18 19:56:52 +00:00
|
|
|
|
2021-11-08 12:32:04 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2018-09-11 21:25:24 +00:00
|
|
|
)
|
|
|
|
|
2021-11-08 12:32:04 +00:00
|
|
|
var guiCfg config.GUIConfiguration
|
2018-09-18 19:56:52 +00:00
|
|
|
|
|
|
|
func init() {
|
2021-11-08 12:32:04 +00:00
|
|
|
guiCfg.User = "user"
|
2023-09-24 17:23:49 +00:00
|
|
|
guiCfg.SetPassword("pass")
|
2018-09-18 19:56:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 21:25:24 +00:00
|
|
|
func TestStaticAuthOK(t *testing.T) {
|
2019-09-05 11:35:51 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-11-08 12:32:04 +00:00
|
|
|
ok := authStatic("user", "pass", guiCfg)
|
2018-09-11 21:25:24 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should pass auth")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleAuthUsernameFail(t *testing.T) {
|
2019-09-05 11:35:51 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-11-08 12:32:04 +00:00
|
|
|
ok := authStatic("userWRONG", "pass", guiCfg)
|
2018-09-11 21:25:24 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatalf("should fail auth")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStaticAuthPasswordFail(t *testing.T) {
|
2019-09-05 11:35:51 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-11-08 12:32:04 +00:00
|
|
|
ok := authStatic("user", "passWRONG", guiCfg)
|
2018-09-11 21:25:24 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatalf("should fail auth")
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 05:52:02 +00:00
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
func TestFormatOptionalPercentS(t *testing.T) {
|
2023-05-10 05:52:02 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
cases := []struct {
|
|
|
|
template string
|
|
|
|
username string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"cn=%s,dc=some,dc=example,dc=com", "username", "cn=username,dc=some,dc=example,dc=com"},
|
|
|
|
{"cn=fixedusername,dc=some,dc=example,dc=com", "username", "cn=fixedusername,dc=some,dc=example,dc=com"},
|
|
|
|
{"cn=%%s,dc=%s,dc=example,dc=com", "username", "cn=%s,dc=username,dc=example,dc=com"},
|
|
|
|
{"cn=%%s,dc=%%s,dc=example,dc=com", "username", "cn=%s,dc=%s,dc=example,dc=com"},
|
|
|
|
{"cn=%s,dc=%s,dc=example,dc=com", "username", "cn=username,dc=username,dc=example,dc=com"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
templatedDn := formatOptionalPercentS(c.template, c.username)
|
|
|
|
if c.expected != templatedDn {
|
|
|
|
t.Fatalf("result should be %s != %s", c.expected, templatedDn)
|
|
|
|
}
|
2023-05-10 05:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
func TestEscapeForLDAPFilter(t *testing.T) {
|
2023-05-10 05:52:02 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
cases := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{"username", `username`},
|
|
|
|
{"user(name", `user\28name`},
|
|
|
|
{"user)name", `user\29name`},
|
|
|
|
{"user\\name", `user\5Cname`},
|
|
|
|
{"user*name", `user\2Aname`},
|
|
|
|
{"*,CN=asdf", `\2A,CN=asdf`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
res := escapeForLDAPFilter(c.in)
|
|
|
|
if c.out != res {
|
|
|
|
t.Fatalf("result should be %s != %s", c.out, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEscapeForLDAPDN(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{"username", `username`},
|
|
|
|
{"* ,CN=asdf", `*\20\2CCN\3Dasdf`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
res := escapeForLDAPDN(c.in)
|
|
|
|
if c.out != res {
|
|
|
|
t.Fatalf("result should be %s != %s", c.out, res)
|
|
|
|
}
|
2023-05-10 05:52:02 +00:00
|
|
|
}
|
|
|
|
}
|