add a token container with serialization as CARv1 file

This commit is contained in:
Michael Muré
2024-09-18 12:53:31 +02:00
parent 8615f6c72b
commit df9beadf9c
7 changed files with 378 additions and 4 deletions

40
pkg/container/car_test.go Normal file
View File

@@ -0,0 +1,40 @@
package container
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestCarRoundTrip(t *testing.T) {
// this car file is a complex and legal CARv1 file
original, err := os.ReadFile("testdata/sample-v1.car")
require.NoError(t, err)
roots, it, err := readCar(bytes.NewReader(original))
require.NoError(t, err)
var blks []carBlock
for blk, err := range it {
require.NoError(t, err)
blks = append(blks, blk)
}
require.Len(t, blks, 1049)
buf := bytes.NewBuffer(nil)
err = writeCar(buf, roots, func(yield func(carBlock) bool) {
for _, blk := range blks {
if !yield(blk) {
return
}
}
})
require.NoError(t, err)
// Bytes equal after the round-trip
require.Equal(t, original, buf.Bytes())
}