diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-06 00:52:43 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-07-23 00:07:30 -0600 |
commit | c65d6effc26c3d97a6193f65c5b7698c830d9ff0 (patch) | |
tree | ed498b394332d888477c75fcb3b12f51bcf70c62 /lib/btrfs/btrfssum | |
parent | 325178506187df037f9a85eb2e09100eb794f4f9 (diff) |
btrfssum: Don't emit JSON strings that are too long
Split it, and wrap it in an array.
Diffstat (limited to 'lib/btrfs/btrfssum')
5 files changed, 75 insertions, 2 deletions
diff --git a/lib/btrfs/btrfssum/shortsum.go b/lib/btrfs/btrfssum/shortsum.go index 490c40a..a3a6d11 100644 --- a/lib/btrfs/btrfssum/shortsum.go +++ b/lib/btrfs/btrfssum/shortsum.go @@ -11,6 +11,7 @@ import ( "git.lukeshu.com/go/lowmemjson" "git.lukeshu.com/btrfs-progs-ng/lib/jsonutil" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) type ShortSum string @@ -27,12 +28,12 @@ func (sum ShortSum) ToFullSum() CSum { } func (sum ShortSum) EncodeJSON(w io.Writer) error { - return jsonutil.EncodeHexString(w, sum) + return jsonutil.EncodeSplitHexString(w, sum, textui.Tunable(80)) } func (sum *ShortSum) DecodeJSON(r io.RuneScanner) error { var out strings.Builder - if err := jsonutil.DecodeHexString(r, &out); err != nil { + if err := jsonutil.DecodeSplitHexString(r, &out); err != nil { return err } *sum = ShortSum(out.String()) diff --git a/lib/btrfs/btrfssum/shortsum_test.go b/lib/btrfs/btrfssum/shortsum_test.go new file mode 100644 index 0000000..aa7849a --- /dev/null +++ b/lib/btrfs/btrfssum/shortsum_test.go @@ -0,0 +1,66 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package btrfssum_test + +import ( + "bytes" + "testing" + + "git.lukeshu.com/go/lowmemjson" + "github.com/stretchr/testify/assert" + + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum" +) + +func TestShortSumEncodeJSON(t *testing.T) { + t.Parallel() + type TestCase struct { + InputSum btrfssum.ShortSum + OutputJSON string + } + testcases := map[string]TestCase{ + "short": { + InputSum: "xyz", + OutputJSON: `"78797a"`, + }, + "long": { + InputSum: "0123456789abcdefghijklmnopqrstuvwxyz;:.,ABCDEFG", + OutputJSON: `["303132333435363738396162636465666768696a6b6c6d6e6f707172737475767778797a3b3a2e2c","41424344454647"]`, + }, + "medium": { // exactly the maximum string length + InputSum: "0123456789abcdefghijklmnopqrstuvwxyz;:.,", + OutputJSON: `"303132333435363738396162636465666768696a6b6c6d6e6f707172737475767778797a3b3a2e2c"`, + }, + } + for tcName, tc := range testcases { + tc := tc + t.Run(tcName, func(t *testing.T) { + t.Parallel() + + var jsonBuf bytes.Buffer + assert.NoError(t, lowmemjson.NewEncoder(&jsonBuf).Encode(tc.InputSum)) + assert.Equal(t, tc.OutputJSON, jsonBuf.String()) + + var rtSum btrfssum.ShortSum + assert.NoError(t, lowmemjson.NewDecoder(&jsonBuf).DecodeThenEOF(&rtSum)) + assert.Equal(t, tc.InputSum, rtSum) + }) + } +} + +func FuzzShortSumJSONFuzz(f *testing.F) { + f.Fuzz(func(t *testing.T, _inSum []byte) { + t.Logf("in = %q", _inSum) + inSum := btrfssum.ShortSum(_inSum) + + var jsonBuf bytes.Buffer + assert.NoError(t, lowmemjson.NewEncoder(&jsonBuf).Encode(inSum)) + t.Logf("json = %q", jsonBuf.Bytes()) + + var outSum btrfssum.ShortSum + assert.NoError(t, lowmemjson.NewDecoder(&jsonBuf).DecodeThenEOF(&outSum)) + assert.Equal(t, inSum, outSum) + }) +} diff --git a/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/162fbf5f010234baa3e0f8c91825ebb8bce17b9ad8365ae1cad6977b4114d1ec b/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/162fbf5f010234baa3e0f8c91825ebb8bce17b9ad8365ae1cad6977b4114d1ec new file mode 100644 index 0000000..338adec --- /dev/null +++ b/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/162fbf5f010234baa3e0f8c91825ebb8bce17b9ad8365ae1cad6977b4114d1ec @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("00000000000000000000000000000000000000000") diff --git a/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/b15b16f1ef330f9113455291780be2915cecf2bd1b7dbe2e3af0505f042751e8 b/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/b15b16f1ef330f9113455291780be2915cecf2bd1b7dbe2e3af0505f042751e8 new file mode 100644 index 0000000..c9fd0e4 --- /dev/null +++ b/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/b15b16f1ef330f9113455291780be2915cecf2bd1b7dbe2e3af0505f042751e8 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("Z") diff --git a/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/fecd8dd05aeedabe b/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/fecd8dd05aeedabe new file mode 100644 index 0000000..66b87de --- /dev/null +++ b/lib/btrfs/btrfssum/testdata/fuzz/FuzzShortSumJSONFuzz/fecd8dd05aeedabe @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("\xb8") |