test_sendmsg.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python.sendmsg}.
  5. """
  6. import sys
  7. import errno
  8. import warnings
  9. from os import devnull, pipe, read, close, pathsep
  10. from struct import pack
  11. from socket import SOL_SOCKET, AF_INET, AF_INET6, socket, error
  12. try:
  13. from socket import AF_UNIX, socketpair
  14. except ImportError:
  15. nonUNIXSkip = "Platform does not support AF_UNIX sockets"
  16. else:
  17. nonUNIXSkip = None
  18. from twisted.internet import reactor
  19. from twisted.internet.defer import Deferred, inlineCallbacks
  20. from twisted.internet.error import ProcessDone
  21. from twisted.internet.protocol import ProcessProtocol
  22. from twisted.python.compat import _PY3, intToBytes, bytesEnviron
  23. from twisted.python.filepath import FilePath
  24. from twisted.python.runtime import platform
  25. from twisted.trial.unittest import TestCase
  26. if platform.isLinux():
  27. from socket import MSG_DONTWAIT
  28. dontWaitSkip = None
  29. else:
  30. # It would be nice to be able to test flags on more platforms, but finding
  31. # a flag that works *at all* is somewhat challenging.
  32. dontWaitSkip = "MSG_DONTWAIT is only known to work as intended on Linux"
  33. try:
  34. from twisted.python.sendmsg import sendmsg, recvmsg
  35. from twisted.python.sendmsg import SCM_RIGHTS, getSocketFamily
  36. except ImportError:
  37. importSkip = "Platform doesn't support sendmsg."
  38. else:
  39. importSkip = None
  40. try:
  41. from twisted.python.sendmsg import send1msg, recv1msg
  42. from twisted.python.sendmsg import getsockfam
  43. except ImportError:
  44. CModuleImportSkip = "Cannot import twisted.python.sendmsg"
  45. else:
  46. CModuleImportSkip = None
  47. class _FDHolder(object):
  48. """
  49. A wrapper around a FD that will remember if it has been closed or not.
  50. """
  51. def __init__(self, fd):
  52. self._fd = fd
  53. def fileno(self):
  54. """
  55. Return the fileno of this FD.
  56. """
  57. return self._fd
  58. def close(self):
  59. """
  60. Close the FD. If it's already been closed, do nothing.
  61. """
  62. if self._fd:
  63. close(self._fd)
  64. self._fd = None
  65. def __del__(self):
  66. """
  67. If C{self._fd} is unclosed, raise a warning.
  68. """
  69. if self._fd:
  70. if not _PY3:
  71. ResourceWarning = Warning
  72. warnings.warn("FD %s was not closed!" % (self._fd,),
  73. ResourceWarning)
  74. self.close()
  75. def __enter__(self):
  76. return self
  77. def __exit__(self, exc_type, exc_value, traceback):
  78. self.close()
  79. def _makePipe():
  80. """
  81. Create a pipe, and return the two FDs wrapped in L{_FDHolders}.
  82. """
  83. r, w = pipe()
  84. return (_FDHolder(r), _FDHolder(w))
  85. class ExitedWithStderr(Exception):
  86. """
  87. A process exited with some stderr.
  88. """
  89. def __str__(self):
  90. """
  91. Dump the errors in a pretty way in the event of a subprocess traceback.
  92. """
  93. result = b'\n'.join([b''] + list(self.args))
  94. if _PY3:
  95. result = repr(result)
  96. return result
  97. class StartStopProcessProtocol(ProcessProtocol):
  98. """
  99. An L{IProcessProtocol} with a Deferred for events where the subprocess
  100. starts and stops.
  101. @ivar started: A L{Deferred} which fires with this protocol's
  102. L{IProcessTransport} provider when it is connected to one.
  103. @ivar stopped: A L{Deferred} which fires with the process output or a
  104. failure if the process produces output on standard error.
  105. @ivar output: A C{str} used to accumulate standard output.
  106. @ivar errors: A C{str} used to accumulate standard error.
  107. """
  108. def __init__(self):
  109. self.started = Deferred()
  110. self.stopped = Deferred()
  111. self.output = b''
  112. self.errors = b''
  113. def connectionMade(self):
  114. self.started.callback(self.transport)
  115. def outReceived(self, data):
  116. self.output += data
  117. def errReceived(self, data):
  118. self.errors += data
  119. def processEnded(self, reason):
  120. if reason.check(ProcessDone):
  121. self.stopped.callback(self.output)
  122. else:
  123. self.stopped.errback(ExitedWithStderr(
  124. self.errors, self.output))
  125. def _spawn(script, outputFD):
  126. """
  127. Start a script that is a peer of this test as a subprocess.
  128. @param script: the module name of the script in this directory (no
  129. package prefix, no '.py')
  130. @type script: C{str}
  131. @rtype: L{StartStopProcessProtocol}
  132. """
  133. pyExe = FilePath(sys.executable).asBytesMode().path
  134. env = bytesEnviron()
  135. env[b"PYTHONPATH"] = FilePath(
  136. pathsep.join(sys.path)).asBytesMode().path
  137. sspp = StartStopProcessProtocol()
  138. reactor.spawnProcess(
  139. sspp, pyExe, [
  140. pyExe,
  141. FilePath(__file__).sibling(script + ".py").asBytesMode().path,
  142. intToBytes(outputFD),
  143. ],
  144. env=env,
  145. childFDs={0: "w", 1: "r", 2: "r", outputFD: outputFD}
  146. )
  147. return sspp
  148. class BadList(list):
  149. """
  150. A list which cannot be iterated sometimes.
  151. This is a C{list} subclass to get past the type check in L{send1msg}, not
  152. as an example of how real programs might want to interact with L{send1msg}
  153. (or anything else). A custom C{list} subclass makes it easier to trigger
  154. certain error cases in the implementation.
  155. @ivar iterate: A flag which indicates whether an instance of L{BadList}
  156. will allow iteration over itself or not. If C{False}, an attempt to
  157. iterate over the instance will raise an exception.
  158. """
  159. iterate = True
  160. def __iter__(self):
  161. """
  162. Allow normal list iteration, or raise an exception.
  163. If C{self.iterate} is C{True}, it will be flipped to C{False} and then
  164. normal iteration will proceed. If C{self.iterate} is C{False},
  165. L{RuntimeError} is raised instead.
  166. """
  167. if self.iterate:
  168. self.iterate = False
  169. return super(BadList, self).__iter__()
  170. raise RuntimeError("Something bad happened")
  171. class WorseList(list):
  172. """
  173. A list which at first gives the appearance of being iterable, but then
  174. raises an exception.
  175. See L{BadList} for a warning about not writing code like this.
  176. """
  177. def __iter__(self):
  178. """
  179. Return an iterator which will raise an exception as soon as C{next} is
  180. called on it.
  181. """
  182. class BadIterator(object):
  183. def next(self):
  184. raise RuntimeError("This is a really bad case.")
  185. return BadIterator()
  186. class CModuleSendmsgTests(TestCase):
  187. """
  188. Tests for sendmsg extension module and associated file-descriptor sending
  189. functionality.
  190. """
  191. if nonUNIXSkip is not None:
  192. skip = nonUNIXSkip
  193. elif CModuleImportSkip is not None:
  194. skip = CModuleImportSkip
  195. def setUp(self):
  196. """
  197. Create a pair of UNIX sockets.
  198. """
  199. self.input, self.output = socketpair(AF_UNIX)
  200. def tearDown(self):
  201. """
  202. Close the sockets opened by setUp.
  203. """
  204. self.input.close()
  205. self.output.close()
  206. def test_sendmsgBadArguments(self):
  207. """
  208. The argument types accepted by L{send1msg} are:
  209. 1. C{int}
  210. 2. read-only character buffer
  211. 3. C{int}
  212. 4. sequence
  213. The 3rd and 4th arguments are optional. If fewer than two arguments or
  214. more than four arguments are passed, or if any of the arguments passed
  215. are not compatible with these types, L{TypeError} is raised.
  216. """
  217. # Exercise the wrong number of arguments cases
  218. self.assertRaises(TypeError, send1msg)
  219. self.assertRaises(TypeError, send1msg, 1)
  220. self.assertRaises(TypeError, send1msg,
  221. 1, "hello world", 2, [], object())
  222. # Exercise the wrong type of arguments cases
  223. self.assertRaises(TypeError, send1msg, object(), "hello world", 2, [])
  224. self.assertRaises(TypeError, send1msg, 1, object(), 2, [])
  225. self.assertRaises(TypeError, send1msg, 1, "hello world", object(), [])
  226. self.assertRaises(TypeError, send1msg, 1, "hello world", 2, object())
  227. def test_badAncillaryIter(self):
  228. """
  229. If iteration over the ancillary data list fails (at the point of the
  230. C{__iter__} call), the exception with which it fails is propagated to
  231. the caller of L{send1msg}.
  232. """
  233. badList = BadList()
  234. badList.append((1, 2, "hello world"))
  235. badList.iterate = False
  236. self.assertRaises(RuntimeError, send1msg, 1, "hello world", 2, badList)
  237. # Hit the second iteration
  238. badList.iterate = True
  239. self.assertRaises(RuntimeError, send1msg, 1, "hello world", 2, badList)
  240. def test_badAncillaryNext(self):
  241. """
  242. If iteration over the ancillary data list fails (at the point of a
  243. C{next} call), the exception with which it fails is propagated to the
  244. caller of L{send1msg}.
  245. """
  246. worseList = WorseList()
  247. self.assertRaises(RuntimeError, send1msg,
  248. 1, "hello world", 2,worseList)
  249. def test_sendmsgBadAncillaryItem(self):
  250. """
  251. The ancillary data list contains three-tuples with element types of:
  252. 1. C{int}
  253. 2. C{int}
  254. 3. read-only character buffer
  255. If a tuple in the ancillary data list does not elements of these types,
  256. L{TypeError} is raised.
  257. """
  258. # Exercise the wrong number of arguments cases
  259. self.assertRaises(TypeError, send1msg, 1, "hello world", 2, [()])
  260. self.assertRaises(TypeError, send1msg, 1, "hello world", 2, [(1,)])
  261. self.assertRaises(TypeError, send1msg, 1, "hello world", 2, [(1, 2)])
  262. self.assertRaises(
  263. TypeError,
  264. send1msg, 1, "hello world", 2, [(1, 2, "goodbye", object())])
  265. # Exercise the wrong type of arguments cases
  266. exc = self.assertRaises(
  267. TypeError, send1msg, 1, "hello world", 2, [object()])
  268. self.assertEqual(
  269. "send1msg argument 3 expected list of tuple, "
  270. "got list containing object",
  271. str(exc))
  272. self.assertRaises(
  273. TypeError,
  274. send1msg, 1, "hello world", 2, [(object(), 1, "goodbye")])
  275. self.assertRaises(
  276. TypeError,
  277. send1msg, 1, "hello world", 2, [(1, object(), "goodbye")])
  278. self.assertRaises(
  279. TypeError,
  280. send1msg, 1, "hello world", 2, [(1, 1, object())])
  281. def test_syscallError(self):
  282. """
  283. If the underlying C{sendmsg} call fails, L{send1msg} raises
  284. L{socket.error} with its errno set to the underlying errno value.
  285. """
  286. with open(devnull) as probe:
  287. fd = probe.fileno()
  288. exc = self.assertRaises(error, send1msg, fd, "hello, world")
  289. self.assertEqual(exc.args[0], errno.EBADF)
  290. def test_syscallErrorWithControlMessage(self):
  291. """
  292. The behavior when the underlying C{sendmsg} call fails is the same
  293. whether L{send1msg} is passed ancillary data or not.
  294. """
  295. with open(devnull) as probe:
  296. fd = probe.fileno()
  297. exc = self.assertRaises(
  298. error, send1msg, fd, "hello, world", 0, [(0, 0, "0123")])
  299. self.assertEqual(exc.args[0], errno.EBADF)
  300. def test_roundtrip(self):
  301. """
  302. L{recv1msg} will retrieve a message sent via L{send1msg}.
  303. """
  304. message = "hello, world!"
  305. self.assertEqual(
  306. len(message),
  307. send1msg(self.input.fileno(), message, 0))
  308. result = recv1msg(fd=self.output.fileno())
  309. self.assertEqual(result, (message, 0, []))
  310. def test_shortsend(self):
  311. """
  312. L{send1msg} returns the number of bytes which it was able to send.
  313. """
  314. message = "x" * 1024 * 1024
  315. self.input.setblocking(False)
  316. sent = send1msg(self.input.fileno(), message)
  317. # Sanity check - make sure the amount of data we sent was less than the
  318. # message, but not the whole message, as we should have filled the send
  319. # buffer. This won't work if the send buffer is more than 1MB, though.
  320. self.assertTrue(sent < len(message))
  321. received = recv1msg(self.output.fileno(), 0, len(message))
  322. self.assertEqual(len(received[0]), sent)
  323. def test_roundtripEmptyAncillary(self):
  324. """
  325. L{send1msg} treats an empty ancillary data list the same way it treats
  326. receiving no argument for the ancillary parameter at all.
  327. """
  328. send1msg(self.input.fileno(), "hello, world!", 0, [])
  329. result = recv1msg(fd=self.output.fileno())
  330. self.assertEqual(result, ("hello, world!", 0, []))
  331. def test_flags(self):
  332. """
  333. The C{flags} argument to L{send1msg} is passed on to the underlying
  334. C{sendmsg} call, to affect it in whatever way is defined by those
  335. flags.
  336. """
  337. # Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT
  338. # makes the send a non-blocking call, even if the socket is in blocking
  339. # mode. See also test_flags in RecvmsgTests
  340. for i in range(1024):
  341. try:
  342. send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT)
  343. except error as e:
  344. self.assertEqual(e.args[0], errno.EAGAIN)
  345. break
  346. else:
  347. self.fail(
  348. "Failed to fill up the send buffer, "
  349. "or maybe send1msg blocked for a while")
  350. if dontWaitSkip is not None:
  351. test_flags.skip = dontWaitSkip
  352. def test_wrongTypeAncillary(self):
  353. """
  354. L{send1msg} will show a helpful exception message when given the wrong
  355. type of object for the 'ancillary' argument.
  356. """
  357. error = self.assertRaises(TypeError,
  358. send1msg, self.input.fileno(),
  359. "hello, world!", 0, 4321)
  360. self.assertEqual(str(error),
  361. "send1msg argument 3 expected list, got int")
  362. @inlineCallbacks
  363. def test_sendSubProcessFD(self):
  364. """
  365. Calling L{sendsmsg} with SOL_SOCKET, SCM_RIGHTS, and a platform-endian
  366. packed file descriptor number should send that file descriptor to a
  367. different process, where it can be retrieved by using L{recv1msg}.
  368. """
  369. sspp = _spawn("cmodulepullpipe", self.output.fileno())
  370. yield sspp.started
  371. pipeOut, pipeIn = _makePipe()
  372. self.addCleanup(pipeOut.close)
  373. self.addCleanup(pipeIn.close)
  374. with pipeIn:
  375. send1msg(
  376. self.input.fileno(), "blonk", 0,
  377. [(SOL_SOCKET, SCM_RIGHTS, pack("i", pipeIn.fileno()))])
  378. yield sspp.stopped
  379. self.assertEqual(read(pipeOut.fileno(), 1024),
  380. "Test fixture data: blonk.\n")
  381. # Make sure that the pipe is actually closed now.
  382. self.assertEqual(read(pipeOut.fileno(), 1024), "")
  383. def test_sendmsgTwoAncillaryDoesNotSegfault(self):
  384. """
  385. L{sendmsg} with two FDs in two separate ancillary entries
  386. does not segfault.
  387. """
  388. ancillary = [
  389. (SOL_SOCKET, SCM_RIGHTS, pack("i", self.input.fileno())),
  390. (SOL_SOCKET, SCM_RIGHTS, pack("i", self.output.fileno())),
  391. ]
  392. try:
  393. send1msg(self.input.fileno(), b"some data", 0, ancillary)
  394. except error:
  395. # Ok as long as it doesn't segfault.
  396. pass
  397. class CModuleRecvmsgTests(TestCase):
  398. """
  399. Tests for L{recv1msg} (primarily error handling cases).
  400. """
  401. if CModuleImportSkip is not None:
  402. skip = CModuleImportSkip
  403. def test_badArguments(self):
  404. """
  405. The argument types accepted by L{recv1msg} are:
  406. 1. C{int}
  407. 2. C{int}
  408. 3. C{int}
  409. 4. C{int}
  410. The 2nd, 3rd, and 4th arguments are optional. If fewer than one
  411. argument or more than four arguments are passed, or if any of the
  412. arguments passed are not compatible with these types, L{TypeError} is
  413. raised.
  414. """
  415. # Exercise the wrong number of arguments cases
  416. self.assertRaises(TypeError, recv1msg)
  417. self.assertRaises(TypeError, recv1msg, 1, 2, 3, 4, object())
  418. # Exercise the wrong type of arguments cases
  419. self.assertRaises(TypeError, recv1msg, object(), 2, 3, 4)
  420. self.assertRaises(TypeError, recv1msg, 1, object(), 3, 4)
  421. self.assertRaises(TypeError, recv1msg, 1, 2, object(), 4)
  422. self.assertRaises(TypeError, recv1msg, 1, 2, 3, object())
  423. def test_cmsgSpaceOverflow(self):
  424. """
  425. L{recv1msg} raises L{OverflowError} if passed a value for the
  426. C{cmsg_size} argument which exceeds C{SOCKLEN_MAX}.
  427. """
  428. self.assertRaises(OverflowError, recv1msg, 0, 0, 0, 0x7FFFFFFF)
  429. def test_syscallError(self):
  430. """
  431. If the underlying C{recvmsg} call fails, L{recv1msg} raises
  432. L{socket.error} with its errno set to the underlying errno value.
  433. """
  434. with open(devnull) as probe:
  435. fd = probe.fileno()
  436. exc = self.assertRaises(error, recv1msg, fd)
  437. self.assertEqual(exc.args[0], errno.EBADF)
  438. def test_flags(self):
  439. """
  440. The C{flags} argument to L{recv1msg} is passed on to the underlying
  441. C{recvmsg} call, to affect it in whatever way is defined by those
  442. flags.
  443. """
  444. # See test_flags in SendmsgTests
  445. reader, writer = socketpair(AF_UNIX)
  446. exc = self.assertRaises(
  447. error, recv1msg, reader.fileno(), MSG_DONTWAIT)
  448. self.assertEqual(exc.args[0], errno.EAGAIN)
  449. if dontWaitSkip is not None:
  450. test_flags.skip = dontWaitSkip
  451. class CModuleGetSocketFamilyTests(TestCase):
  452. """
  453. Tests for L{getsockfam}, a helper which reveals the address family of an
  454. arbitrary socket.
  455. """
  456. if CModuleImportSkip is not None:
  457. skip = CModuleImportSkip
  458. def _socket(self, addressFamily):
  459. """
  460. Create a new socket using the given address family and return that
  461. socket's file descriptor. The socket will automatically be closed when
  462. the test is torn down.
  463. """
  464. s = socket(addressFamily)
  465. self.addCleanup(s.close)
  466. return s.fileno()
  467. def test_badArguments(self):
  468. """
  469. L{getsockfam} accepts a single C{int} argument. If it is called in
  470. some other way, L{TypeError} is raised.
  471. """
  472. self.assertRaises(TypeError, getsockfam)
  473. self.assertRaises(TypeError, getsockfam, 1, 2)
  474. self.assertRaises(TypeError, getsockfam, object())
  475. def test_syscallError(self):
  476. """
  477. If the underlying C{getsockname} call fails, L{getsockfam} raises
  478. L{socket.error} with its errno set to the underlying errno value.
  479. """
  480. with open(devnull) as probe:
  481. fd = probe.fileno()
  482. exc = self.assertRaises(error, getsockfam, fd)
  483. self.assertEqual(errno.EBADF, exc.args[0])
  484. def test_inet(self):
  485. """
  486. When passed the file descriptor of a socket created with the C{AF_INET}
  487. address family, L{getsockfam} returns C{AF_INET}.
  488. """
  489. self.assertEqual(AF_INET, getsockfam(self._socket(AF_INET)))
  490. def test_inet6(self):
  491. """
  492. When passed the file descriptor of a socket created with the
  493. C{AF_INET6} address family, L{getsockfam} returns C{AF_INET6}.
  494. """
  495. self.assertEqual(AF_INET6, getsockfam(self._socket(AF_INET6)))
  496. def test_unix(self):
  497. """
  498. When passed the file descriptor of a socket created with the C{AF_UNIX}
  499. address family, L{getsockfam} returns C{AF_UNIX}.
  500. """
  501. self.assertEqual(AF_UNIX, getsockfam(self._socket(AF_UNIX)))
  502. if nonUNIXSkip is not None:
  503. test_unix.skip = nonUNIXSkip
  504. class SendmsgTests(TestCase):
  505. """
  506. Tests for the Python2/3 compatible L{sendmsg} interface.
  507. """
  508. if importSkip is not None:
  509. skip = importSkip
  510. def setUp(self):
  511. """
  512. Create a pair of UNIX sockets.
  513. """
  514. self.input, self.output = socketpair(AF_UNIX)
  515. def tearDown(self):
  516. """
  517. Close the sockets opened by setUp.
  518. """
  519. self.input.close()
  520. self.output.close()
  521. def test_syscallError(self):
  522. """
  523. If the underlying C{sendmsg} call fails, L{send1msg} raises
  524. L{socket.error} with its errno set to the underlying errno value.
  525. """
  526. self.input.close()
  527. exc = self.assertRaises(error, sendmsg, self.input, b"hello, world")
  528. self.assertEqual(exc.args[0], errno.EBADF)
  529. def test_syscallErrorWithControlMessage(self):
  530. """
  531. The behavior when the underlying C{sendmsg} call fails is the same
  532. whether L{sendmsg} is passed ancillary data or not.
  533. """
  534. self.input.close()
  535. exc = self.assertRaises(
  536. error, sendmsg, self.input, b"hello, world", [(0, 0, b"0123")], 0)
  537. self.assertEqual(exc.args[0], errno.EBADF)
  538. def test_roundtrip(self):
  539. """
  540. L{recvmsg} will retrieve a message sent via L{sendmsg}.
  541. """
  542. message = b"hello, world!"
  543. self.assertEqual(
  544. len(message),
  545. sendmsg(self.input, message))
  546. result = recvmsg(self.output)
  547. self.assertEqual(result.data, b"hello, world!")
  548. self.assertEqual(result.flags, 0)
  549. self.assertEqual(result.ancillary, [])
  550. def test_shortsend(self):
  551. """
  552. L{sendmsg} returns the number of bytes which it was able to send.
  553. """
  554. message = b"x" * 1024 * 1024
  555. self.input.setblocking(False)
  556. sent = sendmsg(self.input, message)
  557. # Sanity check - make sure the amount of data we sent was less than the
  558. # message, but not the whole message, as we should have filled the send
  559. # buffer. This won't work if the send buffer is more than 1MB, though.
  560. self.assertTrue(sent < len(message))
  561. received = recvmsg(self.output, len(message))
  562. self.assertEqual(len(received[0]), sent)
  563. def test_roundtripEmptyAncillary(self):
  564. """
  565. L{sendmsg} treats an empty ancillary data list the same way it treats
  566. receiving no argument for the ancillary parameter at all.
  567. """
  568. sendmsg(self.input, b"hello, world!", [], 0)
  569. result = recvmsg(self.output)
  570. self.assertEqual(result, (b"hello, world!", [], 0))
  571. def test_flags(self):
  572. """
  573. The C{flags} argument to L{sendmsg} is passed on to the underlying
  574. C{sendmsg} call, to affect it in whatever way is defined by those
  575. flags.
  576. """
  577. # Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT
  578. # makes the send a non-blocking call, even if the socket is in blocking
  579. # mode. See also test_flags in RecvmsgTests
  580. for i in range(1024):
  581. try:
  582. sendmsg(self.input, b"x" * 1024, flags=MSG_DONTWAIT)
  583. except error as e:
  584. self.assertEqual(e.args[0], errno.EAGAIN)
  585. break
  586. else:
  587. self.fail(
  588. "Failed to fill up the send buffer, "
  589. "or maybe send1msg blocked for a while")
  590. if dontWaitSkip is not None:
  591. test_flags.skip = dontWaitSkip
  592. @inlineCallbacks
  593. def test_sendSubProcessFD(self):
  594. """
  595. Calling L{sendmsg} with SOL_SOCKET, SCM_RIGHTS, and a platform-endian
  596. packed file descriptor number should send that file descriptor to a
  597. different process, where it can be retrieved by using L{recv1msg}.
  598. """
  599. sspp = _spawn("pullpipe", self.output.fileno())
  600. yield sspp.started
  601. pipeOut, pipeIn = _makePipe()
  602. self.addCleanup(pipeOut.close)
  603. self.addCleanup(pipeIn.close)
  604. with pipeIn:
  605. sendmsg(
  606. self.input, b"blonk",
  607. [(SOL_SOCKET, SCM_RIGHTS, pack("i", pipeIn.fileno()))])
  608. yield sspp.stopped
  609. self.assertEqual(read(pipeOut.fileno(), 1024),
  610. b"Test fixture data: blonk.\n")
  611. # Make sure that the pipe is actually closed now.
  612. self.assertEqual(read(pipeOut.fileno(), 1024), b"")
  613. class GetSocketFamilyTests(TestCase):
  614. """
  615. Tests for L{getSocketFamily}.
  616. """
  617. if importSkip is not None:
  618. skip = importSkip
  619. def _socket(self, addressFamily):
  620. """
  621. Create a new socket using the given address family and return that
  622. socket's file descriptor. The socket will automatically be closed when
  623. the test is torn down.
  624. """
  625. s = socket(addressFamily)
  626. self.addCleanup(s.close)
  627. return s
  628. def test_inet(self):
  629. """
  630. When passed the file descriptor of a socket created with the C{AF_INET}
  631. address family, L{getSocketFamily} returns C{AF_INET}.
  632. """
  633. self.assertEqual(AF_INET, getSocketFamily(self._socket(AF_INET)))
  634. def test_inet6(self):
  635. """
  636. When passed the file descriptor of a socket created with the
  637. C{AF_INET6} address family, L{getSocketFamily} returns C{AF_INET6}.
  638. """
  639. self.assertEqual(AF_INET6, getSocketFamily(self._socket(AF_INET6)))
  640. def test_unix(self):
  641. """
  642. When passed the file descriptor of a socket created with the C{AF_UNIX}
  643. address family, L{getSocketFamily} returns C{AF_UNIX}.
  644. """
  645. self.assertEqual(AF_UNIX, getSocketFamily(self._socket(AF_UNIX)))
  646. if nonUNIXSkip is not None:
  647. test_unix.skip = nonUNIXSkip