2 8704c7ce 2021-03-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 8704c7ce 2021-03-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 8704c7ce 2021-03-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 8704c7ce 2021-03-10 stsp * copyright notice and this permission notice appear in all copies.
8 8704c7ce 2021-03-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8704c7ce 2021-03-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8704c7ce 2021-03-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8704c7ce 2021-03-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8704c7ce 2021-03-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8704c7ce 2021-03-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8704c7ce 2021-03-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8704c7ce 2021-03-10 stsp #include <sys/queue.h>
19 8704c7ce 2021-03-10 stsp #include <stdio.h>
20 8704c7ce 2021-03-10 stsp #include <stdlib.h>
21 8704c7ce 2021-03-10 stsp #include <string.h>
22 8704c7ce 2021-03-10 stsp #include <err.h>
23 8704c7ce 2021-03-10 stsp #include <unistd.h>
24 8704c7ce 2021-03-10 stsp #include <getopt.h>
26 8704c7ce 2021-03-10 stsp #include "got_error.h"
27 8704c7ce 2021-03-10 stsp #include "got_opentemp.h"
29 8704c7ce 2021-03-10 stsp #include "got_lib_deltify.h"
31 8704c7ce 2021-03-10 stsp #ifndef nitems
32 8704c7ce 2021-03-10 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 8704c7ce 2021-03-10 stsp deltify_abc_axc(void)
38 8704c7ce 2021-03-10 stsp const struct got_error *err = NULL;
40 8704c7ce 2021-03-10 stsp FILE *base_file, *derived_file, *result_file;
41 8704c7ce 2021-03-10 stsp struct got_delta_table *dt;
42 8704c7ce 2021-03-10 stsp struct got_delta_instruction *deltas;
43 8704c7ce 2021-03-10 stsp int ndeltas;
44 8704c7ce 2021-03-10 stsp int have_nblocks = 0;
46 8704c7ce 2021-03-10 stsp base_file = got_opentemp();
47 8704c7ce 2021-03-10 stsp if (base_file == NULL)
50 8704c7ce 2021-03-10 stsp derived_file = got_opentemp();
51 8704c7ce 2021-03-10 stsp if (derived_file == NULL)
54 8704c7ce 2021-03-10 stsp result_file = got_opentemp();
55 8704c7ce 2021-03-10 stsp if (result_file == NULL)
58 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
59 8704c7ce 2021-03-10 stsp fputc('a', base_file);
60 8704c7ce 2021-03-10 stsp fputc('a', derived_file);
62 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 8704c7ce 2021-03-10 stsp fputc('b', base_file);
64 8704c7ce 2021-03-10 stsp fputc('x', derived_file);
66 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 8704c7ce 2021-03-10 stsp fputc('c', base_file);
68 8704c7ce 2021-03-10 stsp fputc('c', derived_file);
71 8704c7ce 2021-03-10 stsp rewind(base_file);
72 8704c7ce 2021-03-10 stsp rewind(derived_file);
74 8704c7ce 2021-03-10 stsp err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
78 8704c7ce 2021-03-10 stsp for (i = 0; i < dt->nalloc; i++) {
79 8704c7ce 2021-03-10 stsp if (dt->blocks[i].len > 0)
80 8704c7ce 2021-03-10 stsp have_nblocks++;
82 8704c7ce 2021-03-10 stsp if (have_nblocks != dt->nblocks) {
83 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
87 8704c7ce 2021-03-10 stsp err = got_deltify(&deltas, &ndeltas, derived_file, 0,
88 f34b169e 2021-06-18 stsp 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
89 f34b169e 2021-06-18 stsp 3 * GOT_DELTIFY_MAXCHUNK);
93 8704c7ce 2021-03-10 stsp if (ndeltas != 3) {
94 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
97 8704c7ce 2021-03-10 stsp /* Copy 'aaaa...' from base file. */
98 8704c7ce 2021-03-10 stsp if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
99 8704c7ce 2021-03-10 stsp deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
100 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
103 8704c7ce 2021-03-10 stsp /* Copy 'xxxx...' from derived file. */
104 8704c7ce 2021-03-10 stsp if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
105 8704c7ce 2021-03-10 stsp deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
106 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
109 8704c7ce 2021-03-10 stsp /* Copy 'ccccc...' from base file. */
110 8704c7ce 2021-03-10 stsp if (!(deltas[2].copy == 1 &&
111 8704c7ce 2021-03-10 stsp deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
112 8704c7ce 2021-03-10 stsp deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
113 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
118 8704c7ce 2021-03-10 stsp got_deltify_free(dt);
119 8704c7ce 2021-03-10 stsp fclose(base_file);
120 8704c7ce 2021-03-10 stsp fclose(derived_file);
121 64a8571e 2022-01-07 stsp fclose(result_file);
122 64a8571e 2022-01-07 stsp return (err == NULL);
126 64a8571e 2022-01-07 stsp deltify_abc_axc_file_mem(void)
128 64a8571e 2022-01-07 stsp const struct got_error *err = NULL;
130 64a8571e 2022-01-07 stsp uint8_t base_data[3 * GOT_DELTIFY_MAXCHUNK];
131 64a8571e 2022-01-07 stsp FILE *derived_file, *result_file;
132 64a8571e 2022-01-07 stsp struct got_delta_table *dt;
133 64a8571e 2022-01-07 stsp struct got_delta_instruction *deltas;
134 64a8571e 2022-01-07 stsp int ndeltas;
135 64a8571e 2022-01-07 stsp int have_nblocks = 0;
137 64a8571e 2022-01-07 stsp derived_file = got_opentemp();
138 64a8571e 2022-01-07 stsp if (derived_file == NULL)
141 64a8571e 2022-01-07 stsp result_file = got_opentemp();
142 64a8571e 2022-01-07 stsp if (result_file == NULL)
145 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
146 64a8571e 2022-01-07 stsp base_data[i] = 'a';
147 64a8571e 2022-01-07 stsp fputc('a', derived_file);
149 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
150 64a8571e 2022-01-07 stsp base_data[GOT_DELTIFY_MAXCHUNK + i] = 'b';
151 64a8571e 2022-01-07 stsp fputc('x', derived_file);
153 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
154 64a8571e 2022-01-07 stsp base_data[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
155 64a8571e 2022-01-07 stsp fputc('c', derived_file);
158 64a8571e 2022-01-07 stsp rewind(derived_file);
160 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&dt, base_data, 0, 3 * GOT_DELTIFY_MAXCHUNK);
164 64a8571e 2022-01-07 stsp for (i = 0; i < dt->nalloc; i++) {
165 64a8571e 2022-01-07 stsp if (dt->blocks[i].len > 0)
166 64a8571e 2022-01-07 stsp have_nblocks++;
168 64a8571e 2022-01-07 stsp if (have_nblocks != dt->nblocks) {
169 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
173 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas, derived_file, 0,
174 64a8571e 2022-01-07 stsp 3 * GOT_DELTIFY_MAXCHUNK, dt, base_data, 0,
175 64a8571e 2022-01-07 stsp 3 * GOT_DELTIFY_MAXCHUNK);
179 64a8571e 2022-01-07 stsp if (ndeltas != 3) {
180 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
183 64a8571e 2022-01-07 stsp /* Copy 'aaaa...' from base file. */
184 64a8571e 2022-01-07 stsp if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
185 64a8571e 2022-01-07 stsp deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
186 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
189 64a8571e 2022-01-07 stsp /* Copy 'xxxx...' from derived file. */
190 64a8571e 2022-01-07 stsp if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
191 64a8571e 2022-01-07 stsp deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
192 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
195 64a8571e 2022-01-07 stsp /* Copy 'ccccc...' from base file. */
196 64a8571e 2022-01-07 stsp if (!(deltas[2].copy == 1 &&
197 64a8571e 2022-01-07 stsp deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
198 64a8571e 2022-01-07 stsp deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
199 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
204 64a8571e 2022-01-07 stsp got_deltify_free(dt);
205 64a8571e 2022-01-07 stsp fclose(derived_file);
206 64a8571e 2022-01-07 stsp fclose(result_file);
207 64a8571e 2022-01-07 stsp return (err == NULL);
211 64a8571e 2022-01-07 stsp deltify_abc_axc_mem_file(void)
213 64a8571e 2022-01-07 stsp const struct got_error *err = NULL;
215 64a8571e 2022-01-07 stsp FILE *base_file, *result_file;
216 64a8571e 2022-01-07 stsp uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
217 64a8571e 2022-01-07 stsp struct got_delta_table *dt;
218 64a8571e 2022-01-07 stsp struct got_delta_instruction *deltas;
219 64a8571e 2022-01-07 stsp int ndeltas;
220 64a8571e 2022-01-07 stsp int have_nblocks = 0;
222 64a8571e 2022-01-07 stsp base_file = got_opentemp();
223 64a8571e 2022-01-07 stsp if (base_file == NULL)
226 64a8571e 2022-01-07 stsp result_file = got_opentemp();
227 64a8571e 2022-01-07 stsp if (result_file == NULL)
230 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
231 64a8571e 2022-01-07 stsp fputc('a', base_file);
232 64a8571e 2022-01-07 stsp derived_file[i] = 'a';
234 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
235 64a8571e 2022-01-07 stsp fputc('b', base_file);
236 64a8571e 2022-01-07 stsp derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
238 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
239 64a8571e 2022-01-07 stsp fputc('c', base_file);
240 64a8571e 2022-01-07 stsp derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
243 64a8571e 2022-01-07 stsp rewind(base_file);
245 64a8571e 2022-01-07 stsp err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
249 64a8571e 2022-01-07 stsp for (i = 0; i < dt->nalloc; i++) {
250 64a8571e 2022-01-07 stsp if (dt->blocks[i].len > 0)
251 64a8571e 2022-01-07 stsp have_nblocks++;
253 64a8571e 2022-01-07 stsp if (have_nblocks != dt->nblocks) {
254 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
258 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas, derived_file, 0,
259 64a8571e 2022-01-07 stsp 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
260 64a8571e 2022-01-07 stsp 3 * GOT_DELTIFY_MAXCHUNK);
264 64a8571e 2022-01-07 stsp if (ndeltas != 3) {
265 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
268 64a8571e 2022-01-07 stsp /* Copy 'aaaa...' from base file. */
269 64a8571e 2022-01-07 stsp if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
270 64a8571e 2022-01-07 stsp deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
271 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
274 64a8571e 2022-01-07 stsp /* Copy 'xxxx...' from derived file. */
275 64a8571e 2022-01-07 stsp if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
276 64a8571e 2022-01-07 stsp deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
277 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
280 64a8571e 2022-01-07 stsp /* Copy 'ccccc...' from base file. */
281 64a8571e 2022-01-07 stsp if (!(deltas[2].copy == 1 &&
282 64a8571e 2022-01-07 stsp deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
283 64a8571e 2022-01-07 stsp deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
284 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
289 64a8571e 2022-01-07 stsp got_deltify_free(dt);
290 64a8571e 2022-01-07 stsp fclose(base_file);
291 8704c7ce 2021-03-10 stsp fclose(result_file);
292 8704c7ce 2021-03-10 stsp return (err == NULL);
296 64a8571e 2022-01-07 stsp deltify_abc_axc_mem_mem(void)
298 64a8571e 2022-01-07 stsp const struct got_error *err = NULL;
300 64a8571e 2022-01-07 stsp FILE *result_file;
301 64a8571e 2022-01-07 stsp uint8_t base_file[3 * GOT_DELTIFY_MAXCHUNK];
302 64a8571e 2022-01-07 stsp uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
303 64a8571e 2022-01-07 stsp struct got_delta_table *dt;
304 64a8571e 2022-01-07 stsp struct got_delta_instruction *deltas;
305 64a8571e 2022-01-07 stsp int ndeltas;
306 64a8571e 2022-01-07 stsp int have_nblocks = 0;
308 64a8571e 2022-01-07 stsp result_file = got_opentemp();
309 64a8571e 2022-01-07 stsp if (result_file == NULL)
312 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
313 64a8571e 2022-01-07 stsp base_file[i] = 'a';
314 64a8571e 2022-01-07 stsp derived_file[i] = 'a';
316 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
317 64a8571e 2022-01-07 stsp base_file[GOT_DELTIFY_MAXCHUNK + i] = 'b';
318 64a8571e 2022-01-07 stsp derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
320 64a8571e 2022-01-07 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
321 64a8571e 2022-01-07 stsp base_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
322 64a8571e 2022-01-07 stsp derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
325 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
329 64a8571e 2022-01-07 stsp for (i = 0; i < dt->nalloc; i++) {
330 64a8571e 2022-01-07 stsp if (dt->blocks[i].len > 0)
331 64a8571e 2022-01-07 stsp have_nblocks++;
333 64a8571e 2022-01-07 stsp if (have_nblocks != dt->nblocks) {
334 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
338 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas, derived_file, 0,
339 64a8571e 2022-01-07 stsp 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
340 64a8571e 2022-01-07 stsp 3 * GOT_DELTIFY_MAXCHUNK);
344 64a8571e 2022-01-07 stsp if (ndeltas != 3) {
345 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
348 64a8571e 2022-01-07 stsp /* Copy 'aaaa...' from base file. */
349 64a8571e 2022-01-07 stsp if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
350 64a8571e 2022-01-07 stsp deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
351 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
354 64a8571e 2022-01-07 stsp /* Copy 'xxxx...' from derived file. */
355 64a8571e 2022-01-07 stsp if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
356 64a8571e 2022-01-07 stsp deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
357 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
360 64a8571e 2022-01-07 stsp /* Copy 'ccccc...' from base file. */
361 64a8571e 2022-01-07 stsp if (!(deltas[2].copy == 1 &&
362 64a8571e 2022-01-07 stsp deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
363 64a8571e 2022-01-07 stsp deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
364 64a8571e 2022-01-07 stsp err = got_error(GOT_ERR_BAD_DELTA);
369 64a8571e 2022-01-07 stsp got_deltify_free(dt);
370 64a8571e 2022-01-07 stsp fclose(result_file);
371 64a8571e 2022-01-07 stsp return (err == NULL);
374 8704c7ce 2021-03-10 stsp static int quiet;
376 8704c7ce 2021-03-10 stsp #define RUN_TEST(expr, name) \
377 8704c7ce 2021-03-10 stsp { test_ok = (expr); \
378 8704c7ce 2021-03-10 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
379 8704c7ce 2021-03-10 stsp failure = (failure || !test_ok); }
381 8704c7ce 2021-03-10 stsp static void
382 8704c7ce 2021-03-10 stsp usage(void)
384 8704c7ce 2021-03-10 stsp fprintf(stderr, "usage: delta_test [-q]\n");
388 8704c7ce 2021-03-10 stsp main(int argc, char *argv[])
390 8704c7ce 2021-03-10 stsp int test_ok;
391 8704c7ce 2021-03-10 stsp int failure = 0;
394 8704c7ce 2021-03-10 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
395 8704c7ce 2021-03-10 stsp switch (ch) {
405 8704c7ce 2021-03-10 stsp argc -= optind;
406 8704c7ce 2021-03-10 stsp argv += optind;
408 8704c7ce 2021-03-10 stsp if (argc != 0) {
413 8704c7ce 2021-03-10 stsp #ifndef PROFILE
414 8704c7ce 2021-03-10 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
415 8704c7ce 2021-03-10 stsp err(1, "pledge");
417 8704c7ce 2021-03-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
418 8704c7ce 2021-03-10 stsp err(1, "unveil");
420 8704c7ce 2021-03-10 stsp if (unveil(NULL, NULL) != 0)
421 8704c7ce 2021-03-10 stsp err(1, "unveil");
423 8704c7ce 2021-03-10 stsp RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
424 64a8571e 2022-01-07 stsp RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
425 64a8571e 2022-01-07 stsp RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
426 64a8571e 2022-01-07 stsp RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
428 8704c7ce 2021-03-10 stsp return failure ? 1 : 0;