[pypy-svn] r49269 - in pypy/dist/pypy: module/_socket/test rlib rlib/test

arigo at codespeak.net arigo at codespeak.net
Sun Dec 2 12:44:50 CET 2007


Author: arigo
Date: Sun Dec  2 12:44:50 2007
New Revision: 49269

Modified:
   pypy/dist/pypy/module/_socket/test/test_sock_app.py
   pypy/dist/pypy/rlib/rsocket.py
   pypy/dist/pypy/rlib/test/test_rsocket.py
Log:
issue318 resolved

Fix accept() on AF_UNIX sockets.  Add tests.


Modified: pypy/dist/pypy/module/_socket/test/test_sock_app.py
==============================================================================
--- pypy/dist/pypy/module/_socket/test/test_sock_app.py	(original)
+++ pypy/dist/pypy/module/_socket/test/test_sock_app.py	Sun Dec  2 12:44:50 2007
@@ -1,13 +1,13 @@
 from pypy.conftest import gettestobjspace
 import sys
 import py
+from pypy.tool.udir import udir
 
 def setup_module(mod):
     mod.space = gettestobjspace(usemodules=['_socket'])
     global socket
     import socket
     mod.w_socket = space.appexec([], "(): import _socket as m; return m")
-    from pypy.tool.udir import udir
     mod.path = udir.join('fd')
     mod.path.write('fo')
     mod.raises = py.test.raises # make raises available from app-level tests
@@ -233,6 +233,7 @@
 class AppTestSocket:
     def setup_class(cls):
         cls.space = space
+        cls.w_udir = space.wrap(str(udir))
 
     def test_ntoa_exception(self):
         import _socket
@@ -407,7 +408,32 @@
         s = _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM, 0)
         s.sendto(buffer(''), ('localhost', 9)) # Send to discard port.
         s.close()
-        
+
+    def test_unix_socket_connect(self):
+        import _socket, os
+        if not hasattr(_socket, 'AF_UNIX'):
+            skip('AF_UNIX not supported.')
+        sockpath = os.path.join(self.udir, 'app_test_unix_socket_connect')
+
+        serversock = _socket.socket(_socket.AF_UNIX)
+        serversock.bind(sockpath)
+        serversock.listen(1)
+
+        clientsock = _socket.socket(_socket.AF_UNIX)
+        clientsock.connect(sockpath)
+        s, addr = serversock.accept()
+        assert not addr
+
+        s.send('X')
+        data = clientsock.recv(100)
+        assert data == 'X'
+        clientsock.send('Y')
+        data = s.recv(100)
+        assert data == 'Y'
+
+        clientsock.close()
+        s.close()
+
 
 class AppTestSocketTCP:
     def setup_class(cls):

Modified: pypy/dist/pypy/rlib/rsocket.py
==============================================================================
--- pypy/dist/pypy/rlib/rsocket.py	(original)
+++ pypy/dist/pypy/rlib/rsocket.py	Sun Dec  2 12:44:50 2007
@@ -377,7 +377,7 @@
     class UNIXAddress(Address):
         family = AF_UNIX
         struct = _c.sockaddr_un
-        minlen = offsetof(_c.sockaddr_un, 'c_sun_path') + 1
+        minlen = offsetof(_c.sockaddr_un, 'c_sun_path')
         maxlen = sizeof(struct)
 
         def __init__(self, path):
@@ -406,7 +406,7 @@
         def get_path(self):
             a = self.lock(_c.sockaddr_un)
             maxlength = self.addrlen - offsetof(_c.sockaddr_un, 'c_sun_path')
-            if _c.linux and a.c_sun_path[0] == '\x00':
+            if _c.linux and maxlength > 0 and a.c_sun_path[0] == '\x00':
                 # Linux abstract namespace
                 length = maxlength
             else:

Modified: pypy/dist/pypy/rlib/test/test_rsocket.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rsocket.py	(original)
+++ pypy/dist/pypy/rlib/test/test_rsocket.py	Sun Dec  2 12:44:50 2007
@@ -345,6 +345,32 @@
         py.test.skip("no inet_ntop()")
     assert inet_ntop(AF_INET, '\x01\x02\x03\x05') == '1.2.3.5'
 
+def test_unix_socket_connect():
+    if getattr(rsocket, 'AF_UNIX', None) is None:
+        py.test.skip('AF_UNIX not supported.')
+    from pypy.tool.udir import udir
+    sockpath = str(udir.join('test_unix_socket_connect'))
+    a = UNIXAddress(sockpath)
+
+    serversock = RSocket(AF_UNIX)
+    serversock.bind(a)
+    serversock.listen(1)
+
+    clientsock = RSocket(AF_UNIX)
+    clientsock.connect(a)
+    s, addr = serversock.accept()
+
+    s.send('X')
+    data = clientsock.recv(100)
+    assert data == 'X'
+    clientsock.send('Y')
+    data = s.recv(100)
+    assert data == 'Y'
+
+    clientsock.close()
+    s.close()
+
+
 class TestTCP:
     PORT = 50007
     HOST = 'localhost'



More information about the Pypy-commit mailing list