~vanvugt/ubuntu/xenial/bluez/trunk

« back to all changes in this revision

Viewing changes to unit/test-crc.c

  • Committer: Daniel van Vugt
  • Date: 2017-05-24 07:16:52 UTC
  • Revision ID: daniel.van.vugt@canonical.com-20170524071652-8fnev589l3n104m1
Initial import from xenial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2011  Intel Corporation
 
6
 *
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include <config.h>
 
26
#endif
 
27
 
 
28
#include "monitor/crc.h"
 
29
#include "src/shared/tester.h"
 
30
 
 
31
#include <glib.h>
 
32
 
 
33
struct crc_data {
 
34
        const void *packet;
 
35
        size_t size;
 
36
        uint32_t crc_init;
 
37
};
 
38
 
 
39
static const unsigned char packet_1[] = {
 
40
        0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
 
41
        0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
 
42
        0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
 
43
        0x16, 0x52, 0x02, 0x0a, 0x00, 0xf4, 0x09, 0x92,
 
44
};
 
45
 
 
46
static const struct crc_data crc_1 = {
 
47
        .packet = packet_1,
 
48
        .size = sizeof(packet_1),
 
49
};
 
50
 
 
51
static const unsigned char packet_2[] = {
 
52
        0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
 
53
        0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
 
54
        0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
 
55
        0x16, 0x54, 0x02, 0x0a, 0x00, 0x95, 0x5f, 0x14,
 
56
};
 
57
 
 
58
static const struct crc_data crc_2 = {
 
59
        .packet = packet_2,
 
60
        .size = sizeof(packet_2),
 
61
};
 
62
 
 
63
static const unsigned char packet_3[] = {
 
64
        0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
 
65
        0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
 
66
        0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
 
67
        0x16, 0x55, 0x02, 0x0a, 0x00, 0x85, 0x66, 0x63,
 
68
};
 
69
 
 
70
static const struct crc_data crc_3 = {
 
71
        .packet = packet_3,
 
72
        .size = sizeof(packet_3),
 
73
};
 
74
 
 
75
static const unsigned char packet_4[] = {
 
76
        0xd6, 0xbe, 0x89, 0x8e, 0x00, 0x17, 0x7e, 0x01,
 
77
        0x00, 0xd0, 0x22, 0x00, 0x02, 0x01, 0x06, 0x03,
 
78
        0x02, 0x0d, 0x18, 0x06, 0xff, 0x6b, 0x00, 0x03,
 
79
        0x16, 0x53, 0x02, 0x0a, 0x00, 0xe4, 0x30, 0xe5,
 
80
};
 
81
 
 
82
static const struct crc_data crc_4 = {
 
83
        .packet = packet_4,
 
84
        .size = sizeof(packet_4),
 
85
};
 
86
 
 
87
static const unsigned char packet_5[] = {
 
88
        0xd6, 0xbe, 0x89, 0x8e, 0x03, 0x0c, 0x46, 0x1c,
 
89
        0xda, 0x72, 0x02, 0x00, 0x7e, 0x01, 0x00, 0xd0,
 
90
        0x22, 0x00, 0x6e, 0xf4, 0x6f,
 
91
};
 
92
 
 
93
static const struct crc_data crc_5 = {
 
94
        .packet = packet_5,
 
95
        .size = sizeof(packet_5),
 
96
};
 
97
 
 
98
static const unsigned char packet_6[] = {
 
99
        0xd6, 0xbe, 0x89, 0x8e, 0x04, 0x17, 0x7e, 0x01,
 
100
        0x00, 0xd0, 0x22, 0x00, 0x10, 0x09, 0x50, 0x6f,
 
101
        0x6c, 0x61, 0x72, 0x20, 0x48, 0x37, 0x20, 0x30,
 
102
        0x30, 0x30, 0x31, 0x37, 0x45, 0x0f, 0x8a, 0x65,
 
103
};
 
104
 
 
105
static const struct crc_data crc_6 = {
 
106
        .packet = packet_6,
 
107
        .size = sizeof(packet_6),
 
108
};
 
