Line data Source code
1 : // SPDX-License-Identifier: GPL-3.0-or-later
2 : // Copyright (C) 2015 Andrea Mazzoleni
3 :
4 : #include "portable.h"
5 :
6 : #include "stream.h"
7 : #include "support.h"
8 :
9 : volatile int global_interrupt = 0;
10 :
11 : #define STREAM_MAX 8
12 : #define BUFFER_MAX 64
13 : #define STR_MAX 128
14 :
15 16 : void test(void)
16 : {
17 : struct stream* s;
18 : char file[32];
19 : unsigned char buffer[BUFFER_MAX];
20 : char str[STR_MAX];
21 : unsigned i, j;
22 16 : uint32_t u32 = -1L;
23 16 : uint64_t u64 = -1LL;
24 : uint32_t put_crc_stored;
25 : uint32_t put_crc_computed;
26 :
27 16 : crc32c_init();
28 :
29 16 : s = sopen_multi_write(STREAM_MAX, STREAM_FLAGS_CRC);
30 144 : for (i = 0; i < STREAM_MAX; ++i) {
31 128 : snprintf(file, sizeof(file), "stream%u.bin", i);
32 128 : remove(file);
33 128 : if (sopen_multi_file(s, i, file) != 0) {
34 : /* LCOV_EXCL_START */
35 : exit(EXIT_FAILURE);
36 : /* LCOV_EXCL_STOP */
37 : }
38 : }
39 :
40 4112 : for (j = 0; j < 256; ++j) {
41 4096 : if (sputc(j, s) != 0) {
42 : /* LCOV_EXCL_START */
43 : exit(EXIT_FAILURE);
44 : /* LCOV_EXCL_STOP */
45 : }
46 : }
47 :
48 528 : for (j = 0; j < 32; ++j) {
49 512 : if (sputb32(u32 >> j, s) != 0) {
50 : /* LCOV_EXCL_START */
51 : exit(EXIT_FAILURE);
52 : /* LCOV_EXCL_STOP */
53 : }
54 : }
55 :
56 1040 : for (j = 0; j < 64; ++j) {
57 1024 : if (sputb64(u64 >> j, s) != 0) {
58 : /* LCOV_EXCL_START */
59 : exit(EXIT_FAILURE);
60 : /* LCOV_EXCL_STOP */
61 : }
62 : }
63 :
64 1040 : for (j = 0; j < BUFFER_MAX; ++j) {
65 1024 : memset(buffer, j, j);
66 1024 : if (swrite(buffer, j, s) != 0) {
67 : /* LCOV_EXCL_START */
68 : exit(EXIT_FAILURE);
69 : /* LCOV_EXCL_STOP */
70 : }
71 : }
72 :
73 2048 : for (j = 1; j < STR_MAX; ++j) {
74 2032 : memset(str, ' ' + j, j - 1);
75 2032 : str[j - 1] = 0;
76 2032 : if (sputbs(str, s) != 0) {
77 : /* LCOV_EXCL_START */
78 : exit(EXIT_FAILURE);
79 : /* LCOV_EXCL_STOP */
80 : }
81 : }
82 :
83 16 : put_crc_stored = scrc(s);
84 16 : put_crc_computed = scrc_stream(s);
85 :
86 16 : if (put_crc_stored != put_crc_computed) {
87 : /* LCOV_EXCL_START */
88 : exit(EXIT_FAILURE);
89 : /* LCOV_EXCL_STOP */
90 : }
91 :
92 16 : if (sputble32(put_crc_stored, s) != 0) {
93 : /* LCOV_EXCL_START */
94 : exit(EXIT_FAILURE);
95 : /* LCOV_EXCL_STOP */
96 : }
97 :
98 16 : if (sclose(s) != 0) {
99 : /* LCOV_EXCL_START */
100 : exit(EXIT_FAILURE);
101 : /* LCOV_EXCL_STOP */
102 : }
103 :
104 144 : for (i = 0; i < STREAM_MAX; ++i) {
105 : uint32_t get_crc_stored;
106 : uint32_t get_crc_computed;
107 128 : snprintf(file, sizeof(file), "stream%u.bin", i);
108 :
109 128 : s = sopen_read(file, STREAM_FLAGS_CRC);
110 128 : if (s == 0) {
111 : /* LCOV_EXCL_START */
112 : exit(EXIT_FAILURE);
113 : /* LCOV_EXCL_STOP */
114 : }
115 :
116 32896 : for (j = 0; j < 256; ++j) {
117 32768 : int c = sgetc(s);
118 32768 : if (c == EOF || (unsigned char)c != j) {
119 : /* LCOV_EXCL_START */
120 : exit(EXIT_FAILURE);
121 : /* LCOV_EXCL_STOP */
122 : }
123 : }
124 :
125 4224 : for (j = 0; j < 32; ++j) {
126 : uint32_t v32;
127 4096 : if (sgetb32(s, &v32) != 0 || v32 != (u32 >> j)) {
128 : /* LCOV_EXCL_START */
129 : exit(EXIT_FAILURE);
130 : /* LCOV_EXCL_STOP */
131 : }
132 : }
133 :
134 8320 : for (j = 0; j < 64; ++j) {
135 : uint64_t v64;
136 8192 : if (sgetb64(s, &v64) != 0 || v64 != (u64 >> j)) {
137 : /* LCOV_EXCL_START */
138 : exit(EXIT_FAILURE);
139 : /* LCOV_EXCL_STOP */
140 : }
141 : }
142 :
143 8192 : for (j = 1; j < BUFFER_MAX; ++j) {
144 : char copy[BUFFER_MAX];
145 8064 : memset(buffer, j, j);
146 8064 : if (sread(s, copy, j) != 0 || memcmp(copy, buffer, j) != 0) {
147 : /* LCOV_EXCL_START */
148 : exit(EXIT_FAILURE);
149 : /* LCOV_EXCL_STOP */
150 : }
151 : }
152 :
153 16384 : for (j = 1; j < STR_MAX; ++j) {
154 : char copy[STR_MAX];
155 16256 : memset(str, ' ' + j, j - 1);
156 16256 : str[j - 1] = 0;
157 16256 : if (sgetbs(s, copy, sizeof(copy)) != 0 || strcmp(copy, str) != 0) {
158 : /* LCOV_EXCL_START */
159 : exit(EXIT_FAILURE);
160 : /* LCOV_EXCL_STOP */
161 : }
162 : }
163 :
164 : /* get the computed CRC *before* reading the stored one */
165 128 : get_crc_computed = scrc(s);
166 :
167 128 : if (sgetble32(s, &get_crc_stored) != 0) {
168 : /* LCOV_EXCL_START */
169 : exit(EXIT_FAILURE);
170 : /* LCOV_EXCL_STOP */
171 : }
172 :
173 128 : if (get_crc_stored != put_crc_stored) {
174 : /* LCOV_EXCL_START */
175 : exit(EXIT_FAILURE);
176 : /* LCOV_EXCL_STOP */
177 : }
178 :
179 128 : if (get_crc_stored != get_crc_computed) {
180 : /* LCOV_EXCL_START */
181 : exit(EXIT_FAILURE);
182 : /* LCOV_EXCL_STOP */
183 : }
184 :
185 128 : if (sclose(s) != 0) {
186 : /* LCOV_EXCL_START */
187 : exit(EXIT_FAILURE);
188 : /* LCOV_EXCL_STOP */
189 : }
190 : }
191 :
192 144 : for (i = 0; i < STREAM_MAX; ++i) {
193 : uint32_t get_crc_stored;
194 : uint32_t get_crc_computed;
195 : unsigned char buf[4];
196 128 : snprintf(file, sizeof(file), "stream%u.bin", i);
197 :
198 128 : s = sopen_read(file, STREAM_FLAGS_CRC);
199 128 : if (s == 0) {
200 : /* LCOV_EXCL_START */
201 : exit(EXIT_FAILURE);
202 : /* LCOV_EXCL_STOP */
203 : }
204 :
205 128 : if (sdeplete(s, buf) != 0) {
206 : /* LCOV_EXCL_START */
207 : exit(EXIT_FAILURE);
208 : /* LCOV_EXCL_STOP */
209 : }
210 :
211 : /* get the stored crc from the last four bytes */
212 128 : get_crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
213 :
214 128 : if (get_crc_stored != put_crc_stored) {
215 : /* LCOV_EXCL_START */
216 : exit(EXIT_FAILURE);
217 : /* LCOV_EXCL_STOP */
218 : }
219 :
220 : /* get the computed CRC *after* reading the stored one */
221 128 : get_crc_computed = scrc(s);
222 :
223 : /* adjust the stored crc to include itself */
224 128 : get_crc_stored = crc32c(get_crc_stored, buf, 4);
225 :
226 128 : if (get_crc_stored != get_crc_computed) {
227 : /* LCOV_EXCL_START */
228 : exit(EXIT_FAILURE);
229 : /* LCOV_EXCL_STOP */
230 : }
231 :
232 128 : if (sclose(s) != 0) {
233 : /* LCOV_EXCL_START */
234 : exit(EXIT_FAILURE);
235 : /* LCOV_EXCL_STOP */
236 : }
237 : }
238 16 : }
239 :
240 1 : int main(void)
241 : {
242 : unsigned i;
243 :
244 1 : lock_init();
245 :
246 17 : for (i = 1; i <= 16; ++i) {
247 :
248 : /* test with different stream buffer size */
249 16 : STREAM_SIZE = i;
250 :
251 16 : printf("Test stream buffer size %u\n", i);
252 :
253 16 : test();
254 : }
255 :
256 1 : return 0;
257 : }
258 :
|