api_auth_test.go 971 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. "golang.org/x/crypto/bcrypt"
  10. )
  11. var passwordHashBytes []byte
  12. func init() {
  13. passwordHashBytes, _ = bcrypt.GenerateFromPassword([]byte("pass"), 0)
  14. }
  15. func TestStaticAuthOK(t *testing.T) {
  16. t.Parallel()
  17. ok := authStatic("user", "pass", "user", string(passwordHashBytes))
  18. if !ok {
  19. t.Fatalf("should pass auth")
  20. }
  21. }
  22. func TestSimpleAuthUsernameFail(t *testing.T) {
  23. t.Parallel()
  24. ok := authStatic("userWRONG", "pass", "user", string(passwordHashBytes))
  25. if ok {
  26. t.Fatalf("should fail auth")
  27. }
  28. }
  29. func TestStaticAuthPasswordFail(t *testing.T) {
  30. t.Parallel()
  31. ok := authStatic("user", "passWRONG", "user", string(passwordHashBytes))
  32. if ok {
  33. t.Fatalf("should fail auth")
  34. }
  35. }