api_auth_test.go 926 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. ok := authStatic("user", "pass", "user", string(passwordHashBytes))
  17. if !ok {
  18. t.Fatalf("should pass auth")
  19. }
  20. }
  21. func TestSimpleAuthUsernameFail(t *testing.T) {
  22. ok := authStatic("userWRONG", "pass", "user", string(passwordHashBytes))
  23. if ok {
  24. t.Fatalf("should fail auth")
  25. }
  26. }
  27. func TestStaticAuthPasswordFail(t *testing.T) {
  28. ok := authStatic("user", "passWRONG", "user", string(passwordHashBytes))
  29. if ok {
  30. t.Fatalf("should fail auth")
  31. }
  32. }