2024-09-18 12:20:54 -04:00
|
|
|
package delegation_test
|
2024-08-30 22:06:59 +02:00
|
|
|
|
|
|
|
|
import (
|
2024-09-18 12:20:54 -04:00
|
|
|
_ "embed"
|
2024-08-30 22:06:59 +02:00
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/ipld/go-ipld-prime"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-09-18 12:20:54 -04:00
|
|
|
"gotest.tools/v3/golden"
|
2024-09-19 10:48:25 +02:00
|
|
|
|
|
|
|
|
"github.com/ucan-wg/go-ucan/delegation"
|
2024-09-19 13:29:33 -04:00
|
|
|
"github.com/ucan-wg/go-ucan/internal/envelope"
|
2024-08-30 22:06:59 +02:00
|
|
|
)
|
|
|
|
|
|
2024-09-18 12:20:54 -04:00
|
|
|
//go:embed delegation.ipldsch
|
|
|
|
|
var schemaBytes []byte
|
|
|
|
|
|
2024-08-30 22:06:59 +02:00
|
|
|
func TestSchemaRoundTrip(t *testing.T) {
|
2024-09-19 13:29:33 -04:00
|
|
|
t.Parallel()
|
2024-09-18 12:20:54 -04:00
|
|
|
|
|
|
|
|
delegationJson := golden.Get(t, "new.dagjson")
|
|
|
|
|
privKey := privKey(t, issuerPrivKeyCfg)
|
|
|
|
|
|
2024-09-02 02:26:48 +02:00
|
|
|
// format: dagJson --> PayloadModel --> dagCbor --> PayloadModel --> dagJson
|
|
|
|
|
// function: DecodeDagJson() EncodeDagCbor() DecodeDagCbor() EncodeDagJson()
|
|
|
|
|
|
2024-09-19 13:29:33 -04:00
|
|
|
p1, id1, err := delegation.FromDagJson([]byte(delegationJson))
|
2024-09-02 02:26:48 +02:00
|
|
|
require.NoError(t, err)
|
2024-09-19 13:29:33 -04:00
|
|
|
require.Equal(t, newCID, envelope.CIDToBase58BTC(id1))
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-09-18 12:20:54 -04:00
|
|
|
cborBytes, err := p1.ToDagCbor(privKey)
|
2024-08-30 22:06:59 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
fmt.Println("cborBytes length", len(cborBytes))
|
|
|
|
|
fmt.Println("cbor", string(cborBytes))
|
|
|
|
|
|
2024-09-19 13:29:33 -04:00
|
|
|
p2, id2, err := delegation.FromDagCbor(cborBytes)
|
2024-08-30 22:06:59 +02:00
|
|
|
require.NoError(t, err)
|
2024-09-02 02:26:48 +02:00
|
|
|
fmt.Println("read Cbor", p2)
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-09-18 12:20:54 -04:00
|
|
|
readJson, err := p2.ToDagJson(privKey)
|
2024-08-30 22:06:59 +02:00
|
|
|
require.NoError(t, err)
|
2024-09-19 13:29:33 -04:00
|
|
|
require.Equal(t, newCID, envelope.CIDToBase58BTC(id2))
|
2024-09-02 02:26:48 +02:00
|
|
|
fmt.Println("readJson length", len(readJson))
|
|
|
|
|
fmt.Println("json: ", string(readJson))
|
2024-08-30 22:06:59 +02:00
|
|
|
|
2024-09-18 12:20:54 -04:00
|
|
|
require.JSONEq(t, string(delegationJson), string(readJson))
|
2024-08-30 22:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkSchemaLoad(b *testing.B) {
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, _ = ipld.LoadSchemaBytes(schemaBytes)
|
|
|
|
|
}
|
|
|
|
|
}
|