mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 03:01:03 +01:00
9dbb300d9b
Relative to this one as well: https://github.com/kataras/iris/issues/1200 Former-commit-id: 3a29e7398b7fdeb9b48a118b742d419d5681d56b
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package sign
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
var (
|
|
testPrivateKey = MustGenerateKey()
|
|
testPublicKey = &testPrivateKey.PublicKey
|
|
)
|
|
|
|
func TestGenerateKey(t *testing.T) {
|
|
privateKeyB, err := marshalPrivateKey(testPrivateKey)
|
|
if err != nil {
|
|
t.Fatalf("private key: %v", err)
|
|
}
|
|
publicKeyB, err := marshalPublicKey(testPublicKey)
|
|
if err != nil {
|
|
t.Fatalf("public key: %v", err)
|
|
}
|
|
|
|
t.Logf("%s", privateKeyB)
|
|
t.Logf("%s", publicKeyB)
|
|
|
|
privateKeyParsed, err := ParsePrivateKey(privateKeyB)
|
|
if err != nil {
|
|
t.Fatalf("private key: %v", err)
|
|
}
|
|
|
|
publicKeyParsed, err := ParsePublicKey(publicKeyB)
|
|
if err != nil {
|
|
t.Fatalf("public key: %v", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(testPrivateKey, privateKeyParsed) {
|
|
t.Fatalf("expected private key to be:\n%#+v\nbut got:\n%#+v", testPrivateKey, privateKeyParsed)
|
|
}
|
|
if !reflect.DeepEqual(testPublicKey, publicKeyParsed) {
|
|
t.Fatalf("expected public key to be:\n%#+v\nbut got:\n%#+v", testPublicKey, publicKeyParsed)
|
|
}
|
|
}
|
|
|
|
func TestSignAndVerify(t *testing.T) {
|
|
tests := []struct {
|
|
payload []byte
|
|
}{
|
|
{[]byte("test my content 1")},
|
|
{[]byte("test my content 2")},
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
sig, err := Sign(testPrivateKey, tt.payload)
|
|
if err != nil {
|
|
t.Fatalf("[%d] sign error: %v", i, err)
|
|
}
|
|
|
|
ok, err := Verify(testPublicKey, sig, tt.payload)
|
|
if err != nil {
|
|
t.Fatalf("[%d] verify error: %v", i, err)
|
|
}
|
|
if !ok {
|
|
t.Fatalf("[%d] verification failed for '%s'", i, tt.payload)
|
|
}
|
|
|
|
// test with other, invalid public key, should fail to verify.
|
|
tempPublicKey := &MustGenerateKey().PublicKey
|
|
|
|
ok, err = Verify(tempPublicKey, sig, tt.payload)
|
|
if ok {
|
|
t.Fatalf("[%d] verification should fail but passed for '%s'", i, tt.payload)
|
|
}
|
|
}
|
|
}
|