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.
17 #include <sys/queue.h>
26 #include "got_error.h"
27 #include "got_opentemp.h"
29 #include "got_lib_deltify.h"
32 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 const struct got_error *err = NULL;
40 FILE *base_file, *derived_file, *result_file;
41 struct got_delta_table *dt;
42 struct got_delta_instruction *deltas;
46 base_file = got_opentemp();
47 if (base_file == NULL)
50 derived_file = got_opentemp();
51 if (derived_file == NULL)
54 result_file = got_opentemp();
55 if (result_file == NULL)
58 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
59 fputc('a', base_file);
60 fputc('a', derived_file);
62 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 fputc('b', base_file);
64 fputc('x', derived_file);
66 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 fputc('c', base_file);
68 fputc('c', derived_file);
74 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
78 for (i = 0; i < dt->nalloc; i++) {
79 if (dt->blocks[i].len > 0)
82 if (have_nblocks != dt->nblocks) {
83 err = got_error(GOT_ERR_BAD_DELTA);
87 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
88 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
89 3 * GOT_DELTIFY_MAXCHUNK);
94 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
118 got_deltify_free(dt);
120 fclose(derived_file);
122 return (err == NULL);
126 deltify_abc_axc_file_mem(void)
128 const struct got_error *err = NULL;
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;
135 int have_nblocks = 0;
137 derived_file = got_opentemp();
138 if (derived_file == NULL)
141 result_file = got_opentemp();
142 if (result_file == NULL)
145 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
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);
164 for (i = 0; i < dt->nalloc; i++) {
165 if (dt->blocks[i].len > 0)
168 if (have_nblocks != dt->nblocks) {
169 err = got_error(GOT_ERR_BAD_DELTA);
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);
180 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
204 got_deltify_free(dt);
205 fclose(derived_file);
207 return (err == NULL);
211 deltify_abc_axc_mem_file(void)
213 const struct got_error *err = NULL;
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;
220 int have_nblocks = 0;
222 base_file = got_opentemp();
223 if (base_file == NULL)
226 result_file = got_opentemp();
227 if (result_file == NULL)
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';
245 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
249 for (i = 0; i < dt->nalloc; i++) {
250 if (dt->blocks[i].len > 0)
253 if (have_nblocks != dt->nblocks) {
254 err = got_error(GOT_ERR_BAD_DELTA);
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);
265 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
289 got_deltify_free(dt);
292 return (err == NULL);
296 deltify_abc_axc_mem_mem(void)
298 const struct got_error *err = NULL;
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;
306 int have_nblocks = 0;
308 result_file = got_opentemp();
309 if (result_file == NULL)
312 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
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);
329 for (i = 0; i < dt->nalloc; i++) {
330 if (dt->blocks[i].len > 0)
333 if (have_nblocks != dt->nblocks) {
334 err = got_error(GOT_ERR_BAD_DELTA);
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);
345 err = got_error(GOT_ERR_BAD_DELTA);
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);
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);
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);
369 got_deltify_free(dt);
371 return (err == NULL);
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); }
384 fprintf(stderr, "usage: delta_test [-q]\n");
388 main(int argc, char *argv[])
394 while ((ch = getopt(argc, argv, "q")) != -1) {
414 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
417 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
420 if (unveil(NULL, NULL) != 0)
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;