|
5 | 5 | "fmt" |
6 | 6 | "io" |
7 | 7 | "net" |
| 8 | + "net/textproto" |
8 | 9 | "syscall" |
9 | 10 | "testing" |
10 | 11 | "time" |
@@ -417,3 +418,47 @@ func TestDialWithDialer(t *testing.T) { |
417 | 418 |
|
418 | 419 | assert.Equal(t, true, dialerCalled) |
419 | 420 | } |
| 421 | + |
| 422 | +func runEPSVWithResponse(response string) (int, error) { |
| 423 | + client, server := net.Pipe() |
| 424 | + tpServer := textproto.NewConn(server) |
| 425 | + go func() { |
| 426 | + defer tpServer.Close() |
| 427 | + _, _ = tpServer.ReadLine() |
| 428 | + tpServer.PrintfLine("229 %s", response) |
| 429 | + }() |
| 430 | + |
| 431 | + c := &ServerConn{options: &dialOptions{}, conn: textproto.NewConn(client)} |
| 432 | + defer c.conn.Close() |
| 433 | + return c.epsv() |
| 434 | +} |
| 435 | + |
| 436 | +func TestEPSV_Parse_Valid(t *testing.T) { |
| 437 | + port, err := runEPSVWithResponse("Entering Extended Passive Mode (|||4242|)") |
| 438 | + assert.NoError(t, err) |
| 439 | + assert.Equal(t, 4242, port) |
| 440 | +} |
| 441 | + |
| 442 | +func TestEPSV_Parse_MissingTrailingPipe_ShouldError(t *testing.T) { |
| 443 | + defer func() { |
| 444 | + if r := recover(); r != nil { |
| 445 | + t.Fatalf("epsv() panicked, want graceful error: %v", r) |
| 446 | + } |
| 447 | + }() |
| 448 | + _, err := runEPSVWithResponse("Entering Extended Passive Mode (|||4242)") |
| 449 | + if err == nil { |
| 450 | + t.Fatalf("expected error for malformed EPSV response, got nil") |
| 451 | + } |
| 452 | +} |
| 453 | + |
| 454 | +func TestEPSV_Parse_MissingPortBetweenPipes_ShouldError(t *testing.T) { |
| 455 | + defer func() { |
| 456 | + if r := recover(); r != nil { |
| 457 | + t.Fatalf("epsv() panicked, want graceful error: %v", r) |
| 458 | + } |
| 459 | + }() |
| 460 | + _, err := runEPSVWithResponse("Entering Extended Passive Mode (||||)") |
| 461 | + if err == nil { |
| 462 | + t.Fatalf("expected error for malformed EPSV response, got nil") |
| 463 | + } |
| 464 | +} |
0 commit comments