2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
26 #include "got_error.h"
27 #include "got_opentemp.h"
29 #include "got_lib_deltify.h"
32 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 const struct got_error *err = NULL;
40 FILE *base_file, *derived_file, *result_file;
41 struct got_delta_table *dt;
42 struct got_delta_instruction *deltas;
46 base_file = got_opentemp();
47 if (base_file == NULL)
50 derived_file = got_opentemp();
51 if (derived_file == NULL)
54 result_file = got_opentemp();
55 if (result_file == NULL)
58 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
59 fputc('a', base_file);
60 fputc('a', derived_file);
62 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 fputc('b', base_file);
64 fputc('x', derived_file);
66 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 fputc('c', base_file);
68 fputc('c', derived_file);
74 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
78 for (i = 0; i < dt->nalloc; i++) {
79 if (dt->blocks[i].len > 0)
82 if (have_nblocks != dt->nblocks) {
83 err = got_error(GOT_ERR_BAD_DELTA);
87 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
88 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 3 * GOT_DELTIFY_MAXCHUNK);
93 err = got_error(GOT_ERR_BAD_DELTA);
96 /* Copy 'aaaa...' from base file. */
97 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
98 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
99 err = got_error(GOT_ERR_BAD_DELTA);
102 /* Copy 'xxxx...' from derived file. */
103 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
104 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
105 err = got_error(GOT_ERR_BAD_DELTA);
108 /* Copy 'ccccc...' from base file. */
109 if (!(deltas[2].copy == 1 &&
110 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
111 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
112 err = got_error(GOT_ERR_BAD_DELTA);
117 got_deltify_free(dt);
119 fclose(derived_file);
121 return (err == NULL);
126 #define RUN_TEST(expr, name) \
127 { test_ok = (expr); \
128 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
129 failure = (failure || !test_ok); }
134 fprintf(stderr, "usage: delta_test [-q]\n");
138 main(int argc, char *argv[])
144 while ((ch = getopt(argc, argv, "q")) != -1) {
164 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
167 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
170 if (unveil(NULL, NULL) != 0)
173 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
175 return failure ? 1 : 0;