cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
carrier/carrier_test.go
254lines · modecode
| 1 | package carrier |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "sync" |
| 10 | "testing" |
| 11 | |
| 12 | ws "github.com/gorilla/websocket" |
| 13 | "github.com/rs/zerolog" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | // example in Sec-Websocket-Key in rfc6455 |
| 19 | testSecWebsocketKey = "dGhlIHNhbXBsZSBub25jZQ==" |
| 20 | ) |
| 21 | |
| 22 | type testStreamer struct { |
| 23 | buf *bytes.Buffer |
| 24 | l sync.RWMutex |
| 25 | } |
| 26 | |
| 27 | func newTestStream() *testStreamer { |
| 28 | return &testStreamer{buf: new(bytes.Buffer)} |
| 29 | } |
| 30 | |
| 31 | func (s *testStreamer) Read(p []byte) (int, error) { |
| 32 | s.l.RLock() |
| 33 | defer s.l.RUnlock() |
| 34 | return s.buf.Read(p) |
| 35 | |
| 36 | } |
| 37 | |
| 38 | func (s *testStreamer) Write(p []byte) (int, error) { |
| 39 | s.l.Lock() |
| 40 | defer s.l.Unlock() |
| 41 | return s.buf.Write(p) |
| 42 | } |
| 43 | |
| 44 | func TestStartClient(t *testing.T) { |
| 45 | message := "Good morning Austin! Time for another sunny day in the great state of Texas." |
| 46 | log := zerolog.Nop() |
| 47 | wsConn := NewWSConnection(&log) |
| 48 | ts := newTestWebSocketServer() |
| 49 | defer ts.Close() |
| 50 | |
| 51 | buf := newTestStream() |
| 52 | options := &StartOptions{ |
| 53 | OriginURL: "http://" + ts.Listener.Addr().String(), |
| 54 | Headers: nil, |
| 55 | } |
| 56 | err := StartClient(wsConn, buf, options) |
| 57 | assert.NoError(t, err) |
| 58 | _, _ = buf.Write([]byte(message)) |
| 59 | |
| 60 | readBuffer := make([]byte, len(message)) |
| 61 | _, _ = buf.Read(readBuffer) |
| 62 | assert.Equal(t, message, string(readBuffer)) |
| 63 | } |
| 64 | |
| 65 | func TestStartServer(t *testing.T) { |
| 66 | listener, err := net.Listen("tcp", "localhost:") |
| 67 | if err != nil { |
| 68 | t.Fatalf("Error starting listener: %v", err) |
| 69 | } |
| 70 | message := "Good morning Austin! Time for another sunny day in the great state of Texas." |
| 71 | log := zerolog.Nop() |
| 72 | shutdownC := make(chan struct{}) |
| 73 | wsConn := NewWSConnection(&log) |
| 74 | ts := newTestWebSocketServer() |
| 75 | defer ts.Close() |
| 76 | options := &StartOptions{ |
| 77 | OriginURL: "http://" + ts.Listener.Addr().String(), |
| 78 | Headers: nil, |
| 79 | } |
| 80 | |
| 81 | go func() { |
| 82 | err := Serve(wsConn, listener, shutdownC, options) |
| 83 | if err != nil { |
| 84 | t.Errorf("Error running server: %v", err) |
| 85 | return |
| 86 | } |
| 87 | }() |
| 88 | |
| 89 | conn, err := net.Dial("tcp", listener.Addr().String()) |
| 90 | _, _ = conn.Write([]byte(message)) |
| 91 | |
| 92 | readBuffer := make([]byte, len(message)) |
| 93 | _, _ = conn.Read(readBuffer) |
| 94 | assert.Equal(t, string(readBuffer), message) |
| 95 | } |
| 96 | |
| 97 | func TestIsAccessResponse(t *testing.T) { |
| 98 | validLocationHeader := http.Header{} |
| 99 | validLocationHeader.Add("location", "https://test.cloudflareaccess.com/cdn-cgi/access/login/blahblah") |
| 100 | invalidLocationHeader := http.Header{} |
| 101 | invalidLocationHeader.Add("location", "https://google.com") |
| 102 | testCases := []struct { |
| 103 | Description string |
| 104 | In *http.Response |
| 105 | ExpectedOut bool |
| 106 | }{ |
| 107 | {"nil response", nil, false}, |
| 108 | {"redirect with no location", &http.Response{StatusCode: http.StatusFound}, false}, |
| 109 | {"200 ok", &http.Response{StatusCode: http.StatusOK}, false}, |
| 110 | {"redirect with location", &http.Response{StatusCode: http.StatusFound, Header: validLocationHeader}, true}, |
| 111 | {"redirect with invalid location", &http.Response{StatusCode: http.StatusFound, Header: invalidLocationHeader}, false}, |
| 112 | } |
| 113 | |
| 114 | for i, tc := range testCases { |
| 115 | if IsAccessResponse(tc.In) != tc.ExpectedOut { |
| 116 | t.Fatalf("Failed case %d -- %s", i, tc.Description) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | func newTestWebSocketServer() *httptest.Server { |
| 123 | upgrader := ws.Upgrader{ |
| 124 | ReadBufferSize: 1024, |
| 125 | WriteBufferSize: 1024, |
| 126 | } |
| 127 | |
| 128 | return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 129 | conn, _ := upgrader.Upgrade(w, r, nil) |
| 130 | defer conn.Close() |
| 131 | for { |
| 132 | mt, message, err := conn.ReadMessage() |
| 133 | if err != nil { |
| 134 | break |
| 135 | } |
| 136 | |
| 137 | if err := conn.WriteMessage(mt, []byte(message)); err != nil { |
| 138 | break |
| 139 | } |
| 140 | } |
| 141 | })) |
| 142 | } |
| 143 | |
| 144 | func testRequest(t *testing.T, url string, stream io.ReadWriter) *http.Request { |
| 145 | req, err := http.NewRequest("GET", url, stream) |
| 146 | if err != nil { |
| 147 | t.Fatalf("testRequestHeader error") |
| 148 | } |
| 149 | |
| 150 | req.Header.Add("Connection", "Upgrade") |
| 151 | req.Header.Add("Upgrade", "WebSocket") |
| 152 | req.Header.Add("Sec-Websocket-Key", testSecWebsocketKey) |
| 153 | req.Header.Add("Sec-Websocket-Protocol", "tunnel-protocol") |
| 154 | req.Header.Add("Sec-Websocket-Version", "13") |
| 155 | req.Header.Add("User-Agent", "curl/7.59.0") |
| 156 | |
| 157 | return req |
| 158 | } |
| 159 | |
| 160 | func TestBastionDestination(t *testing.T) { |
| 161 | tests := []struct { |
| 162 | name string |
| 163 | header http.Header |
| 164 | expectedDest string |
| 165 | wantErr bool |
| 166 | }{ |
| 167 | { |
| 168 | name: "hostname destination", |
| 169 | header: http.Header{ |
| 170 | cfJumpDestinationHeader: []string{"localhost"}, |
| 171 | }, |
| 172 | expectedDest: "localhost", |
| 173 | }, |
| 174 | { |
| 175 | name: "hostname destination with port", |
| 176 | header: http.Header{ |
| 177 | cfJumpDestinationHeader: []string{"localhost:9000"}, |
| 178 | }, |
| 179 | expectedDest: "localhost:9000", |
| 180 | }, |
| 181 | { |
| 182 | name: "hostname destination with scheme and port", |
| 183 | header: http.Header{ |
| 184 | cfJumpDestinationHeader: []string{"ssh://localhost:9000"}, |
| 185 | }, |
| 186 | expectedDest: "localhost:9000", |
| 187 | }, |
| 188 | { |
| 189 | name: "full hostname url", |
| 190 | header: http.Header{ |
| 191 | cfJumpDestinationHeader: []string{"ssh://localhost:9000/metrics"}, |
| 192 | }, |
| 193 | expectedDest: "localhost:9000", |
| 194 | }, |
| 195 | { |
| 196 | name: "hostname destination with port and path", |
| 197 | header: http.Header{ |
| 198 | cfJumpDestinationHeader: []string{"localhost:9000/metrics"}, |
| 199 | }, |
| 200 | expectedDest: "localhost:9000", |
| 201 | }, |
| 202 | { |
| 203 | name: "ip destination", |
| 204 | header: http.Header{ |
| 205 | cfJumpDestinationHeader: []string{"127.0.0.1"}, |
| 206 | }, |
| 207 | expectedDest: "127.0.0.1", |
| 208 | }, |
| 209 | { |
| 210 | name: "ip destination with port", |
| 211 | header: http.Header{ |
| 212 | cfJumpDestinationHeader: []string{"127.0.0.1:9000"}, |
| 213 | }, |
| 214 | expectedDest: "127.0.0.1:9000", |
| 215 | }, |
| 216 | { |
| 217 | name: "ip destination with port and path", |
| 218 | header: http.Header{ |
| 219 | cfJumpDestinationHeader: []string{"127.0.0.1:9000/metrics"}, |
| 220 | }, |
| 221 | expectedDest: "127.0.0.1:9000", |
| 222 | }, |
| 223 | { |
| 224 | name: "ip destination with schem and port", |
| 225 | header: http.Header{ |
| 226 | cfJumpDestinationHeader: []string{"tcp://127.0.0.1:9000"}, |
| 227 | }, |
| 228 | expectedDest: "127.0.0.1:9000", |
| 229 | }, |
| 230 | { |
| 231 | name: "full ip url", |
| 232 | header: http.Header{ |
| 233 | cfJumpDestinationHeader: []string{"ssh://127.0.0.1:9000/metrics"}, |
| 234 | }, |
| 235 | expectedDest: "127.0.0.1:9000", |
| 236 | }, |
| 237 | { |
| 238 | name: "no destination", |
| 239 | wantErr: true, |
| 240 | }, |
| 241 | } |
| 242 | for _, test := range tests { |
| 243 | r := &http.Request{ |
| 244 | Header: test.header, |
| 245 | } |
| 246 | dest, err := ResolveBastionDest(r) |
| 247 | if test.wantErr { |
| 248 | assert.Error(t, err, "Test %s expects error", test.name) |
| 249 | } else { |
| 250 | assert.NoError(t, err, "Test %s expects no error, got error %v", test.name, err) |
| 251 | assert.Equal(t, test.expectedDest, dest, "Test %s expect dest %s, got %s", test.name, test.expectedDest, dest) |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |