Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Sep 2, 2024
1 parent 62a1a1f commit 3f8ac13
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 51 deletions.
14 changes: 8 additions & 6 deletions pkg/versia/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/versia/crypto/federation_headers_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package versiacrypto

import (
"github.com/stretchr/testify/assert"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFederationHeaders_String(t *testing.T) {
one := SignatureData{
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())
Expand Down
33 changes: 0 additions & 33 deletions pkg/versia/crypto/spki_public_key_test.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/versia/crypto/utils_test.go

This file was deleted.

38 changes: 38 additions & 0 deletions pkg/versia/public_key_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
10 changes: 10 additions & 0 deletions pkg/versia/utils/fns.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3f8ac13

Please sign in to comment.