Passed
Pull Request — master (#321)
by
unknown
01:13
created

UnixSocketConnectionTestCase.check_default_values()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
21
import os
22
import socketserver
23
import tempfile
24
import threading
25
import uuid
26
27
from gvm.connections import (
28
    UnixSocketConnection,
29
    DEFAULT_TIMEOUT,
30
    DEFAULT_UNIX_SOCKET_PATH,
31
)
32
from gvm.errors import GvmError
33
34
35
class DummyRequestHandler(socketserver.BaseRequestHandler):
36
    def handle(self):
37
        response = bytes(
38
            "<gmp_response status=\"200\" status_text=\"OK\"/>", 'utf-8'
39
        )
40
        self.request.sendall(response)
41
42
43
class ThreadedUnixStreamServer(
44
    socketserver.ThreadingMixIn, socketserver.UnixStreamServer
45
):
46
    pass
47
48
49
class UnixSocketConnectionTestCase(unittest.TestCase):
50
    # pylint: disable=protected-access
51
    def setUp(self):
52
        self.socketname = "%s/%s.sock" % (
53
            tempfile.gettempdir(),
54
            str(uuid.uuid4()),
55
        )
56
        self.sockserv = ThreadedUnixStreamServer(
57
            self.socketname, DummyRequestHandler
58
        )
59
        self.server_thread = threading.Thread(
60
            target=self.sockserv.serve_forever
61
        )
62
        self.server_thread.daemon = True
63
        self.server_thread.start()
64
65
    def tearDown(self):
66
        self.sockserv.shutdown()
67
        self.sockserv.server_close()
68
        os.unlink(self.socketname)
69
70
    def test_unix_socket_connection_connect_read(self):
71
        self.connection = UnixSocketConnection(
72
            path=self.socketname, timeout=DEFAULT_TIMEOUT
73
        )
74
        self.connection.connect()
75
        self.connection.read()
76
        self.connection.disconnect()
77
78
    def test_unix_socket_connection_connect_send_bytes_read(self):
79
        self.connection = UnixSocketConnection(
80
            path=self.socketname, timeout=DEFAULT_TIMEOUT
81
        )
82
        self.connection.connect()
83
        self.connection.send(bytes("<gmp/>", 'utf-8'))
84
        self.connection.read()
85
        self.connection.disconnect()
86
87
    def test_unix_socket_connection_connect_send_str_read(self):
88
        self.connection = UnixSocketConnection(
89
            path=self.socketname, timeout=DEFAULT_TIMEOUT
90
        )
91
        self.connection.connect()
92
        self.connection.send("<gmp/>")
93
        self.connection.read()
94
        self.connection.disconnect()
95
96
    def test_unix_socket_send_unconnected_socket(self):
97
        self.connection = UnixSocketConnection(
98
            path=self.socketname, timeout=DEFAULT_TIMEOUT
99
        )
100
        with self.assertRaises(GvmError):
101
            self.connection.send("<gmp>/")
102
103
    def test_init_no_args(self):
104
        connection = UnixSocketConnection()
105
        self.check_default_values(connection)
106
107
    def test_init_with_none(self):
108
        connection = UnixSocketConnection(path=None, timeout=None)
109
        self.check_default_values(connection)
110
111
    def check_default_values(self, connection: UnixSocketConnection):
112
        self.assertEqual(connection._timeout, DEFAULT_TIMEOUT)
113
        self.assertEqual(connection.path, DEFAULT_UNIX_SOCKET_PATH)
114
115
116
if __name__ == '__main__':
117
    unittest.main()
118