oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package provisioner
4
5import (
6 "bytes"
7 "fmt"
8 "io"
9 "log/slog"
10 "os"
11 "os/exec"
12 "testing"
13
14 "github.com/go-json-experiment/json"
15)
16
17func TestCLIPrivateOutput(t *testing.T) {
18 if os.Getenv("OFICINA_CLI_CHILD") == "1" {
19 args := []string{"provision", "init", "--state", os.Getenv("OFICINA_CLI_STATE"), "--floor-dir", os.Getenv("OFICINA_CLI_FLOOR"), "--password-fd", "3"}
20 if os.Getenv("OFICINA_CLI_INVALID") == "1" {
21 args = append(args, "--unknown", "canary-8f3a@example.invalid")
22 }
23 if e := Run(args, os.Stdout); e != nil {
24 slog.Error("provisioner", "err", e)
25 os.Exit(1)
26 }
27 return
28 }
29 state, floor := persistentDirectory(t), persistentDirectory(t)
30 run := func(invalid bool) ([]byte, []byte, error) {
31 reader, writer, e := os.Pipe()
32 if e != nil {
33 t.Fatal(e)
34 }
35 cmd := exec.Command(os.Args[0], "-test.run=^TestCLIPrivateOutput$")
36 cmd.ExtraFiles = []*os.File{reader}
37 cmd.Env = append(os.Environ(), "OFICINA_CLI_CHILD=1", "OFICINA_CLI_STATE="+state, "OFICINA_CLI_FLOOR="+floor)
38 if invalid {
39 cmd.Env = append(cmd.Env, "OFICINA_CLI_INVALID=1")
40 }
41 var out, errout bytes.Buffer
42 cmd.Stdout = &out
43 cmd.Stderr = &errout
44 if e = cmd.Start(); e != nil {
45 t.Fatal(e)
46 }
47 reader.Close()
48 io.WriteString(writer, "synthetic-private-password")
49 writer.Close()
50 e = cmd.Wait()
51 return out.Bytes(), errout.Bytes(), e
52 }
53 out, errout, e := run(false)
54 if e != nil {
55 t.Fatalf("child failed: %v %s", e, errout)
56 }
57 // The Go test runner adds PASS after the helper returns. Keep the actual Run
58 // result separate while still observing the complete process streams.
59 line, _, ok := bytes.Cut(out, []byte("\n"))
60 if !ok {
61 t.Fatal("missing structured output")
62 }
63 var got RegistryInfo
64 if json.Unmarshal(line, &got, json.RejectUnknownMembers(true)) != nil || got.Sequence != 0 || len(got.PublicKey) != 87 || len(got.Registry) != 43 || len(got.Device) != 43 {
65 t.Fatal("unexpected public result")
66 }
67 if len(errout) != 0 || bytes.Contains(out, []byte("synthetic-private-password")) {
68 t.Fatal("private CLI output")
69 }
70 out, errout, e = run(true)
71 if e == nil || len(out) != 0 || bytes.Contains(errout, []byte("canary-8f3a@example.invalid")) || bytes.Contains(errout, []byte("synthetic-private-password")) || !bytes.Contains(errout, []byte(ErrRefused.Error())) {
72 t.Fatal(fmt.Sprint("invalid input privacy: ", e))
73 }
74}
75