Removing the error raising in validate_data
This commit is contained in:
parent
a985dac4b0
commit
bb62669722
|
@ -707,11 +707,10 @@ class Worm(Thread):
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
while True:
|
while True:
|
||||||
try:
|
pkt, hazelnut = self.squirrel.receive_packet()
|
||||||
pkt, hazelnut = self.squirrel.receive_packet()
|
correct = pkt.validate_data()
|
||||||
pkt.validate_data()
|
if not correct :
|
||||||
except ValueError as error:
|
self.squirrel.add_system_message("I received a incorrect packet")
|
||||||
self.squirrel.add_system_message("An error occurred while receiving a packet: {}".format(error))
|
|
||||||
else:
|
else:
|
||||||
for tlv in pkt.body:
|
for tlv in pkt.body:
|
||||||
tlv.handle(self.squirrel, hazelnut)
|
tlv.handle(self.squirrel, hazelnut)
|
||||||
|
|
|
@ -74,8 +74,7 @@ class Pad1TLV(TLV):
|
||||||
return self.type.to_bytes(1, sys.byteorder)
|
return self.type.to_bytes(1, sys.byteorder)
|
||||||
|
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||||
# TODO Add some easter eggs
|
squirrel.add_system_message("I received a Pad1TLV, how disapointing")
|
||||||
squirrel.add_system_message("For each byte in the packet that I received, you will die today. And eat cookies.")
|
|
||||||
|
|
||||||
def __len__(self) -> int:
|
def __len__(self) -> int:
|
||||||
"""
|
"""
|
||||||
|
@ -100,7 +99,8 @@ class PadNTLV(TLV):
|
||||||
|
|
||||||
def validate_data(self) -> bool:
|
def validate_data(self) -> bool:
|
||||||
if self.mbz != int(0).to_bytes(self.length, sys.byteorder):
|
if self.mbz != int(0).to_bytes(self.length, sys.byteorder):
|
||||||
raise ValueError("The body of a PadN TLV is not filled with zeros.")
|
return False
|
||||||
|
#raise ValueError("The body of a PadN TLV is not filled with zeros.")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def unmarshal(self, raw_data: bytes) -> None:
|
def unmarshal(self, raw_data: bytes) -> None:
|
||||||
|
@ -119,8 +119,8 @@ class PadNTLV(TLV):
|
||||||
+ self.mbz[:self.length]
|
+ self.mbz[:self.length]
|
||||||
|
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||||
# TODO Add some easter eggs
|
if self.validate_data():
|
||||||
squirrel.add_system_message(f"I received {self.length} zeros, am I so a bad guy ? :cold_sweat:")
|
squirrel.add_system_message(f"I received {self.length} zeros")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def construct(length: int) -> "PadNTLV":
|
def construct(length: int) -> "PadNTLV":
|
||||||
|
@ -139,8 +139,9 @@ class HelloTLV(TLV):
|
||||||
|
|
||||||
def validate_data(self) -> bool:
|
def validate_data(self) -> bool:
|
||||||
if self.length != 8 and self.length != 16:
|
if self.length != 8 and self.length != 16:
|
||||||
raise ValueError("The length of a Hello TLV must be 8 for a short Hello, or 16 for a long Hello,"
|
return False
|
||||||
f"found {self.length}")
|
#raise ValueError("The length of a Hello TLV must be 8 for a short Hello, or 16 for a long Hello,"
|
||||||
|
# f"found {self.length}")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def unmarshal(self, raw_data: bytes) -> None:
|
def unmarshal(self, raw_data: bytes) -> None:
|
||||||
|
@ -158,6 +159,8 @@ class HelloTLV(TLV):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||||
|
if not self.validate_data(self):
|
||||||
|
return None
|
||||||
time_h = time.time()
|
time_h = time.time()
|
||||||
if not squirrel.is_active(sender):
|
if not squirrel.is_active(sender):
|
||||||
sender.id = self.source_id # The sender we are given misses an id
|
sender.id = self.source_id # The sender we are given misses an id
|
||||||
|
@ -421,16 +424,19 @@ class Packet:
|
||||||
def validate_data(self) -> bool:
|
def validate_data(self) -> bool:
|
||||||
"""
|
"""
|
||||||
Ensure that the packet is well-formed.
|
Ensure that the packet is well-formed.
|
||||||
Raises a ValueError if the packet contains bad data.
|
Returns False if the packet contains bad data.
|
||||||
"""
|
"""
|
||||||
if self.magic != 95:
|
if self.magic != 95:
|
||||||
raise ValueError("The magic code of the packet must be 95, found: {:d}".format(self.magic))
|
return False
|
||||||
|
#raise ValueError("The magic code of the packet must be 95, found: {:d}".format(self.magic))
|
||||||
if self.version != 0:
|
if self.version != 0:
|
||||||
raise ValueError("The version of the packet is not supported: {:d}".format(self.version))
|
return False
|
||||||
|
#raise ValueError("The version of the packet is not supported: {:d}".format(self.version))
|
||||||
if not (0 <= self.body_length <= 1200):
|
if not (0 <= self.body_length <= 1200):
|
||||||
raise ValueError("The body length of the packet is negative or too high. It must be between 0 and 1020,"
|
return False
|
||||||
"found: {:d}".format(self.body_length))
|
#raise ValueError("The body length of the packet is negative or too high. It must be between 0 and 1020,"
|
||||||
return all(tlv.validate_data() for tlv in self.body)
|
# "found: {:d}".format(self.body_length))
|
||||||
|
return True #all(tlv.validate_data() for tlv in self.body)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def unmarshal(data: bytes) -> "Packet":
|
def unmarshal(data: bytes) -> "Packet":
|
||||||
|
@ -446,15 +452,13 @@ class Packet:
|
||||||
read_bytes = 0
|
read_bytes = 0
|
||||||
while read_bytes < min(len(data) - 4, pkt.body_length):
|
while read_bytes < min(len(data) - 4, pkt.body_length):
|
||||||
tlv_type = data[4 + read_bytes]
|
tlv_type = data[4 + read_bytes]
|
||||||
if not (0 <= tlv_type < len(TLV.tlv_classes())):
|
if (0 <= tlv_type < len(TLV.tlv_classes())):
|
||||||
raise ValueError(f"TLV type is not supported: {tlv_type}")
|
tlv = TLV.tlv_classes()[tlv_type]()
|
||||||
tlv = TLV.tlv_classes()[tlv_type]()
|
tlv.unmarshal(data[4 + read_bytes:4 + read_bytes + pkt.body_length])
|
||||||
tlv.unmarshal(data[4 + read_bytes:4 + read_bytes + pkt.body_length])
|
pkt.body.append(tlv)
|
||||||
pkt.body.append(tlv)
|
read_bytes += len(tlv)
|
||||||
read_bytes += len(tlv)
|
# Other TLV types are ignored
|
||||||
|
|
||||||
pkt.validate_data()
|
|
||||||
|
|
||||||
return pkt
|
return pkt
|
||||||
|
|
||||||
def marshal(self) -> bytes:
|
def marshal(self) -> bytes:
|
||||||
|
|
Loading…
Reference in New Issue