1
# Copyright (c) 2008 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Tests for implementations of L{IReactorUNIX}.
8
from stat import S_IMODE
10
from socket import socket, SOCK_DGRAM
12
from socket import AF_UNIX
16
from twisted.trial import util
17
from twisted.internet.protocol import ServerFactory, DatagramProtocol
18
from twisted.internet.protocol import ConnectedDatagramProtocol
19
from twisted.internet.test.reactormixins import ReactorBuilder
22
_deprecatedModeMessage = (
23
'The mode parameter of %(interface)s.%(method)s will be removed. Do '
24
'not pass a value for it. Set permissions on the containing directory '
25
'before calling %(interface)s.%(method)s, instead.')
28
class UNIXFamilyMixin:
30
Test-helper defining mixin for things related to AF_UNIX sockets.
33
skip = "Platform does not support AF_UNIX sockets"
35
def _modeTest(self, methodName, path, factory):
37
Assert that the mode of the created unix socket is set to the mode
38
specified to the reactor method.
41
reactor = self.buildReactor()
42
unixPort = getattr(reactor, methodName)(path, factory, mode=mode)
43
unixPort.stopListening()
44
self.assertEqual(S_IMODE(stat(path).st_mode), mode)
47
def _deprecatedModeTest(self, interfaceName, methodName, path, factory):
49
Assert that a deprecation warning is emitted when a value is specified
50
for the mode parameter to the indicated reactor method.
52
reactor = self.buildReactor()
53
method = getattr(reactor, methodName)
54
port = self.assertWarns(
56
_deprecatedModeMessage % dict(
57
interface=interfaceName, method=methodName),
59
lambda: method(path, factory, mode=0246))
64
class UNIXTestsBuilder(UNIXFamilyMixin, ReactorBuilder):
66
Builder defining tests relating to L{IReactorUNIX}.
70
The UNIX socket created by L{IReactorUNIX.listenUNIX} is created with
73
self._modeTest('listenUNIX', self.mktemp(), ServerFactory())
74
test_mode.suppress = [
75
util.suppress(category=DeprecationWarning,
76
message=_deprecatedModeMessage % dict(
77
interface='IReactorUNIX',
78
method='listenUNIX'))]
81
def test_deprecatedMode(self):
83
Passing any value for the C{mode} parameter of L{listenUNIX} causes a
84
deprecation warning to be emitted.
86
self._deprecatedModeTest(
87
'IReactorUNIX', 'listenUNIX', self.mktemp(), ServerFactory())
91
class UNIXDatagramTestsBuilder(UNIXFamilyMixin, ReactorBuilder):
93
Builder defining tests relating to L{IReactorUNIXDatagram}.
95
# There's no corresponding test_connectMode because the mode parameter to
96
# connectUNIXDatagram has been completely ignored since that API was first
98
def test_listenMode(self):
100
The UNIX socket created by L{IReactorUNIXDatagram.listenUNIXDatagram}
101
is created with the mode specified.
103
self._modeTest('listenUNIXDatagram', self.mktemp(), DatagramProtocol())
104
test_listenMode.suppress = [
105
util.suppress(category=DeprecationWarning,
106
message=_deprecatedModeMessage % dict(
107
interface='IReactorUNIXDatagram',
108
method='listenUNIXDatagram'))]
111
def test_deprecatedListenMode(self):
113
Passing any value for the C{mode} parameter of L{listenUNIXDatagram}
114
causes a deprecation warning to be emitted.
116
self._deprecatedModeTest(
117
'IReactorUNIXDatagram', 'listenUNIXDatagram', self.mktemp(),
121
def test_deprecatedConnectMode(self):
123
Passing any value for the C{mode} parameter of L{connectUNIXDatagram}
124
causes a deprecation warning to be emitted.
127
server = socket(AF_UNIX, SOCK_DGRAM)
129
self.addCleanup(server.close)
131
self._deprecatedModeTest(
132
'IReactorUNIXDatagram', 'connectUNIXDatagram',
133
path, ConnectedDatagramProtocol())
136
globals().update(UNIXTestsBuilder.makeTestCaseClasses())
137
globals().update(UNIXDatagramTestsBuilder.makeTestCaseClasses())