#2041: ntp_server test fixes.

This commit is contained in:
Nick Todd
2023-11-20 18:08:55 +00:00
parent b0b37f9da5
commit f7215847d4
3 changed files with 21 additions and 12 deletions

View File

@@ -34,11 +34,11 @@ class NTPPacket(DataPacket):
"NTP Request packet sent by NTP Client." "NTP Request packet sent by NTP Client."
ntp_reply: Optional[NTPReply] = None ntp_reply: Optional[NTPReply] = None
def generate_reply(self, time: datetime) -> NTPPacket: def generate_reply(self, ntp_server_time: datetime) -> NTPPacket:
"""Generate a NTPPacket containing the time in a NTPReply object. """Generate a NTPPacket containing the time in a NTPReply object.
:param time: datetime object representing the time from the NTP server. :param time: datetime object representing the time from the NTP server.
:return: A new NTPPacket object. :return: A new NTPPacket object.
""" """
self.ntp_reply = NTPReply(time) self.ntp_reply = NTPReply(ntp_datetime=ntp_server_time)
return self return self

View File

@@ -50,7 +50,8 @@ class NTPServer(Service):
session_id: Optional[str] = None, session_id: Optional[str] = None,
**kwargs, **kwargs,
) -> bool: ) -> bool:
"""Receives a request from NTPClient. """
Receives a request from NTPClient.
Check that request has a valid IP address. Check that request has a valid IP address.
@@ -75,5 +76,6 @@ class NTPServer(Service):
f"with current time: {time}" f"with current time: {time}"
) )
# send reply # send reply
self.send(payload, session_id) if self.send(payload, session_id):
return True return True
return False

View File

@@ -5,7 +5,7 @@ import pytest
from primaite.simulator.network.container import Network from primaite.simulator.network.container import Network
from primaite.simulator.network.hardware.nodes.computer import Computer from primaite.simulator.network.hardware.nodes.computer import Computer
from primaite.simulator.network.hardware.nodes.server import Server from primaite.simulator.network.hardware.nodes.server import Server
from primaite.simulator.network.protocols.ntp import NTPPacket from primaite.simulator.network.protocols.ntp import NTPPacket, NTPRequest
from primaite.simulator.system.services.ntp.ntp_client import NTPClient from primaite.simulator.system.services.ntp.ntp_client import NTPClient
from primaite.simulator.system.services.ntp.ntp_server import NTPServer from primaite.simulator.system.services.ntp.ntp_server import NTPServer
from primaite.simulator.system.services.service import ServiceOperatingState from primaite.simulator.system.services.service import ServiceOperatingState
@@ -58,9 +58,11 @@ def test_ntp_client_server():
assert ntp_server.operating_state == ServiceOperatingState.RUNNING assert ntp_server.operating_state == ServiceOperatingState.RUNNING
assert ntp_client.operating_state == ServiceOperatingState.RUNNING assert ntp_client.operating_state == ServiceOperatingState.RUNNING
ntp_client.send(payload=NTPPacket()) ntp_request = NTPRequest(ntp_client="192.168.1.3")
assert ntp_server.receive() is True ntp_packet = NTPPacket(ntp_request=ntp_request)
assert ntp_client.receive() is True ntp_client.send(payload=ntp_packet)
assert ntp_server.receive(payload=ntp_packet) is True
assert ntp_client.receive(payload=ntp_packet) is True
assert ntp_client.apply_timestep(1) is True assert ntp_client.apply_timestep(1) is True
@@ -71,15 +73,20 @@ def test_ntp_server_failure():
server: Server = network.get_node_by_hostname("ntp_server") server: Server = network.get_node_by_hostname("ntp_server")
client: Computer = network.get_node_by_hostname("ntp_client") client: Computer = network.get_node_by_hostname("ntp_client")
ntp_server: NTPServer = server.software_manager.software["NTP_Server"] ntp_server: NTPServer = server.software_manager.software["NTPServer"]
ntp_client: NTPClient = client.software_manager.software["NTP_Client"] ntp_client: NTPClient = client.software_manager.software["NTPClient"]
assert ntp_client.operating_state == ServiceOperatingState.RUNNING assert ntp_client.operating_state == ServiceOperatingState.RUNNING
# Turn off ntp server. # Turn off ntp server.
ntp_server.stop() ntp_server.stop()
assert ntp_server.operating_state == ServiceOperatingState.STOPPED assert ntp_server.operating_state == ServiceOperatingState.STOPPED
assert ntp_client.receive() is False # And request a time update.
ntp_request = NTPRequest(ntp_client="192.168.1.3")
ntp_packet = NTPPacket(ntp_request=ntp_request)
ntp_client.send(payload=ntp_packet)
assert ntp_server.receive(payload=ntp_packet) is False
assert ntp_client.receive(payload=ntp_packet) is False
# Restart ntp server. # Restart ntp server.
ntp_server.start() ntp_server.start()