errors_test.go 1000 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package pia
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "testing"
  6. )
  7. func TestNilErrorHelpers(t *testing.T) {
  8. if code := CodeOf(nil); code != "" {
  9. t.Fatalf("CodeOf(nil)=%q, want empty", code)
  10. }
  11. var typed *Error
  12. var err error = typed
  13. if code := CodeOf(err); code != CodeNetworkUnavailable {
  14. t.Fatalf("CodeOf(nil *Error)=%q, want %q", code, CodeNetworkUnavailable)
  15. }
  16. if unwrapped := errors.Unwrap(err); unwrapped != nil {
  17. t.Fatalf("errors.Unwrap(nil *Error)=%v, want nil", unwrapped)
  18. }
  19. }
  20. func TestValidWGKeyRequiresBase64Encoded32Bytes(t *testing.T) {
  21. valid := base64.StdEncoding.EncodeToString(make([]byte, 32))
  22. if !validWGKey(valid) {
  23. t.Fatalf("valid WireGuard key rejected: %q", valid)
  24. }
  25. for _, invalid := range []string{
  26. "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",
  27. base64.StdEncoding.EncodeToString(make([]byte, 31)),
  28. base64.StdEncoding.EncodeToString(make([]byte, 33)),
  29. } {
  30. if validWGKey(invalid) {
  31. t.Fatalf("invalid WireGuard key accepted: %q", invalid)
  32. }
  33. }
  34. }