cmd/gitbay/stdinpayload_test.go

c6c5cb340929ee44650344d2fe40f5badb7bd65a
gitbay/cmd/gitbay/stdinpayload_test.go

116 lines · 3072 bytes

  1package main
  2
  3import (
  4	"io"
  5	"os"
  6	"sort"
  7	"strings"
  8	"testing"
  9
 10	"github.com/spf13/cobra"
 11)
 12
 13// The piped path must stay byte-identical: a script doing
 14// `printf %s "$TOKEN" | gitbay repo secret set ...` has to send exactly
 15// the token, with no prompt written and no byte added or removed. This is
 16// the half that can silently corrupt a credential, so it is the half with
 17// a test.
 18func TestStdinPayloadPipedIsUntouched(t *testing.T) {
 19	defer swapTerminal(func(*os.File) bool { return false })()
 20
 21	r, w, err := os.Pipe()
 22	if err != nil {
 23		t.Fatal(err)
 24	}
 25	const token = "sqp_0123456789abcdef"
 26	go func() { io.WriteString(w, token); w.Close() }()
 27
 28	got, err := stdinPayload(r, "the secret value", true)
 29	if err != nil {
 30		t.Fatal(err)
 31	}
 32	if got != io.Reader(r) {
 33		t.Fatal("piped stdin was replaced rather than passed through")
 34	}
 35	b, _ := io.ReadAll(got)
 36	if string(b) != token {
 37		t.Fatalf("piped payload = %q, want %q", b, token)
 38	}
 39}
 40
 41// A non-terminal stdin must not be prompted at, in either mode. The
 42// prompt goes to stderr, so a test watching stdout would miss this.
 43func TestStdinPayloadPipedIsSilent(t *testing.T) {
 44	defer swapTerminal(func(*os.File) bool { return false })()
 45
 46	prev := os.Stderr
 47	r, w, err := os.Pipe()
 48	if err != nil {
 49		t.Fatal(err)
 50	}
 51	os.Stderr = w
 52	for _, secret := range []bool{false, true} {
 53		if _, err := stdinPayload(os.Stdin, "the secret value", secret); err != nil {
 54			os.Stderr = prev
 55			t.Fatalf("secret=%v: %v", secret, err)
 56		}
 57	}
 58	w.Close()
 59	os.Stderr = prev
 60	out, _ := io.ReadAll(r)
 61	if len(out) != 0 {
 62		t.Fatalf("piped stdin was prompted at: %q", out)
 63	}
 64}
 65
 66// On a terminal the non-secret path says what it wants and how to end it.
 67// Someone who does not know Ctrl-D is the person this exists for.
 68func TestStdinPayloadTerminalExplainsItself(t *testing.T) {
 69	defer swapTerminal(func(*os.File) bool { return true })()
 70
 71	prev := os.Stderr
 72	r, w, err := os.Pipe()
 73	if err != nil {
 74		t.Fatal(err)
 75	}
 76	os.Stderr = w
 77	_, err = stdinPayload(os.Stdin, "an SSH public key", false)
 78	w.Close()
 79	os.Stderr = prev
 80	if err != nil {
 81		t.Fatal(err)
 82	}
 83	out, _ := io.ReadAll(r)
 84	got := string(out)
 85	for _, want := range []string{"an SSH public key", "Ctrl-D"} {
 86		if !strings.Contains(got, want) {
 87			t.Errorf("terminal prompt %q lacks %q", got, want)
 88		}
 89	}
 90}
 91
 92// Every command whose payload is stdin names it, or the prompt above says
 93// the unhelpful word "input".
 94func TestStdinCommandsNameTheirPayload(t *testing.T) {
 95	var missing []string
 96	var walk func(*cobra.Command, string)
 97	walk = func(c *cobra.Command, path string) {
 98		if c.Annotations[stdinMode] == "always" && c.Annotations[stdinWhat] == "" {
 99			missing = append(missing, strings.TrimSpace(path))
100		}
101		for _, sub := range c.Commands() {
102			walk(sub, path+" "+sub.Name())
103		}
104	}
105	walk(newRoot(), "")
106	sort.Strings(missing)
107	if len(missing) > 0 {
108		t.Errorf("stdin-payload commands with no stdinWhat: %s", strings.Join(missing, ", "))
109	}
110}
111
112func swapTerminal(f func(*os.File) bool) func() {
113	prev := isTerminal
114	isTerminal = f
115	return func() { isTerminal = prev }
116}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form