Files
ucan/delegation/schema_test.go

72 lines
1.5 KiB
Go
Raw Normal View History

package delegation
import (
"fmt"
"testing"
"github.com/ipld/go-ipld-prime"
"github.com/stretchr/testify/require"
)
func TestSchemaRoundTrip(t *testing.T) {
2024-09-02 02:26:48 +02:00
const delegationJson = `
{
"aud":"did:key:def456",
"cmd":"/foo/bar",
"exp":123456,
"iss":"did:key:abc123",
"meta":{
"bar":"baaar",
"foo":"fooo"
},
"nbf":123456,
"nonce":{
"/":{
"bytes":"c3VwZXItcmFuZG9t"
}
},
"pol":[
["==", ".status", "draft"],
["all", ".reviewer", [
["like", ".email", "*@example.com"]]
],
["any", ".tags", [
["or", [
["==", ".", "news"],
["==", ".", "press"]]
]]
]
],
"sub":""
}
`
// format: dagJson --> PayloadModel --> dagCbor --> PayloadModel --> dagJson
// function: DecodeDagJson() EncodeDagCbor() DecodeDagCbor() EncodeDagJson()
p1, err := DecodeDagJson([]byte(delegationJson))
require.NoError(t, err)
2024-09-02 02:26:48 +02:00
cborBytes, err := p1.EncodeDagCbor()
require.NoError(t, err)
fmt.Println("cborBytes length", len(cborBytes))
fmt.Println("cbor", string(cborBytes))
2024-09-02 02:26:48 +02:00
p2, err := DecodeDagCbor(cborBytes)
require.NoError(t, err)
2024-09-02 02:26:48 +02:00
fmt.Println("read Cbor", p2)
2024-09-02 02:26:48 +02:00
readJson, err := p2.EncodeDagJson()
require.NoError(t, err)
2024-09-02 02:26:48 +02:00
fmt.Println("readJson length", len(readJson))
fmt.Println("json: ", string(readJson))
2024-09-02 02:26:48 +02:00
require.JSONEq(t, delegationJson, string(readJson))
}
func BenchmarkSchemaLoad(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = ipld.LoadSchemaBytes(schemaBytes)
}
}