Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <err.h>
24 #include <unistd.h>
25 #include <getopt.h>
27 #include "got_error.h"
28 #include "got_opentemp.h"
30 #include "got_lib_deltify.h"
32 #ifndef nitems
33 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 #endif
36 static int
37 deltify_abc_axc(void)
38 {
39 const struct got_error *err = NULL;
40 size_t i;
41 FILE *base_file, *derived_file, *result_file;
42 struct got_delta_table *dt;
43 struct got_delta_instruction *deltas;
44 int ndeltas;
45 int have_nblocks = 0;
46 uint32_t seed;
48 seed = arc4random();
50 base_file = got_opentemp();
51 if (base_file == NULL)
52 return 1;
54 derived_file = got_opentemp();
55 if (derived_file == NULL)
56 return 1;
58 result_file = got_opentemp();
59 if (result_file == NULL)
60 return 1;
62 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 fputc('a', base_file);
64 fputc('a', derived_file);
65 }
66 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 fputc('b', base_file);
68 fputc('x', derived_file);
69 }
70 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
71 fputc('c', base_file);
72 fputc('c', derived_file);
73 }
75 rewind(base_file);
76 rewind(derived_file);
78 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
79 seed);
80 if (err)
81 goto done;
83 for (i = 0; i < dt->nalloc; i++) {
84 if (dt->blocks[i].len > 0)
85 have_nblocks++;
86 }
87 if (have_nblocks != dt->nblocks) {
88 err = got_error(GOT_ERR_BAD_DELTA);
89 goto done;
90 }
92 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
93 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
94 3 * GOT_DELTIFY_MAXCHUNK);
95 if (err)
96 goto done;
98 if (ndeltas != 3) {
99 err = got_error(GOT_ERR_BAD_DELTA);
100 goto done;
102 /* Copy 'aaaa...' from base file. */
103 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
104 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
105 err = got_error(GOT_ERR_BAD_DELTA);
106 goto done;
108 /* Copy 'xxxx...' from derived file. */
109 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
110 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
111 err = got_error(GOT_ERR_BAD_DELTA);
112 goto done;
114 /* Copy 'ccccc...' from base file. */
115 if (!(deltas[2].copy == 1 &&
116 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
117 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
118 err = got_error(GOT_ERR_BAD_DELTA);
119 goto done;
122 done:
123 got_deltify_free(dt);
124 fclose(base_file);
125 fclose(derived_file);
126 fclose(result_file);
127 return (err == NULL);
130 static int
131 deltify_abc_axc_file_mem(void)
133 const struct got_error *err = NULL;
134 size_t i;
135 uint8_t base_data[3 * GOT_DELTIFY_MAXCHUNK];
136 FILE *derived_file, *result_file;
137 struct got_delta_table *dt;
138 struct got_delta_instruction *deltas;
139 int ndeltas;
140 int have_nblocks = 0;
141 uint32_t seed;
143 seed = arc4random();
145 derived_file = got_opentemp();
146 if (derived_file == NULL)
147 return 1;
149 result_file = got_opentemp();
150 if (result_file == NULL)
151 return 1;
153 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
154 base_data[i] = 'a';
155 fputc('a', derived_file);
157 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
158 base_data[GOT_DELTIFY_MAXCHUNK + i] = 'b';
159 fputc('x', derived_file);
161 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
162 base_data[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
163 fputc('c', derived_file);
166 rewind(derived_file);
168 err = got_deltify_init_mem(&dt, base_data, 0, 3 * GOT_DELTIFY_MAXCHUNK,
169 seed);
170 if (err)
171 goto done;
173 for (i = 0; i < dt->nalloc; i++) {
174 if (dt->blocks[i].len > 0)
175 have_nblocks++;
177 if (have_nblocks != dt->nblocks) {
178 err = got_error(GOT_ERR_BAD_DELTA);
179 goto done;
182 err = got_deltify_file_mem(&deltas, &ndeltas, derived_file, 0,
183 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_data, 0,
184 3 * GOT_DELTIFY_MAXCHUNK);
185 if (err)
186 goto done;
188 if (ndeltas != 3) {
189 err = got_error(GOT_ERR_BAD_DELTA);
190 goto done;
192 /* Copy 'aaaa...' from base file. */
193 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
194 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
195 err = got_error(GOT_ERR_BAD_DELTA);
196 goto done;
198 /* Copy 'xxxx...' from derived file. */
199 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
200 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
201 err = got_error(GOT_ERR_BAD_DELTA);
202 goto done;
204 /* Copy 'ccccc...' from base file. */
205 if (!(deltas[2].copy == 1 &&
206 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
207 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
208 err = got_error(GOT_ERR_BAD_DELTA);
209 goto done;
212 done:
213 got_deltify_free(dt);
214 fclose(derived_file);
215 fclose(result_file);
216 return (err == NULL);
219 static int
220 deltify_abc_axc_mem_file(void)
222 const struct got_error *err = NULL;
223 size_t i;
224 FILE *base_file, *result_file;
225 uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
226 struct got_delta_table *dt;
227 struct got_delta_instruction *deltas;
228 int ndeltas;
229 int have_nblocks = 0;
230 uint32_t seed;
232 seed = arc4random();
234 base_file = got_opentemp();
235 if (base_file == NULL)
236 return 1;
238 result_file = got_opentemp();
239 if (result_file == NULL)
240 return 1;
242 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
243 fputc('a', base_file);
244 derived_file[i] = 'a';
246 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
247 fputc('b', base_file);
248 derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
250 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
251 fputc('c', base_file);
252 derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
255 rewind(base_file);
257 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
258 seed);
259 if (err)
260 goto done;
262 for (i = 0; i < dt->nalloc; i++) {
263 if (dt->blocks[i].len > 0)
264 have_nblocks++;
266 if (have_nblocks != dt->nblocks) {
267 err = got_error(GOT_ERR_BAD_DELTA);
268 goto done;
271 err = got_deltify_mem_file(&deltas, &ndeltas, derived_file, 0,
272 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
273 3 * GOT_DELTIFY_MAXCHUNK);
274 if (err)
275 goto done;
277 if (ndeltas != 3) {
278 err = got_error(GOT_ERR_BAD_DELTA);
279 goto done;
281 /* Copy 'aaaa...' from base file. */
282 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
283 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
284 err = got_error(GOT_ERR_BAD_DELTA);
285 goto done;
287 /* Copy 'xxxx...' from derived file. */
288 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
289 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
290 err = got_error(GOT_ERR_BAD_DELTA);
291 goto done;
293 /* Copy 'ccccc...' from base file. */
294 if (!(deltas[2].copy == 1 &&
295 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
296 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
297 err = got_error(GOT_ERR_BAD_DELTA);
298 goto done;
301 done:
302 got_deltify_free(dt);
303 fclose(base_file);
304 fclose(result_file);
305 return (err == NULL);
308 static int
309 deltify_abc_axc_mem_mem(void)
311 const struct got_error *err = NULL;
312 size_t i;
313 FILE *result_file;
314 uint8_t base_file[3 * GOT_DELTIFY_MAXCHUNK];
315 uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
316 struct got_delta_table *dt;
317 struct got_delta_instruction *deltas;
318 int ndeltas;
319 int have_nblocks = 0;
320 uint32_t seed;
322 seed = arc4random();
324 result_file = got_opentemp();
325 if (result_file == NULL)
326 return 1;
328 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
329 base_file[i] = 'a';
330 derived_file[i] = 'a';
332 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
333 base_file[GOT_DELTIFY_MAXCHUNK + i] = 'b';
334 derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
336 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
337 base_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
338 derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
341 err = got_deltify_init_mem(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
342 seed);
343 if (err)
344 goto done;
346 for (i = 0; i < dt->nalloc; i++) {
347 if (dt->blocks[i].len > 0)
348 have_nblocks++;
350 if (have_nblocks != dt->nblocks) {
351 err = got_error(GOT_ERR_BAD_DELTA);
352 goto done;
355 err = got_deltify_mem_mem(&deltas, &ndeltas, derived_file, 0,
356 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
357 3 * GOT_DELTIFY_MAXCHUNK);
358 if (err)
359 goto done;
361 if (ndeltas != 3) {
362 err = got_error(GOT_ERR_BAD_DELTA);
363 goto done;
365 /* Copy 'aaaa...' from base file. */
366 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
367 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
368 err = got_error(GOT_ERR_BAD_DELTA);
369 goto done;
371 /* Copy 'xxxx...' from derived file. */
372 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
373 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
374 err = got_error(GOT_ERR_BAD_DELTA);
375 goto done;
377 /* Copy 'ccccc...' from base file. */
378 if (!(deltas[2].copy == 1 &&
379 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
380 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
381 err = got_error(GOT_ERR_BAD_DELTA);
382 goto done;
385 done:
386 got_deltify_free(dt);
387 fclose(result_file);
388 return (err == NULL);
391 static int quiet;
393 #define RUN_TEST(expr, name) \
394 { test_ok = (expr); \
395 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
396 failure = (failure || !test_ok); }
398 static void
399 usage(void)
401 fprintf(stderr, "usage: delta_test [-q]\n");
404 int
405 main(int argc, char *argv[])
407 int test_ok;
408 int failure = 0;
409 int ch;
411 while ((ch = getopt(argc, argv, "q")) != -1) {
412 switch (ch) {
413 case 'q':
414 quiet = 1;
415 break;
416 default:
417 usage();
418 return 1;
422 argc -= optind;
423 argv += optind;
425 if (argc != 0) {
426 usage();
427 return 1;
430 #ifndef PROFILE
431 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
432 err(1, "pledge");
433 #endif
434 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
435 err(1, "unveil");
437 if (unveil(NULL, NULL) != 0)
438 err(1, "unveil");
440 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
441 RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
442 RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
443 RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
445 return failure ? 1 : 0;