Completed
Pull Request — master (#42)
by TJ
01:41
created

TestStarStructNone   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 64
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pseudo_salted_md5() 0 7 2
D test_single_element_2() 0 56 11
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
        TestStruct = Message('TestStruct', [
30
            ('length_in_objects', 'H', 'vardata'),
31
            ('vardata', self.VarTest, 'length_in_objects'),
32
        ])
33
34
        CRCedMessage = Message('CRCedMessage', [
35
            ('data', TestStruct),
36
            ('salted', None),
37
            ('function_data', '16B', pseudo_salted_md5, ['salted', b'data'], False),
38
        ])
39
40
        test_data = {
41
            'data': {
42
                'length_in_objects': 2,
43
                'vardata': [
44
                    {'x': 1, 'y': 2},
45
                    {'x': 3, 'y': 4},
46
                ],
47
            },
48
            'salted': b'random_salter',
49
        }
50
51
        made = CRCedMessage.make(test_data)
52
        # assert len(made) == 5
53
        assert len(made.data.vardata) == 2
54
        assert made.data.vardata[0].x == 1
55
        assert made.data.vardata[0].y == 2
56
57
        no_data = made.pack()
58
        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...
59
        assert regular == no_data
60
61
        # Show that there's no room to have the random salter be packed
62
        len_data = len(no_data) - 16
63
        assert no_data[0:len_data] == struct.pack('HBBBB', 2, 1, 2, 3, 4)
64
        assert md5(
65
            b'random_salter' +
66
            md5(no_data[0:len_data]).digest()
67
        ).digest() == no_data[len_data:]
68
69
        unpacked = CRCedMessage.unpack(no_data)
70
71
        assert unpacked.salted is None
72
73
        # TEMP
74
        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...
75
        assert new.salted == b'random_salter'
76
        # print(new._asdict())
77