Blame


1 8704c7ce 2021-03-10 stsp /*
2 8704c7ce 2021-03-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 8704c7ce 2021-03-10 stsp *
4 8704c7ce 2021-03-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 8704c7ce 2021-03-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 8704c7ce 2021-03-10 stsp * copyright notice and this permission notice appear in all copies.
7 8704c7ce 2021-03-10 stsp *
8 8704c7ce 2021-03-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8704c7ce 2021-03-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8704c7ce 2021-03-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8704c7ce 2021-03-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8704c7ce 2021-03-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8704c7ce 2021-03-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8704c7ce 2021-03-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 8704c7ce 2021-03-10 stsp */
16 09e05ed5 2023-03-08 thomas
17 09e05ed5 2023-03-08 thomas #include "got_compat.h"
18 8704c7ce 2021-03-10 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 8704c7ce 2021-03-10 stsp #include <stdio.h>
21 8704c7ce 2021-03-10 stsp #include <stdlib.h>
22 8704c7ce 2021-03-10 stsp #include <string.h>
23 8704c7ce 2021-03-10 stsp #include <err.h>
24 8704c7ce 2021-03-10 stsp #include <unistd.h>
25 8704c7ce 2021-03-10 stsp #include <getopt.h>
26 8704c7ce 2021-03-10 stsp
27 8704c7ce 2021-03-10 stsp #include "got_error.h"
28 8704c7ce 2021-03-10 stsp #include "got_opentemp.h"
29 8704c7ce 2021-03-10 stsp
30 8704c7ce 2021-03-10 stsp #include "got_lib_deltify.h"
31 8704c7ce 2021-03-10 stsp
32 8704c7ce 2021-03-10 stsp #ifndef nitems
33 8704c7ce 2021-03-10 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 8704c7ce 2021-03-10 stsp #endif
35 8704c7ce 2021-03-10 stsp
36 8704c7ce 2021-03-10 stsp static int
37 8704c7ce 2021-03-10 stsp deltify_abc_axc(void)
38 8704c7ce 2021-03-10 stsp {
39 8704c7ce 2021-03-10 stsp const struct got_error *err = NULL;
40 8704c7ce 2021-03-10 stsp size_t i;
41 8704c7ce 2021-03-10 stsp FILE *base_file, *derived_file, *result_file;
42 8704c7ce 2021-03-10 stsp struct got_delta_table *dt;
43 8704c7ce 2021-03-10 stsp struct got_delta_instruction *deltas;
44 8704c7ce 2021-03-10 stsp int ndeltas;
45 8704c7ce 2021-03-10 stsp int have_nblocks = 0;
46 cc524d36 2022-05-31 thomas uint32_t seed;
47 8704c7ce 2021-03-10 stsp
48 cc524d36 2022-05-31 thomas seed = arc4random();
49 cc524d36 2022-05-31 thomas
50 8704c7ce 2021-03-10 stsp base_file = got_opentemp();
51 8704c7ce 2021-03-10 stsp if (base_file == NULL)
52 8704c7ce 2021-03-10 stsp return 1;
53 8704c7ce 2021-03-10 stsp
54 8704c7ce 2021-03-10 stsp derived_file = got_opentemp();
55 8704c7ce 2021-03-10 stsp if (derived_file == NULL)
56 8704c7ce 2021-03-10 stsp return 1;
57 8704c7ce 2021-03-10 stsp
58 8704c7ce 2021-03-10 stsp result_file = got_opentemp();
59 8704c7ce 2021-03-10 stsp if (result_file == NULL)
60 8704c7ce 2021-03-10 stsp return 1;
61 8704c7ce 2021-03-10 stsp
62 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 8704c7ce 2021-03-10 stsp fputc('a', base_file);
64 8704c7ce 2021-03-10 stsp fputc('a', derived_file);
65 8704c7ce 2021-03-10 stsp }
66 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 8704c7ce 2021-03-10 stsp fputc('b', base_file);
68 8704c7ce 2021-03-10 stsp fputc('x', derived_file);
69 8704c7ce 2021-03-10 stsp }
70 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
71 8704c7ce 2021-03-10 stsp fputc('c', base_file);
72 8704c7ce 2021-03-10 stsp fputc('c', derived_file);
73 8704c7ce 2021-03-10 stsp }
74 8704c7ce 2021-03-10 stsp
75 8704c7ce 2021-03-10 stsp rewind(base_file);
76 8704c7ce 2021-03-10 stsp rewind(derived_file);
77 8704c7ce 2021-03-10 stsp
78 cc524d36 2022-05-31 thomas err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
79 cc524d36 2022-05-31 thomas seed);
80 8704c7ce 2021-03-10 stsp if (err)
81 8704c7ce 2021-03-10 stsp goto done;
82 8704c7ce 2021-03-10 stsp
83 8704c7ce 2021-03-10 stsp for (i = 0; i < dt->nalloc; i++) {
84 8704c7ce 2021-03-10 stsp if (dt->blocks[i].len > 0)
85 8704c7ce 2021-03-10 stsp have_nblocks++;
86 8704c7ce 2021-03-10 stsp }
87 8704c7ce 2021-03-10 stsp if (have_nblocks != dt->nblocks) {
88 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
89 8704c7ce 2021-03-10 stsp goto done;
90 8704c7ce 2021-03-10 stsp }
91 8704c7ce 2021-03-10 stsp
92 8704c7ce 2021-03-10 stsp err = got_deltify(&deltas, &ndeltas, derived_file, 0,
93 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
94 f34b169e 2021-06-18 stsp 3 * GOT_DELTIFY_MAXCHUNK);
95 8704c7ce 2021-03-10 stsp if (err)
96 8704c7ce 2021-03-10 stsp goto done;
97 8704c7ce 2021-03-10 stsp
98 8704c7ce 2021-03-10 stsp if (ndeltas != 3) {
99 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
100 8704c7ce 2021-03-10 stsp goto done;
101 8704c7ce 2021-03-10 stsp }
102 8704c7ce 2021-03-10 stsp /* Copy 'aaaa...' from base file. */
103 8704c7ce 2021-03-10 stsp if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
104 8704c7ce 2021-03-10 stsp deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
105 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
106 8704c7ce 2021-03-10 stsp goto done;
107 8704c7ce 2021-03-10 stsp }
108 8704c7ce 2021-03-10 stsp /* Copy 'xxxx...' from derived file. */
109 8704c7ce 2021-03-10 stsp if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
110 8704c7ce 2021-03-10 stsp deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
111 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
112 8704c7ce 2021-03-10 stsp goto done;
113 8704c7ce 2021-03-10 stsp }
114 8704c7ce 2021-03-10 stsp /* Copy 'ccccc...' from base file. */
115 8704c7ce 2021-03-10 stsp if (!(deltas[2].copy == 1 &&
116 8704c7ce 2021-03-10 stsp deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
117 8704c7ce 2021-03-10 stsp deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
118 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
119 8704c7ce 2021-03-10 stsp goto done;
120 8704c7ce 2021-03-10 stsp }
121 8704c7ce 2021-03-10 stsp
122 8704c7ce 2021-03-10 stsp done:
123 8704c7ce 2021-03-10 stsp got_deltify_free(dt);
124 8704c7ce 2021-03-10 stsp fclose(base_file);
125 8704c7ce 2021-03-10 stsp fclose(derived_file);
126 2b0ae357 2022-01-10 thomas fclose(result_file);
127 2b0ae357 2022-01-10 thomas return (err == NULL);
128 2b0ae357 2022-01-10 thomas }
129 2b0ae357 2022-01-10 thomas
130 2b0ae357 2022-01-10 thomas static int
131 2b0ae357 2022-01-10 thomas deltify_abc_axc_file_mem(void)
132 2b0ae357 2022-01-10 thomas {
133 2b0ae357 2022-01-10 thomas const struct got_error *err = NULL;
134 2b0ae357 2022-01-10 thomas size_t i;
135 2b0ae357 2022-01-10 thomas uint8_t base_data[3 * GOT_DELTIFY_MAXCHUNK];
136 2b0ae357 2022-01-10 thomas FILE *derived_file, *result_file;
137 2b0ae357 2022-01-10 thomas struct got_delta_table *dt;
138 2b0ae357 2022-01-10 thomas struct got_delta_instruction *deltas;
139 2b0ae357 2022-01-10 thomas int ndeltas;
140 2b0ae357 2022-01-10 thomas int have_nblocks = 0;
141 cc524d36 2022-05-31 thomas uint32_t seed;
142 2b0ae357 2022-01-10 thomas
143 cc524d36 2022-05-31 thomas seed = arc4random();
144 cc524d36 2022-05-31 thomas
145 2b0ae357 2022-01-10 thomas derived_file = got_opentemp();
146 2b0ae357 2022-01-10 thomas if (derived_file == NULL)
147 2b0ae357 2022-01-10 thomas return 1;
148 2b0ae357 2022-01-10 thomas
149 2b0ae357 2022-01-10 thomas result_file = got_opentemp();
150 2b0ae357 2022-01-10 thomas if (result_file == NULL)
151 2b0ae357 2022-01-10 thomas return 1;
152 2b0ae357 2022-01-10 thomas
153 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
154 2b0ae357 2022-01-10 thomas base_data[i] = 'a';
155 2b0ae357 2022-01-10 thomas fputc('a', derived_file);
156 2b0ae357 2022-01-10 thomas }
157 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
158 2b0ae357 2022-01-10 thomas base_data[GOT_DELTIFY_MAXCHUNK + i] = 'b';
159 2b0ae357 2022-01-10 thomas fputc('x', derived_file);
160 2b0ae357 2022-01-10 thomas }
161 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
162 2b0ae357 2022-01-10 thomas base_data[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
163 2b0ae357 2022-01-10 thomas fputc('c', derived_file);
164 2b0ae357 2022-01-10 thomas }
165 2b0ae357 2022-01-10 thomas
166 2b0ae357 2022-01-10 thomas rewind(derived_file);
167 2b0ae357 2022-01-10 thomas
168 cc524d36 2022-05-31 thomas err = got_deltify_init_mem(&dt, base_data, 0, 3 * GOT_DELTIFY_MAXCHUNK,
169 cc524d36 2022-05-31 thomas seed);
170 2b0ae357 2022-01-10 thomas if (err)
171 2b0ae357 2022-01-10 thomas goto done;
172 2b0ae357 2022-01-10 thomas
173 2b0ae357 2022-01-10 thomas for (i = 0; i < dt->nalloc; i++) {
174 2b0ae357 2022-01-10 thomas if (dt->blocks[i].len > 0)
175 2b0ae357 2022-01-10 thomas have_nblocks++;
176 2b0ae357 2022-01-10 thomas }
177 2b0ae357 2022-01-10 thomas if (have_nblocks != dt->nblocks) {
178 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
179 2b0ae357 2022-01-10 thomas goto done;
180 2b0ae357 2022-01-10 thomas }
181 2b0ae357 2022-01-10 thomas
182 2b0ae357 2022-01-10 thomas err = got_deltify_file_mem(&deltas, &ndeltas, derived_file, 0,
183 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_data, 0,
184 2b0ae357 2022-01-10 thomas 3 * GOT_DELTIFY_MAXCHUNK);
185 2b0ae357 2022-01-10 thomas if (err)
186 2b0ae357 2022-01-10 thomas goto done;
187 2b0ae357 2022-01-10 thomas
188 2b0ae357 2022-01-10 thomas if (ndeltas != 3) {
189 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
190 2b0ae357 2022-01-10 thomas goto done;
191 2b0ae357 2022-01-10 thomas }
192 2b0ae357 2022-01-10 thomas /* Copy 'aaaa...' from base file. */
193 2b0ae357 2022-01-10 thomas if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
194 2b0ae357 2022-01-10 thomas deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
195 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
196 2b0ae357 2022-01-10 thomas goto done;
197 2b0ae357 2022-01-10 thomas }
198 2b0ae357 2022-01-10 thomas /* Copy 'xxxx...' from derived file. */
199 2b0ae357 2022-01-10 thomas if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
200 2b0ae357 2022-01-10 thomas deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
201 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
202 2b0ae357 2022-01-10 thomas goto done;
203 2b0ae357 2022-01-10 thomas }
204 2b0ae357 2022-01-10 thomas /* Copy 'ccccc...' from base file. */
205 2b0ae357 2022-01-10 thomas if (!(deltas[2].copy == 1 &&
206 2b0ae357 2022-01-10 thomas deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
207 2b0ae357 2022-01-10 thomas deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
208 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
209 2b0ae357 2022-01-10 thomas goto done;
210 2b0ae357 2022-01-10 thomas }
211 2b0ae357 2022-01-10 thomas
212 2b0ae357 2022-01-10 thomas done:
213 2b0ae357 2022-01-10 thomas got_deltify_free(dt);
214 2b0ae357 2022-01-10 thomas fclose(derived_file);
215 2b0ae357 2022-01-10 thomas fclose(result_file);
216 2b0ae357 2022-01-10 thomas return (err == NULL);
217 2b0ae357 2022-01-10 thomas }
218 2b0ae357 2022-01-10 thomas
219 2b0ae357 2022-01-10 thomas static int
220 2b0ae357 2022-01-10 thomas deltify_abc_axc_mem_file(void)
221 2b0ae357 2022-01-10 thomas {
222 2b0ae357 2022-01-10 thomas const struct got_error *err = NULL;
223 2b0ae357 2022-01-10 thomas size_t i;
224 2b0ae357 2022-01-10 thomas FILE *base_file, *result_file;
225 2b0ae357 2022-01-10 thomas uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
226 2b0ae357 2022-01-10 thomas struct got_delta_table *dt;
227 2b0ae357 2022-01-10 thomas struct got_delta_instruction *deltas;
228 2b0ae357 2022-01-10 thomas int ndeltas;
229 2b0ae357 2022-01-10 thomas int have_nblocks = 0;
230 cc524d36 2022-05-31 thomas uint32_t seed;
231 2b0ae357 2022-01-10 thomas
232 cc524d36 2022-05-31 thomas seed = arc4random();
233 cc524d36 2022-05-31 thomas
234 2b0ae357 2022-01-10 thomas base_file = got_opentemp();
235 2b0ae357 2022-01-10 thomas if (base_file == NULL)
236 2b0ae357 2022-01-10 thomas return 1;
237 2b0ae357 2022-01-10 thomas
238 2b0ae357 2022-01-10 thomas result_file = got_opentemp();
239 2b0ae357 2022-01-10 thomas if (result_file == NULL)
240 2b0ae357 2022-01-10 thomas return 1;
241 2b0ae357 2022-01-10 thomas
242 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
243 2b0ae357 2022-01-10 thomas fputc('a', base_file);
244 2b0ae357 2022-01-10 thomas derived_file[i] = 'a';
245 2b0ae357 2022-01-10 thomas }
246 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
247 2b0ae357 2022-01-10 thomas fputc('b', base_file);
248 2b0ae357 2022-01-10 thomas derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
249 2b0ae357 2022-01-10 thomas }
250 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
251 2b0ae357 2022-01-10 thomas fputc('c', base_file);
252 2b0ae357 2022-01-10 thomas derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
253 2b0ae357 2022-01-10 thomas }
254 2b0ae357 2022-01-10 thomas
255 2b0ae357 2022-01-10 thomas rewind(base_file);
256 2b0ae357 2022-01-10 thomas
257 cc524d36 2022-05-31 thomas err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
258 cc524d36 2022-05-31 thomas seed);
259 2b0ae357 2022-01-10 thomas if (err)
260 2b0ae357 2022-01-10 thomas goto done;
261 2b0ae357 2022-01-10 thomas
262 2b0ae357 2022-01-10 thomas for (i = 0; i < dt->nalloc; i++) {
263 2b0ae357 2022-01-10 thomas if (dt->blocks[i].len > 0)
264 2b0ae357 2022-01-10 thomas have_nblocks++;
265 2b0ae357 2022-01-10 thomas }
266 2b0ae357 2022-01-10 thomas if (have_nblocks != dt->nblocks) {
267 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
268 2b0ae357 2022-01-10 thomas goto done;
269 2b0ae357 2022-01-10 thomas }
270 2b0ae357 2022-01-10 thomas
271 2b0ae357 2022-01-10 thomas err = got_deltify_mem_file(&deltas, &ndeltas, derived_file, 0,
272 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
273 2b0ae357 2022-01-10 thomas 3 * GOT_DELTIFY_MAXCHUNK);
274 2b0ae357 2022-01-10 thomas if (err)
275 2b0ae357 2022-01-10 thomas goto done;
276 2b0ae357 2022-01-10 thomas
277 2b0ae357 2022-01-10 thomas if (ndeltas != 3) {
278 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
279 2b0ae357 2022-01-10 thomas goto done;
280 2b0ae357 2022-01-10 thomas }
281 2b0ae357 2022-01-10 thomas /* Copy 'aaaa...' from base file. */
282 2b0ae357 2022-01-10 thomas if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
283 2b0ae357 2022-01-10 thomas deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
284 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
285 2b0ae357 2022-01-10 thomas goto done;
286 2b0ae357 2022-01-10 thomas }
287 2b0ae357 2022-01-10 thomas /* Copy 'xxxx...' from derived file. */
288 2b0ae357 2022-01-10 thomas if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
289 2b0ae357 2022-01-10 thomas deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
290 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
291 2b0ae357 2022-01-10 thomas goto done;
292 2b0ae357 2022-01-10 thomas }
293 2b0ae357 2022-01-10 thomas /* Copy 'ccccc...' from base file. */
294 2b0ae357 2022-01-10 thomas if (!(deltas[2].copy == 1 &&
295 2b0ae357 2022-01-10 thomas deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
296 2b0ae357 2022-01-10 thomas deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
297 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
298 2b0ae357 2022-01-10 thomas goto done;
299 2b0ae357 2022-01-10 thomas }
300 2b0ae357 2022-01-10 thomas
301 2b0ae357 2022-01-10 thomas done:
302 2b0ae357 2022-01-10 thomas got_deltify_free(dt);
303 2b0ae357 2022-01-10 thomas fclose(base_file);
304 8704c7ce 2021-03-10 stsp fclose(result_file);
305 8704c7ce 2021-03-10 stsp return (err == NULL);
306 8704c7ce 2021-03-10 stsp }
307 8704c7ce 2021-03-10 stsp
308 2b0ae357 2022-01-10 thomas static int
309 2b0ae357 2022-01-10 thomas deltify_abc_axc_mem_mem(void)
310 2b0ae357 2022-01-10 thomas {
311 2b0ae357 2022-01-10 thomas const struct got_error *err = NULL;
312 2b0ae357 2022-01-10 thomas size_t i;
313 2b0ae357 2022-01-10 thomas FILE *result_file;
314 2b0ae357 2022-01-10 thomas uint8_t base_file[3 * GOT_DELTIFY_MAXCHUNK];
315 2b0ae357 2022-01-10 thomas uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
316 2b0ae357 2022-01-10 thomas struct got_delta_table *dt;
317 2b0ae357 2022-01-10 thomas struct got_delta_instruction *deltas;
318 2b0ae357 2022-01-10 thomas int ndeltas;
319 2b0ae357 2022-01-10 thomas int have_nblocks = 0;
320 cc524d36 2022-05-31 thomas uint32_t seed;
321 2b0ae357 2022-01-10 thomas
322 cc524d36 2022-05-31 thomas seed = arc4random();
323 cc524d36 2022-05-31 thomas
324 2b0ae357 2022-01-10 thomas result_file = got_opentemp();
325 2b0ae357 2022-01-10 thomas if (result_file == NULL)
326 2b0ae357 2022-01-10 thomas return 1;
327 2b0ae357 2022-01-10 thomas
328 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
329 2b0ae357 2022-01-10 thomas base_file[i] = 'a';
330 2b0ae357 2022-01-10 thomas derived_file[i] = 'a';
331 2b0ae357 2022-01-10 thomas }
332 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
333 2b0ae357 2022-01-10 thomas base_file[GOT_DELTIFY_MAXCHUNK + i] = 'b';
334 2b0ae357 2022-01-10 thomas derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
335 2b0ae357 2022-01-10 thomas }
336 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
337 2b0ae357 2022-01-10 thomas base_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
338 2b0ae357 2022-01-10 thomas derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
339 2b0ae357 2022-01-10 thomas }
340 2b0ae357 2022-01-10 thomas
341 cc524d36 2022-05-31 thomas err = got_deltify_init_mem(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
342 cc524d36 2022-05-31 thomas seed);
343 2b0ae357 2022-01-10 thomas if (err)
344 2b0ae357 2022-01-10 thomas goto done;
345 2b0ae357 2022-01-10 thomas
346 2b0ae357 2022-01-10 thomas for (i = 0; i < dt->nalloc; i++) {
347 2b0ae357 2022-01-10 thomas if (dt->blocks[i].len > 0)
348 2b0ae357 2022-01-10 thomas have_nblocks++;
349 2b0ae357 2022-01-10 thomas }
350 2b0ae357 2022-01-10 thomas if (have_nblocks != dt->nblocks) {
351 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
352 2b0ae357 2022-01-10 thomas goto done;
353 2b0ae357 2022-01-10 thomas }
354 2b0ae357 2022-01-10 thomas
355 2b0ae357 2022-01-10 thomas err = got_deltify_mem_mem(&deltas, &ndeltas, derived_file, 0,
356 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
357 2b0ae357 2022-01-10 thomas 3 * GOT_DELTIFY_MAXCHUNK);
358 2b0ae357 2022-01-10 thomas if (err)
359 2b0ae357 2022-01-10 thomas goto done;
360 2b0ae357 2022-01-10 thomas
361 2b0ae357 2022-01-10 thomas if (ndeltas != 3) {
362 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
363 2b0ae357 2022-01-10 thomas goto done;
364 2b0ae357 2022-01-10 thomas }
365 2b0ae357 2022-01-10 thomas /* Copy 'aaaa...' from base file. */
366 2b0ae357 2022-01-10 thomas if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
367 2b0ae357 2022-01-10 thomas deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
368 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
369 2b0ae357 2022-01-10 thomas goto done;
370 2b0ae357 2022-01-10 thomas }
371 2b0ae357 2022-01-10 thomas /* Copy 'xxxx...' from derived file. */
372 2b0ae357 2022-01-10 thomas if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
373 2b0ae357 2022-01-10 thomas deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
374 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
375 2b0ae357 2022-01-10 thomas goto done;
376 2b0ae357 2022-01-10 thomas }
377 2b0ae357 2022-01-10 thomas /* Copy 'ccccc...' from base file. */
378 2b0ae357 2022-01-10 thomas if (!(deltas[2].copy == 1 &&
379 2b0ae357 2022-01-10 thomas deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
380 2b0ae357 2022-01-10 thomas deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
381 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
382 2b0ae357 2022-01-10 thomas goto done;
383 2b0ae357 2022-01-10 thomas }
384 2b0ae357 2022-01-10 thomas
385 2b0ae357 2022-01-10 thomas done:
386 2b0ae357 2022-01-10 thomas got_deltify_free(dt);
387 2b0ae357 2022-01-10 thomas fclose(result_file);
388 2b0ae357 2022-01-10 thomas return (err == NULL);
389 2b0ae357 2022-01-10 thomas }
390 2b0ae357 2022-01-10 thomas
391 8704c7ce 2021-03-10 stsp static int quiet;
392 8704c7ce 2021-03-10 stsp
393 8704c7ce 2021-03-10 stsp #define RUN_TEST(expr, name) \
394 8704c7ce 2021-03-10 stsp { test_ok = (expr); \
395 8704c7ce 2021-03-10 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
396 8704c7ce 2021-03-10 stsp failure = (failure || !test_ok); }
397 8704c7ce 2021-03-10 stsp
398 8704c7ce 2021-03-10 stsp static void
399 8704c7ce 2021-03-10 stsp usage(void)
400 8704c7ce 2021-03-10 stsp {
401 8704c7ce 2021-03-10 stsp fprintf(stderr, "usage: delta_test [-q]\n");
402 8704c7ce 2021-03-10 stsp }
403 8704c7ce 2021-03-10 stsp
404 8704c7ce 2021-03-10 stsp int
405 8704c7ce 2021-03-10 stsp main(int argc, char *argv[])
406 8704c7ce 2021-03-10 stsp {
407 8704c7ce 2021-03-10 stsp int test_ok;
408 8704c7ce 2021-03-10 stsp int failure = 0;
409 8704c7ce 2021-03-10 stsp int ch;
410 8704c7ce 2021-03-10 stsp
411 8704c7ce 2021-03-10 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
412 8704c7ce 2021-03-10 stsp switch (ch) {
413 8704c7ce 2021-03-10 stsp case 'q':
414 8704c7ce 2021-03-10 stsp quiet = 1;
415 8704c7ce 2021-03-10 stsp break;
416 8704c7ce 2021-03-10 stsp default:
417 8704c7ce 2021-03-10 stsp usage();
418 8704c7ce 2021-03-10 stsp return 1;
419 8704c7ce 2021-03-10 stsp }
420 8704c7ce 2021-03-10 stsp }
421 8704c7ce 2021-03-10 stsp
422 8704c7ce 2021-03-10 stsp argc -= optind;
423 8704c7ce 2021-03-10 stsp argv += optind;
424 8704c7ce 2021-03-10 stsp
425 8704c7ce 2021-03-10 stsp if (argc != 0) {
426 8704c7ce 2021-03-10 stsp usage();
427 8704c7ce 2021-03-10 stsp return 1;
428 8704c7ce 2021-03-10 stsp }
429 8704c7ce 2021-03-10 stsp
430 8704c7ce 2021-03-10 stsp #ifndef PROFILE
431 8704c7ce 2021-03-10 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
432 8704c7ce 2021-03-10 stsp err(1, "pledge");
433 8704c7ce 2021-03-10 stsp #endif
434 8704c7ce 2021-03-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
435 8704c7ce 2021-03-10 stsp err(1, "unveil");
436 8704c7ce 2021-03-10 stsp
437 8704c7ce 2021-03-10 stsp if (unveil(NULL, NULL) != 0)
438 8704c7ce 2021-03-10 stsp err(1, "unveil");
439 8704c7ce 2021-03-10 stsp
440 8704c7ce 2021-03-10 stsp RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
441 2b0ae357 2022-01-10 thomas RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
442 2b0ae357 2022-01-10 thomas RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
443 2b0ae357 2022-01-10 thomas RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
444 8704c7ce 2021-03-10 stsp
445 8704c7ce 2021-03-10 stsp return failure ? 1 : 0;
446 8704c7ce 2021-03-10 stsp }