Skip to content

Commit

Permalink
Change: A connection is not a xml reader
Browse files Browse the repository at this point in the history
Use composition instead of inheritance for reading xml data.
  • Loading branch information
bjoernricks authored and greenbonebot committed Jan 29, 2024
1 parent a860493 commit dd53b48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
15 changes: 8 additions & 7 deletions gvm/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ 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)
self._parser = etree.XMLPullParser(
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
Expand All @@ -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:
Expand All @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
18 changes: 10 additions & 8 deletions tests/connections/test_gvm_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit dd53b48

Please sign in to comment.