From 3f8ac132897216815877128950d0f3280818302f Mon Sep 17 00:00:00 2001 From: DevMiner Date: Mon, 2 Sep 2024 15:46:32 +0200 Subject: [PATCH] fix: tests --- pkg/versia/crypto/crypto_test.go | 14 ++++---- pkg/versia/crypto/federation_headers_test.go | 5 +-- pkg/versia/crypto/spki_public_key_test.go | 33 ----------------- pkg/versia/crypto/utils_test.go | 10 ------ pkg/versia/public_key_test.go | 38 ++++++++++++++++++++ pkg/versia/utils/fns.go | 10 ++++++ 6 files changed, 59 insertions(+), 51 deletions(-) delete mode 100644 pkg/versia/crypto/spki_public_key_test.go delete mode 100644 pkg/versia/crypto/utils_test.go create mode 100644 pkg/versia/public_key_test.go create mode 100644 pkg/versia/utils/fns.go diff --git a/pkg/versia/crypto/crypto_test.go b/pkg/versia/crypto/crypto_test.go index 515af1f..eb622cf 100644 --- a/pkg/versia/crypto/crypto_test.go +++ b/pkg/versia/crypto/crypto_test.go @@ -4,21 +4,23 @@ import ( "crypto/ed25519" "crypto/x509" "encoding/base64" - "github.com/stretchr/testify/assert" "net/url" "testing" + + "github.com/stretchr/testify/assert" + versiautils "github.com/versia-pub/versia-go/pkg/versia/utils" ) func TestFederationClient_ValidateSignatureHeader(t *testing.T) { var ( bobURL = &url.URL{Scheme: "https", Host: "bob.com"} - bobPrivBytes = must(base64.StdEncoding.DecodeString, "MC4CAQAwBQYDK2VwBCIEINOATgmaya61Ha9OEE+DD3RnOEqDaHyQ3yLf5upwskUU") - bobPriv = must(x509.ParsePKCS8PrivateKey, bobPrivBytes).(ed25519.PrivateKey) + bobPrivBytes = versiautils.Must(base64.StdEncoding.DecodeString, "MC4CAQAwBQYDK2VwBCIEINOATgmaya61Ha9OEE+DD3RnOEqDaHyQ3yLf5upwskUU") + bobPriv = versiautils.Must(x509.ParsePKCS8PrivateKey, bobPrivBytes).(ed25519.PrivateKey) signer = Signer{PrivateKey: bobPriv, UserURL: bobURL} - bobPubBytes = must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAQ08Z/FJ5f16o8mthLaFZMo4ssn0fJ7c+bipNYm3kId4=") - bobPub = must(x509.ParsePKIXPublicKey, bobPubBytes).(ed25519.PublicKey) + bobPubBytes = versiautils.Must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAQ08Z/FJ5f16o8mthLaFZMo4ssn0fJ7c+bipNYm3kId4=") + bobPub = versiautils.Must(x509.ParsePKIXPublicKey, bobPubBytes).(ed25519.PublicKey) verifier = Verifier{PublicKey: bobPub} method = "POST" @@ -27,7 +29,7 @@ func TestFederationClient_ValidateSignatureHeader(t *testing.T) { body = []byte("hello") ) - toSign := NewSignatureData(method, nonce, u, hashSHA256(body)) + toSign := NewSignatureData(method, nonce, u, SHA256(body)) assert.Equal(t, `post /a/b/c?z=foo&a=bar myrandomnonce LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=`, toSign.String()) signed := signer.Sign(*toSign) diff --git a/pkg/versia/crypto/federation_headers_test.go b/pkg/versia/crypto/federation_headers_test.go index 9afac22..15a415b 100644 --- a/pkg/versia/crypto/federation_headers_test.go +++ b/pkg/versia/crypto/federation_headers_test.go @@ -1,9 +1,10 @@ package versiacrypto import ( - "github.com/stretchr/testify/assert" "net/url" "testing" + + "github.com/stretchr/testify/assert" ) func TestFederationHeaders_String(t *testing.T) { @@ -11,7 +12,7 @@ func TestFederationHeaders_String(t *testing.T) { RequestMethod: "POST", Nonce: "1234567890", URL: &url.URL{Scheme: "https", Host: "bob.com", Path: "/users/bob", RawQuery: "z=foo&a=bar"}, - Digest: hashSHA256([]byte("hello")), + Digest: SHA256([]byte("hello")), } assert.Equal(t, "post /users/bob?z=foo&a=bar 1234567890 LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=", one.String()) diff --git a/pkg/versia/crypto/spki_public_key_test.go b/pkg/versia/crypto/spki_public_key_test.go deleted file mode 100644 index f1cb054..0000000 --- a/pkg/versia/crypto/spki_public_key_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package versiacrypto - -import ( - "crypto/ed25519" - "crypto/x509" - "encoding/base64" - "encoding/json" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestSPKIPublicKey_UnmarshalJSON(t *testing.T) { - expectedPk := must(x509.ParsePKIXPublicKey, must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAgKNt+9eyOXdb7MSrrmHlsFD2H9NGwC+56PjpWD46Tcs=")) - - pk := UserPublicKey{} - raw := []byte(`{"public_key":"MCowBQYDK2VwAyEAgKNt+9eyOXdb7MSrrmHlsFD2H9NGwC+56PjpWD46Tcs="}`) - if err := json.Unmarshal(raw, &pk); err != nil { - t.Error(err) - } - - assert.Equal(t, expectedPk, ed25519.PublicKey(pk.Key)) -} - -func TestSPKIPublicKey_MarshalJSON(t *testing.T) { - expectedPk := must(x509.ParsePKIXPublicKey, must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAgKNt+9eyOXdb7MSrrmHlsFD2H9NGwC+56PjpWD46Tcs=")).(ed25519.PublicKey) - - pk := UserPublicKey{ - Key: SPKIPublicKey(expectedPk), - } - if _, err := json.Marshal(pk); err != nil { - t.Error(err) - } -} diff --git a/pkg/versia/crypto/utils_test.go b/pkg/versia/crypto/utils_test.go deleted file mode 100644 index 46e8f14..0000000 --- a/pkg/versia/crypto/utils_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package versiacrypto - -func must[In any, Out any](fn func(In) (Out, error), v In) Out { - out, err := fn(v) - if err != nil { - panic(err) - } - - return out -} diff --git a/pkg/versia/public_key_test.go b/pkg/versia/public_key_test.go new file mode 100644 index 0000000..645495e --- /dev/null +++ b/pkg/versia/public_key_test.go @@ -0,0 +1,38 @@ +package versia + +import ( + "crypto/ed25519" + "crypto/x509" + "encoding/base64" + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + versiacrypto "github.com/versia-pub/versia-go/pkg/versia/crypto" + versiautils "github.com/versia-pub/versia-go/pkg/versia/utils" +) + +func TestUserPublicKey_UnmarshalJSON(t *testing.T) { + expectedPk := versiautils.Must(x509.ParsePKIXPublicKey, versiautils.Must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAgKNt+9eyOXdb7MSrrmHlsFD2H9NGwC+56PjpWD46Tcs=")) + + pk := UserPublicKey{} + raw := []byte(`{"algorithm":"ed25519","key":"MCowBQYDK2VwAyEAgKNt+9eyOXdb7MSrrmHlsFD2H9NGwC+56PjpWD46Tcs="}`) + if err := json.Unmarshal(raw, &pk); err != nil { + t.Error(err) + } + + assert.Equal(t, "ed25519", pk.Algorithm) + assert.Equal(t, "ed25519", pk.Key.Algorithm) + assert.Equal(t, expectedPk, pk.Key.Key.(ed25519.PublicKey)) +} + +func TestUserPublicKey_MarshalJSON(t *testing.T) { + expectedPk := versiautils.Must(x509.ParsePKIXPublicKey, versiautils.Must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAgKNt+9eyOXdb7MSrrmHlsFD2H9NGwC+56PjpWD46Tcs=")).(ed25519.PublicKey) + + pk := UserPublicKey{ + Key: &versiacrypto.SPKIPublicKey{Key: expectedPk, Algorithm: "ed25519"}, + } + if _, err := json.Marshal(pk); err != nil { + t.Error(err) + } +} diff --git a/pkg/versia/utils/fns.go b/pkg/versia/utils/fns.go new file mode 100644 index 0000000..b2376aa --- /dev/null +++ b/pkg/versia/utils/fns.go @@ -0,0 +1,10 @@ +package versiautils + +func Must[In any, Out any](fn func(In) (Out, error), v In) Out { + out, err := fn(v) + if err != nil { + panic(err) + } + + return out +}