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