def test_roundtripEmptyAncillary(self): """ L{send1msg} treats an empty ancillary data list the same way it treats receiving no argument for the ancillary parameter at all. """ send1msg(self.input.fileno(), "hello, world!", 0, []) result = recv1msg(fd=self.output.fileno()) self.assertEqual(result, ("hello, world!", 0, []))
def doWrite(self): """ Write some data. """ while self.statusQueue: msg = self.statusQueue.pop(0) try: send1msg(self.fd, msg, 0) except SocketError, se: if se.errno in (EAGAIN, ENOBUFS): self.statusQueue.insert(0, msg) return raise
def test_sendmsgTwoAncillaryDoesNotSegfault(self): """ L{sendmsg} with two FDs in two separate ancillary entries does not segfault. """ ancillary = [ (SOL_SOCKET, SCM_RIGHTS, pack("i", self.input.fileno())), (SOL_SOCKET, SCM_RIGHTS, pack("i", self.output.fileno())), ] try: send1msg(self.input.fileno(), b"some data", 0, ancillary) except error: # Ok as long as it doesn't segfault. pass
def test_flags(self): """ The C{flags} argument to L{send1msg} is passed on to the underlying C{sendmsg} call, to affect it in whatever way is defined by those flags. """ # Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT # makes the send a non-blocking call, even if the socket is in blocking # mode. See also test_flags in RecvmsgTestCase for i in range(1024): try: send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT) except error, e: self.assertEqual(e.args[0], errno.EAGAIN) break
def test_flags(self): """ The C{flags} argument to L{send1msg} is passed on to the underlying C{sendmsg} call, to affect it in whatever way is defined by those flags. """ # Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT # makes the send a non-blocking call, even if the socket is in blocking # mode. See also test_flags in RecvmsgTests for i in range(1024): try: send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT) except error, e: self.assertEqual(e.args[0], errno.EAGAIN) break
def main(): foo, bar = socketpair() sent = send1msg(foo.fileno(), "Hello, world") print "Sent", sent, "bytes" (received, flags, ancillary) = recv1msg(bar.fileno(), 1024) print "Received", repr(received) print "Extra stuff, boring in this case", flags, ancillary
def test_sendSubProcessFD(self): """ Calling L{sendsmsg} with SOL_SOCKET, SCM_RIGHTS, and a platform-endian packed file descriptor number should send that file descriptor to a different process, where it can be retrieved by using L{recv1msg}. """ sspp = self.spawn("pullpipe") yield sspp.started pipeOut, pipeIn = pipe() self.addCleanup(close, pipeOut) send1msg(self.input.fileno(), "blonk", 0, [(SOL_SOCKET, SCM_RIGHTS, pack("i", pipeIn))]) close(pipeIn) yield sspp.stopped self.assertEqual(read(pipeOut, 1024), "Test fixture data: blonk.\n") # Make sure that the pipe is actually closed now. self.assertEqual(read(pipeOut, 1024), "")
def test_roundtrip(self): """ L{recv1msg} will retrieve a message sent via L{send1msg}. """ message = "hello, world!" self.assertEqual(len(message), send1msg(self.input.fileno(), message, 0)) result = recv1msg(fd=self.output.fileno()) self.assertEqual(result, (message, 0, []))
def test_shortsend(self): """ L{send1msg} returns the number of bytes which it was able to send. """ message = "x" * 1024 * 1024 self.input.setblocking(False) sent = send1msg(self.input.fileno(), message) # Sanity check - make sure we did fill the send buffer and then some self.assertTrue(sent < len(message)) received = recv1msg(self.output.fileno(), 0, len(message)) self.assertEqual(len(received[0]), sent)
def sendfd(socketfd, fd, description): """ Send the given FD to another process via L{send1msg} on the given C{AF_UNIX} socket. @param socketfd: An C{AF_UNIX} socket, attached to another process waiting to receive sockets via the ancillary data mechanism in L{send1msg}. @type socketfd: C{int} @param fd: A file descriptor to be sent to the other process. @type fd: C{int} @param description: a string describing the socket that was passed. @type description: C{str} """ send1msg(socketfd, description, 0, [(SOL_SOCKET, SCM_RIGHTS, pack("i", fd))])
def test_roundtrip(self): """ L{recv1msg} will retrieve a message sent via L{send1msg}. """ message = "hello, world!" self.assertEqual( len(message), send1msg(self.input.fileno(), message, 0)) result = recv1msg(fd=self.output.fileno()) self.assertEqual(result, (message, 0, []))
def test_sendSubProcessFD(self): """ Calling L{sendsmsg} with SOL_SOCKET, SCM_RIGHTS, and a platform-endian packed file descriptor number should send that file descriptor to a different process, where it can be retrieved by using L{recv1msg}. """ sspp = self.spawn("pullpipe") yield sspp.started pipeOut, pipeIn = pipe() self.addCleanup(close, pipeOut) send1msg( self.input.fileno(), "blonk", 0, [(SOL_SOCKET, SCM_RIGHTS, pack("i", pipeIn))]) close(pipeIn) yield sspp.stopped self.assertEqual(read(pipeOut, 1024), "Test fixture data: blonk.\n") # Make sure that the pipe is actually closed now. self.assertEqual(read(pipeOut, 1024), "")
def test_shortsend(self): """ L{send1msg} returns the number of bytes which it was able to send. """ message = "x" * 1024 * 1024 self.input.setblocking(False) sent = send1msg(self.input.fileno(), message) # Sanity check - make sure the amount of data we sent was less than the # message, but not the whole message, as we should have filled the send # buffer. This won't work if the send buffer is more than 1MB, though. self.assertTrue(sent < len(message)) received = recv1msg(self.output.fileno(), 0, len(message)) self.assertEqual(len(received[0]), sent)
def test_SetDatafd(): fd, _fd = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM) sock_path = os.getenv('SOCK_PATH') cmd_set_data_fd = bytearray([0x00, 0x00, 0x00, 0x10]) expected_res = bytearray([0x00, 0x00, 0x00, 0x00]) try: fds = array("i") fds.append(_fd.fileno()) ctrlfd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) print("Connecting to server at : %s" % sock_path) ctrlfd.connect(sock_path) print("Sending data fd over ctrl fd...") if sys.version_info[0] < 3: sendmsg.send1msg(ctrlfd.fileno(), str(cmd_set_data_fd), 0, [(socket.SOL_SOCKET, sendmsg.SCM_RIGHTS, struct.pack("i", _fd.fileno()))]) else: ctrlfd.sendmsg([cmd_set_data_fd], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)]) except socket.error as e: print("SocketError: " + str(e)) ctrlfd.close() buf = ctrlfd.recv(4) print("Received bytes.. : %s" % buf) if buf: caps = bytearray(buf) if caps == expected_res: return test_ReadPCR10(fd) else: print("Unexpected reply for CMD_SET_DATA_FD: \n" " actual: %s\n expected: %s" % (toString(caps), toString(expected_res))) return False else: print("Null reply from swtpm") return False
def main(): foo, bar = socketpair() reader, writer = pipe() # Send a copy of the descriptor. Notice that there must be at least one # byte of normal data passed in. sent = send1msg(foo.fileno(), "\x00", 0, [(SOL_SOCKET, SCM_RIGHTS, pack("i", reader))]) # Receive the copy, including that one byte of normal data. data, flags, ancillary = recv1msg(bar.fileno(), 1024) duplicate = unpack("i", ancillary[0][2])[0] # Demonstrate that the copy works just like the original write(writer, "Hello, world") print "Read from original (%d): %r" % (reader, read(reader, 6)) print "Read from duplicate (%d): %r" % (duplicate, read(duplicate, 6))