| 1 | package main_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // TestServeStopsOnABadSidecarAccount: serve reads CARLOS_SIDECAR_ACCOUNTS |
| 14 | // and stops before it opens anything on an entry that is not a sqid, or on a |
| 15 | // value set to a list that names no sqid at all ("," would otherwise parse |
| 16 | // to nil and quietly turn the gate off). The timeout is the failure mode it |
| 17 | // guards: a serve that ignored the variable would carry on and listen. |
| 18 | func TestServeStopsOnABadSidecarAccount(t *testing.T) { |
| 19 | bin := buildConsole(t) |
| 20 | for _, c := range []struct{ val, want string }{ |
| 21 | {"bab,ops", `"ops"`}, |
| 22 | {",", "names no account sqid"}, |
| 23 | {" , ", "names no account sqid"}, |
| 24 | } { |
| 25 | tmp := t.TempDir() |
| 26 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 27 | cmd := exec.CommandContext(ctx, bin, "serve") |
| 28 | cmd.Dir = tmp |
| 29 | cmd.Env = append(os.Environ(), |
| 30 | "CONSOLE_ORIGIN=http://127.0.0.1:1", |
| 31 | "CONSOLE_ADDR=127.0.0.1:0", |
| 32 | "CONSOLE_DB="+filepath.Join(tmp, "console.db"), |
| 33 | // 32 zero bytes: a well-formed Ed25519 public key, so serve gets |
| 34 | // past the key check to the variable under test. |
| 35 | "CARLOS_RELEASE_PUBKEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", |
| 36 | "CARLOS_RELEASE_KEY=", |
| 37 | "CARLOS_SIDECAR_ACCOUNTS="+c.val, |
| 38 | ) |
| 39 | out, err := cmd.CombinedOutput() |
| 40 | timedOut := ctx.Err() != nil |
| 41 | cancel() |
| 42 | if timedOut { |
| 43 | t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: serve did not stop: %s", c.val, out) |
| 44 | } |
| 45 | if err == nil { |
| 46 | t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: serve exited 0: %s", c.val, out) |
| 47 | } |
| 48 | if !strings.Contains(string(out), "CARLOS_SIDECAR_ACCOUNTS") || !strings.Contains(string(out), c.want) { |
| 49 | t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: the refusal does not name the variable and %s: %s", c.val, c.want, out) |
| 50 | } |
| 51 | if _, err := os.Stat(filepath.Join(tmp, "console.db")); err == nil { |
| 52 | t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: serve opened its database before refusing", c.val) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |