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 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <err.h>
22 #include <unistd.h>
23 #include <getopt.h>
25 #include "got_error.h"
26 #include "got_opentemp.h"
27 #include "got_path.h"
29 #include "got_lib_delta.h"
31 #ifndef nitems
32 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
33 #endif
35 struct delta_test {
36 const char *base;
37 size_t base_len;
38 const char *delta;
39 size_t delta_len;
40 const char *expected;
41 size_t result_len;
42 } delta_tests[] = {
43 /* base len 0, target len 4, append 4 'x' */
44 { "", 0, "\x00\x04\x04xxxx", 7, "xxxx", 4 },
45 /* copy 4 bytes at offset 0 from base, append 4 'x' */
46 { "aabbccdd", 8, "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx", 8 },
47 /* copy 4 bytes at offset 4 from base, append 4 'x' */
48 { "aabbccdd", 8, "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx", 8 },
49 /* git 48fb7deb5 Fix big left-shifts of unsigned char, 2009-06-17) */
50 { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
51 16, "\x10\x10\xff\xff\xff\xff\xff\x10\00\00", 10 , NULL, 0 },
52 /* libgit2 9844d38be delta: fix out-of-bounds read of delta */
53 { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
54 16, "\x10\x70\xff", 3, NULL, 0}
55 };
57 static int
58 delta_apply(void)
59 {
60 const struct got_error *err = NULL;
61 size_t i;
62 FILE *result_file;
64 result_file = got_opentemp();
65 if (result_file == NULL)
66 return 1;
68 for (i = 0; i < nitems(delta_tests); i++) {
69 struct delta_test *dt = &delta_tests[i];
70 FILE *base_file;
71 char buf[1024];
72 size_t n, result_len;
74 base_file = got_opentemp();
75 if (base_file == NULL) {
76 err = got_error_from_errno("got_opentemp");
77 break;
78 }
80 n = fwrite(dt->base, 1, dt->base_len, base_file);
81 if (n != dt->base_len) {
82 err = got_ferror(base_file, GOT_ERR_IO);
83 break;
84 }
85 rewind(base_file);
87 err = got_delta_apply(base_file, dt->delta, dt->delta_len,
88 result_file, &result_len);
89 if (fclose(base_file) == EOF && err == NULL)
90 err = got_error_from_errno("fclose");
91 if (dt->expected == NULL) {
92 /* Invalid delta, expect an error. */
93 if (err == NULL)
94 err = got_error(GOT_ERR_EXPECTED);
95 else if (err->code == GOT_ERR_BAD_DELTA)
96 err = NULL;
97 } else {
98 if (err)
99 break;
100 if (result_len != dt->result_len) {
101 err = got_ferror(result_file,
102 GOT_ERR_BAD_DELTA);
103 break;
105 n = fread(buf, result_len, 1, result_file);
106 if (n != 1 ||
107 strncmp(buf, dt->expected, result_len) != 0) {
108 err = got_ferror(result_file,
109 GOT_ERR_BAD_DELTA);
110 break;
113 rewind(result_file);
116 fclose(result_file);
117 return (err == NULL);
120 static int quiet;
122 #define RUN_TEST(expr, name) \
123 { test_ok = (expr); \
124 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
125 failure = (failure || !test_ok); }
127 static void
128 usage(void)
130 fprintf(stderr, "usage: delta_test [-q]\n");
133 int
134 main(int argc, char *argv[])
136 int test_ok;
137 int failure = 0;
138 int ch;
140 while ((ch = getopt(argc, argv, "q")) != -1) {
141 switch (ch) {
142 case 'q':
143 quiet = 1;
144 break;
145 default:
146 usage();
147 return 1;
151 argc -= optind;
152 argv += optind;
154 if (argc != 0) {
155 usage();
156 return 1;
159 #ifndef PROFILE
160 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
161 err(1, "pledge");
162 #endif
163 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
164 err(1, "unveil");
166 if (unveil(NULL, NULL) != 0)
167 err(1, "unveil");
169 RUN_TEST(delta_apply(), "delta_apply");
171 return failure ? 1 : 0;