11import pickle
2+ import _pickle
23import pytest
34import unittest
45from unittest import mock
910
1011
1112class TestCCSDSPacketCheck (unittest .TestCase ):
12-
1313 # Check if packet length is at least 7 bytes
1414 def test_ccsds_packet_length (self ):
1515 handler = CCSDSPacketHandler (packet_types = {"01011100111" : "CCSDS_HEADER" })
@@ -52,76 +52,106 @@ def test_ccsds_packet_uid(self):
5252 self .assertEqual (packet_uid , pickle .loads (result )[0 ])
5353
5454
55- class TestHandlerClassWithInputOutputTypes (object ):
56- handler = PacketHandler (packet = "CCSDS_HEADER" , input_type = "int" , output_type = "str" )
55+ class TestCCSDSHandlerClassWithInputOutputTypes (object ):
56+ handler = CCSDSPacketHandler (
57+ packet_types = {"01011100111" : "CCSDS_HEADER" },
58+ input_type = "int" ,
59+ output_type = "str" ,
60+ )
5761
5862 def test_handler_creation (self ):
5963 assert self .handler .input_type is "int"
6064 assert self .handler .output_type is "str"
6165
6266 @mock .patch (
63- "ait.core.server.handlers.PacketHandler.handle" , return_value = "SpecialReturn"
67+ "ait.core.server.handlers.CCSDSPacketHandler.handle" ,
68+ return_value = "SpecialReturn" ,
6469 )
6570 def test_execute_handler_returns_handle_return_on_input (self , handle_mock ):
66- returned = self .handler .handle ("2" )
71+ data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
72+ returned = self .handler .handle (data )
6773 assert returned == "SpecialReturn"
6874
6975
70- class TestHandlerClassWith1553HSPacket (object ):
76+ class TestCCSDSHandlerClassWithoutInputOutputTypes (object ):
77+ handler = CCSDSPacketHandler (packet_types = {"01011100111" : "CCSDS_HEADER" })
78+
79+ def test_ccsds_handler_default_params (self ):
80+ assert self .handler .input_type is None
81+ assert self .handler .output_type is None
82+
83+ @mock .patch (
84+ "ait.core.server.handlers.CCSDSPacketHandler.handle" ,
85+ return_value = "SpecialReturn" ,
86+ )
87+ def test_execute_handler_returns_handle_return_on_input (self , handle_mock ):
88+ data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
89+ returned = self .handler .handle (data )
90+ assert returned == "SpecialReturn"
91+
92+ def test_handler_repr (self ):
93+ assert self .handler .__repr__ () == "<handler.CCSDSPacketHandler>"
94+
95+
96+ class TestHandlerClassWith1553HSPacket (unittest .TestCase ):
7197 tlm_dict = tlm .getDefaultDict ()
72- pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
98+ pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x02 \x03 \x04 " )
7399 pkt_1553 = tlm_dict ['1553_HS_Packet' ]
74- handler = PacketHandler (pkt_data , packet = "1553_HS_Packet" )
100+ handler = PacketHandler (packet_type = "1553_HS_Packet" )
75101
76102 def test_word_array (self ):
77103 packet = tlm .Packet (self .pkt_1553 , self .pkt_data )
78- assert packet .words .__len__ () == 3.5
104+ assert packet .words .__len__ () == self . pkt_1553 . nbytes / 2
79105
80- def test_execute_handler_returns_handle_return_on_input (self ):
81- packet = tlm . Packet ( self .pkt_1553 , self . pkt_data )
82- result = self .handler .handle (packet )
83- assert 'Ethernet 1553 packet' in str (result )
106+ def test_1553_uid (self ):
107+ packet_uid = self .tlm_dict [ "1553_HS_Packet" ]. uid
108+ result = self .handler .handle (self . pkt_data )
109+ self . assertEqual ( packet_uid , pickle . loads (result )[ 0 ] )
84110
85- # Test packet length by sending a Ethernet_HS_Packet to a 1553_HS_Packet Handler
111+ # Send only 5 bytes 1553 Packet expects 10 bytes
86112 def test_bad_packet_length (self ):
87- ethernet_pkt = self .tlm_dict ['Ethernet_HS_Packet' ]
88- e_packet = tlm .Packet (ethernet_pkt , self .pkt_data )
89- with pytest .raises (ValueError ):
90- self .handler .handle (e_packet )
113+ pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 " )
114+ with self .assertLogs ("ait" , level = "INFO" ) as cm :
115+ self .handler .handle (pkt_data )
116+ self .assertIn (
117+ "Packet data length does not match packet definition." ,
118+ cm .output [0 ],
119+ )
91120
92121 def test_packet_name_error_and_no_packet_type (self ):
93- pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
94122 with pytest .raises (ValueError ):
95- PacketHandler (pkt_data , packet = "1553_HS_Packe" )
96- with pytest .raises (ValueError ):
97- PacketHandler (pkt_data )
123+ PacketHandler (packet_type = "" )
98124
99125
100- class TestHandlerClassWithEthernetHSPacket (object ):
126+ class TestHandlerClassWithEthernetHSPacket (unittest . TestCase ):
101127 tlm_dict = tlm .getDefaultDict ()
102- pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x07 \x08 \x0a " )
103- ethernet_pkt = tlm_dict ['Ethernet_HS_Packet' ]
104- handler = PacketHandler (pkt_data , packet = "Ethernet_HS_Packet" )
128+ pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x40 \x00 \x03 \x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x40 \x00 \x03 "
129+ b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x40 \x00 \x03 \x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
130+ ethernet_pkt_def = tlm_dict ['Ethernet_HS_Packet' ]
131+ handler = PacketHandler (packet_type = "Ethernet_HS_Packet" )
105132
106133 def test_word_array (self ):
107- e_packet = tlm .Packet (self .ethernet_pkt , self .pkt_data )
108- assert e_packet .words .__len__ () == 5
134+ e_packet = tlm .Packet (self .ethernet_pkt_def , self .pkt_data )
135+ assert e_packet .words .__len__ () == self . ethernet_pkt_def . nbytes / 2.0
109136
110- def test_execute_handler_returns_handle_return_on_input (self ):
111- e_packet = tlm . Packet ( self .ethernet_pkt , self . pkt_data )
112- result = self .handler .handle (e_packet )
113- assert 'Ethernet Health and Status Packet' in str (result )
137+ def test_1553_uid (self ):
138+ packet_uid = self .tlm_dict [ "Ethernet_HS_Packet" ]. uid
139+ result = self .handler .handle (self . pkt_data )
140+ self . assertEqual ( packet_uid , pickle . loads (result )[ 0 ] )
114141
115- # Send a 1553 packet to an Ethernet_HS_Packet Handler
142+ # Ethernet packet expects 37 bytes 1552 expects 10
116143 def test_bad_packet_length (self ):
117- pkt_1553 = self .tlm_dict ['1553_HS_Packet' ]
118- packet = tlm .Packet (pkt_1553 , self .pkt_data )
119- with pytest .raises (ValueError ):
120- self .handler .handle (packet )
144+ pkt_data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x07 \x08 \x0a " )
145+ with self .assertLogs ("ait" , level = "INFO" ) as cm :
146+ self .handler .handle (pkt_data )
147+ self .assertIn (
148+ "Packet data length does not match packet definition." ,
149+ cm .output [0 ],
150+ )
121151
122152
123153class TestHandlerClassWithoutInputOutputTypes (object ):
124- handler = PacketHandler (packet = "CCSDS_HEADER " )
154+ handler = PacketHandler (packet_type = "Ethernet_HS_Packet " )
125155
126156 def test_handler_default_params (self ):
127157 assert self .handler .input_type is None
@@ -138,9 +168,9 @@ def test_handler_repr(self):
138168 assert self .handler .__repr__ () == "<handler.PacketHandler>"
139169
140170
141- class TestCCSDSHandlerClassWithInputOutputTypes (object ):
142- handler = CCSDSPacketHandler (
143- packet_types = { "01011100111" : "CCSDS_HEADER" } ,
171+ class TestHandlerClassWithInputOutputTypes (object ):
172+ handler = PacketHandler (
173+ packet_type = '1553_HS_Packet' ,
144174 input_type = "int" ,
145175 output_type = "str" ,
146176 )
@@ -150,30 +180,31 @@ def test_handler_creation(self):
150180 assert self .handler .output_type is "str"
151181
152182 @mock .patch (
153- "ait.core.server.handlers.CCSDSPacketHandler .handle" ,
183+ "ait.core.server.handlers.PacketHandler .handle" ,
154184 return_value = "SpecialReturn" ,
155185 )
156186 def test_execute_handler_returns_handle_return_on_input (self , handle_mock ):
157- data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
187+ data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x02 \x03 \x04 " )
158188 returned = self .handler .handle (data )
159189 assert returned == "SpecialReturn"
160190
161191
162- class TestCCSDSHandlerClassWithoutInputOutputTypes (object ):
163- handler = CCSDSPacketHandler ( packet_types = { "01011100111" : "CCSDS_HEADER" } )
192+ class TestHandlerClassWithoutInputOutputTypes (object ):
193+ handler = PacketHandler ( packet_type = "Ethernet_HS_Packet" )
164194
165195 def test_ccsds_handler_default_params (self ):
166196 assert self .handler .input_type is None
167197 assert self .handler .output_type is None
168198
169199 @mock .patch (
170- "ait.core.server.handlers.CCSDSPacketHandler .handle" ,
200+ "ait.core.server.handlers.PacketHandler .handle" ,
171201 return_value = "SpecialReturn" ,
172202 )
173203 def test_execute_handler_returns_handle_return_on_input (self , handle_mock ):
174- data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 " )
204+ # Note: Using 'mock' handler, the data will not be tested for length.
205+ data = bytearray (b"\x02 \xE7 \x40 \x00 \x00 \x00 \x01 \x02 \x03 \x04 " )
175206 returned = self .handler .handle (data )
176207 assert returned == "SpecialReturn"
177208
178209 def test_handler_repr (self ):
179- assert self .handler .__repr__ () == "<handler.CCSDSPacketHandler >"
210+ assert self .handler .__repr__ () == "<handler.PacketHandler >"
0 commit comments