//go:build linux package host import ( _ "embed" "os" "path/filepath" "strconv" "strings" "testing" ) // leo-climbing's real tree as metadata only (dev cohort-1, step 8 readiness): // every path under /data, /opt/carlos, /etc/systemd/system and /etc/carlos // on 2026-10-19 at 21:07Z, read-only over SSM with find -xdev -printf // '\t%y\t%u:%g\t%m\t%s\t%n\t%P\t%l\n'. No file contents. The /etc/carlos // names that could be secrets are redacted. leo-units-post-8.1.tsv is what // the window's step 8.1 changes; leo-routes.tsv is leo's one registry route. var ( //go:embed testdata/leo-step8/leo-tree-snapshot.tsv leoTreeSnapshot string //go:embed testdata/leo-step8/leo-units-post-8.1.tsv leoUnitsPostWindow string //go:embed testdata/leo-step8/leo-routes.tsv leoRoutes string ) type leoRow struct { Root, Type, Owner string Mode os.FileMode Size int64 Path, Target string State string // post-8.1 rows only } func parseLeoRows(t *testing.T, raw string) []leoRow { t.Helper() var rows []leoRow for i, line := range strings.Split(strings.TrimRight(raw, "\n"), "\n") { if i == 0 { continue } f := strings.Split(line, "\t") if len(f) < 8 { t.Fatalf("snapshot line %d: %q", i+1, line) } mode, err := strconv.ParseUint(f[3], 8, 32) if err != nil { t.Fatalf("snapshot line %d mode: %v", i+1, err) } size, err := strconv.ParseInt(f[4], 10, 64) if err != nil { t.Fatal(err) } row := leoRow{Root: f[0], Type: f[1], Owner: f[2], Mode: unixMode(uint32(mode)), Size: size, Path: f[6], Target: f[7]} if len(f) > 8 { row.State = f[8] } rows = append(rows, row) } return rows } func unixMode(m uint32) os.FileMode { mode := os.FileMode(m & 0o777) if m&0o4000 != 0 { mode |= os.ModeSetuid } if m&0o2000 != 0 { mode |= os.ModeSetgid } if m&0o1000 != 0 { mode |= os.ModeSticky } return mode } // leoTreeRows is the snapshot with step 8.1 applied: the twelve window units // replaced by /dev/null links and the edge's wants link removed. The held // originals (under /etc/carlos) are left out: the window directory is not a // census root, and a witness makes its own. func leoTreeRows(t *testing.T, window bool) []leoRow { t.Helper() rows := parseLeoRows(t, leoTreeSnapshot) if !window { return rows } post := map[string]leoRow{} for _, r := range parseLeoRows(t, leoUnitsPostWindow) { if r.Root == "/etc/systemd/system" { post[r.Path] = r } } var out []leoRow for _, r := range rows { if p, ok := post[r.Path]; ok && r.Root == "/etc/systemd/system" { if p.State == "expected-post-8.1-absent" { continue } out = append(out, p) continue } out = append(out, r) } return out } // leoTreeOptions maps snapshot roots to where a test builds them, and // rewrites absolute link targets that point into a rebuilt root. type leoTreeOptions struct { Roots map[string]string // snapshot root -> directory // Content gives the synthetic bytes for a file a reader needs; nil // writes an empty file. Content func(root, path string) []byte // Owner, when set, applies a row's owner (root runner only). Owner func(path, owner string) error // Skip leaves a row out (and, for a directory, nothing below it is // expected to be skipped by the caller). Skip func(r leoRow) bool } func buildLeoTree(t *testing.T, rows []leoRow, o leoTreeOptions) { t.Helper() type dirMode struct { path string mode os.FileMode } var dirs []dirMode for _, r := range rows { base, ok := o.Roots[r.Root] if !ok || (o.Skip != nil && o.Skip(r)) { continue } path := base if r.Path != "." { path = filepath.Join(base, r.Path) } switch r.Type { case "d": if r.Path != "." { if err := os.Mkdir(path, 0o700); err != nil && !os.IsExist(err) { t.Fatal(err) } } dirs = append(dirs, dirMode{path, r.Mode}) case "f": var body []byte if o.Content != nil { body = o.Content(r.Root, r.Path) } if err := os.WriteFile(path, body, 0o600); err != nil { t.Fatal(err) } if err := os.Chmod(path, r.Mode); err != nil { t.Fatal(err) } case "l": target := r.Target for root, dir := range o.Roots { if target == root || strings.HasPrefix(target, root+"/") { target = dir + strings.TrimPrefix(target, root) } } if err := os.Symlink(target, path); err != nil { t.Fatal(err) } default: t.Fatalf("snapshot type %q at %s", r.Type, r.Path) } if o.Owner != nil { if err := o.Owner(path, r.Owner); err != nil { t.Fatal(err) } } } // Directory modes last, deepest first, so a 0700 or 0750 parent does // not stop the build of what is below it. for i := len(dirs) - 1; i >= 0; i-- { if err := os.Chmod(dirs[i].path, dirs[i].mode); err != nil { t.Fatal(err) } } }