Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <err.h>
23 #include <unistd.h>
25 #include "got_error.h"
27 #include "got_delta_lib.h"
28 #include "got_path_lib.h"
30 #ifndef nitems
31 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
32 #endif
34 struct delta_test {
35 const char *base;
36 const char *delta;
37 size_t delta_len;
38 const char *expected;
39 } delta_tests[] = {
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" },
46 };
48 static int
49 delta_apply()
50 {
51 const struct got_error *err = NULL;
52 int i;
53 FILE *result_file;
55 result_file = got_opentemp();
56 if (result_file == NULL)
57 return 1;
59 for (i = 0; i < nitems(delta_tests); i++) {
60 struct delta_test *dt = &delta_tests[i];
61 FILE *base_file;
62 char buf[1024];
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();
69 break;
70 }
72 n = fwrite(dt->base, 1, len, base_file);
73 if (n != len) {
74 err = got_ferror(base_file, GOT_ERR_IO);
75 break;
76 }
77 rewind(base_file);
79 err = got_delta_apply(base_file, dt->delta, dt->delta_len,
80 result_file);
81 fclose(base_file);
82 if (err)
83 break;
84 result_len = strlen(dt->expected);
85 n = fread(buf, result_len, 1, result_file);
86 if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
87 err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
88 break;
89 }
90 rewind(result_file);
91 }
93 fclose(result_file);
94 return (err == NULL);
95 }
97 #define RUN_TEST(expr, name) \
98 { test_ok = (expr); \
99 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
100 failure = (failure || !test_ok); }
102 int
103 main(int argc, const char *argv[])
105 int test_ok;
106 int failure = 0;
108 if (argc != 1) {
109 fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
110 return 1;
113 if (pledge("stdio rpath wpath cpath", NULL) == -1)
114 err(1, "pledge");
116 RUN_TEST(delta_apply(), "delta_apply");
118 return failure ? 1 : 0;