Skip to content

Commit 88a0c52

Browse files
bjoernricksgreenbonebot
authored andcommitted
Add: Tests for protocol states and xml reader
1 parent d00f702 commit 88a0c52

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# SPDX-FileCopyrightText: 2024 Greenbone AG
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
import unittest
6+
from unittest.mock import MagicMock
7+
8+
from gvm.protocols.gmp.core import Response
9+
from gvm.protocols.gmp.core._connection import (
10+
AwaitingResponseState,
11+
Context,
12+
ErrorState,
13+
InitialState,
14+
InvalidStateError,
15+
ReceivingDataState,
16+
)
17+
18+
19+
class RequestMock:
20+
def __init__(self, data: bytes) -> None:
21+
self._data = data
22+
23+
def __bytes__(self) -> bytes:
24+
return self._data
25+
26+
def __str__(self) -> str:
27+
return self._data.decode()
28+
29+
30+
class InitialStateTestCase(unittest.TestCase):
31+
def test_receive_data(self) -> None:
32+
context = MagicMock(spec=Context)
33+
state = InitialState()
34+
state.__set_context__(context)
35+
36+
with self.assertRaisesRegex(InvalidStateError, "Invalid State"):
37+
state.receive_data(b"some data")
38+
39+
def test_close(self) -> None:
40+
context = MagicMock(spec=Context)
41+
state = InitialState()
42+
state.__set_context__(context)
43+
44+
state.close()
45+
46+
context.__set_state__.assert_not_called()
47+
48+
def test_send(self) -> None:
49+
context = MagicMock(spec=Context)
50+
state = InitialState()
51+
state.__set_context__(context)
52+
request = RequestMock(b"some data")
53+
54+
data = state.send(request)
55+
self.assertEqual(data, b"some data")
56+
57+
self.assertIsInstance(
58+
context.__set_state__.call_args[0][0], AwaitingResponseState
59+
)
60+
61+
62+
class AwaitingResponseStateTestCase(unittest.TestCase):
63+
64+
def test_receive_data(self) -> None:
65+
context = MagicMock(spec=Context)
66+
request = RequestMock(b"<start>")
67+
state = AwaitingResponseState(request)
68+
state.__set_context__(context)
69+
70+
response = state.receive_data(b"<element>")
71+
self.assertIsNone(response)
72+
73+
self.assertIsInstance(
74+
context.__set_state__.call_args[0][0], ReceivingDataState
75+
)
76+
77+
def test_close(self) -> None:
78+
context = MagicMock(spec=Context)
79+
request = RequestMock(b"<start>")
80+
state = AwaitingResponseState(request)
81+
state.__set_context__(context)
82+
83+
state.close()
84+
85+
self.assertIsInstance(
86+
context.__set_state__.call_args[0][0], InitialState
87+
)
88+
89+
def test_send(self) -> None:
90+
context = MagicMock(spec=Context)
91+
request = RequestMock(b"<start>")
92+
state = AwaitingResponseState(request)
93+
state.__set_context__(context)
94+
95+
with self.assertRaisesRegex(InvalidStateError, "Invalid State"):
96+
another_request = RequestMock(b"<element>")
97+
state.send(another_request)
98+
99+
100+
class ErrorStateTestCase(unittest.TestCase):
101+
102+
def test_receive_data(self) -> None:
103+
context = MagicMock(spec=Context)
104+
state = ErrorState()
105+
state.__set_context__(context)
106+
107+
with self.assertRaisesRegex(
108+
InvalidStateError,
109+
"^The connection is in an error state. Please close the connection.$",
110+
):
111+
state.receive_data(b"some data")
112+
113+
def test_close(self) -> None:
114+
context = MagicMock(spec=Context)
115+
state = ErrorState()
116+
state.__set_context__(context)
117+
118+
state.close()
119+
120+
self.assertIsInstance(
121+
context.__set_state__.call_args[0][0], InitialState
122+
)
123+
124+
def test_send(self) -> None:
125+
context = MagicMock(spec=Context)
126+
state = ErrorState()
127+
state.__set_context__(context)
128+
129+
with self.assertRaisesRegex(
130+
InvalidStateError,
131+
"^The connection is in an error state. Please close the connection.$",
132+
):
133+
state.receive_data(b"some data")
134+
135+
136+
class ReceivingDataStateTestCase(unittest.TestCase):
137+
138+
def test_receive_data(self) -> None:
139+
context = MagicMock(spec=Context)
140+
request = RequestMock(b"<start/>")
141+
state = ReceivingDataState(request)
142+
state.__set_context__(context)
143+
144+
response = state.receive_data(b"<response>")
145+
self.assertIsNone(response)
146+
147+
response = state.receive_data(b"</response>")
148+
self.assertIsNotNone(response)
149+
self.assertIsInstance(response, Response)
150+
self.assertEqual(response.data, b"<response></response>") # type: ignore
151+
152+
self.assertIsInstance(
153+
context.__set_state__.call_args[0][0], InitialState
154+
)
155+
156+
def test_close(self) -> None:
157+
context = MagicMock(spec=Context)
158+
request = RequestMock(b"<start/>")
159+
state = ReceivingDataState(request)
160+
state.__set_context__(context)
161+
162+
state.close()
163+
164+
self.assertIsInstance(
165+
context.__set_state__.call_args[0][0], InitialState
166+
)
167+
168+
def test_send(self) -> None:
169+
context = MagicMock(spec=Context)
170+
request = RequestMock(b"<start/>")
171+
state = ReceivingDataState(request)
172+
state.__set_context__(context)
173+
174+
with self.assertRaisesRegex(InvalidStateError, "Invalid State"):
175+
another_request = RequestMock(b"<element>")
176+
state.send(another_request)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: 2020-2024 Greenbone AG
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
import unittest
6+
7+
from gvm.protocols.gmp.core._connection import XmlReader
8+
9+
10+
class XmlReaderTestCase(unittest.TestCase):
11+
def test_is_end_xml_false(self):
12+
reader = XmlReader()
13+
reader.start_xml()
14+
15+
false = reader.is_end_xml()
16+
self.assertFalse(false)

0 commit comments

Comments
 (0)