@@ -13,6 +13,7 @@ import (
13
13
"io"
14
14
"io/ioutil"
15
15
"net"
16
+ "net/http"
16
17
"testing"
17
18
"time"
18
19
)
@@ -83,7 +84,6 @@ func TestRequiredWithReadHeaderTimeout(t *testing.T) {
83
84
start := time .Now ()
84
85
85
86
l , err := net .Listen ("tcp" , "127.0.0.1:0" )
86
-
87
87
if err != nil {
88
88
t .Fatalf ("err: %v" , err )
89
89
}
@@ -138,7 +138,6 @@ func TestUseWithReadHeaderTimeout(t *testing.T) {
138
138
start := time .Now ()
139
139
140
140
l , err := net .Listen ("tcp" , "127.0.0.1:0" )
141
-
142
141
if err != nil {
143
142
t .Fatalf ("err: %v" , err )
144
143
}
@@ -848,6 +847,7 @@ func TestReadingIsRefusedWhenProxyHeaderPresentButNotAllowed(t *testing.T) {
848
847
t .Fatalf ("client error: %v" , err )
849
848
}
850
849
}
850
+
851
851
func TestIgnorePolicyIgnoresIpFromProxyHeader (t * testing.T ) {
852
852
l , err := net .Listen ("tcp" , "127.0.0.1:0" )
853
853
if err != nil {
@@ -1275,6 +1275,48 @@ func Test_ConnectionErrorsWhenHeaderValidationFails(t *testing.T) {
1275
1275
}
1276
1276
}
1277
1277
1278
+ func Test_ConnectionHandlesInvalidUpstreamError (t * testing.T ) {
1279
+ l , err := net .Listen ("tcp" , "localhost:8080" )
1280
+ if err != nil {
1281
+ t .Fatalf ("error creating listener: %v" , err )
1282
+ }
1283
+
1284
+ times := 0
1285
+
1286
+ newLn := & Listener {
1287
+ Listener : l ,
1288
+ ConnPolicy : func (_ ConnPolicyOptions ) (Policy , error ) {
1289
+ // Return the invalid upstream error on the first call, the listener
1290
+ // should remain open and accepting.
1291
+ if times == 0 {
1292
+ times ++
1293
+ return REJECT , ErrInvalidUpstream
1294
+ }
1295
+
1296
+ return REJECT , ErrNoProxyProtocol
1297
+ },
1298
+ }
1299
+
1300
+ // Kick off the listener and capture any error.
1301
+ var listenerErr error
1302
+ go func (t * testing.T ) {
1303
+ _ , listenerErr = newLn .Accept ()
1304
+ }(t )
1305
+
1306
+ // Make two calls to trigger the listener's accept, the first should experience
1307
+ // the ErrInvalidUpstream and keep the listener open, the second should experience
1308
+ // a different error which will cause the listener to close.
1309
+ _ , _ = http .Get ("http://localhost:8080" )
1310
+ if listenerErr != nil {
1311
+ t .Fatalf ("invalid upstream shouldn't return an error: %v" , listenerErr )
1312
+ }
1313
+
1314
+ _ , _ = http .Get ("http://localhost:8080" )
1315
+ if listenerErr == nil {
1316
+ t .Fatalf ("errors other than invalid upstream should error" )
1317
+ }
1318
+ }
1319
+
1278
1320
type TestTLSServer struct {
1279
1321
Listener net.Listener
1280
1322
@@ -1483,9 +1525,11 @@ func (c *testConn) ReadFrom(r io.Reader) (int64, error) {
1483
1525
b , err := ioutil .ReadAll (r )
1484
1526
return int64 (len (b )), err
1485
1527
}
1528
+
1486
1529
func (c * testConn ) Write (p []byte ) (int , error ) {
1487
1530
return len (p ), nil
1488
1531
}
1532
+
1489
1533
func (c * testConn ) Read (p []byte ) (int , error ) {
1490
1534
if c .reads == 0 {
1491
1535
return 0 , io .EOF
@@ -1534,7 +1578,7 @@ func TestCopyFromWrappedConnectionToWrappedConnection(t *testing.T) {
1534
1578
}
1535
1579
1536
1580
func benchmarkTCPProxy (size int , b * testing.B ) {
1537
- //create and start the echo backend
1581
+ // create and start the echo backend
1538
1582
backend , err := net .Listen ("tcp" , "127.0.0.1:0" )
1539
1583
if err != nil {
1540
1584
b .Fatalf ("err: %v" , err )
@@ -1555,7 +1599,7 @@ func benchmarkTCPProxy(size int, b *testing.B) {
1555
1599
}
1556
1600
}()
1557
1601
1558
- //start the proxyprotocol enabled tcp proxy
1602
+ // start the proxyprotocol enabled tcp proxy
1559
1603
l , err := net .Listen ("tcp" , "127.0.0.1:0" )
1560
1604
if err != nil {
1561
1605
b .Fatalf ("err: %v" , err )
@@ -1604,7 +1648,7 @@ func benchmarkTCPProxy(size int, b *testing.B) {
1604
1648
},
1605
1649
}
1606
1650
1607
- //now for the actual benchmark
1651
+ // now for the actual benchmark
1608
1652
b .ResetTimer ()
1609
1653
for n := 0 ; n < b .N ; n ++ {
1610
1654
conn , err := net .Dial ("tcp" , pl .Addr ().String ())
@@ -1615,16 +1659,15 @@ func benchmarkTCPProxy(size int, b *testing.B) {
1615
1659
if _ , err := header .WriteTo (conn ); err != nil {
1616
1660
b .Fatalf ("err: %v" , err )
1617
1661
}
1618
- //send data
1662
+ // send data
1619
1663
go func () {
1620
1664
_ , err = conn .Write (data )
1621
1665
_ = conn .(* net.TCPConn ).CloseWrite ()
1622
1666
if err != nil {
1623
1667
panic (fmt .Sprintf ("Failed to write data: %v" , err ))
1624
1668
}
1625
-
1626
1669
}()
1627
- //receive data
1670
+ // receive data
1628
1671
n , err := io .Copy (ioutil .Discard , conn )
1629
1672
if n != int64 (len (data )) {
1630
1673
b .Fatalf ("Expected to receive %d bytes, got %d" , len (data ), n )
@@ -1639,24 +1682,31 @@ func benchmarkTCPProxy(size int, b *testing.B) {
1639
1682
func BenchmarkTCPProxy16KB (b * testing.B ) {
1640
1683
benchmarkTCPProxy (16 * 1024 , b )
1641
1684
}
1685
+
1642
1686
func BenchmarkTCPProxy32KB (b * testing.B ) {
1643
1687
benchmarkTCPProxy (32 * 1024 , b )
1644
1688
}
1689
+
1645
1690
func BenchmarkTCPProxy64KB (b * testing.B ) {
1646
1691
benchmarkTCPProxy (64 * 1024 , b )
1647
1692
}
1693
+
1648
1694
func BenchmarkTCPProxy128KB (b * testing.B ) {
1649
1695
benchmarkTCPProxy (128 * 1024 , b )
1650
1696
}
1697
+
1651
1698
func BenchmarkTCPProxy256KB (b * testing.B ) {
1652
1699
benchmarkTCPProxy (256 * 1024 , b )
1653
1700
}
1701
+
1654
1702
func BenchmarkTCPProxy512KB (b * testing.B ) {
1655
1703
benchmarkTCPProxy (512 * 1024 , b )
1656
1704
}
1705
+
1657
1706
func BenchmarkTCPProxy1024KB (b * testing.B ) {
1658
1707
benchmarkTCPProxy (1024 * 1024 , b )
1659
1708
}
1709
+
1660
1710
func BenchmarkTCPProxy2048KB (b * testing.B ) {
1661
1711
benchmarkTCPProxy (2048 * 1024 , b )
1662
1712
}
0 commit comments