mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
* export: fail the tar export on an unreadable live needle * export: honor -newer after needle body read errors Skip a damaged record when its decoded modification time excludes it from the tar. Publish the date flag only after decoding the timestamp so incomplete metadata still fails the export. * needle: keep reading a record whose fields end before its timestamp The last-modified field was decoded without the length check the other optional fields keep, so a record whose bytes end right after its flags, name or mime now failed as corrupted. Older reads, and the Rust volume server, read it without a timestamp. Keep the check; the flag stays off until the timestamp is decoded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
5.3 KiB
Go
153 lines
5.3 KiB
Go
package needle
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/backend"
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
|
)
|
|
|
|
// readNeedleBodyBytes runs ReadNeedleBodyBytes and turns a panic into a test
|
|
// failure, so a regression reports which case broke instead of killing the run.
|
|
func readNeedleBodyBytes(t *testing.T, n *Needle, body []byte, version Version) (err error) {
|
|
t.Helper()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Fatalf("ReadNeedleBodyBytes panicked for size %d, body length %d: %v", n.Size, len(body), r)
|
|
}
|
|
}()
|
|
return n.ReadNeedleBodyBytes(body, version)
|
|
}
|
|
|
|
// The size feeds the read buffer's length, so a negative one never reaches make().
|
|
func TestReadNeedleBlobRejectsNegativeSize(t *testing.T) {
|
|
for _, version := range []Version{Version1, Version2, Version3} {
|
|
for _, size := range []Size{TombstoneFileSize, -100} {
|
|
if _, err := ReadNeedleBlob(nil, 0, size, version); !errors.Is(err, ErrorSizeInvalid) {
|
|
t.Fatalf("version %d size %d: expected ErrorSizeInvalid, got %v", version, size, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// A corrupted .dat header can carry a size that does not fit the body read for
|
|
// it. Vacuum used to panic on it with "slice bounds out of range [:-1]" (#6763).
|
|
func TestReadNeedleBodyBytesRejectsCorruptSize(t *testing.T) {
|
|
for version := Version1; IsSupportedVersion(version); version++ {
|
|
t.Run(versionString(version), func(t *testing.T) {
|
|
// A size of -1 is the case from #6763: its body length is still
|
|
// positive, so the scan reads a body and hands it over.
|
|
bodyLength := NeedleBodyLength(-1, version)
|
|
if bodyLength <= 0 {
|
|
t.Fatalf("expected a positive body length for size -1, got %d", bodyLength)
|
|
}
|
|
|
|
cases := []struct {
|
|
name string
|
|
size Size
|
|
body int
|
|
}{
|
|
{"size -1", -1, int(bodyLength)},
|
|
{"size -12", -12, 32},
|
|
{"size larger than body", 64, 32},
|
|
{"no room for the tail", 32, 32},
|
|
{"empty body", 0, 0},
|
|
{"empty body with data size", 1, 0},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
n := &Needle{Size: c.size}
|
|
err := readNeedleBodyBytes(t, n, make([]byte, c.body), version)
|
|
if !errors.Is(err, ErrorCorrupted) {
|
|
t.Fatalf("expected an error wrapping ErrorCorrupted, got %v", err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The size guard must still accept every record the writer produces,
|
|
// including the size-0 record a delete appends.
|
|
func TestReadNeedleBodyBytesWrittenNeedles(t *testing.T) {
|
|
for version := Version1; IsSupportedVersion(version); version++ {
|
|
t.Run(versionString(version), func(t *testing.T) {
|
|
for _, data := range [][]byte{nil, []byte("hello seaweed")} {
|
|
written := &Needle{Id: 7, Cookie: 9, Data: data, Checksum: NewCRC(data), AppendAtNs: 42}
|
|
buf := new(bytes.Buffer)
|
|
if _, _, err := writeNeedleByVersion(version, written, 0, buf); err != nil {
|
|
// Some builds can read a version they cannot write; skip
|
|
// only that recognized case so a real writer regression
|
|
// still fails the test.
|
|
if strings.Contains(strings.ToLower(err.Error()), "unsupported version") {
|
|
t.Skipf("version %d is not writable in this build: %v", version, err)
|
|
}
|
|
t.Fatalf("write needle: %v", err)
|
|
}
|
|
|
|
n := new(Needle)
|
|
n.ParseNeedleHeader(buf.Bytes())
|
|
body := buf.Bytes()[NeedleHeaderSize:]
|
|
if int64(len(body)) != NeedleBodyLength(n.Size, version) {
|
|
t.Fatalf("body length %d, want %d", len(body), NeedleBodyLength(n.Size, version))
|
|
}
|
|
if err := readNeedleBodyBytes(t, n, body, version); err != nil {
|
|
t.Fatalf("read %d-byte needle: %v", len(data), err)
|
|
}
|
|
if !bytes.Equal(n.Data, data) {
|
|
t.Fatalf("data %q, want %q", n.Data, data)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReadNeedleBlobRejectsSizePastInt(t *testing.T) {
|
|
if strconv.IntSize > 32 {
|
|
t.Skip("int holds every needle size")
|
|
}
|
|
f, err := os.CreateTemp(t.TempDir(), "dat")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
datFile := backend.NewDiskFile(f)
|
|
defer datFile.Close()
|
|
if _, err := ReadNeedleBlob(datFile, 0, Size(0x7ffffff0), Version3); !errors.Is(err, ErrorSizeInvalid) {
|
|
t.Fatalf("ReadNeedleBlob: %v, want ErrorSizeInvalid", err)
|
|
}
|
|
}
|
|
|
|
func TestReadNeedleDataRejectsDataSizePastBody(t *testing.T) {
|
|
n := new(Needle)
|
|
body := []byte{0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0}
|
|
if err := n.readNeedleDataVersion2(body); !errors.Is(err, ErrorCorrupted) {
|
|
t.Fatalf("readNeedleDataVersion2: %v, want ErrorCorrupted", err)
|
|
}
|
|
}
|
|
|
|
func TestReadNeedleNonDataLastModifiedFlag(t *testing.T) {
|
|
n := new(Needle)
|
|
if _, err := n.readNeedleDataVersion2NonData([]byte{FlagHasLastModifiedDate}); err != nil {
|
|
t.Fatalf("flags with no fields after them: %v", err)
|
|
}
|
|
if n.HasLastModifiedDate() {
|
|
t.Fatal("last modified flag set without a decoded timestamp")
|
|
}
|
|
n = new(Needle)
|
|
if _, err := n.readNeedleDataVersion2NonData([]byte{FlagHasLastModifiedDate, 0, 0}); !errors.Is(err, ErrorCorrupted) {
|
|
t.Fatalf("truncated timestamp: %v, want ErrorCorrupted", err)
|
|
}
|
|
if n.HasLastModifiedDate() {
|
|
t.Fatal("last modified flag set for a truncated timestamp")
|
|
}
|
|
n = new(Needle)
|
|
if _, err := n.readNeedleDataVersion2NonData([]byte{FlagHasLastModifiedDate, 0, 0, 0, 0, 42}); err != nil || !n.HasLastModifiedDate() || n.LastModified != 42 {
|
|
t.Fatalf("complete timestamp: err %v flag %v value %d", err, n.HasLastModifiedDate(), n.LastModified)
|
|
}
|
|
}
|