From dd53b4869cb8a25767615e8f740b7f9609c96153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Mon, 29 Jan 2024 14:17:07 +0100 Subject: [PATCH] Change: A connection is not a xml reader Use composition instead of inheritance for reading xml data. --- gvm/connections.py | 15 ++++++++------- tests/connections/test_gvm_connection.py | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/gvm/connections.py b/gvm/connections.py index 654ab3c42..e1a1fb38a 100644 --- a/gvm/connections.py +++ b/gvm/connections.py @@ -46,7 +46,7 @@ class XmlReader: Read a XML command until its closing element """ - def _start_xml(self) -> None: + def start_xml(self) -> None: self._first_element = None # act on start and end element events and # allow huge text data (for report content) @@ -54,7 +54,7 @@ def _start_xml(self) -> None: events=("start", "end"), huge_tree=True ) - def _is_end_xml(self) -> bool: + def is_end_xml(self) -> bool: for action, obj in self._parser.read_events(): if not self._first_element and action in "start": self._first_element = obj.tag @@ -67,7 +67,7 @@ def _is_end_xml(self) -> bool: return True return False - def _feed_xml(self, data: Data) -> None: + def feed_xml(self, data: Data) -> None: try: self._parser.feed(data) except etree.ParseError as e: @@ -77,7 +77,7 @@ def _feed_xml(self, data: Data) -> None: ) from None -class GvmConnection(XmlReader): +class GvmConnection: """ Base class for establishing a connection to a remote server daemon. @@ -89,6 +89,7 @@ class GvmConnection(XmlReader): def __init__(self, timeout: Optional[Union[int, float]] = DEFAULT_TIMEOUT): self._socket = None self._timeout = timeout if timeout is not None else DEFAULT_TIMEOUT + self._xml_reader = XmlReader() def _read(self) -> bytes: if self._socket is None: @@ -123,7 +124,7 @@ def read(self) -> str: """ response = "" - self._start_xml() + self._xml_reader.start_xml() break_timeout = ( time.time() + self._timeout if self._timeout is not None else None @@ -136,11 +137,11 @@ def read(self) -> str: # Connection was closed by server raise GvmError("Remote closed the connection") - self._feed_xml(data) + self._xml_reader.feed_xml(data) response += data.decode("utf-8", errors="ignore") - if self._is_end_xml(): + if self._xml_reader.is_end_xml(): break if break_timeout and time.time() > break_timeout: diff --git a/tests/connections/test_gvm_connection.py b/tests/connections/test_gvm_connection.py index 5711da240..fea552073 100644 --- a/tests/connections/test_gvm_connection.py +++ b/tests/connections/test_gvm_connection.py @@ -6,10 +6,19 @@ import unittest from unittest.mock import patch -from gvm.connections import DEFAULT_TIMEOUT, GvmConnection +from gvm.connections import DEFAULT_TIMEOUT, GvmConnection, XmlReader from gvm.errors import GvmError +class XmlReaderTestCase(unittest.TestCase): + def test_is_end_xml_false(self): + reader = XmlReader() + reader.start_xml() + + false = reader.is_end_xml() + self.assertFalse(false) + + class GvmConnectionTestCase(unittest.TestCase): # pylint: disable=protected-access def test_init_no_args(self): @@ -29,13 +38,6 @@ def test_connect_not_implemented(self): with self.assertRaises(NotImplementedError): connection.connect() - def test_is_end_xml_false(self): - connection = GvmConnection() - connection._start_xml() - - false = connection._is_end_xml() - self.assertFalse(false) - @patch("gvm.connections.GvmConnection._read") def test_read_no_data(self, _read_mock): _read_mock.return_value = None