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>
24 #include "got_error.h"
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 /* base len 0, target len 4, append 4 'x' */
40 { "", "\x00\x04\x04xxxx", 7, "xxxx" },
41 /* copy 4 bytes at offset 0 from base, append 4 'x' */
42 { "aabbccdd", "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx" },
43 /* copy 4 bytes at offset 4 from base, append 4 'x' */
44 { "aabbccdd", "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx" },
47 static const struct got_error *
48 compress_to_file(FILE **outfile, const char *input, size_t inlen)
50 const struct got_error *err = NULL;
57 memset(&z, 0, sizeof(z));
61 if (deflateInit(&z, 8) != Z_OK)
62 return got_error(GOT_ERR_IO);
64 *outfile = got_opentemp();
66 return got_error_from_errno();
68 z.next_in = (Bytef *)input;
71 z.avail_out = sizeof(buf);
72 /* Our output buffer is large enough so one round should be enough. */
73 ret = deflate(&z, Z_FINISH);
74 if (ret != Z_STREAM_END || z.avail_out == 0) {
75 err = got_error(GOT_ERR_COMPRESSION);
81 n = fwrite(buf, 1, z.avail_out, *outfile);
83 err = got_ferror(*outfile, GOT_ERR_IO);
96 const struct got_error *err = NULL;
100 result_file = got_opentemp();
101 if (result_file == NULL)
104 for (i = 0; i < nitems(delta_tests); i++) {
105 struct delta_test *dt = &delta_tests[i];
108 size_t n, len, result_len;
110 len = strlen(dt->base);
111 err = compress_to_file(&base_file, dt->base, len);
115 err = got_delta_apply(base_file, dt->delta, dt->delta_len,
120 result_len = strlen(dt->expected);
121 n = fread(buf, result_len, 1, result_file);
122 if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
123 err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
130 return (err == NULL);
133 #define RUN_TEST(expr, name) \
134 { test_ok = (expr); \
135 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
136 failure = (failure || !test_ok); }
139 main(int argc, const char *argv[])
145 fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
149 RUN_TEST(delta_combine(), "delta_combine");
151 return failure ? 1 : 0;