Blob


1 /*
2 * Copyright (c) 2021 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <err.h>
21 #include <unistd.h>
22 #include <getopt.h>
24 #include "got_error.h"
25 #include "got_opentemp.h"
27 #include "got_lib_deltify.h"
29 #ifndef nitems
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
31 #endif
33 static int
34 deltify_abc_axc(void)
35 {
36 const struct got_error *err = NULL;
37 size_t i;
38 FILE *base_file, *derived_file, *result_file;
39 struct got_delta_table *dt;
40 struct got_delta_instruction *deltas;
41 int ndeltas;
42 int have_nblocks = 0;
44 base_file = got_opentemp();
45 if (base_file == NULL)
46 return 1;
48 derived_file = got_opentemp();
49 if (derived_file == NULL)
50 return 1;
52 result_file = got_opentemp();
53 if (result_file == NULL)
54 return 1;
56 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
57 fputc('a', base_file);
58 fputc('a', derived_file);
59 }
60 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
61 fputc('b', base_file);
62 fputc('x', derived_file);
63 }
64 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
65 fputc('c', base_file);
66 fputc('c', derived_file);
67 }
69 rewind(base_file);
70 rewind(derived_file);
72 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
73 if (err)
74 goto done;
76 for (i = 0; i < dt->nalloc; i++) {
77 if (dt->blocks[i].len > 0)
78 have_nblocks++;
79 }
80 if (have_nblocks != dt->nblocks) {
81 err = got_error(GOT_ERR_BAD_DELTA);
82 goto done;
83 }
85 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
86 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
87 3 * GOT_DELTIFY_MAXCHUNK);
88 if (err)
89 goto done;
91 if (ndeltas != 3) {
92 err = got_error(GOT_ERR_BAD_DELTA);
93 goto done;
94 }
95 /* Copy 'aaaa...' from base file. */
96 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
97 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
98 err = got_error(GOT_ERR_BAD_DELTA);
99 goto done;
101 /* Copy 'xxxx...' from derived file. */
102 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
103 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
104 err = got_error(GOT_ERR_BAD_DELTA);
105 goto done;
107 /* Copy 'ccccc...' from base file. */
108 if (!(deltas[2].copy == 1 &&
109 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
110 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
111 err = got_error(GOT_ERR_BAD_DELTA);
112 goto done;
115 done:
116 got_deltify_free(dt);
117 fclose(base_file);
118 fclose(derived_file);
119 fclose(result_file);
120 return (err == NULL);
123 static int
124 deltify_abc_axc_file_mem(void)
126 const struct got_error *err = NULL;
127 size_t i;
128 uint8_t base_data[3 * GOT_DELTIFY_MAXCHUNK];
129 FILE *derived_file, *result_file;
130 struct got_delta_table *dt;
131 struct got_delta_instruction *deltas;
132 int ndeltas;
133 int have_nblocks = 0;
135 derived_file = got_opentemp();
136 if (derived_file == NULL)
137 return 1;
139 result_file = got_opentemp();
140 if (result_file == NULL)
141 return 1;
143 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
144 base_data[i] = 'a';
145 fputc('a', derived_file);
147 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
148 base_data[GOT_DELTIFY_MAXCHUNK + i] = 'b';
149 fputc('x', derived_file);
151 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
152 base_data[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
153 fputc('c', derived_file);
156 rewind(derived_file);
158 err = got_deltify_init_mem(&dt, base_data, 0, 3 * GOT_DELTIFY_MAXCHUNK);
159 if (err)
160 goto done;
162 for (i = 0; i < dt->nalloc; i++) {
163 if (dt->blocks[i].len > 0)
164 have_nblocks++;
166 if (have_nblocks != dt->nblocks) {
167 err = got_error(GOT_ERR_BAD_DELTA);
168 goto done;
171 err = got_deltify_file_mem(&deltas, &ndeltas, derived_file, 0,
172 3 * GOT_DELTIFY_MAXCHUNK, dt, base_data, 0,
173 3 * GOT_DELTIFY_MAXCHUNK);
174 if (err)
175 goto done;
177 if (ndeltas != 3) {
178 err = got_error(GOT_ERR_BAD_DELTA);
179 goto done;
181 /* Copy 'aaaa...' from base file. */
182 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
183 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
184 err = got_error(GOT_ERR_BAD_DELTA);
185 goto done;
187 /* Copy 'xxxx...' from derived file. */
188 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
189 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
190 err = got_error(GOT_ERR_BAD_DELTA);
191 goto done;
193 /* Copy 'ccccc...' from base file. */
194 if (!(deltas[2].copy == 1 &&
195 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
196 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
197 err = got_error(GOT_ERR_BAD_DELTA);
198 goto done;
201 done:
202 got_deltify_free(dt);
203 fclose(derived_file);
204 fclose(result_file);
205 return (err == NULL);
208 static int
209 deltify_abc_axc_mem_file(void)
211 const struct got_error *err = NULL;
212 size_t i;
213 FILE *base_file, *result_file;
214 uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
215 struct got_delta_table *dt;
216 struct got_delta_instruction *deltas;
217 int ndeltas;
218 int have_nblocks = 0;
220 base_file = got_opentemp();
221 if (base_file == NULL)
222 return 1;
224 result_file = got_opentemp();
225 if (result_file == NULL)
226 return 1;
228 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
229 fputc('a', base_file);
230 derived_file[i] = 'a';
232 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
233 fputc('b', base_file);
234 derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
236 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
237 fputc('c', base_file);
238 derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
241 rewind(base_file);
243 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
244 if (err)
245 goto done;
247 for (i = 0; i < dt->nalloc; i++) {
248 if (dt->blocks[i].len > 0)
249 have_nblocks++;
251 if (have_nblocks != dt->nblocks) {
252 err = got_error(GOT_ERR_BAD_DELTA);
253 goto done;
256 err = got_deltify_mem_file(&deltas, &ndeltas, derived_file, 0,
257 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
258 3 * GOT_DELTIFY_MAXCHUNK);
259 if (err)
260 goto done;
262 if (ndeltas != 3) {
263 err = got_error(GOT_ERR_BAD_DELTA);
264 goto done;
266 /* Copy 'aaaa...' from base file. */
267 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
268 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
269 err = got_error(GOT_ERR_BAD_DELTA);
270 goto done;
272 /* Copy 'xxxx...' from derived file. */
273 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
274 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
275 err = got_error(GOT_ERR_BAD_DELTA);
276 goto done;
278 /* Copy 'ccccc...' from base file. */
279 if (!(deltas[2].copy == 1 &&
280 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
281 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
282 err = got_error(GOT_ERR_BAD_DELTA);
283 goto done;
286 done:
287 got_deltify_free(dt);
288 fclose(base_file);
289 fclose(result_file);
290 return (err == NULL);
293 static int
294 deltify_abc_axc_mem_mem(void)
296 const struct got_error *err = NULL;
297 size_t i;
298 FILE *result_file;
299 uint8_t base_file[3 * GOT_DELTIFY_MAXCHUNK];
300 uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
301 struct got_delta_table *dt;
302 struct got_delta_instruction *deltas;
303 int ndeltas;
304 int have_nblocks = 0;
306 result_file = got_opentemp();
307 if (result_file == NULL)
308 return 1;
310 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
311 base_file[i] = 'a';
312 derived_file[i] = 'a';
314 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
315 base_file[GOT_DELTIFY_MAXCHUNK + i] = 'b';
316 derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
318 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
319 base_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
320 derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
323 err = got_deltify_init_mem(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
324 if (err)
325 goto done;
327 for (i = 0; i < dt->nalloc; i++) {
328 if (dt->blocks[i].len > 0)
329 have_nblocks++;
331 if (have_nblocks != dt->nblocks) {
332 err = got_error(GOT_ERR_BAD_DELTA);
333 goto done;
336 err = got_deltify_mem_mem(&deltas, &ndeltas, derived_file, 0,
337 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
338 3 * GOT_DELTIFY_MAXCHUNK);
339 if (err)
340 goto done;
342 if (ndeltas != 3) {
343 err = got_error(GOT_ERR_BAD_DELTA);
344 goto done;
346 /* Copy 'aaaa...' from base file. */
347 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
348 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
349 err = got_error(GOT_ERR_BAD_DELTA);
350 goto done;
352 /* Copy 'xxxx...' from derived file. */
353 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
354 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
355 err = got_error(GOT_ERR_BAD_DELTA);
356 goto done;
358 /* Copy 'ccccc...' from base file. */
359 if (!(deltas[2].copy == 1 &&
360 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
361 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
362 err = got_error(GOT_ERR_BAD_DELTA);
363 goto done;
366 done:
367 got_deltify_free(dt);
368 fclose(result_file);
369 return (err == NULL);
372 static int quiet;
374 #define RUN_TEST(expr, name) \
375 { test_ok = (expr); \
376 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
377 failure = (failure || !test_ok); }
379 static void
380 usage(void)
382 fprintf(stderr, "usage: delta_test [-q]\n");
385 int
386 main(int argc, char *argv[])
388 int test_ok;
389 int failure = 0;
390 int ch;
392 while ((ch = getopt(argc, argv, "q")) != -1) {
393 switch (ch) {
394 case 'q':
395 quiet = 1;
396 break;
397 default:
398 usage();
399 return 1;
403 argc -= optind;
404 argv += optind;
406 if (argc != 0) {
407 usage();
408 return 1;
411 #ifndef PROFILE
412 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
413 err(1, "pledge");
414 #endif
415 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
416 err(1, "unveil");
418 if (unveil(NULL, NULL) != 0)
419 err(1, "unveil");
421 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
422 RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
423 RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
424 RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
426 return failure ? 1 : 0;