Completed
Pull Request — master (#42)
by TJ
10:39
created

TestStarStructNone.pseudo_salted_md5()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
1
#!/usr/bin/env python3
2
3
"""Tests for the starstruct class"""
4
5
import struct
6
import unittest
7
from hashlib import md5
8
9
from starstruct.message import Message
10
11
12
class TestStarStructNone(unittest.TestCase):
13
    """StarStruct module tests"""
14
15
    VarTest = Message('VarTest', [
16
        ('x', 'B'),
17
        ('y', 'B'),
18
    ])
19
20
    def test_single_element_2(self):
21
        def pseudo_salted_md5(salt, original):
22
            temp_md5 = md5(original)
23
24
            if salt is None:
25
                salt = b''
26
27
            return md5(salt + temp_md5.digest()).digest()
28
29
        def pack_salt(data):
30
            return md5(data).digest()
31
32
        TestStruct = Message('TestStruct', [
33
            ('length_in_objects', 'H', 'vardata'),
34
            ('vardata', self.VarTest, 'length_in_objects'),
35
        ])
36
37
        CRCedMessage = Message('CRCedMessage', [
38
            ('data', TestStruct),
39
            ('salted', None),
40
            ('function_data', '16B', {
41
                'make': (pseudo_salted_md5, 'salted', b'data'),
42
                'pack': (pseudo_salted_md5, 'salted', b'data'),
43
                'unpack': (pack_salt, b'data'),
44
            }, False),
45
        ])
46
47
        test_data = {
48
            'data': {
49
                'length_in_objects': 2,
50
                'vardata': [
51
                    {'x': 1, 'y': 2},
52
                    {'x': 3, 'y': 4},
53
                ],
54
            },
55
            'salted': b'random_salter',
56
        }
57
58
        made = CRCedMessage.make(test_data)
59
        # assert len(made) == 5
60
        assert len(made.data.vardata) == 2
61
        assert made.data.vardata[0].x == 1
62
        assert made.data.vardata[0].y == 2
63
64
        no_data = made.pack()
65
        regular = CRCedMessage.pack(**test_data)
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
66
        assert regular == no_data
67
68
        # Show that there's no room to have the random salter be packed
69
        len_data = len(no_data) - 16
70
        assert no_data[0:len_data] == struct.pack('HBBBB', 2, 1, 2, 3, 4)
71
        assert md5(
72
            b'random_salter' +
73
            md5(no_data[0:len_data]).digest()
74
        ).digest() == no_data[len_data:]
75
76
        unpacked = CRCedMessage.unpack(no_data)
77
78
        assert unpacked.salted is None
79
        assert unpacked.function_data == made.function_data
80
81
        # TEMP
82
        new = unpacked._replace(**{'salted': b'random_salter'})
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
83
        assert new.salted == b'random_salter'
84
        # print(new._asdict())
85