Skip to content

Commit

Permalink
[WIP]: Mon Dec 19 15:24:25 EST 2016
Browse files Browse the repository at this point in the history
  • Loading branch information
TJ DeVries committed Dec 19, 2016
1 parent 51dbf45 commit 8f3b8d2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
7 changes: 4 additions & 3 deletions starstruct/elementcallable.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ def pack(self, msg):
def unpack(self, msg, buf):
"""Unpack data from the supplied buffer using the initialized format."""
ret = self._struct.unpack_from(buf)
if isinstance(ret, (list, tuple)):
# TODO: I don't know if there is a case where we want to keep
# it as a list... but for now I'm just going to do this
if isinstance(ret, (list, tuple)) and len(ret) == 1:
# We only change it not to a list if we expected one value.
# Otherwise, we keep it as a list, because that's what we would
# expect (like for a 16I type of struct
ret = ret[0]

# Only check for errors if they haven't told us not to
Expand Down
2 changes: 1 addition & 1 deletion starstruct/elementnone.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def pack(self, msg):
return b''

def unpack(self, msg, buf):
return (msg, buf)
return (None, buf)

def make(self, msg):
return msg[self.name]
2 changes: 2 additions & 0 deletions starstruct/startuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def StarTuple(name, named_fields, elements):

named_tuple = collections.namedtuple(name, named_fields)

# TODO: Auto update and replace!

def this_pack(self):
packed = bytes()
for _, value in self._elements.items():
Expand Down
24 changes: 23 additions & 1 deletion starstruct/tests/test_elementnone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""Tests for the starstruct class"""

import struct
import unittest
from hashlib import md5

Expand All @@ -19,6 +20,10 @@ class TestStarStructNone(unittest.TestCase):
def test_single_element_2(self):
def pseudo_salted_md5(salt, original):
temp_md5 = md5(original)

if salt is None:
salt = b''

return md5(salt + temp_md5.digest()).digest()

TestStruct = Message('TestStruct', [
Expand All @@ -29,7 +34,7 @@ def pseudo_salted_md5(salt, original):
CRCedMessage = Message('CRCedMessage', [
('data', TestStruct),
('salted', None),
('function_data', '16B', pseudo_salted_md5, ['salted', b'data']),
('function_data', '16B', pseudo_salted_md5, ['salted', b'data'], False),
])

test_data = {
Expand All @@ -52,3 +57,20 @@ def pseudo_salted_md5(salt, original):
no_data = made.pack()
regular = CRCedMessage.pack(**test_data)
assert regular == no_data

# Show that there's no room to have the random salter be packed
len_data = len(no_data) - 16
assert no_data[0:len_data] == struct.pack('HBBBB', 2, 1, 2, 3, 4)
assert md5(
b'random_salter' +
md5(no_data[0:len_data]).digest()
).digest() == no_data[len_data:]

unpacked = CRCedMessage.unpack(no_data)

assert unpacked.salted is None

# TEMP
new = unpacked._replace(**{'salted': b'random_salter'})
assert new.salted == b'random_salter'
# print(new._asdict())

0 comments on commit 8f3b8d2

Please sign in to comment.