container: add cbor serialisation

This commit is contained in:
Michael Muré
2024-09-19 21:26:42 +02:00
parent df9beadf9c
commit 346efbd31d
5 changed files with 66 additions and 8 deletions

View File

@@ -19,17 +19,22 @@ func TestContainerRoundTrip(t *testing.T) {
}{
{"carBytes", Container.ToCar, FromCar},
{"carBase64", Container.ToCarBase64, FromCarBase64},
{"cbor", Container.ToCbor, FromCbor},
{"cborBase64", Container.ToCborBase64, FromCborBase64},
} {
t.Run(tc.name, func(t *testing.T) {
ctn := New()
builder := cid.V1Builder{Codec: cid.Raw, MhType: mh.SHA2_256}
builder := cid.V1Builder{Codec: cid.DagCBOR, MhType: mh.SHA2_256}
var dataSize int
for i := 0; i < 10; i++ {
data := randBytes(32)
c, err := builder.Sum(data)
require.NoError(t, err)
ctn.AddBytes(c, data)
dataSize += len(data)
}
buf := bytes.NewBuffer(nil)
@@ -37,6 +42,9 @@ func TestContainerRoundTrip(t *testing.T) {
err := tc.writer(ctn, buf)
require.NoError(t, err)
t.Logf("data size %d", dataSize)
t.Logf("container overhead: %d%%, %d bytes", int(float32(buf.Len()-dataSize)/float32(dataSize)*100.0), buf.Len()-dataSize)
ctn2, err := tc.reader(bytes.NewReader(buf.Bytes()))
require.NoError(t, err)