@@ -1538,7 +1538,6 @@ def testRecvFromNegative(self):
15381538 def _testRecvFromNegative (self ):
15391539 self .cli .sendto (MSG , 0 , (HOST , self .port ))
15401540
1541-
15421541# Tests for the sendmsg()/recvmsg() interface. Where possible, the
15431542# same test code is used with different families and types of socket
15441543# (e.g. stream, datagram), and tests using recvmsg() are repeated
@@ -4241,6 +4240,66 @@ def testNameOverflow(self):
42414240 with socket .socket (socket .AF_UNIX , socket .SOCK_STREAM ) as s :
42424241 self .assertRaises (socket .error , s .bind , address )
42434242
4243+ def testStrName (self ):
4244+ # Check that an abstract name can be passed as a string.
4245+ s = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
4246+ try :
4247+ s .bind ("\x00 python\x00 test\x00 " )
4248+ self .assertEqual (s .getsockname (), b"\x00 python\x00 test\x00 " )
4249+ finally :
4250+ s .close ()
4251+
4252+ class TestUnixDomain (unittest .TestCase ):
4253+
4254+ def setUp (self ):
4255+ self .sock = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
4256+
4257+ def tearDown (self ):
4258+ self .sock .close ()
4259+
4260+ def encoded (self , path ):
4261+ # Return the given path encoded in the file system encoding,
4262+ # or skip the test if this is not possible.
4263+ try :
4264+ return os .fsencode (path )
4265+ except UnicodeEncodeError :
4266+ self .skipTest (
4267+ "Pathname {0!a} cannot be represented in file "
4268+ "system encoding {1!r}" .format (
4269+ path , sys .getfilesystemencoding ()))
4270+
4271+ def testStrAddr (self ):
4272+ # Test binding to and retrieving a normal string pathname.
4273+ path = os .path .abspath (support .TESTFN )
4274+ self .sock .bind (path )
4275+ self .addCleanup (support .unlink , path )
4276+ self .assertEqual (self .sock .getsockname (), path )
4277+
4278+ def testBytesAddr (self ):
4279+ # Test binding to a bytes pathname.
4280+ path = os .path .abspath (support .TESTFN )
4281+ self .sock .bind (self .encoded (path ))
4282+ self .addCleanup (support .unlink , path )
4283+ self .assertEqual (self .sock .getsockname (), path )
4284+
4285+ def testSurrogateescapeBind (self ):
4286+ # Test binding to a valid non-ASCII pathname, with the
4287+ # non-ASCII bytes supplied using surrogateescape encoding.
4288+ path = os .path .abspath (support .TESTFN_UNICODE )
4289+ b = self .encoded (path )
4290+ self .sock .bind (b .decode ("ascii" , "surrogateescape" ))
4291+ self .addCleanup (support .unlink , path )
4292+ self .assertEqual (self .sock .getsockname (), path )
4293+
4294+ def testUnencodableAddr (self ):
4295+ # Test binding to a pathname that cannot be encoded in the
4296+ # file system encoding.
4297+ if support .TESTFN_UNENCODABLE is None :
4298+ self .skipTest ("No unencodable filename available" )
4299+ path = os .path .abspath (support .TESTFN_UNENCODABLE )
4300+ self .sock .bind (path )
4301+ self .addCleanup (support .unlink , path )
4302+ self .assertEqual (self .sock .getsockname (), path )
42444303
42454304@unittest .skipUnless (thread , 'Threading required for this test.' )
42464305class BufferIOTest (SocketConnectedTest ):
@@ -4517,6 +4576,8 @@ def test_main():
45174576 ])
45184577 if hasattr (socket , "socketpair" ):
45194578 tests .append (BasicSocketPairTest )
4579+ if hasattr (socket , "AF_UNIX" ):
4580+ tests .append (TestUnixDomain )
45204581 if sys .platform == 'linux' :
45214582 tests .append (TestLinuxAbstractNamespace )
45224583 if isTipcAvailable ():
0 commit comments