Blame


1 ac25a292 2018-01-26 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 ac25a292 2018-01-26 stsp *
4 ac25a292 2018-01-26 stsp * Permission to use, copy, modify, and distribute this software for any
5 ac25a292 2018-01-26 stsp * purpose with or without fee is hereby granted, provided that the above
6 ac25a292 2018-01-26 stsp * copyright notice and this permission notice appear in all copies.
7 ac25a292 2018-01-26 stsp *
8 ac25a292 2018-01-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ac25a292 2018-01-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ac25a292 2018-01-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ac25a292 2018-01-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ac25a292 2018-01-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ac25a292 2018-01-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ac25a292 2018-01-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ac25a292 2018-01-26 stsp */
16 ac25a292 2018-01-26 stsp
17 09e05ed5 2023-03-08 thomas #include "got_compat.h"
18 09e05ed5 2023-03-08 thomas
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 ac25a292 2018-01-26 stsp
21 ac25a292 2018-01-26 stsp #include <stdio.h>
22 ac25a292 2018-01-26 stsp #include <stdlib.h>
23 885d3e02 2018-01-27 stsp #include <string.h>
24 f8352b2a 2018-03-12 stsp #include <err.h>
25 f8352b2a 2018-03-12 stsp #include <unistd.h>
26 7fb414ae 2020-08-08 stsp #include <getopt.h>
27 ac25a292 2018-01-26 stsp
28 ac25a292 2018-01-26 stsp #include "got_error.h"
29 511a516b 2018-05-19 stsp #include "got_opentemp.h"
30 324d37e7 2019-05-11 stsp #include "got_path.h"
31 ac25a292 2018-01-26 stsp
32 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
33 ac25a292 2018-01-26 stsp
34 885d3e02 2018-01-27 stsp #ifndef nitems
35 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 885d3e02 2018-01-27 stsp #endif
37 885d3e02 2018-01-27 stsp
38 fa8129f7 2022-03-22 thomas const struct delta_test {
39 885d3e02 2018-01-27 stsp const char *base;
40 89817b30 2018-11-11 stsp size_t base_len;
41 885d3e02 2018-01-27 stsp const char *delta;
42 885d3e02 2018-01-27 stsp size_t delta_len;
43 885d3e02 2018-01-27 stsp const char *expected;
44 89817b30 2018-11-11 stsp size_t result_len;
45 885d3e02 2018-01-27 stsp } delta_tests[] = {
46 885d3e02 2018-01-27 stsp /* base len 0, target len 4, append 4 'x' */
47 89817b30 2018-11-11 stsp { "", 0, "\x00\x04\x04xxxx", 7, "xxxx", 4 },
48 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 0 from base, append 4 'x' */
49 89817b30 2018-11-11 stsp { "aabbccdd", 8, "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx", 8 },
50 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 4 from base, append 4 'x' */
51 89817b30 2018-11-11 stsp { "aabbccdd", 8, "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx", 8 },
52 89817b30 2018-11-11 stsp /* git 48fb7deb5 Fix big left-shifts of unsigned char, 2009-06-17) */
53 89817b30 2018-11-11 stsp { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
54 53509745 2018-11-11 stsp 16, "\x10\x10\xff\xff\xff\xff\xff\x10\00\00", 10 , NULL, 0 },
55 53509745 2018-11-11 stsp /* libgit2 9844d38be delta: fix out-of-bounds read of delta */
56 53509745 2018-11-11 stsp { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
57 53509745 2018-11-11 stsp 16, "\x10\x70\xff", 3, NULL, 0}
58 885d3e02 2018-01-27 stsp };
59 885d3e02 2018-01-27 stsp
60 ac25a292 2018-01-26 stsp static int
61 2ff12563 2018-09-15 stsp delta_apply(void)
62 ac25a292 2018-01-26 stsp {
63 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
64 6059809a 2020-12-17 stsp size_t i;
65 885d3e02 2018-01-27 stsp FILE *result_file;
66 885d3e02 2018-01-27 stsp
67 885d3e02 2018-01-27 stsp result_file = got_opentemp();
68 885d3e02 2018-01-27 stsp if (result_file == NULL)
69 885d3e02 2018-01-27 stsp return 1;
70 885d3e02 2018-01-27 stsp
71 885d3e02 2018-01-27 stsp for (i = 0; i < nitems(delta_tests); i++) {
72 fa8129f7 2022-03-22 thomas const struct delta_test *dt = &delta_tests[i];
73 885d3e02 2018-01-27 stsp FILE *base_file;
74 885d3e02 2018-01-27 stsp char buf[1024];
75 89817b30 2018-11-11 stsp size_t n, result_len;
76 885d3e02 2018-01-27 stsp
77 f8aea23e 2018-02-11 stsp base_file = got_opentemp();
78 f8aea23e 2018-02-11 stsp if (base_file == NULL) {
79 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
80 885d3e02 2018-01-27 stsp break;
81 f8aea23e 2018-02-11 stsp }
82 885d3e02 2018-01-27 stsp
83 89817b30 2018-11-11 stsp n = fwrite(dt->base, 1, dt->base_len, base_file);
84 89817b30 2018-11-11 stsp if (n != dt->base_len) {
85 f8aea23e 2018-02-11 stsp err = got_ferror(base_file, GOT_ERR_IO);
86 f8aea23e 2018-02-11 stsp break;
87 f8aea23e 2018-02-11 stsp }
88 f8aea23e 2018-02-11 stsp rewind(base_file);
89 f8aea23e 2018-02-11 stsp
90 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, dt->delta, dt->delta_len,
91 89817b30 2018-11-11 stsp result_file, &result_len);
92 56b63ca4 2021-01-22 stsp if (fclose(base_file) == EOF && err == NULL)
93 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
94 89817b30 2018-11-11 stsp if (dt->expected == NULL) {
95 89817b30 2018-11-11 stsp /* Invalid delta, expect an error. */
96 89817b30 2018-11-11 stsp if (err == NULL)
97 89817b30 2018-11-11 stsp err = got_error(GOT_ERR_EXPECTED);
98 89817b30 2018-11-11 stsp else if (err->code == GOT_ERR_BAD_DELTA)
99 89817b30 2018-11-11 stsp err = NULL;
100 89817b30 2018-11-11 stsp } else {
101 89817b30 2018-11-11 stsp if (err)
102 89817b30 2018-11-11 stsp break;
103 89817b30 2018-11-11 stsp if (result_len != dt->result_len) {
104 89817b30 2018-11-11 stsp err = got_ferror(result_file,
105 89817b30 2018-11-11 stsp GOT_ERR_BAD_DELTA);
106 89817b30 2018-11-11 stsp break;
107 89817b30 2018-11-11 stsp }
108 89817b30 2018-11-11 stsp n = fread(buf, result_len, 1, result_file);
109 89817b30 2018-11-11 stsp if (n != 1 ||
110 89817b30 2018-11-11 stsp strncmp(buf, dt->expected, result_len) != 0) {
111 89817b30 2018-11-11 stsp err = got_ferror(result_file,
112 89817b30 2018-11-11 stsp GOT_ERR_BAD_DELTA);
113 89817b30 2018-11-11 stsp break;
114 89817b30 2018-11-11 stsp }
115 b29656e2 2018-03-16 stsp }
116 885d3e02 2018-01-27 stsp rewind(result_file);
117 885d3e02 2018-01-27 stsp }
118 885d3e02 2018-01-27 stsp
119 885d3e02 2018-01-27 stsp fclose(result_file);
120 885d3e02 2018-01-27 stsp return (err == NULL);
121 ac25a292 2018-01-26 stsp }
122 ac25a292 2018-01-26 stsp
123 7fb414ae 2020-08-08 stsp static int quiet;
124 7fb414ae 2020-08-08 stsp
125 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
126 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
127 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
128 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
129 b08fe7be 2018-01-26 stsp
130 7fb414ae 2020-08-08 stsp static void
131 7fb414ae 2020-08-08 stsp usage(void)
132 7fb414ae 2020-08-08 stsp {
133 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: delta_test [-q]\n");
134 7fb414ae 2020-08-08 stsp }
135 7fb414ae 2020-08-08 stsp
136 ac25a292 2018-01-26 stsp int
137 7fb414ae 2020-08-08 stsp main(int argc, char *argv[])
138 ac25a292 2018-01-26 stsp {
139 b08fe7be 2018-01-26 stsp int test_ok;
140 ac25a292 2018-01-26 stsp int failure = 0;
141 7fb414ae 2020-08-08 stsp int ch;
142 ac25a292 2018-01-26 stsp
143 7fb414ae 2020-08-08 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
144 7fb414ae 2020-08-08 stsp switch (ch) {
145 7fb414ae 2020-08-08 stsp case 'q':
146 7fb414ae 2020-08-08 stsp quiet = 1;
147 7fb414ae 2020-08-08 stsp break;
148 7fb414ae 2020-08-08 stsp default:
149 7fb414ae 2020-08-08 stsp usage();
150 7fb414ae 2020-08-08 stsp return 1;
151 7fb414ae 2020-08-08 stsp }
152 7fb414ae 2020-08-08 stsp }
153 7fb414ae 2020-08-08 stsp
154 7fb414ae 2020-08-08 stsp argc -= optind;
155 7fb414ae 2020-08-08 stsp argv += optind;
156 7fb414ae 2020-08-08 stsp
157 7fb414ae 2020-08-08 stsp if (argc != 0) {
158 7fb414ae 2020-08-08 stsp usage();
159 ac25a292 2018-01-26 stsp return 1;
160 ac25a292 2018-01-26 stsp }
161 ac25a292 2018-01-26 stsp
162 2ff12563 2018-09-15 stsp #ifndef PROFILE
163 c89f2770 2019-01-04 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
164 f8352b2a 2018-03-12 stsp err(1, "pledge");
165 2ff12563 2018-09-15 stsp #endif
166 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
167 c89f2770 2019-01-04 stsp err(1, "unveil");
168 f8352b2a 2018-03-12 stsp
169 c89f2770 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
170 c89f2770 2019-01-04 stsp err(1, "unveil");
171 c89f2770 2019-01-04 stsp
172 6df54056 2018-03-03 stsp RUN_TEST(delta_apply(), "delta_apply");
173 ac25a292 2018-01-26 stsp
174 ac25a292 2018-01-26 stsp return failure ? 1 : 0;
175 ac25a292 2018-01-26 stsp }