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.
24 #include "got_error.h"
25 #include "got_opentemp.h"
27 #include "got_lib_deltify.h"
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 const struct got_error *err = NULL;
38 FILE *base_file, *derived_file, *result_file;
39 struct got_delta_table *dt;
40 struct got_delta_instruction *deltas;
44 base_file = got_opentemp();
45 if (base_file == NULL)
48 derived_file = got_opentemp();
49 if (derived_file == NULL)
52 result_file = got_opentemp();
53 if (result_file == NULL)
56 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
57 fputc('a', base_file);
58 fputc('a', derived_file);
60 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
61 fputc('b', base_file);
62 fputc('x', derived_file);
64 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
65 fputc('c', base_file);
66 fputc('c', derived_file);
72 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
76 for (i = 0; i < dt->nalloc; i++) {
77 if (dt->blocks[i].len > 0)
80 if (have_nblocks != dt->nblocks) {
81 err = got_error(GOT_ERR_BAD_DELTA);
85 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
86 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
87 3 * GOT_DELTIFY_MAXCHUNK);
92 err = got_error(GOT_ERR_BAD_DELTA);
95 /* Copy 'aaaa...' from base file. */
96 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
97 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
98 err = got_error(GOT_ERR_BAD_DELTA);
101 /* Copy 'xxxx...' from derived file. */
102 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
103 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
104 err = got_error(GOT_ERR_BAD_DELTA);
107 /* Copy 'ccccc...' from base file. */
108 if (!(deltas[2].copy == 1 &&
109 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
110 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
111 err = got_error(GOT_ERR_BAD_DELTA);
116 got_deltify_free(dt);
118 fclose(derived_file);
120 return (err == NULL);
125 #define RUN_TEST(expr, name) \
126 { test_ok = (expr); \
127 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
128 failure = (failure || !test_ok); }
133 fprintf(stderr, "usage: delta_test [-q]\n");
137 main(int argc, char *argv[])
143 while ((ch = getopt(argc, argv, "q")) != -1) {
163 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
166 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
169 if (unveil(NULL, NULL) != 0)
172 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
174 return failure ? 1 : 0;