@@ -15,6 +15,7 @@ import (
1515 "github.com/fclairamb/go-log/gokit"
1616 gklog "github.com/go-kit/log"
1717 "github.com/spf13/afero"
18+ "github.com/stretchr/testify/require"
1819)
1920
2021type tlsVerificationReply int8
@@ -557,3 +558,71 @@ B8waIgXRIjSWT4Fje7RTMT948qhguVhpoAgVzwzMqizzq6YIQbL7MHwXj7oZNUoQ
557558CARLpnYLaeWP2nxQyzwGx5pn9TJwg79Yknr8PbSjeym1BSbE5C9ruqar4PfiIzYx
558559di02m2YJAvRsG9VDpXogi+c=
559560-----END PRIVATE KEY-----` )
561+
562+ // TestPortRangeFetchNextError tests error handling in PortRange.FetchNext
563+ func TestPortRangeFetchNextError (t * testing.T ) {
564+ req := require .New (t )
565+
566+ // Test with a range that could potentially cause issues
567+ portRange := PortRange {
568+ Start : 65535 ,
569+ End : 65535 ,
570+ }
571+
572+ // This should still work since it's a valid range
573+ exposedPort , listenedPort , ok := portRange .FetchNext ()
574+ req .True (ok )
575+ req .Equal (65535 , exposedPort )
576+ req .Equal (65535 , listenedPort )
577+ }
578+
579+ // TestPortMappingRangeFetchNextError tests error handling in PortMappingRange.FetchNext
580+ func TestPortMappingRangeFetchNextError (t * testing.T ) {
581+ req := require .New (t )
582+
583+ // Test with a valid range
584+ portMappingRange := PortMappingRange {
585+ ExposedStart : 8000 ,
586+ ListenedStart : 9000 ,
587+ Count : 10 ,
588+ }
589+
590+ exposedPort , listenedPort , ok := portMappingRange .FetchNext ()
591+ req .True (ok )
592+ req .GreaterOrEqual (exposedPort , 8000 )
593+ req .LessOrEqual (exposedPort , 8009 )
594+ req .GreaterOrEqual (listenedPort , 9000 )
595+ req .LessOrEqual (listenedPort , 9009 )
596+ req .Equal (exposedPort - 8000 , listenedPort - 9000 ) // Should maintain offset
597+ }
598+
599+ // TestPortMappingRangeNumberAttempts tests the NumberAttempts method
600+ func TestPortMappingRangeNumberAttempts (t * testing.T ) {
601+ req := require .New (t )
602+
603+ portMappingRange := PortMappingRange {
604+ ExposedStart : 8000 ,
605+ ListenedStart : 9000 ,
606+ Count : 25 ,
607+ }
608+
609+ req .Equal (25 , portMappingRange .NumberAttempts ())
610+ }
611+
612+ // TestCryptoRandError tests what happens when crypto/rand fails
613+ func TestCryptoRandError (t * testing.T ) {
614+ req := require .New (t )
615+
616+ // We can't easily mock crypto/rand.Int, but we can test with edge cases
617+ // Test with zero count (should handle gracefully)
618+ portMappingRange := PortMappingRange {
619+ ExposedStart : 8000 ,
620+ ListenedStart : 9000 ,
621+ Count : 1 , // Minimum valid count
622+ }
623+
624+ exposedPort , listenedPort , ok := portMappingRange .FetchNext ()
625+ req .True (ok )
626+ req .Equal (8000 , exposedPort )
627+ req .Equal (9000 , listenedPort )
628+ }
0 commit comments