summaryrefslogtreecommitdiff
path: root/util/test_acroterm.py
blob: ed1092524c019891859d495764ae57dfecc74e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
# -*- coding: utf-8 -*-"
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
'Unit tests for acroterm.py'

import sys
import unittest

import acroterm

def report_packet_errors(errors):
    """Write packet error strings into stderr

    Called if number of error strings does not match test expectations.
    """
    if not errors:
        return
    sys.stderr.write('unexpected error set:\n')
    for error in errors:
        sys.stderr.write('%s\n' % error)


def process_samples(packet, samples):
    'Submit various data samples to packet and validate results'

    for data, handler in samples:
        for b in data:
            packet.add_byte(b)
        handler(data)
        packet.errors = []
        packet.get_decoded()

class TestPacket(unittest.TestCase):
    'Test various base Acropora packet failures'

    def good_packet(self, packet):
        'Verify good packet handling'
        if self.p.errors:
            report_packet_errors(self.p.errors)
            self.fail()
        d = self.p.get_decoded()
        self.assertEqual(d, '[13581998.891532/t1]')
        self.assertEqual(self.p.next_seq, (packet[1] & 0xf) + 1)

    def bad_seq(self, _):
        'Verify bad sequence number handling'
        if len(self.p.errors) != 1:
            report_packet_errors(self.p.errors)
            self.fail()
        self.assertEqual(self.p.errors[0],
                         '(missing packet(s)); got 0 expect 1')
        self.assertEqual(self.p.next_seq, 1)

    def with_data(self, packet):
        'Verify both cases of packet with data'
        if packet[-1] != 0xc1:
            if len(self.p.errors) != 1:
                report_packet_errors(self.p.errors)
                self.fail()
            self.assertEqual(self.p.errors[0],
                             '(packet data missing end magic; may be corrupt)')
        else:
            if self.p.errors:
                report_packet_errors(self.p.errors)
                self.fail()
        d = self.p.get_decoded()
        self.assertEqual(d, '[13590588.826124/t1] 67305985->134678021')

    def test_acorpora_packet(self):
        'Test various good and bad packets'

        # Tuple of two-tuples, the first element in the tuple pair is the
        # packet to send to the Packet class, the second element is the
        # function to invoke once the packet has been sent.
        samples = (
            ((0xc0, 0, 1, 0, 12, 34, 56, 78, 90, 12, 0, 0, 33),
             self.good_packet),
            ((0xc0, 0, 1, 0, 12, 34, 56, 78, 91, 12, 0, 0, 55),
             self.bad_seq),
            # Packet with valid data, but with an incorrect trailing character.
            ((0xc0, 0x41, 1, 4, 12, 34, 56, 78, 92, 12, 13, 0, 149,
              0x24, 0x2d, 0x3e, 0x24, 0x22, 1, 2, 3, 4, 5, 6, 7, 8, 0),
             self.with_data),
            # A valid packet with data.
            ((0xc0, 0x42, 1, 4, 12, 34, 56, 78, 92, 12, 13, 0, 180,
              0x24, 0x2d, 0x3e, 0x24, 0x22, 1, 2, 3, 4, 5, 6, 7, 8, 0xc1),
             self.with_data),
            )
        self.p = acroterm.Packet()
        process_samples(self.p, samples)

class TestCr50Packet(unittest.TestCase):
    'Test various base Acropora packet failures'

    def good_packet(self, packet):
        'Verify good packet handling'
        if self.p.errors:
            report_packet_errors(self.p.errors)
            self.fail()
        d = self.p.get_decoded()
        self.assertEqual(d, 'string 0')
        self.assertEqual(self.p.next_seq, (packet[1] & 0xf) + 1)

    def bad_seq(self, _):
        'Verify bad sequence number handling'
        if len(self.p.errors) != 1:
            report_packet_errors(self.p.errors)
            self.fail()
        self.assertEqual(self.p.errors[0],
                         '(missing packet(s)); got 0 expect 1')
        self.assertEqual(self.p.next_seq, 1)

    def with_data(self, packet):
        'Verify both cases of packet with data'
        d = self.p.get_decoded()
        if packet[-1] != 0xc1:
            if len(self.p.errors) != 1:
                report_packet_errors(self.p.errors)
                self.fail()
            self.assertEqual(self.p.errors[0],
                             '(packet data missing end magic; may be corrupt)')
        else:
            if self.p.errors:
                report_packet_errors(self.p.errors)
                self.fail()
        self.assertEqual(d, 'string ext param 230')

    def test_acorpora_packet(self):
        'Test various good and bad packets'

        strings = ['string 0', 'string 1', 'string %s %d']
        # Tuple of two-tuples, the first element in the tuple pair is the
        # packet to send to the Packet class, the second element is the
        # function to invoke once the packet has been sent.
        samples = (
            ((0xc2, 0, 1, 12, 34, 56, 78, 90, 12, 0, 0, 0, 79),
             self.good_packet),
            ((0xc2, 0, 1, 12, 34, 56, 78, 90, 13, 0, 0, 0, 89),
             self.bad_seq),
            ((0xc2, 1, 1, 12, 34, 56, 78, 90, 14, 0, 0, 0, 124),
             self.good_packet),
            # Packet with valid data, but with an incorrect trailing character.
            ((0xc2, 2, 1, 12, 34, 56, 78, 90, 15, 15, 2, 0, 38,
              101, 120, 116, 32, 112, 97, 114, 97, 109, 0, 230, 0, 0, 0, 0, 0),
             self.with_data),
            # A valid packet with data.
            ((0xc2, 3, 1, 12, 34, 56, 78, 90, 15, 15, 2, 0, 57,
              101, 120, 116, 32, 112, 97, 114, 97, 109, 0, 230, 0, 0, 0, 0,
              0xc1),
             self.with_data),
            )
        self.p = acroterm.Cr50Packet(strings)
        process_samples(self.p, samples)

if __name__ == '__main__':
    unittest.main()