Exemple #1
0
    def test4sendAnnotatedFileDescriptor(self):
        """A valid annotated file descriptor is to be sent by a socket."""
        sock_name = '/tmp/test4unixSocket.sock'
        data = b'readmeStream' + b'\x00' + b'You should read these README instructions for better understanding.'

        # skipcq: BAN-B607, BAN-B603
        proc = subprocess.Popen(['python3', 'unit/util/clientTest4.py'])

        if os.path.exists(sock_name):
            os.remove(sock_name)

        # print(self.opening_socket)
        server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        server.bind(sock_name)
        server.listen(1)
        connection = server.accept()[0]
        unix_socket_log_data_resource = UnixSocketLogDataResource(
            b'unix:///tmp/test4unixSocket.sock', connection.fileno())

        # print(self.listening)
        unix_socket_log_data_resource.fill_buffer()
        self.assertEqual(unix_socket_log_data_resource.buffer, data)
        # print('Data received: %s' % unix_socket_log_data_resource.buffer)

        unix_socket_log_data_resource.update_position(
            len(unix_socket_log_data_resource.buffer))
        self.assertEqual(unix_socket_log_data_resource.total_consumed_length,
                         80)
        self.assertEqual(unix_socket_log_data_resource.buffer, b'')

        # print("Shutting down...")
        unix_socket_log_data_resource.close()
        proc.terminate()
        proc.wait()
Exemple #2
0
    def test6send_logstream_descriptor(self):
        """A valid logstream descriptor is to be sent."""
        sock_name = '/tmp/test6unixSocket.sock'
        data = b'logstream' + b'\x00' + b'/var/log/syslog'

        # skipcq: BAN-B607, BAN-B603
        subprocess.Popen(['python3', 'unit/util/clientTest6.py'])

        if os.path.exists(sock_name):
            os.remove(sock_name)

        # print(self.opening_socket)
        server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        server.bind(sock_name)
        server.listen(1)
        connection = server.accept()[0]
        unix_socket_log_data_resource = UnixSocketLogDataResource(
            b'unix:///tmp/test6unixSocket.sock', connection.fileno())

        # print(self.listening)
        unix_socket_log_data_resource.fill_buffer()
        self.assertEqual(unix_socket_log_data_resource.buffer, data)
        # print('Data received: %s' % unix_socket_log_data_resource.buffer)

        unix_socket_log_data_resource.update_position(
            len(unix_socket_log_data_resource.buffer))
        self.assertEqual(unix_socket_log_data_resource.total_consumed_length,
                         25)
        self.assertEqual(unix_socket_log_data_resource.buffer, b'')

        # print("Shutting down...")
        unix_socket_log_data_resource.close()
Exemple #3
0
    def test5unix_socket_log_data_resource(self):
        """
        In this case the log_stream_fd is -1. The next step is to open the stream successfully.
        Therefor a server socket is set up listen to data to the server. Afterwards the buffer object is filled with data and the position
        is updated.
        """
        sockName = b'/tmp/test5unixSocket.sock'
        # skipcq: BAN-B607, BAN-B603
        proc = subprocess.Popen(['python3', 'unit/input/client.py'])

        if os.path.exists(sockName):
            os.remove(sockName)

        print("Opening socket...")
        server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        server.bind(sockName)
        server.listen(1)
        connection = server.accept()[0]
        unix_socket_log_data_resource = UnixSocketLogDataResource(
            b'unix://' + sockName, connection.fileno())

        print("Listening...")
        unix_socket_log_data_resource.fill_buffer()
        self.assertEqual(repr(unix_socket_log_data_resource.buffer),
                         repr(b'data'))
        print('Data received: %s' %
              unix_socket_log_data_resource.buffer.decode())

        unix_socket_log_data_resource.update_position(
            len(unix_socket_log_data_resource.buffer))
        self.assertEqual(unix_socket_log_data_resource.total_consumed_length,
                         4)
        self.assertEqual(unix_socket_log_data_resource.buffer, b'')

        print("Shutting down...")
        unix_socket_log_data_resource.close()
        server.close()
        proc.terminate()
        proc.wait()
        print("Done")