Skip to content

Commit

Permalink
add validity check for packet formats
Browse files Browse the repository at this point in the history
  • Loading branch information
VanL15 committed Nov 10, 2023
1 parent 967e8bb commit d037746
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions EosLib/format/base_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ def encode(self) -> bytes:
@abstractmethod
def decode(cls, data: bytes) -> Self:
raise NotImplementedError

@abstractmethod
def get_validity(self) -> bool:
raise NotImplementedError
2 changes: 1 addition & 1 deletion EosLib/format/formats/cutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __eq__(self, other):
return self.ack == other.ack and \
self.valid == other.valid

def get_validity(self):
def get_validity(self) -> bool:
if self.ack < 0 or self.ack > 255:
return False
else:
Expand Down
2 changes: 1 addition & 1 deletion EosLib/format/formats/e_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __eq__(self, other):
self.voltage_c == other.voltage_c and \
self.valid == other.valid

def get_validity(self):
def get_validity(self) -> bool:
"""Checks if data is valid
voltage range will be between 0-1.5 volts
Expand Down
3 changes: 3 additions & 0 deletions EosLib/format/formats/empty_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ def decode(cls, data: bytes) -> Self:

def get_format_string(self) -> str:
return self.num_bytes * "x"

def get_validity(self) -> bool:
return self.num_bytes >= 0
2 changes: 1 addition & 1 deletion EosLib/format/formats/ping_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __eq__(self, other):
self.num == other.num and \
self.valid == other.valid

def get_validity(self):
def get_validity(self) -> bool:
if self.num < 0 or self.num > 255:
return False
else:
Expand Down
2 changes: 1 addition & 1 deletion EosLib/format/formats/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def decode_from_csv(cls, csv_string: str) -> Self:
int(csv_list[5]),
FlightState(int(csv_list[6])))

def get_validity(self):
def get_validity(self) -> bool:
if (self.number_of_satellites < 4 or
self.latitude == 0 or
self.longitude == 0):
Expand Down
2 changes: 1 addition & 1 deletion EosLib/format/formats/telemetry_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __eq__(self, other):
self.z_rotation == other.z_rotation and \
self.valid == other.valid

def get_validity(self):
def get_validity(self) -> bool:
"""Checks if data is valid (NEED A MORE CONCRETE WAY OF VALIDATING)
"""
Expand Down
2 changes: 1 addition & 1 deletion EosLib/format/formats/valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __eq__(self, other):
return self.ack == other.ack and \
self.valid == other.valid

def get_validity(self):
def get_validity(self) -> bool:
if self.ack < 0 or self.ack > 255:
return False
else:
Expand Down
3 changes: 3 additions & 0 deletions EosLib/packet/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def validate_packet(self):
if not issubclass(self.body.__class__, BaseFormat):
raise PacketFormatError("All packets must have a body that extends BaseFormat")

if self.body.get_validity() is False:
raise PacketFormatError("All packets must contain a valid format")

if self.data_header.priority != Priority.NO_TRANSMIT:
total_length = struct.calcsize(TransmitHeader.transmit_header_struct_format_string) + \
struct.calcsize(DataHeader.data_header_struct_format_string) + \
Expand Down
11 changes: 11 additions & 0 deletions tests/packet/test_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import EosLib.packet.definitions as definitions

from EosLib.format.formats.empty_format import EmptyFormat
from EosLib.format.formats.e_field import EField

from datetime import datetime

Expand Down Expand Up @@ -154,6 +155,16 @@ def test_illegal_body_type(packet, illegal_body):
packet.encode()


def test_valid_body_format(packet):
packet.body = EField(0, 1.5, 1)
assert packet.body.get_validity()


def test_invalid_body_format(packet):
packet.body = EField(-1, 1.5, 1)
assert not packet.body.get_validity()


def test_allow_large_body_no_transmit(packet):
packet.body = EmptyFormat(Packet.radio_body_max_bytes + 1)
packet.data_header.priority = definitions.Priority.NO_TRANSMIT
Expand Down

0 comments on commit d037746

Please sign in to comment.