|
1 | 1 | package proxy
|
2 | 2 |
|
3 |
| -import "testing" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + "time" |
4 | 11 |
|
5 |
| -// Stub test file - tests removed |
6 |
| -func TestStub(t *testing.T) { |
7 |
| - // This is a stub test |
8 |
| - t.Skip("stub test file") |
| 12 | + "github.com/coder/boundary/audit" |
| 13 | + "github.com/coder/boundary/rules" |
| 14 | +) |
| 15 | + |
| 16 | +// mockAuditor is a simple mock auditor for testing |
| 17 | +type mockAuditor struct{} |
| 18 | + |
| 19 | +func (m *mockAuditor) AuditRequest(req audit.Request) { |
| 20 | + // No-op for testing |
| 21 | +} |
| 22 | + |
| 23 | +// TestProxyServerBasicHTTP tests basic HTTP request handling |
| 24 | +func TestProxyServerBasicHTTP(t *testing.T) { |
| 25 | + // Create test logger |
| 26 | + logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
| 27 | + Level: slog.LevelError, // Reduce noise during testing |
| 28 | + })) |
| 29 | + |
| 30 | + // Create test rules (allow all for testing) |
| 31 | + testRules, err := rules.ParseAllowSpecs([]string{"*"}) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Failed to parse test rules: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Create rule engine |
| 37 | + ruleEngine := rules.NewRuleEngine(testRules, logger) |
| 38 | + |
| 39 | + // Create mock auditor |
| 40 | + auditor := &mockAuditor{} |
| 41 | + |
| 42 | + // Create TLS config (minimal for testing) |
| 43 | + tlsConfig := &tls.Config{ |
| 44 | + MinVersion: tls.VersionTLS12, |
| 45 | + } |
| 46 | + |
| 47 | + // Create proxy server |
| 48 | + server := NewProxyServer(Config{ |
| 49 | + HTTPPort: 0, // Use random port |
| 50 | + RuleEngine: ruleEngine, |
| 51 | + Auditor: auditor, |
| 52 | + Logger: logger, |
| 53 | + TLSConfig: tlsConfig, |
| 54 | + }) |
| 55 | + |
| 56 | + // Create context with timeout |
| 57 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 58 | + defer cancel() |
| 59 | + |
| 60 | + // Start server in goroutine |
| 61 | + serverDone := make(chan error, 1) |
| 62 | + go func() { |
| 63 | + serverDone <- server.Start(ctx) |
| 64 | + }() |
| 65 | + |
| 66 | + // Give server time to start |
| 67 | + time.Sleep(100 * time.Millisecond) |
| 68 | + |
| 69 | + // Test basic HTTP request |
| 70 | + t.Run("BasicHTTPRequest", func(t *testing.T) { |
| 71 | + // Create HTTP client |
| 72 | + client := &http.Client{ |
| 73 | + Transport: &http.Transport{ |
| 74 | + TLSClientConfig: &tls.Config{ |
| 75 | + InsecureSkipVerify: true, // Skip cert verification for testing |
| 76 | + }, |
| 77 | + }, |
| 78 | + Timeout: 5 * time.Second, |
| 79 | + } |
| 80 | + |
| 81 | + // Make request to proxy |
| 82 | + req, err := http.NewRequest("GET", "http://localhost:8080/test", nil) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Failed to create request: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Set Host header (important for URL parsing) |
| 88 | + req.Host = "localhost:8080" |
| 89 | + |
| 90 | + // Make the request |
| 91 | + resp, err := client.Do(req) |
| 92 | + if err != nil { |
| 93 | + t.Logf("Request failed (expected for proxy without target): %v", err) |
| 94 | + // This is expected since we're not forwarding to a real target |
| 95 | + } else { |
| 96 | + resp.Body.Close() |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + // Test CONNECT request |
| 101 | + //t.Run("CONNECTRequest", func(t *testing.T) { |
| 102 | + // // Create HTTP client |
| 103 | + // client := &http.Client{ |
| 104 | + // Transport: &http.Transport{ |
| 105 | + // TLSClientConfig: &tls.Config{ |
| 106 | + // InsecureSkipVerify: true, |
| 107 | + // }, |
| 108 | + // }, |
| 109 | + // Timeout: 5 * time.Second, |
| 110 | + // } |
| 111 | + // |
| 112 | + // // Make CONNECT request |
| 113 | + // req, err := http.NewRequest("CONNECT", "http://example.com:443", nil) |
| 114 | + // if err != nil { |
| 115 | + // t.Fatalf("Failed to create CONNECT request: %v", err) |
| 116 | + // } |
| 117 | + // |
| 118 | + // // Set Host header |
| 119 | + // req.Host = "example.com:443" |
| 120 | + // |
| 121 | + // // Make the request |
| 122 | + // resp, err := client.Do(req) |
| 123 | + // if err != nil { |
| 124 | + // t.Logf("CONNECT request failed (expected for proxy without target): %v", err) |
| 125 | + // // This is expected since we're not forwarding to a real target |
| 126 | + // } else { |
| 127 | + // resp.Body.Close() |
| 128 | + // } |
| 129 | + //}) |
| 130 | + // |
| 131 | + //// Cancel context to stop server |
| 132 | + //cancel() |
| 133 | + // |
| 134 | + //// Wait for server to stop |
| 135 | + //select { |
| 136 | + //case err := <-serverDone: |
| 137 | + // if err != nil && err != context.Canceled { |
| 138 | + // t.Errorf("Server stopped with error: %v", err) |
| 139 | + // } |
| 140 | + //case <-time.After(5 * time.Second): |
| 141 | + // t.Error("Server did not stop within timeout") |
| 142 | + //} |
9 | 143 | }
|
| 144 | + |
| 145 | +// TestProxyServerWithRules tests proxy with specific rules |
| 146 | +//func TestProxyServerWithRules(t *testing.T) { |
| 147 | +// // Create test logger |
| 148 | +// logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
| 149 | +// Level: slog.LevelError, |
| 150 | +// })) |
| 151 | +// |
| 152 | +// // Create restrictive rules (only allow github.com) |
| 153 | +// testRules, err := rules.ParseAllowSpecs([]string{"github.com"}) |
| 154 | +// if err != nil { |
| 155 | +// t.Fatalf("Failed to parse test rules: %v", err) |
| 156 | +// } |
| 157 | +// |
| 158 | +// // Create rule engine |
| 159 | +// ruleEngine := rules.NewRuleEngine(testRules, logger) |
| 160 | +// |
| 161 | +// // Create mock auditor |
| 162 | +// auditor := &mockAuditor{} |
| 163 | +// |
| 164 | +// // Create TLS config |
| 165 | +// tlsConfig := &tls.Config{ |
| 166 | +// MinVersion: tls.VersionTLS12, |
| 167 | +// } |
| 168 | +// |
| 169 | +// // Create proxy server |
| 170 | +// server := NewProxyServer(Config{ |
| 171 | +// HTTPPort: 0, // Use random port |
| 172 | +// RuleEngine: ruleEngine, |
| 173 | +// Auditor: auditor, |
| 174 | +// Logger: logger, |
| 175 | +// TLSConfig: tlsConfig, |
| 176 | +// }) |
| 177 | +// |
| 178 | +// // Create context with timeout |
| 179 | +// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 180 | +// defer cancel() |
| 181 | +// |
| 182 | +// // Start server in goroutine |
| 183 | +// serverDone := make(chan error, 1) |
| 184 | +// go func() { |
| 185 | +// serverDone <- server.Start(ctx) |
| 186 | +// }() |
| 187 | +// |
| 188 | +// // Give server time to start |
| 189 | +// time.Sleep(100 * time.Millisecond) |
| 190 | +// |
| 191 | +// // Test allowed request |
| 192 | +// t.Run("AllowedRequest", func(t *testing.T) { |
| 193 | +// client := &http.Client{ |
| 194 | +// Transport: &http.Transport{ |
| 195 | +// TLSClientConfig: &tls.Config{ |
| 196 | +// InsecureSkipVerify: true, |
| 197 | +// }, |
| 198 | +// }, |
| 199 | +// Timeout: 5 * time.Second, |
| 200 | +// } |
| 201 | +// |
| 202 | +// req, err := http.NewRequest("GET", "http://localhost:8080/test", nil) |
| 203 | +// if err != nil { |
| 204 | +// t.Fatalf("Failed to create request: %v", err) |
| 205 | +// } |
| 206 | +// |
| 207 | +// // Set Host header to github.com (should be allowed) |
| 208 | +// req.Host = "github.com" |
| 209 | +// |
| 210 | +// resp, err := client.Do(req) |
| 211 | +// if err != nil { |
| 212 | +// t.Logf("Request failed (expected for proxy without target): %v", err) |
| 213 | +// } else { |
| 214 | +// resp.Body.Close() |
| 215 | +// } |
| 216 | +// }) |
| 217 | +// |
| 218 | +// // Test blocked request |
| 219 | +// t.Run("BlockedRequest", func(t *testing.T) { |
| 220 | +// client := &http.Client{ |
| 221 | +// Transport: &http.Transport{ |
| 222 | +// TLSClientConfig: &tls.Config{ |
| 223 | +// InsecureSkipVerify: true, |
| 224 | +// }, |
| 225 | +// }, |
| 226 | +// Timeout: 5 * time.Second, |
| 227 | +// } |
| 228 | +// |
| 229 | +// req, err := http.NewRequest("GET", "http://localhost:8080/test", nil) |
| 230 | +// if err != nil { |
| 231 | +// t.Fatalf("Failed to create request: %v", err) |
| 232 | +// } |
| 233 | +// |
| 234 | +// // Set Host header to example.com (should be blocked) |
| 235 | +// req.Host = "example.com" |
| 236 | +// |
| 237 | +// resp, err := client.Do(req) |
| 238 | +// if err != nil { |
| 239 | +// t.Logf("Request failed (expected for proxy without target): %v", err) |
| 240 | +// } else { |
| 241 | +// resp.Body.Close() |
| 242 | +// } |
| 243 | +// }) |
| 244 | +// |
| 245 | +// // Cancel context to stop server |
| 246 | +// cancel() |
| 247 | +// |
| 248 | +// // Wait for server to stop |
| 249 | +// select { |
| 250 | +// case err := <-serverDone: |
| 251 | +// if err != nil && err != context.Canceled { |
| 252 | +// t.Errorf("Server stopped with error: %v", err) |
| 253 | +// } |
| 254 | +// case <-time.After(5 * time.Second): |
| 255 | +// t.Error("Server did not stop within timeout") |
| 256 | +// } |
| 257 | +//} |
| 258 | +// |
| 259 | +//// TestProxyServerStartStop tests basic start/stop functionality |
| 260 | +//func TestProxyServerStartStop(t *testing.T) { |
| 261 | +// // Create test logger |
| 262 | +// logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
| 263 | +// Level: slog.LevelError, |
| 264 | +// })) |
| 265 | +// |
| 266 | +// // Create test rules |
| 267 | +// testRules, err := rules.ParseAllowSpecs([]string{"*"}) |
| 268 | +// if err != nil { |
| 269 | +// t.Fatalf("Failed to parse test rules: %v", err) |
| 270 | +// } |
| 271 | +// |
| 272 | +// ruleEngine := rules.NewRuleEngine(testRules, logger) |
| 273 | +// auditor := &mockAuditor{} |
| 274 | +// tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} |
| 275 | +// |
| 276 | +// // Create proxy server |
| 277 | +// server := NewProxyServer(Config{ |
| 278 | +// HTTPPort: 0, |
| 279 | +// RuleEngine: ruleEngine, |
| 280 | +// Auditor: auditor, |
| 281 | +// Logger: logger, |
| 282 | +// TLSConfig: tlsConfig, |
| 283 | +// }) |
| 284 | +// |
| 285 | +// // Test that server can be created |
| 286 | +// if server == nil { |
| 287 | +// t.Fatal("Failed to create proxy server") |
| 288 | +// } |
| 289 | +// |
| 290 | +// // Test that server can be stopped (even if not started) |
| 291 | +// err = server.Stop() |
| 292 | +// if err != nil { |
| 293 | +// t.Errorf("Stop() failed: %v", err) |
| 294 | +// } |
| 295 | +//} |
| 296 | +// |
| 297 | +//// TestProxyServerContextCancellation tests context cancellation |
| 298 | +//func TestProxyServerContextCancellation(t *testing.T) { |
| 299 | +// // Create test logger |
| 300 | +// logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{ |
| 301 | +// Level: slog.LevelError, |
| 302 | +// })) |
| 303 | +// |
| 304 | +// // Create test rules |
| 305 | +// testRules, err := rules.ParseAllowSpecs([]string{"*"}) |
| 306 | +// if err != nil { |
| 307 | +// t.Fatalf("Failed to parse test rules: %v", err) |
| 308 | +// } |
| 309 | +// |
| 310 | +// ruleEngine := rules.NewRuleEngine(testRules, logger) |
| 311 | +// auditor := &mockAuditor{} |
| 312 | +// tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12} |
| 313 | +// |
| 314 | +// // Create proxy server |
| 315 | +// server := NewProxyServer(Config{ |
| 316 | +// HTTPPort: 0, |
| 317 | +// RuleEngine: ruleEngine, |
| 318 | +// Auditor: auditor, |
| 319 | +// Logger: logger, |
| 320 | +// TLSConfig: tlsConfig, |
| 321 | +// }) |
| 322 | +// |
| 323 | +// // Create context that will be cancelled quickly |
| 324 | +// ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 325 | +// defer cancel() |
| 326 | +// |
| 327 | +// // Start server |
| 328 | +// serverDone := make(chan error, 1) |
| 329 | +// go func() { |
| 330 | +// serverDone <- server.Start(ctx) |
| 331 | +// }() |
| 332 | +// |
| 333 | +// // Wait for context cancellation |
| 334 | +// select { |
| 335 | +// case err := <-serverDone: |
| 336 | +// if err != nil && err != context.DeadlineExceeded { |
| 337 | +// t.Errorf("Server stopped with unexpected error: %v", err) |
| 338 | +// } |
| 339 | +// case <-time.After(1 * time.Second): |
| 340 | +// t.Error("Server did not stop within timeout") |
| 341 | +// } |
| 342 | +//} |
0 commit comments