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 <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <err.h>
23 #include <unistd.h>
24 #include <getopt.h>
26 #include "got_error.h"
27 #include "got_opentemp.h"
29 #include "got_lib_deltify.h"
31 #ifndef nitems
32 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
33 #endif
35 static int
36 deltify_abc_axc(void)
37 {
38 const struct got_error *err = NULL;
39 size_t i;
40 FILE *base_file, *derived_file, *result_file;
41 struct got_delta_table *dt;
42 struct got_delta_instruction *deltas;
43 int ndeltas;
44 int have_nblocks = 0;
46 base_file = got_opentemp();
47 if (base_file == NULL)
48 return 1;
50 derived_file = got_opentemp();
51 if (derived_file == NULL)
52 return 1;
54 result_file = got_opentemp();
55 if (result_file == NULL)
56 return 1;
58 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
59 fputc('a', base_file);
60 fputc('a', derived_file);
61 }
62 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 fputc('b', base_file);
64 fputc('x', derived_file);
65 }
66 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 fputc('c', base_file);
68 fputc('c', derived_file);
69 }
71 rewind(base_file);
72 rewind(derived_file);
74 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
75 if (err)
76 goto done;
78 for (i = 0; i < dt->nalloc; i++) {
79 if (dt->blocks[i].len > 0)
80 have_nblocks++;
81 }
82 if (have_nblocks != dt->nblocks) {
83 err = got_error(GOT_ERR_BAD_DELTA);
84 goto done;
85 }
87 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
88 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
89 3 * GOT_DELTIFY_MAXCHUNK);
90 if (err)
91 goto done;
93 if (ndeltas != 3) {
94 err = got_error(GOT_ERR_BAD_DELTA);
95 goto done;
96 }
97 /* Copy 'aaaa...' from base file. */
98 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
99 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
100 err = got_error(GOT_ERR_BAD_DELTA);
101 goto done;
103 /* Copy 'xxxx...' from derived file. */
104 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
105 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
106 err = got_error(GOT_ERR_BAD_DELTA);
107 goto done;
109 /* Copy 'ccccc...' from base file. */
110 if (!(deltas[2].copy == 1 &&
111 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
112 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
113 err = got_error(GOT_ERR_BAD_DELTA);
114 goto done;
117 done:
118 got_deltify_free(dt);
119 fclose(base_file);
120 fclose(derived_file);
121 fclose(result_file);
122 return (err == NULL);
125 static int
126 deltify_abc_axc_file_mem(void)
128 const struct got_error *err = NULL;
129 size_t i;
130 uint8_t base_data[3 * GOT_DELTIFY_MAXCHUNK];
131 FILE *derived_file, *result_file;
132 struct got_delta_table *dt;
133 struct got_delta_instruction *deltas;
134 int ndeltas;
135 int have_nblocks = 0;
137 derived_file = got_opentemp();
138 if (derived_file == NULL)
139 return 1;
141 result_file = got_opentemp();
142 if (result_file == NULL)
143 return 1;
145 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
146 base_data[i] = 'a';
147 fputc('a', derived_file);
149 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
150 base_data[GOT_DELTIFY_MAXCHUNK + i] = 'b';
151 fputc('x', derived_file);
153 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
154 base_data[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
155 fputc('c', derived_file);
158 rewind(derived_file);
160 err = got_deltify_init_mem(&dt, base_data, 0, 3 * GOT_DELTIFY_MAXCHUNK);
161 if (err)
162 goto done;
164 for (i = 0; i < dt->nalloc; i++) {
165 if (dt->blocks[i].len > 0)
166 have_nblocks++;
168 if (have_nblocks != dt->nblocks) {
169 err = got_error(GOT_ERR_BAD_DELTA);
170 goto done;
173 err = got_deltify_file_mem(&deltas, &ndeltas, derived_file, 0,
174 3 * GOT_DELTIFY_MAXCHUNK, dt, base_data, 0,
175 3 * GOT_DELTIFY_MAXCHUNK);
176 if (err)
177 goto done;
179 if (ndeltas != 3) {
180 err = got_error(GOT_ERR_BAD_DELTA);
181 goto done;
183 /* Copy 'aaaa...' from base file. */
184 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
185 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
186 err = got_error(GOT_ERR_BAD_DELTA);
187 goto done;
189 /* Copy 'xxxx...' from derived file. */
190 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
191 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
192 err = got_error(GOT_ERR_BAD_DELTA);
193 goto done;
195 /* Copy 'ccccc...' from base file. */
196 if (!(deltas[2].copy == 1 &&
197 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
198 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
199 err = got_error(GOT_ERR_BAD_DELTA);
200 goto done;
203 done:
204 got_deltify_free(dt);
205 fclose(derived_file);
206 fclose(result_file);
207 return (err == NULL);
210 static int
211 deltify_abc_axc_mem_file(void)
213 const struct got_error *err = NULL;
214 size_t i;
215 FILE *base_file, *result_file;
216 uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
217 struct got_delta_table *dt;
218 struct got_delta_instruction *deltas;
219 int ndeltas;
220 int have_nblocks = 0;
222 base_file = got_opentemp();
223 if (base_file == NULL)
224 return 1;
226 result_file = got_opentemp();
227 if (result_file == NULL)
228 return 1;
230 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
231 fputc('a', base_file);
232 derived_file[i] = 'a';
234 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
235 fputc('b', base_file);
236 derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
238 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
239 fputc('c', base_file);
240 derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
243 rewind(base_file);
245 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
246 if (err)
247 goto done;
249 for (i = 0; i < dt->nalloc; i++) {
250 if (dt->blocks[i].len > 0)
251 have_nblocks++;
253 if (have_nblocks != dt->nblocks) {
254 err = got_error(GOT_ERR_BAD_DELTA);
255 goto done;
258 err = got_deltify_mem_file(&deltas, &ndeltas, derived_file, 0,
259 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
260 3 * GOT_DELTIFY_MAXCHUNK);
261 if (err)
262 goto done;
264 if (ndeltas != 3) {
265 err = got_error(GOT_ERR_BAD_DELTA);
266 goto done;
268 /* Copy 'aaaa...' from base file. */
269 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
270 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
271 err = got_error(GOT_ERR_BAD_DELTA);
272 goto done;
274 /* Copy 'xxxx...' from derived file. */
275 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
276 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
277 err = got_error(GOT_ERR_BAD_DELTA);
278 goto done;
280 /* Copy 'ccccc...' from base file. */
281 if (!(deltas[2].copy == 1 &&
282 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
283 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
284 err = got_error(GOT_ERR_BAD_DELTA);
285 goto done;
288 done:
289 got_deltify_free(dt);
290 fclose(base_file);
291 fclose(result_file);
292 return (err == NULL);
295 static int
296 deltify_abc_axc_mem_mem(void)
298 const struct got_error *err = NULL;
299 size_t i;
300 FILE *result_file;
301 uint8_t base_file[3 * GOT_DELTIFY_MAXCHUNK];
302 uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
303 struct got_delta_table *dt;
304 struct got_delta_instruction *deltas;
305 int ndeltas;
306 int have_nblocks = 0;
308 result_file = got_opentemp();
309 if (result_file == NULL)
310 return 1;
312 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
313 base_file[i] = 'a';
314 derived_file[i] = 'a';
316 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
317 base_file[GOT_DELTIFY_MAXCHUNK + i] = 'b';
318 derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
320 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
321 base_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
322 derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
325 err = got_deltify_init_mem(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
326 if (err)
327 goto done;
329 for (i = 0; i < dt->nalloc; i++) {
330 if (dt->blocks[i].len > 0)
331 have_nblocks++;
333 if (have_nblocks != dt->nblocks) {
334 err = got_error(GOT_ERR_BAD_DELTA);
335 goto done;
338 err = got_deltify_mem_mem(&deltas, &ndeltas, derived_file, 0,
339 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
340 3 * GOT_DELTIFY_MAXCHUNK);
341 if (err)
342 goto done;
344 if (ndeltas != 3) {
345 err = got_error(GOT_ERR_BAD_DELTA);
346 goto done;
348 /* Copy 'aaaa...' from base file. */
349 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
350 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
351 err = got_error(GOT_ERR_BAD_DELTA);
352 goto done;
354 /* Copy 'xxxx...' from derived file. */
355 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
356 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
357 err = got_error(GOT_ERR_BAD_DELTA);
358 goto done;
360 /* Copy 'ccccc...' from base file. */
361 if (!(deltas[2].copy == 1 &&
362 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
363 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
364 err = got_error(GOT_ERR_BAD_DELTA);
365 goto done;
368 done:
369 got_deltify_free(dt);
370 fclose(result_file);
371 return (err == NULL);
374 static int quiet;
376 #define RUN_TEST(expr, name) \
377 { test_ok = (expr); \
378 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
379 failure = (failure || !test_ok); }
381 static void
382 usage(void)
384 fprintf(stderr, "usage: delta_test [-q]\n");
387 int
388 main(int argc, char *argv[])
390 int test_ok;
391 int failure = 0;
392 int ch;
394 while ((ch = getopt(argc, argv, "q")) != -1) {
395 switch (ch) {
396 case 'q':
397 quiet = 1;
398 break;
399 default:
400 usage();
401 return 1;
405 argc -= optind;
406 argv += optind;
408 if (argc != 0) {
409 usage();
410 return 1;
413 #ifndef PROFILE
414 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
415 err(1, "pledge");
416 #endif
417 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
418 err(1, "unveil");
420 if (unveil(NULL, NULL) != 0)
421 err(1, "unveil");
423 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
424 RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
425 RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
426 RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
428 return failure ? 1 : 0;