Blob


1 /*
2 * Copyright (c) 2018, 2019 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>
24 #include <getopt.h>
26 #include "got_error.h"
27 #include "got_opentemp.h"
28 #include "got_path.h"
30 #include "got_lib_delta.h"
32 #ifndef nitems
33 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 #endif
36 const struct delta_test {
37 const char *base;
38 size_t base_len;
39 const char *delta;
40 size_t delta_len;
41 const char *expected;
42 size_t result_len;
43 } delta_tests[] = {
44 /* base len 0, target len 4, append 4 'x' */
45 { "", 0, "\x00\x04\x04xxxx", 7, "xxxx", 4 },
46 /* copy 4 bytes at offset 0 from base, append 4 'x' */
47 { "aabbccdd", 8, "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx", 8 },
48 /* copy 4 bytes at offset 4 from base, append 4 'x' */
49 { "aabbccdd", 8, "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx", 8 },
50 /* git 48fb7deb5 Fix big left-shifts of unsigned char, 2009-06-17) */
51 { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
52 16, "\x10\x10\xff\xff\xff\xff\xff\x10\00\00", 10 , NULL, 0 },
53 /* libgit2 9844d38be delta: fix out-of-bounds read of delta */
54 { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
55 16, "\x10\x70\xff", 3, NULL, 0}
56 };
58 static int
59 delta_apply(void)
60 {
61 const struct got_error *err = NULL;
62 size_t i;
63 FILE *result_file;
65 result_file = got_opentemp();
66 if (result_file == NULL)
67 return 1;
69 for (i = 0; i < nitems(delta_tests); i++) {
70 const struct delta_test *dt = &delta_tests[i];
71 FILE *base_file;
72 char buf[1024];
73 size_t n, result_len;
75 base_file = got_opentemp();
76 if (base_file == NULL) {
77 err = got_error_from_errno("got_opentemp");
78 break;
79 }
81 n = fwrite(dt->base, 1, dt->base_len, base_file);
82 if (n != dt->base_len) {
83 err = got_ferror(base_file, GOT_ERR_IO);
84 break;
85 }
86 rewind(base_file);
88 err = got_delta_apply(base_file, dt->delta, dt->delta_len,
89 result_file, &result_len);
90 if (fclose(base_file) == EOF && err == NULL)
91 err = got_error_from_errno("fclose");
92 if (dt->expected == NULL) {
93 /* Invalid delta, expect an error. */
94 if (err == NULL)
95 err = got_error(GOT_ERR_EXPECTED);
96 else if (err->code == GOT_ERR_BAD_DELTA)
97 err = NULL;
98 } else {
99 if (err)
100 break;
101 if (result_len != dt->result_len) {
102 err = got_ferror(result_file,
103 GOT_ERR_BAD_DELTA);
104 break;
106 n = fread(buf, result_len, 1, result_file);
107 if (n != 1 ||
108 strncmp(buf, dt->expected, result_len) != 0) {
109 err = got_ferror(result_file,
110 GOT_ERR_BAD_DELTA);
111 break;
114 rewind(result_file);
117 fclose(result_file);
118 return (err == NULL);
121 static int quiet;
123 #define RUN_TEST(expr, name) \
124 { test_ok = (expr); \
125 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
126 failure = (failure || !test_ok); }
128 static void
129 usage(void)
131 fprintf(stderr, "usage: delta_test [-q]\n");
134 int
135 main(int argc, char *argv[])
137 int test_ok;
138 int failure = 0;
139 int ch;
141 while ((ch = getopt(argc, argv, "q")) != -1) {
142 switch (ch) {
143 case 'q':
144 quiet = 1;
145 break;
146 default:
147 usage();
148 return 1;
152 argc -= optind;
153 argv += optind;
155 if (argc != 0) {
156 usage();
157 return 1;
160 #ifndef PROFILE
161 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
162 err(1, "pledge");
163 #endif
164 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
165 err(1, "unveil");
167 if (unveil(NULL, NULL) != 0)
168 err(1, "unveil");
170 RUN_TEST(delta_apply(), "delta_apply");
172 return failure ? 1 : 0;