package main_test import ( "context" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) // TestServeStopsOnABadSidecarAccount: serve reads CARLOS_SIDECAR_ACCOUNTS // and stops before it opens anything on an entry that is not a sqid, or on a // value set to a list that names no sqid at all ("," would otherwise parse // to nil and quietly turn the gate off). The timeout is the failure mode it // guards: a serve that ignored the variable would carry on and listen. func TestServeStopsOnABadSidecarAccount(t *testing.T) { bin := buildConsole(t) for _, c := range []struct{ val, want string }{ {"bab,ops", `"ops"`}, {",", "names no account sqid"}, {" , ", "names no account sqid"}, } { tmp := t.TempDir() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) cmd := exec.CommandContext(ctx, bin, "serve") cmd.Dir = tmp cmd.Env = append(os.Environ(), "CONSOLE_ORIGIN=http://127.0.0.1:1", "CONSOLE_ADDR=127.0.0.1:0", "CONSOLE_DB="+filepath.Join(tmp, "console.db"), // 32 zero bytes: a well-formed Ed25519 public key, so serve gets // past the key check to the variable under test. "CARLOS_RELEASE_PUBKEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", "CARLOS_RELEASE_KEY=", "CARLOS_SIDECAR_ACCOUNTS="+c.val, ) out, err := cmd.CombinedOutput() timedOut := ctx.Err() != nil cancel() if timedOut { t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: serve did not stop: %s", c.val, out) } if err == nil { t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: serve exited 0: %s", c.val, out) } if !strings.Contains(string(out), "CARLOS_SIDECAR_ACCOUNTS") || !strings.Contains(string(out), c.want) { t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: the refusal does not name the variable and %s: %s", c.val, c.want, out) } if _, err := os.Stat(filepath.Join(tmp, "console.db")); err == nil { t.Fatalf("CARLOS_SIDECAR_ACCOUNTS=%q: serve opened its database before refusing", c.val) } } }