2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
24 #include "got_error.h"
25 #include "got_opentemp.h"
27 #include "got_lib_deltify.h"
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 const struct got_error *err = NULL;
38 FILE *base_file, *derived_file, *result_file;
39 struct got_delta_table *dt;
40 struct got_delta_instruction *deltas;
44 base_file = got_opentemp();
45 if (base_file == NULL)
48 derived_file = got_opentemp();
49 if (derived_file == NULL)
52 result_file = got_opentemp();
53 if (result_file == NULL)
56 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
57 fputc('a', base_file);
58 fputc('a', derived_file);
60 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
61 fputc('b', base_file);
62 fputc('x', derived_file);
64 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
65 fputc('c', base_file);
66 fputc('c', derived_file);
72 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
76 for (i = 0; i < dt->nalloc; i++) {
77 if (dt->blocks[i].len > 0)
80 if (have_nblocks != dt->nblocks) {
81 err = got_error(GOT_ERR_BAD_DELTA);
85 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
86 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
87 3 * GOT_DELTIFY_MAXCHUNK);
92 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
116 got_deltify_free(dt);
118 fclose(derived_file);
120 return (err == NULL);
124 deltify_abc_axc_file_mem(void)
126 const struct got_error *err = NULL;
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;
133 int have_nblocks = 0;
135 derived_file = got_opentemp();
136 if (derived_file == NULL)
139 result_file = got_opentemp();
140 if (result_file == NULL)
143 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
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);
162 for (i = 0; i < dt->nalloc; i++) {
163 if (dt->blocks[i].len > 0)
166 if (have_nblocks != dt->nblocks) {
167 err = got_error(GOT_ERR_BAD_DELTA);
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);
178 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
202 got_deltify_free(dt);
203 fclose(derived_file);
205 return (err == NULL);
209 deltify_abc_axc_mem_file(void)
211 const struct got_error *err = NULL;
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;
218 int have_nblocks = 0;
220 base_file = got_opentemp();
221 if (base_file == NULL)
224 result_file = got_opentemp();
225 if (result_file == NULL)
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';
243 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
247 for (i = 0; i < dt->nalloc; i++) {
248 if (dt->blocks[i].len > 0)
251 if (have_nblocks != dt->nblocks) {
252 err = got_error(GOT_ERR_BAD_DELTA);
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);
263 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
287 got_deltify_free(dt);
290 return (err == NULL);
294 deltify_abc_axc_mem_mem(void)
296 const struct got_error *err = NULL;
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;
304 int have_nblocks = 0;
306 result_file = got_opentemp();
307 if (result_file == NULL)
310 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
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);
327 for (i = 0; i < dt->nalloc; i++) {
328 if (dt->blocks[i].len > 0)
331 if (have_nblocks != dt->nblocks) {
332 err = got_error(GOT_ERR_BAD_DELTA);
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);
343 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
367 got_deltify_free(dt);
369 return (err == NULL);
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); }
382 fprintf(stderr, "usage: delta_test [-q]\n");
386 main(int argc, char *argv[])
392 while ((ch = getopt(argc, argv, "q")) != -1) {
412 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
415 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
418 if (unveil(NULL, NULL) != 0)
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;