2 * Copyright (c) 2018 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>
25 #include "got_error.h"
27 #include "got_lib_delta.h"
28 #include "got_lib_path.h"
31 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 /* base len 0, target len 4, append 4 'x' */
41 { "", "\x00\x04\x04xxxx", 7, "xxxx" },
42 /* copy 4 bytes at offset 0 from base, append 4 'x' */
43 { "aabbccdd", "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx" },
44 /* copy 4 bytes at offset 4 from base, append 4 'x' */
45 { "aabbccdd", "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx" },
51 const struct got_error *err = NULL;
55 result_file = got_opentemp();
56 if (result_file == NULL)
59 for (i = 0; i < nitems(delta_tests); i++) {
60 struct delta_test *dt = &delta_tests[i];
63 size_t n, len, result_len;
65 len = strlen(dt->base);
66 base_file = got_opentemp();
67 if (base_file == NULL) {
68 err = got_error_from_errno();
72 n = fwrite(dt->base, 1, len, base_file);
74 err = got_ferror(base_file, GOT_ERR_IO);
79 err = got_delta_apply(base_file, dt->delta, dt->delta_len,
84 result_len = strlen(dt->expected);
85 if (result_len != len) {
86 err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
89 n = fread(buf, result_len, 1, result_file);
90 if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
91 err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
101 #define RUN_TEST(expr, name) \
102 { test_ok = (expr); \
103 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
104 failure = (failure || !test_ok); }
107 main(int argc, const char *argv[])
113 fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
117 if (pledge("stdio rpath wpath cpath", NULL) == -1)
120 RUN_TEST(delta_apply(), "delta_apply");
122 return failure ? 1 : 0;