|
15 | 15 | package main |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "bufio" |
| 19 | + "bytes" |
18 | 20 | "context" |
| 21 | + "io" |
| 22 | + "net" |
19 | 23 | "os" |
20 | 24 | "path/filepath" |
| 25 | + "strings" |
21 | 26 | "syscall" |
22 | 27 | "testing" |
23 | 28 | "time" |
@@ -69,3 +74,164 @@ func TestIngressSocketIsOwnerOnly(t *testing.T) { |
69 | 74 | t.Error("serveIngress did not return after cancellation") |
70 | 75 | } |
71 | 76 | } |
| 77 | + |
| 78 | +// floodConn is a client that opens the ingress socket and then never sends the |
| 79 | +// newline the handshake ends with. It counts what the handler reads, so a test |
| 80 | +// can hold the handler to the bound the package documents. |
| 81 | +type floodConn struct { |
| 82 | + read int |
| 83 | + limit int |
| 84 | + reply bytes.Buffer |
| 85 | +} |
| 86 | + |
| 87 | +func (c *floodConn) Read(p []byte) (int, error) { |
| 88 | + if c.read >= c.limit { |
| 89 | + return 0, io.EOF |
| 90 | + } |
| 91 | + n := len(p) |
| 92 | + if remaining := c.limit - c.read; n > remaining { |
| 93 | + n = remaining |
| 94 | + } |
| 95 | + for i := range p[:n] { |
| 96 | + p[i] = 'A' |
| 97 | + } |
| 98 | + c.read += n |
| 99 | + return n, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (c *floodConn) Write(p []byte) (int, error) { return c.reply.Write(p) } |
| 103 | +func (c *floodConn) Close() error { return nil } |
| 104 | +func (c *floodConn) LocalAddr() net.Addr { return floodAddr{} } |
| 105 | +func (c *floodConn) RemoteAddr() net.Addr { return floodAddr{} } |
| 106 | +func (c *floodConn) SetDeadline(time.Time) error { return nil } |
| 107 | +func (c *floodConn) SetReadDeadline(time.Time) error { return nil } |
| 108 | +func (c *floodConn) SetWriteDeadline(time.Time) error { return nil } |
| 109 | + |
| 110 | +type floodAddr struct{} |
| 111 | + |
| 112 | +func (floodAddr) Network() string { return "flood" } |
| 113 | +func (floodAddr) String() string { return "flood" } |
| 114 | + |
| 115 | +// TestHandleIngressBoundsTheHandshake holds the handshake read to the size the |
| 116 | +// package names. ingressMaxHandshake sizes a bufio.Reader, and that bounds only |
| 117 | +// what one fill holds: ReadString goes on growing a buffer of its own until it |
| 118 | +// finds a newline, so a client that sends none could make this process -- PID 1 |
| 119 | +// in the sandbox -- accumulate for as long as the read deadline allows. |
| 120 | +func TestHandleIngressBoundsTheHandshake(t *testing.T) { |
| 121 | + const flood = 1 << 20 |
| 122 | + conn := &floodConn{limit: flood} |
| 123 | + |
| 124 | + err := handleIngress(context.Background(), conn) |
| 125 | + if err == nil { |
| 126 | + t.Fatal("handleIngress accepted a handshake with no newline, want an error") |
| 127 | + } |
| 128 | + if got := conn.read; got > ingressMaxHandshake { |
| 129 | + t.Errorf("handleIngress read %d bytes of a %d byte flood, want at most %d", got, flood, ingressMaxHandshake) |
| 130 | + } |
| 131 | + if answer := conn.reply.String(); !strings.HasPrefix(answer, "ERR ") { |
| 132 | + t.Errorf("handleIngress answered %q, want an ERR line: a gateway that gets nothing back cannot tell a refusal from a sandbox that never started", answer) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// TestHandleIngressRelaysPipelinedBytes covers what a bounded read must not |
| 137 | +// break. The gateway may send its first request bytes in the same write as the |
| 138 | +// handshake, and those are in the reader rather than the socket by the time the |
| 139 | +// agent is dialled, so they are forwarded by hand. |
| 140 | +func TestHandleIngressRelaysPipelinedBytes(t *testing.T) { |
| 141 | + const pipelined = "HELLO" |
| 142 | + |
| 143 | + agent, err := net.Listen("tcp", "127.0.0.1:0") |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("listen as the agent: %v", err) |
| 146 | + } |
| 147 | + defer func() { _ = agent.Close() }() |
| 148 | + _, port, err := net.SplitHostPort(agent.Addr().String()) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("split the agent address: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + delivered := make(chan string, 1) |
| 154 | + go func() { |
| 155 | + c, err := agent.Accept() |
| 156 | + if err != nil { |
| 157 | + delivered <- "accept: " + err.Error() |
| 158 | + return |
| 159 | + } |
| 160 | + defer func() { _ = c.Close() }() |
| 161 | + buf := make([]byte, len(pipelined)) |
| 162 | + if _, err := io.ReadFull(c, buf); err != nil { |
| 163 | + delivered <- "read: " + err.Error() |
| 164 | + return |
| 165 | + } |
| 166 | + delivered <- string(buf) |
| 167 | + }() |
| 168 | + |
| 169 | + client, server := net.Pipe() |
| 170 | + defer func() { _ = client.Close() }() |
| 171 | + done := make(chan error, 1) |
| 172 | + go func() { done <- handleIngress(context.Background(), server) }() |
| 173 | + go func() { _, _ = io.WriteString(client, "CONNECT "+port+"\n"+pipelined) }() |
| 174 | + |
| 175 | + reply, err := bufio.NewReader(io.LimitReader(client, 128)).ReadString('\n') |
| 176 | + if err != nil { |
| 177 | + t.Fatalf("read the handshake answer: %v", err) |
| 178 | + } |
| 179 | + if got := strings.TrimSpace(reply); got != "OK" { |
| 180 | + t.Fatalf("the handshake answer is %q, want OK", got) |
| 181 | + } |
| 182 | + |
| 183 | + select { |
| 184 | + case got := <-delivered: |
| 185 | + if got != pipelined { |
| 186 | + t.Errorf("the agent received %q, want %q", got, pipelined) |
| 187 | + } |
| 188 | + case <-time.After(5 * time.Second): |
| 189 | + t.Fatal("the agent never received the bytes pipelined behind the handshake") |
| 190 | + } |
| 191 | + |
| 192 | + select { |
| 193 | + case <-done: |
| 194 | + case <-time.After(5 * time.Second): |
| 195 | + t.Error("handleIngress did not return once both ends were done") |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +// TestParseIngressConnect pins the one line the gateway sends first. Everything |
| 200 | +// past it is relayed verbatim, so this is where a malformed request has to stop. |
| 201 | +func TestParseIngressConnect(t *testing.T) { |
| 202 | + for _, tc := range []struct { |
| 203 | + name string |
| 204 | + line string |
| 205 | + want int |
| 206 | + }{ |
| 207 | + {name: "port", line: "CONNECT 8080\n", want: 8080}, |
| 208 | + {name: "lowercase verb", line: "connect 8080\n", want: 8080}, |
| 209 | + {name: "extra spaces", line: " CONNECT 8080 \n", want: 8080}, |
| 210 | + {name: "lowest port", line: "CONNECT 1\n", want: 1}, |
| 211 | + {name: "highest port", line: "CONNECT 65535\n", want: 65535}, |
| 212 | + {name: "port zero", line: "CONNECT 0\n"}, |
| 213 | + {name: "above the port range", line: "CONNECT 65536\n"}, |
| 214 | + {name: "negative", line: "CONNECT -1\n"}, |
| 215 | + {name: "not a number", line: "CONNECT http\n"}, |
| 216 | + {name: "wrong verb", line: "GET 8080\n"}, |
| 217 | + {name: "no port", line: "CONNECT\n"}, |
| 218 | + {name: "trailing junk", line: "CONNECT 8080 now\n"}, |
| 219 | + {name: "empty", line: "\n"}, |
| 220 | + } { |
| 221 | + t.Run(tc.name, func(t *testing.T) { |
| 222 | + got, err := parseIngressConnect(tc.line) |
| 223 | + if tc.want == 0 { |
| 224 | + if err == nil { |
| 225 | + t.Fatalf("parseIngressConnect(%q) = %d, want an error", tc.line, got) |
| 226 | + } |
| 227 | + return |
| 228 | + } |
| 229 | + if err != nil { |
| 230 | + t.Fatalf("parseIngressConnect(%q) returned %v, want %d", tc.line, err, tc.want) |
| 231 | + } |
| 232 | + if got != tc.want { |
| 233 | + t.Errorf("parseIngressConnect(%q) = %d, want %d", tc.line, got, tc.want) |
| 234 | + } |
| 235 | + }) |
| 236 | + } |
| 237 | +} |
0 commit comments