2024-06-10 20:31:29 +00:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSigned(t *testing.T) {
|
|
|
|
type msg struct {
|
|
|
|
A int
|
|
|
|
B string
|
|
|
|
C bool
|
|
|
|
}
|
|
|
|
|
|
|
|
a := msg{1, "FOO", true}
|
|
|
|
|
|
|
|
signedA, err := Sign(a, hostPrivCredsA.SigningPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signedJSON, err := json.Marshal(signedA)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(string(signedJSON))
|
|
|
|
|
|
|
|
var signedB Signed[msg]
|
|
|
|
if err := json.Unmarshal(signedJSON, &signedB); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = signedB.Unwrap(hostPubCredsB.SigningKey)
|
|
|
|
if !errors.Is(err, ErrInvalidSignature) {
|
2024-12-07 19:36:29 +00:00
|
|
|
t.Fatalf("expected ErrInvalidSignature but got: %v", err)
|
2024-06-10 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := signedB.Unwrap(hostPubCredsA.SigningKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a != b {
|
|
|
|
t.Fatalf("expected:%+v, got:%+v", a, b)
|
|
|
|
}
|
|
|
|
}
|