fix: tests

This commit is contained in:
DevMiner 2024-09-02 15:46:32 +02:00
parent 62a1a1ff83
commit 3f8ac13289
6 changed files with 59 additions and 51 deletions

View file

@ -4,21 +4,23 @@ import (
"crypto/ed25519" "crypto/ed25519"
"crypto/x509" "crypto/x509"
"encoding/base64" "encoding/base64"
"github.com/stretchr/testify/assert"
"net/url" "net/url"
"testing" "testing"
"github.com/stretchr/testify/assert"
versiautils "github.com/versia-pub/versia-go/pkg/versia/utils"
) )
func TestFederationClient_ValidateSignatureHeader(t *testing.T) { func TestFederationClient_ValidateSignatureHeader(t *testing.T) {
var ( var (
bobURL = &url.URL{Scheme: "https", Host: "bob.com"} bobURL = &url.URL{Scheme: "https", Host: "bob.com"}
bobPrivBytes = must(base64.StdEncoding.DecodeString, "MC4CAQAwBQYDK2VwBCIEINOATgmaya61Ha9OEE+DD3RnOEqDaHyQ3yLf5upwskUU") bobPrivBytes = versiautils.Must(base64.StdEncoding.DecodeString, "MC4CAQAwBQYDK2VwBCIEINOATgmaya61Ha9OEE+DD3RnOEqDaHyQ3yLf5upwskUU")
bobPriv = must(x509.ParsePKCS8PrivateKey, bobPrivBytes).(ed25519.PrivateKey) bobPriv = versiautils.Must(x509.ParsePKCS8PrivateKey, bobPrivBytes).(ed25519.PrivateKey)
signer = Signer{PrivateKey: bobPriv, UserURL: bobURL} signer = Signer{PrivateKey: bobPriv, UserURL: bobURL}
bobPubBytes = must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAQ08Z/FJ5f16o8mthLaFZMo4ssn0fJ7c+bipNYm3kId4=") bobPubBytes = versiautils.Must(base64.StdEncoding.DecodeString, "MCowBQYDK2VwAyEAQ08Z/FJ5f16o8mthLaFZMo4ssn0fJ7c+bipNYm3kId4=")
bobPub = must(x509.ParsePKIXPublicKey, bobPubBytes).(ed25519.PublicKey) bobPub = versiautils.Must(x509.ParsePKIXPublicKey, bobPubBytes).(ed25519.PublicKey)
verifier = Verifier{PublicKey: bobPub} verifier = Verifier{PublicKey: bobPub}
method = "POST" method = "POST"
@ -27,7 +29,7 @@ func TestFederationClient_ValidateSignatureHeader(t *testing.T) {
body = []byte("hello") 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()) assert.Equal(t, `post /a/b/c?z=foo&a=bar myrandomnonce LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=`, toSign.String())
signed := signer.Sign(*toSign) signed := signer.Sign(*toSign)

View file

@ -1,9 +1,10 @@
package versiacrypto package versiacrypto
import ( import (
"github.com/stretchr/testify/assert"
"net/url" "net/url"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestFederationHeaders_String(t *testing.T) { func TestFederationHeaders_String(t *testing.T) {
@ -11,7 +12,7 @@ func TestFederationHeaders_String(t *testing.T) {
RequestMethod: "POST", RequestMethod: "POST",
Nonce: "1234567890", Nonce: "1234567890",
URL: &url.URL{Scheme: "https", Host: "bob.com", Path: "/users/bob", RawQuery: "z=foo&a=bar"}, 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()) assert.Equal(t, "post /users/bob?z=foo&a=bar 1234567890 LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=", one.String())

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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
pkg/versia/utils/fns.go Normal file
View file

@ -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
}