]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/fletcher16_test.c
Kernel: Do not use route replace when krt_metric differs
[thirdparty/bird.git] / lib / fletcher16_test.c
1 /*
2 * BIRD Library -- Fletcher-16 Tests
3 *
4 * (c) 2015 CZ.NIC z.s.p.o.
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #include "test/birdtest.h"
10 #include "lib/fletcher16.h"
11
12 static u16
13 straightforward_fletcher16_compute(const char *data)
14 {
15 int count = strlen(data);
16
17 u16 sum1 = 0;
18 u16 sum2 = 0;
19 int index;
20
21 for (index = 0; index < count; ++index)
22 {
23 sum1 = (sum1 + data[index]) % 255;
24 sum2 = (sum2 + sum1) % 255;
25 }
26
27 sum2 = (sum2 + sum1) % 255;
28 sum2 = (sum2 + sum1) % 255;
29
30 return (sum1 << 8) | sum2;
31 }
32
33 static u16
34 straightforward_fletcher16_checksum(const char *data)
35 {
36 u16 csum;
37 u16 c0,c1,x,y;
38
39 csum = straightforward_fletcher16_compute(data);
40 c0 = (csum >> 8) & 0xff;
41 c1 = csum & 0xff;
42
43 x = (255 + c0 - c1) % 255;
44 y = (510 - 2*c0 + c1) % 255;
45
46 if (!x) x = 255;
47 if (!y) y = 255;
48
49 return (x << 8) | y;
50 }
51
52 const u8 zero16[2] = {};
53
54 static int
55 test_fletcher16(void *out_, const void *in_, const void *expected_out_)
56 {
57 u16 *out = out_;
58 const char *in = in_;
59 const u16 *expected_out = expected_out_;
60
61 struct fletcher16_context ctxt;
62
63 fletcher16_init(&ctxt);
64 fletcher16_update(&ctxt, in, strlen(in));
65 fletcher16_update(&ctxt, zero16, 2);
66 *out = fletcher16_compute(&ctxt);
67
68 return *out == *expected_out;
69 }
70
71 static int
72 test_fletcher16_checksum(void *out_, const void *in_, const void *expected_out_)
73 {
74 u16 *out = out_;
75 const char *in = in_;
76 const u16 *expected_out = expected_out_;
77
78 struct fletcher16_context ctxt;
79 int len = strlen(in);
80
81 fletcher16_init(&ctxt);
82 fletcher16_update(&ctxt, in, len);
83 fletcher16_update(&ctxt, zero16, 2);
84 *out = fletcher16_final(&ctxt, len+2, len);
85
86 return *out == *expected_out;
87 }
88
89 static int
90 t_fletcher16_compute(void)
91 {
92 struct bt_pair test_vectors[] = {
93 {
94 .in = "\001\002",
95 .out = & ((const u16) { straightforward_fletcher16_compute("\001\002") }),
96 },
97 {
98 .in = "",
99 .out = & ((const u16) { straightforward_fletcher16_compute("") }),
100 },
101 {
102 .in = "a",
103 .out = & ((const u16) { straightforward_fletcher16_compute("a") }),
104 },
105 {
106 .in = "abcd",
107 .out = & ((const u16) { straightforward_fletcher16_compute("abcd") }),
108 },
109 {
110 .in = "message digest",
111 .out = & ((const u16) { straightforward_fletcher16_compute("message digest") }),
112 },
113 {
114 .in = "abcdefghijklmnopqrstuvwxyz",
115 .out = & ((const u16) { straightforward_fletcher16_compute("abcdefghijklmnopqrstuvwxyz") }),
116 },
117 {
118 .in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
119 .out = & ((const u16) { straightforward_fletcher16_compute("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
120 },
121 {
122 .in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
123 .out = & ((const u16) { straightforward_fletcher16_compute("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
124 },
125 };
126
127 return bt_assert_batch(test_vectors, test_fletcher16, bt_fmt_str, bt_fmt_unsigned);
128 }
129
130 static int
131 t_fletcher16_checksum(void)
132 {
133 struct bt_pair test_vectors[] = {
134 {
135 .in = "\001\002",
136 .out = & ((const u16) { straightforward_fletcher16_checksum("\001\002") }),
137 },
138 {
139 .in = "",
140 .out = & ((const u16) { straightforward_fletcher16_checksum("") }),
141 },
142 {
143 .in = "a",
144 .out = & ((const u16) { straightforward_fletcher16_checksum("a") }),
145 },
146 {
147 .in = "abcd",
148 .out = & ((const u16) { straightforward_fletcher16_checksum("abcd") }),
149 },
150 {
151 .in = "message digest",
152 .out = & ((const u16) { straightforward_fletcher16_checksum("message digest") }),
153 },
154 {
155 .in = "abcdefghijklmnopqrstuvwxyz",
156 .out = & ((const u16) { straightforward_fletcher16_checksum("abcdefghijklmnopqrstuvwxyz") }),
157 },
158 {
159 .in = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
160 .out = & ((const u16) { straightforward_fletcher16_checksum("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") }),
161 },
162 {
163 .in = "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
164 .out = & ((const u16) { straightforward_fletcher16_checksum("12345678901234567890123456789012345678901234567890123456789012345678901234567890") }),
165 },
166 };
167
168 return bt_assert_batch(test_vectors, test_fletcher16_checksum, bt_fmt_str, bt_fmt_unsigned);
169 }
170
171 int
172 main(int argc, char *argv[])
173 {
174 bt_init(argc, argv);
175
176 bt_test_suite(t_fletcher16_compute, "Fletcher-16 Compute Tests");
177 bt_test_suite(t_fletcher16_checksum, "Fletcher-16 Checksum Tests");
178
179 return bt_exit_value();
180 }