Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / test / test_socket.py
1
2 import errno, socket
3
4 from twisted.python.log import err
5 from twisted.internet.interfaces import IReactorSocket
6 from twisted.internet.error import (
7     UnsupportedAddressFamily, UnsupportedSocketType)
8 from twisted.internet.protocol import ServerFactory
9 from twisted.internet.test.reactormixins import (
10     ReactorBuilder, needsRunningReactor)
11
12
13 class AdoptStreamPortErrorsTestsBuilder(ReactorBuilder):
14     """
15     Builder for testing L{IReactorSocket} implementations.
16
17     Generally only tests for failure cases are found here.  Success cases for
18     this interface are tested elsewhere.  For example, the success case for
19     I{AF_INET} is in L{twisted.internet.test.test_tcp}, since that case should
20     behave exactly the same as L{IReactorTCP.listenTCP}.
21     """
22     requiredInterfaces = [IReactorSocket]
23
24     def test_invalidDescriptor(self):
25         """
26         An implementation of L{IReactorSocket.adoptStreamPort} raises
27         L{socket.error} if passed an integer which is not associated with a
28         socket.
29         """
30         reactor = self.buildReactor()
31
32         probe = socket.socket()
33         fileno = probe.fileno()
34         probe.close()
35
36         exc = self.assertRaises(
37             socket.error,
38             reactor.adoptStreamPort, fileno, socket.AF_INET, ServerFactory())
39         self.assertEqual(exc.args[0], errno.EBADF)
40
41
42     def test_invalidAddressFamily(self):
43         """
44         An implementation of L{IReactorSocket.adoptStreamPort} raises
45         L{UnsupportedAddressFamily} if passed an address family it does not
46         support.
47         """
48         reactor = self.buildReactor()
49
50         port = socket.socket()
51         port.listen(1)
52         self.addCleanup(port.close)
53
54         arbitrary = 2 ** 16 + 7
55
56         self.assertRaises(
57             UnsupportedAddressFamily,
58             reactor.adoptStreamPort, port.fileno(), arbitrary, ServerFactory())
59
60
61     def test_stopOnlyCloses(self):
62         """
63         When the L{IListeningPort} returned by L{IReactorSocket.adoptStreamPort}
64         is stopped using C{stopListening}, the underlying socket is closed but
65         not shutdown.  This allows another process which still has a reference
66         to it to continue accepting connections over it.
67         """
68         reactor = self.buildReactor()
69
70         portSocket = socket.socket()
71         self.addCleanup(portSocket.close)
72
73         portSocket.listen(1)
74         portSocket.setblocking(False)
75
76         # The file descriptor is duplicated by adoptStreamPort
77         port = reactor.adoptStreamPort(
78             portSocket.fileno(), portSocket.family, ServerFactory())
79         d = port.stopListening()
80         def stopped(ignored):
81             # Should still be possible to accept a connection on portSocket.  If
82             # it was shutdown, the exception would be EINVAL instead.
83             exc = self.assertRaises(socket.error, portSocket.accept)
84             self.assertEqual(exc.args[0], errno.EAGAIN)
85         d.addCallback(stopped)
86         d.addErrback(err, "Failed to accept on original port.")
87
88         needsRunningReactor(
89             reactor,
90             lambda: d.addCallback(lambda ignored: reactor.stop()))
91
92         reactor.run()
93
94
95
96 globals().update(AdoptStreamPortErrorsTestsBuilder.makeTestCaseClasses())