test: managed scrub scan witnesses straddle read boundaries
Astra R2 P2: the streaming-scan witness now places tokens across mbHolds' real read boundaries (reads 1, 2 and 17, the last beyond 16 MiB; one byte, half and all but one byte before, and just after), at the file's end, and where read 17 would end without a carried tail. A half token across a boundary is not a match. The scrub test's 18 MiB cloud-init.log holds its token across read 17's boundary, and its 17 MiB clean file now sits inside a scanned tree (/var/lib/cloud) and is kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+57
−4
internal/host/managedboot_linux_test.go+57 −4
CI failed — run details
diff --git a/internal/host/managedboot_linux_test.go b/internal/host/managedboot_linux_test.go| index c203cb1c1..61d20e160 100644 |
| --- a/internal/host/managedboot_linux_test.go |
| +++ b/internal/host/managedboot_linux_test.go |
| @@ -1019,10 +1019,14 @@ func TestManagedBootScrub(t *testing.T) { |
| f := newMBFixture(t) |
| // An oversized log holding the token across a scan-chunk boundary, and |
| // an oversized clean file beside it. |
| - big := bytes.Repeat([]byte("x"), 17<<20) |
| - copy(big[mbScanChunk-10:], mbtToken) |
| + big := bytes.Repeat([]byte("x"), 18<<20) |
| + at := mbReadBoundary(17, len(mbtToken)) - len(mbtToken)/2 |
| + if at <= 16<<20 { |
| + t.Fatalf("token at %d is not beyond 16 MiB", at) |
| + } |
| + copy(big[at:], mbtToken) |
| f.write("/var/log/cloud-init.log", big, 0o644) |
| - f.write("/var/log/big-clean.log", bytes.Repeat([]byte("y"), 17<<20), 0o644) |
| + f.write("/var/lib/cloud/instances/i-0abc/big-clean.dat", bytes.Repeat([]byte("y"), 17<<20), 0o600) |
| if err := f.boot(""); err != nil { |
| t.Fatal(err) |
| } |
| @@ -1049,7 +1053,7 @@ func TestManagedBootScrub(t *testing.T) { |
| t.Errorf("%s survives", p) |
| } |
| } |
| - for _, p := range []string{"/var/lib/cloud/instances/i-0abc/sem/config_scripts_user", "/var/lib/cloud/instances/i-0abc/obj.pkl", "/run/cloud-init/instance-data.json", "/var/log/cloud-init-output.log", "/var/lib/carlos-managed-boot/seed", "/var/log/big-clean.log"} { |
| + for _, p := range []string{"/var/lib/cloud/instances/i-0abc/sem/config_scripts_user", "/var/lib/cloud/instances/i-0abc/obj.pkl", "/run/cloud-init/instance-data.json", "/var/log/cloud-init-output.log", "/var/lib/carlos-managed-boot/seed", "/var/lib/cloud/instances/i-0abc/big-clean.dat"} { |
| if !f.exists(p) { |
| t.Errorf("%s was removed though it holds no token", p) |
| } |
| @@ -1469,3 +1473,52 @@ func TestManagedBootTransportClassLogged(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// mbReadBoundary is where mbHolds' k-th read ends (k >= 1): the first read |
| +// fills the whole buffer (mbScanChunk+len(needle) bytes), each later one |
| +// mbScanChunk+1 bytes after the carried len(needle)-1. |
| +func mbReadBoundary(k, needle int) int { return k*(mbScanChunk+1) + needle - 1 } |
| + |
| +func TestManagedBootScanStraddles(t *testing.T) { |
| + dir := t.TempDir() |
| + size := 18 << 20 |
| + n := len(mbtToken) |
| + cases := map[string]int{"whole in the first read": 100} |
| + for _, k := range []int{1, 2, 17} { |
| + b := mbReadBoundary(k, n) |
| + cases[fmt.Sprintf("read %d boundary, 1 byte before", k)] = b - 1 |
| + cases[fmt.Sprintf("read %d boundary, half", k)] = b - n/2 |
| + cases[fmt.Sprintf("read %d boundary, all but 1 byte before", k)] = b - (n - 1) |
| + cases[fmt.Sprintf("read %d boundary, just after", k)] = b |
| + } |
| + cases["at the very end"] = size - n |
| + // Where read 17 would end if no tail were carried: a scan that drops |
| + // the tail must still miss one token beyond 16 MiB. |
| + cases["read 17 boundary without a carried tail, half"] = 17*(mbScanChunk+n) - n/2 |
| + for name, at := range cases { |
| + t.Run(name, func(t *testing.T) { |
| + body := bytes.Repeat([]byte("x"), size) |
| + copy(body[at:], mbtToken) |
| + path := filepath.Join(dir, "f") |
| + if err := os.WriteFile(path, body, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, err := mbHolds(path, []byte(mbtToken)); err != nil || !got { |
| + t.Errorf("token at %d not found: %v %v", at, got, err) |
| + } |
| + }) |
| + } |
| + t.Run("clean", func(t *testing.T) { |
| + path := filepath.Join(dir, "clean") |
| + // Half a token on each side of a boundary is not a token. |
| + body := bytes.Repeat([]byte("x"), size) |
| + b := mbReadBoundary(2, n) |
| + copy(body[b-n/2:], mbtToken[:n/2]) |
| + if err := os.WriteFile(path, body, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, err := mbHolds(path, []byte(mbtToken)); err != nil || got { |
| + t.Errorf("clean file: %v %v", got, err) |
| + } |
| + }) |
| +} |