2018-09-11 14:25:24 -07: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 12:53:58 -07:00
|
|
|
package api
|
2018-09-11 14:25:24 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-09-18 12:56:52 -07:00
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2018-09-11 14:25:24 -07:00
|
|
|
)
|
|
|
|
|
2018-09-18 12:56:52 -07:00
|
|
|
var passwordHashBytes []byte
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
passwordHashBytes, _ = bcrypt.GenerateFromPassword([]byte("pass"), 0)
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:25:24 -07:00
|
|
|
func TestStaticAuthOK(t *testing.T) {
|
2019-09-05 04:35:51 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-09-11 14:25:24 -07:00
|
|
|
ok := authStatic("user", "pass", "user", string(passwordHashBytes))
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should pass auth")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleAuthUsernameFail(t *testing.T) {
|
2019-09-05 04:35:51 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-09-11 14:25:24 -07:00
|
|
|
ok := authStatic("userWRONG", "pass", "user", string(passwordHashBytes))
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("should fail auth")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStaticAuthPasswordFail(t *testing.T) {
|
2019-09-05 04:35:51 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-09-18 12:56:52 -07:00
|
|
|
ok := authStatic("user", "passWRONG", "user", string(passwordHashBytes))
|
2018-09-11 14:25:24 -07:00
|
|
|
if ok {
|
|
|
|
t.Fatalf("should fail auth")
|
|
|
|
}
|
|
|
|
}
|