@@ -860,6 +860,63 @@ public void UnixSocketPermission_InvalidPermissionFails()
860860 ClassicAssert . IsFalse ( parseSuccessful ) ;
861861 }
862862
863+ [ Test ]
864+ [ TestCase ( ConnectionProtectionOption . No ) ]
865+ [ TestCase ( ConnectionProtectionOption . Local ) ]
866+ [ TestCase ( ConnectionProtectionOption . Yes ) ]
867+ public async Task ConnectionProtectionTest ( ConnectionProtectionOption connectionProtectionOption )
868+ {
869+ List < IPAddress > addresses = [ IPAddress . IPv6Loopback , IPAddress . Loopback ] ;
870+
871+ var hostname = TestUtils . GetHostName ( ) ;
872+
873+ var address = Dns . GetHostAddresses ( hostname ) . Where ( x => ! IPAddress . IsLoopback ( x ) ) . FirstOrDefault ( ) ;
874+ if ( address == default )
875+ {
876+ if ( connectionProtectionOption == ConnectionProtectionOption . Local )
877+ Assert . Ignore ( "No nonloopback address" ) ;
878+ }
879+ else
880+ {
881+ addresses . Add ( address ) ;
882+ }
883+
884+ var endpoints = addresses . Select ( address => new IPEndPoint ( address , TestUtils . TestPort ) ) . ToArray ( ) ;
885+ var server = TestUtils . CreateGarnetServer ( TestUtils . MethodTestDir , endpoints : endpoints ,
886+ enableDebugCommand : connectionProtectionOption ) ;
887+ server . Start ( ) ;
888+
889+ foreach ( var endpoint in endpoints )
890+ {
891+ var shouldfail = connectionProtectionOption == ConnectionProtectionOption . No ||
892+ ( ! IPAddress . IsLoopback ( endpoint . Address ) && connectionProtectionOption == ConnectionProtectionOption . Local ) ;
893+ var client = TestUtils . GetGarnetClientSession ( endPoint : endpoint ) ;
894+ client . Connect ( ) ;
895+
896+ try
897+ {
898+ var result = await client . ExecuteAsync ( "DEBUG" , "LOG" , "Loopback test" ) ;
899+ if ( shouldfail )
900+ Assert . Fail ( "Connection protection should have not allowed the command to run" ) ;
901+ else
902+ ClassicAssert . AreEqual ( "OK" , result ) ;
903+ }
904+ catch ( Exception ex )
905+ {
906+ if ( shouldfail )
907+ ClassicAssert . AreEqual ( "ERR" , ex . Message [ 0 ..3 ] ) ;
908+ else
909+ Assert . Fail ( "Connection protection should have allowed command from this address" ) ;
910+ }
911+ finally
912+ {
913+ client . Dispose ( ) ;
914+ }
915+ }
916+
917+ server . Dispose ( ) ;
918+ }
919+
863920 [ Test ]
864921 public async Task MultiTcpSocketTest ( )
865922 {
@@ -868,7 +925,7 @@ public async Task MultiTcpSocketTest()
868925 var addresses = Dns . GetHostAddresses ( hostname ) ;
869926 addresses = [ .. addresses , IPAddress . IPv6Loopback , IPAddress . Loopback ] ;
870927
871- var endpoints = addresses . Select ( address => new IPEndPoint ( address , TestUtils . TestPort ) ) . ToArray ( ) ;
928+ var endpoints = addresses . Distinct ( ) . Select ( address => new IPEndPoint ( address , TestUtils . TestPort ) ) . ToArray ( ) ;
872929 var server = TestUtils . CreateGarnetServer ( TestUtils . MethodTestDir , endpoints : endpoints ) ;
873930 server . Start ( ) ;
874931
0 commit comments