Skip to content

Commit

Permalink
chore: add garbage roundtrip tests for dag-json
Browse files Browse the repository at this point in the history
To confirm that new refmt JSON decoding changes don't raise any red flags

Ref: polydawn/refmt#58
  • Loading branch information
rvagg committed Sep 26, 2023
1 parent 95fa80e commit 7070996
Show file tree
Hide file tree
Showing 4 changed files with 837 additions and 11 deletions.
53 changes: 53 additions & 0 deletions codec/dagjson/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package dagjson_test

import (
"bytes"
"math/rand"
"strings"
"testing"
"time"

qt "github.com/frankban/quicktest"

"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/fluent"
"github.com/ipld/go-ipld-prime/node/basicnode"
nodetests "github.com/ipld/go-ipld-prime/node/tests"
"github.com/ipld/go-ipld-prime/testutil/garbage"
)

var n = fluent.MustBuildMap(basicnode.Prototype.Map, 4, func(na fluent.MapAssembler) {
Expand Down Expand Up @@ -81,3 +84,53 @@ func TestRoundtripScalar(t *testing.T) {
qt.Check(t, nb.Build(), nodetests.NodeContentEquals, simple)
})
}

func TestGarbage(t *testing.T) {
t.Run("small garbage", func(t *testing.T) {
seed := time.Now().Unix()
t.Logf("randomness seed: %v\n", seed)
rnd := rand.New(rand.NewSource(seed))
for i := 0; i < 1000; i++ {
gbg := garbage.Generate(rnd, garbage.TargetBlockSize(1<<6))
var buf bytes.Buffer
err := dagjson.Encode(gbg, &buf)
qt.Assert(t, err, qt.IsNil)
nb := basicnode.Prototype.Any.NewBuilder()
err = dagjson.Decode(nb, bytes.NewReader(buf.Bytes()))
qt.Assert(t, err, qt.IsNil)
qt.Check(t, nb.Build(), nodetests.DeepNodeContentsEquals, gbg)
}
})

t.Run("medium garbage", func(t *testing.T) {
seed := time.Now().Unix()
t.Logf("randomness seed: %v\n", seed)
rnd := rand.New(rand.NewSource(seed))
for i := 0; i < 100; i++ {
gbg := garbage.Generate(rnd, garbage.TargetBlockSize(1<<16))
var buf bytes.Buffer
err := dagjson.Encode(gbg, &buf)
qt.Assert(t, err, qt.IsNil)
nb := basicnode.Prototype.Any.NewBuilder()
err = dagjson.Decode(nb, bytes.NewReader(buf.Bytes()))
qt.Assert(t, err, qt.IsNil)
qt.Check(t, nb.Build(), nodetests.DeepNodeContentsEquals, gbg)
}
})

t.Run("large garbage", func(t *testing.T) {
seed := time.Now().Unix()
t.Logf("randomness seed: %v\n", seed)
rnd := rand.New(rand.NewSource(seed))
for i := 0; i < 10; i++ {
gbg := garbage.Generate(rnd, garbage.TargetBlockSize(1<<20))
var buf bytes.Buffer
err := dagjson.Encode(gbg, &buf)
qt.Assert(t, err, qt.IsNil)
nb := basicnode.Prototype.Any.NewBuilder()
err = dagjson.Decode(nb, bytes.NewReader(buf.Bytes()))
qt.Assert(t, err, qt.IsNil)
qt.Check(t, nb.Build(), nodetests.DeepNodeContentsEquals, gbg)
}
})
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/ipld/go-ipld-prime

go 1.20

replace github.com/polydawn/refmt => github.com/rvagg/refmt v0.0.0-20230920092505-e600edc52dad

require (
github.com/frankban/quicktest v1.14.6
github.com/google/go-cmp v0.5.9
Expand All @@ -26,6 +28,6 @@ require (
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
lukechampine.com/blake3 v1.1.6 // indirect
)
Loading

0 comments on commit 7070996

Please sign in to comment.