| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package capture_test
- import (
- "os"
- "os/exec"
- "path/filepath"
- "testing"
- "hth/internal/capture"
- )
- func FuzzReadRecords(f *testing.F) {
- f.Add([]byte("{\"v\":1,\"kind\":\"event\"}\n"))
- f.Add([]byte("{"))
- f.Fuzz(func(t *testing.T, data []byte) {
- p := filepath.Join(t.TempDir(), "records.jsonl")
- if err := os.WriteFile(p, data, 0o644); err != nil {
- t.Fatal(err)
- }
- _, _ = capture.ReadRecords(p)
- })
- }
- func TestWriteManifestReadonlyFails(t *testing.T) {
- dir := t.TempDir()
- notDir := filepath.Join(dir, "not-a-dir")
- if err := os.WriteFile(notDir, []byte("x"), 0o644); err != nil {
- t.Fatal(err)
- }
- err := capture.WriteManifest(notDir, capture.Manifest{Status: capture.StatusComplete, APIVersion: "hth.api.v1"})
- if err == nil {
- t.Fatal("writing a manifest through a non-directory must fail")
- }
- }
- func FuzzReadManifest(f *testing.F) {
- f.Add([]byte("{\"v\":1,\"kind\":\"manifest\"}\n"))
- f.Add([]byte("{}\n"))
- f.Add([]byte("{\"v\":1,\"kind\":\"manifest\",\"bonus\":1}\n"))
- f.Fuzz(func(t *testing.T, data []byte) {
- dir := t.TempDir()
- if err := os.WriteFile(filepath.Join(dir, "manifest.json"), data, 0o644); err != nil {
- t.Fatal(err)
- }
- _, _ = capture.ReadManifest(dir)
- })
- }
- func TestWriteManifestENOSPC(t *testing.T) {
- if os.Geteuid() != 0 {
- t.Skip("ENOSPC tmpfs requires root")
- }
- if _, err := exec.LookPath("mount"); err != nil {
- t.Skip("mount not available")
- }
- base := t.TempDir()
- mnt := filepath.Join(base, "mnt")
- if err := os.Mkdir(mnt, 0o755); err != nil {
- t.Fatal(err)
- }
- cmd := exec.Command("mount", "-t", "tmpfs", "-o", "size=8k,nr_inodes=8", "tmpfs", mnt)
- if out, err := cmd.CombinedOutput(); err != nil {
- t.Skipf("mount tmpfs: %v %s", err, out)
- }
- t.Cleanup(func() { _ = exec.Command("umount", mnt).Run() })
- payload := make([]byte, 4096)
- for i := range payload {
- payload[i] = 'x'
- }
- var fail error
- for i := 0; i < 32; i++ {
- fail = capture.WriteManifest(mnt, capture.Manifest{
- Status: capture.StatusIncomplete, APIVersion: "hth.api.v1", IncompleteReason: string(payload),
- })
- if fail != nil {
- break
- }
- }
- if fail == nil {
- t.Log("tmpfs did not hit ENOSPC; not treating as pass of disk-full")
- t.Skip("could not fill tmpfs")
- }
- }
|