109
 
 
110
static const unsigned char packet_7[] = {
 
111
        0xd6, 0xbe, 0x89, 0x8e, 0x05, 0x22, 0x46, 0x1c,
 
112
        0xda, 0x72, 0x02, 0x00, 0x7e, 0x01, 0x00, 0xd0,
 
113
        0x22, 0x00, 0x96, 0x83, 0x9a, 0xaf, 0xbe, 0x1d,
 
114
        0x16, 0x03, 0x05, 0x00, 0x36, 0x00, 0x00, 0x00,
 
115
        0x2a, 0x00, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xa5,
 
116
        0x77, 0x2d, 0x95,
 
117
};
 
118
 
 
119
static const struct crc_data crc_7 = {
 
120
        .packet = packet_7,
 
121
        .size = sizeof(packet_7),
 
122
};
 
123
 
 
124
static const unsigned char packet_8[] = {
 
125
        0x96, 0x83, 0x9a, 0xaf, 0x01, 0x00, 0xc7, 0x15,
 
126
        0x4d,
 
127
};
 
128
 
 
129
static const struct crc_data crc_8 = {
 
130
        .packet = packet_8,
 
131
        .size = sizeof(packet_8),
 
132
        .crc_init = 0x161dbe,           /* from packet_7 = 0xbe 0x1d 0x16 */
 
133
};
 
134
 
 
135
static const unsigned char packet_9[] = {
 
136
        0x96, 0x83, 0x9a, 0xaf, 0x06, 0x14, 0x10, 0x00,
 
137
        0x04, 0x00, 0x09, 0x07, 0x10, 0x00, 0x10, 0x11,
 
138
        0x00, 0x37, 0x2a, 0x13, 0x00, 0x02, 0x14, 0x00,
 
139
        0x38, 0x2a, 0x73, 0x2a, 0xa3,
 
140
};
 
141
 
 
142
static const struct crc_data crc_9 = {
 
143
        .packet = packet_9,
 
144
        .size = sizeof(packet_9),
 
145
        .crc_init = 0x161dbe,           /* from packet_7 = 0xbe 0x1d 0x16 */
 
146
};
 
147
 
 
148
static void test_crc(gconstpointer data)
 
149
{
 
150
        const struct crc_data *test_data = data;
 
151
        const uint8_t *buf = test_data->packet + test_data->size - 3;
 
152
        uint32_t crc_init, crc_value, crc, rev;
 
153
 
 
154
        if (test_data->crc_init)
 
155
                crc_init = crc24_bit_reverse(test_data->crc_init);
 
156
        else
 
157
                crc_init = crc24_bit_reverse(0x555555);
 
158
 
 
159
        crc_value = buf[0] | buf[1] << 8 | buf[2] << 16;
 
160
 
 
161
        crc = crc24_calculate(crc_init, test_data->packet + 4,
 
162
                                                test_data->size - 7);
 
163
 
 
164
        tester_debug("CRC: 0x%6.6x, Calculated: 0x%6.6x", crc_value, crc);
 
165
 
 
166
        g_assert(crc_value == crc);
 
167
 
 
168
        rev = crc24_reverse(crc_value, test_data->packet + 4,
 
169
                                                test_data->size - 7);
 
170
 
 
171
        tester_debug("Preset: 0x%6.6x, Calculated: 0x%6.6x", crc_init, rev);
 
172
 
 
173
        g_assert(crc_init == rev);
 
174
 
 
175
        tester_test_passed();
 
176
}
 
177
 
 
178
int main(int argc, char *argv[])
 
179
{
 
180
        tester_init(&argc, &argv);
 
181
 
 
182
        tester_add("/crc/1", &crc_1, NULL, test_crc, NULL);
 
183
        tester_add("/crc/2", &crc_2, NULL, test_crc, NULL);
 
184
        tester_add("/crc/3", &crc_3, NULL, test_crc, NULL);
 
185
        tester_add("/crc/4", &crc_4, NULL, test_crc, NULL);
 
186
        tester_add("/crc/5", &crc_5, NULL, test_crc, NULL);
 
187
        tester_add("/crc/6", &crc_6, NULL, test_crc, NULL);
 
188
        tester_add("/crc/7", &crc_7, NULL, test_crc, NULL);
 
189
        tester_add("/crc/8", &crc_8, NULL, test_crc, NULL);
 
190
        tester_add("/crc/9", &crc_9, NULL, test_crc, NULL);
 
191
 
 
192
        return tester_run();
 
193
}