The scanner can be used to read any BSR data file. When creating a new scanner, it will first verify the BSR magic string. Individual Chunks can then be read back by calling Scan. The scanner will compute a sha256sum while it is reading chunks. If the scanner was created using the WithSha256Sum option, it will compare the computed checksum with the one provided via the option once the scanner encounters an end chunk (DONE) or an io.EOF error from the reader. If the checksums do not match, and error will be returned from Scan. The current sha256sum can be returned from the scanner using the Sum method. There is also a convenience function, ChunkWalk that can be used to call a function on each Chunk returned by a scanner.
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package bsr_test
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/internal/bsr"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMagic(t *testing.T) {
|
|
assert.Equal(t, string(bsr.Magic), "\x89BSR\r\n\x1a\n")
|
|
assert.Equal(t, bsr.Magic.Bytes(), []byte("\x89BSR\r\n\x1a\n"))
|
|
}
|
|
|
|
func TestReadMagic(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
r io.Reader
|
|
want error
|
|
}{
|
|
{
|
|
"valid",
|
|
bytes.NewBuffer([]byte(bsr.Magic)),
|
|
nil,
|
|
},
|
|
{
|
|
"nil-reader",
|
|
nil,
|
|
errors.New("bsr.ReadMagic: reader is nil: invalid parameter"),
|
|
},
|
|
{
|
|
"not-enough-chars",
|
|
bytes.NewBuffer([]byte(bsr.Magic)[:len(bsr.Magic)-1]),
|
|
errors.New("bsr.ReadMagic: unexpected EOF"),
|
|
},
|
|
{
|
|
"not-magic",
|
|
bytes.NewBuffer([]byte("notthemagic")),
|
|
errors.New("bsr.ReadMagic: invalid magic string"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := bsr.ReadMagic(tc.r)
|
|
if tc.want == nil {
|
|
assert.NoError(t, got)
|
|
return
|
|
}
|
|
assert.EqualError(t, got, tc.want.Error())
|
|
})
|
|
}
|
|
}
|