vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
|
e075258f
Keenan Nemetz
16d ago
|
1 | package sshwire |
| 2 | ||
| 3 | import ( | |
| 4 | "bytes" | |
| 5 | "encoding/binary" | |
| 6 | "encoding/json" | |
| 7 | "errors" | |
| 8 | "io" | |
| 9 | "net" | |
|
30672859
Keenan Nemetz
7d ago
|
10 | "os" |
|
e075258f
Keenan Nemetz
16d ago
|
11 | "path/filepath" |
| 12 | "strings" | |
| 13 | "testing" | |
| 14 | "time" | |
| 15 | ) | |
| 16 | ||
| 17 | func TestFramesRoundTrip(t *testing.T) { | |
| 18 | for _, payload := range [][]byte{nil, []byte("x"), bytes.Repeat([]byte("a"), MaxFrame)} { | |
| 19 | var buf bytes.Buffer | |
| 20 | if err := WriteFrame(&buf, KindStdout, payload); err != nil { | |
| 21 | t.Fatalf("%d-byte frame: %v", len(payload), err) | |
| 22 | } | |
| 23 | kind, got, err := ReadFrame(&buf) | |
| 24 | if err != nil { | |
| 25 | t.Fatalf("%d-byte frame: %v", len(payload), err) | |
| 26 | } | |
| 27 | if kind != KindStdout || !bytes.Equal(got, payload) { | |
| 28 | t.Fatalf("%d-byte frame came back as %q, %d bytes", len(payload), kind, len(got)) | |
| 29 | } | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | func TestOversizedFramesAreRefused(t *testing.T) { | |
| 34 | var buf bytes.Buffer | |
| 35 | if err := WriteFrame(&buf, KindStdout, bytes.Repeat([]byte("a"), MaxFrame+1)); err == nil { | |
| 36 | t.Error("a frame larger than MaxFrame was written") | |
| 37 | } | |
| 38 | ||
| 39 | // A length this side never sent, which is what a broken peer would do: refuse | |
| 40 | // it before allocating the payload. | |
| 41 | var head [5]byte | |
| 42 | head[0] = byte(KindStdout) | |
| 43 | binary.BigEndian.PutUint32(head[1:], MaxFrame+1) | |
| 44 | if _, _, err := ReadFrame(bytes.NewReader(head[:])); err == nil { | |
| 45 | t.Error("an oversized length was accepted") | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | // TestExecRunsOneSession drives the client against a listener that answers by | |
| 50 | // hand, which is what `vole serve` will do: the client's input arrives, then | |
| 51 | // stdout, stderr, and a status. | |
| 52 | func TestExecRunsOneSession(t *testing.T) { | |
| 53 | srv := serveOnce(t, func(t *testing.T, conn net.Conn) { | |
| 54 | if err := WriteJSON(conn, KindAccept, Accept{OK: true}); err != nil { | |
| 55 | t.Error(err) | |
| 56 | return | |
| 57 | } | |
| 58 | var in bytes.Buffer | |
| 59 | for { | |
| 60 | kind, payload, err := ReadFrame(conn) | |
| 61 | if err != nil { | |
| 62 | t.Errorf("read: %v", err) | |
| 63 | return | |
| 64 | } | |
| 65 | if kind == KindStdinEOF { | |
| 66 | break | |
| 67 | } | |
| 68 | if kind != KindStdin { | |
| 69 | t.Errorf("unexpected %q frame before end of input", string(rune(kind))) | |
| 70 | return | |
| 71 | } | |
| 72 | in.Write(payload) | |
| 73 | } | |
| 74 | _ = WriteFrame(conn, KindStdout, append([]byte("saw "), in.Bytes()...)) | |
| 75 | _ = WriteFrame(conn, KindStderr, []byte("a warning")) | |
| 76 | _ = WriteJSON(conn, KindExit, Exit{Code: 129}) | |
| 77 | }) | |
| 78 | ||
| 79 | var out, errOut bytes.Buffer | |
| 80 | hello := Hello{Session: "tok", Command: "git-upload-pack 'demo.git'", Remote: "203.0.113.5 4242 192.0.2.10 22"} | |
| 81 | code, err := Exec(srv.socket, hello, strings.NewReader("hello"), &out, &errOut) | |
| 82 | if err != nil { | |
| 83 | t.Fatalf("exec: %v", err) | |
| 84 | } | |
| 85 | if code != 129 { | |
| 86 | t.Errorf("exit %d, want 129", code) | |
| 87 | } | |
| 88 | if out.String() != "saw hello" { | |
| 89 | t.Errorf("stdout %q", out.String()) | |
| 90 | } | |
| 91 | if errOut.String() != "a warning" { | |
| 92 | t.Errorf("stderr %q", errOut.String()) | |
| 93 | } | |
| 94 | if got := srv.hello(t); got != hello { | |
| 95 | t.Errorf("the hello lost fields: %+v", got) | |
| 96 | } | |
| 97 | } | |
| 98 | ||
| 99 | func TestExecReportsARefusal(t *testing.T) { | |
| 100 | srv := serveOnce(t, func(t *testing.T, conn net.Conn) { | |
| 101 | _ = WriteJSON(conn, KindAccept, Accept{Error: "this session token was already spent"}) | |
| 102 | }) | |
| 103 | _, err := Exec(srv.socket, Hello{Session: "tok"}, nil, io.Discard, io.Discard) | |
| 104 | if err == nil || !strings.Contains(err.Error(), "already spent") { | |
| 105 | t.Fatalf("exec = %v, want the refusal's reason", err) | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | func TestExecWithoutASocket(t *testing.T) { | |
| 110 | if _, err := Exec(filepath.Join(t.TempDir(), "missing.sock"), Hello{Session: "tok"}, nil, io.Discard, io.Discard); err == nil { | |
| 111 | t.Fatal("a session with no socket reached something") | |
| 112 | } | |
| 113 | if _, err := Exec("", Hello{Session: "tok"}, nil, io.Discard, io.Discard); err == nil { | |
| 114 | t.Fatal("an empty socket path was accepted") | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | func TestExecWithoutAnExitStatusFails(t *testing.T) { | |
| 119 | srv := serveOnce(t, func(t *testing.T, conn net.Conn) { | |
| 120 | _ = WriteJSON(conn, KindAccept, Accept{OK: true}) | |
| 121 | _ = WriteFrame(conn, KindStdout, []byte("partial")) | |
| 122 | // Then the connection ends, which a command that crashed would do. | |
| 123 | }) | |
| 124 | _, err := Exec(srv.socket, Hello{Session: "tok"}, nil, io.Discard, io.Discard) | |
| 125 | if err == nil { | |
| 126 | t.Fatal("a session that ended without a status was reported as complete") | |
| 127 | } | |
| 128 | if !errors.Is(err, io.EOF) && !strings.Contains(err.Error(), "exit status") { | |
| 129 | t.Errorf("error %q does not say what was missing", err) | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | // fakeServer is one accepted connection, answered by a test. | |
| 134 | type fakeServer struct { | |
| 135 | socket string | |
| 136 | ln net.Listener | |
| 137 | got chan Hello | |
| 138 | } | |
| 139 | ||
| 140 | // serveOnce starts a Unix listener and answers one connection with fn. | |
| 141 | func serveOnce(t *testing.T, fn func(*testing.T, net.Conn)) *fakeServer { | |
| 142 | t.Helper() | |
|
30672859
Keenan Nemetz
7d ago
|
143 | // macOS caps a Unix socket path at 103 bytes, and t.TempDir names the |
| 144 | // directory after the test, which can already be past the limit. A short, | |
| 145 | // nameless directory keeps the socket bindable on every platform. | |
| 146 | dir, err := os.MkdirTemp("", "sshwire-") | |
| 147 | if err != nil { | |
| 148 | t.Fatal(err) | |
| 149 | } | |
| 150 | t.Cleanup(func() { os.RemoveAll(dir) }) | |
| 151 | s := &fakeServer{socket: filepath.Join(dir, "ssh.sock"), got: make(chan Hello, 1)} | |
|
e075258f
Keenan Nemetz
16d ago
|
152 | ln, err := net.Listen("unix", s.socket) |
| 153 | if err != nil { | |
| 154 | t.Fatalf("listen: %v", err) | |
| 155 | } | |
| 156 | s.ln = ln | |
| 157 | t.Cleanup(func() { ln.Close() }) | |
| 158 | go func() { | |
| 159 | conn, err := ln.Accept() | |
| 160 | if err != nil { | |
| 161 | return | |
| 162 | } | |
| 163 | defer conn.Close() | |
| 164 | var hello Hello | |
| 165 | if kind, payload, err := ReadFrame(conn); err == nil && kind == KindHello { | |
| 166 | _ = json.Unmarshal(payload, &hello) | |
| 167 | } | |
| 168 | s.got <- hello | |
| 169 | fn(t, conn) | |
| 170 | }() | |
| 171 | return s | |
| 172 | } | |
| 173 | ||
| 174 | // hello waits for the request the client sent. | |
| 175 | func (s *fakeServer) hello(t *testing.T) Hello { | |
| 176 | t.Helper() | |
| 177 | select { | |
| 178 | case h := <-s.got: | |
| 179 | return h | |
| 180 | case <-time.After(10 * time.Second): | |
| 181 | t.Fatal("the client never sent a hello") | |
| 182 | return Hello{} | |
| 183 | } | |
| 184 | } |