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 "got_compat.h"
19 #include <sys/queue.h>
27 #include "got_error.h"
28 #include "got_opentemp.h"
30 #include "got_lib_deltify.h"
33 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 const struct got_error
*err
= NULL
;
41 FILE *base_file
, *derived_file
, *result_file
;
42 struct got_delta_table
*dt
;
43 struct got_delta_instruction
*deltas
;
50 base_file
= got_opentemp();
51 if (base_file
== NULL
)
54 derived_file
= got_opentemp();
55 if (derived_file
== NULL
)
58 result_file
= got_opentemp();
59 if (result_file
== NULL
)
62 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
63 fputc('a', base_file
);
64 fputc('a', derived_file
);
66 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
67 fputc('b', base_file
);
68 fputc('x', derived_file
);
70 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
71 fputc('c', base_file
);
72 fputc('c', derived_file
);
78 err
= got_deltify_init(&dt
, base_file
, 0, 3 * GOT_DELTIFY_MAXCHUNK
,
83 for (i
= 0; i
< dt
->nalloc
; i
++) {
84 if (dt
->blocks
[i
].len
> 0)
87 if (have_nblocks
!= dt
->nblocks
) {
88 err
= got_error(GOT_ERR_BAD_DELTA
);
92 err
= got_deltify(&deltas
, &ndeltas
, derived_file
, 0,
93 3 * GOT_DELTIFY_MAXCHUNK
, seed
, dt
, base_file
, 0,
94 3 * GOT_DELTIFY_MAXCHUNK
);
99 err
= got_error(GOT_ERR_BAD_DELTA
);
102 /* Copy 'aaaa...' from base file. */
103 if (!(deltas
[0].copy
== 1 && deltas
[0].offset
== 0 &&
104 deltas
[0].len
== GOT_DELTIFY_MAXCHUNK
)) {
105 err
= got_error(GOT_ERR_BAD_DELTA
);
108 /* Copy 'xxxx...' from derived file. */
109 if (!(deltas
[1].copy
== 0 && deltas
[1].offset
== GOT_DELTIFY_MAXCHUNK
&&
110 deltas
[1].len
== GOT_DELTIFY_MAXCHUNK
)) {
111 err
= got_error(GOT_ERR_BAD_DELTA
);
114 /* Copy 'ccccc...' from base file. */
115 if (!(deltas
[2].copy
== 1 &&
116 deltas
[2].offset
== 2 * GOT_DELTIFY_MAXCHUNK
&&
117 deltas
[2].len
== GOT_DELTIFY_MAXCHUNK
)) {
118 err
= got_error(GOT_ERR_BAD_DELTA
);
123 got_deltify_free(dt
);
125 fclose(derived_file
);
127 return (err
== NULL
);
131 deltify_abc_axc_file_mem(void)
133 const struct got_error
*err
= NULL
;
135 uint8_t base_data
[3 * GOT_DELTIFY_MAXCHUNK
];
136 FILE *derived_file
, *result_file
;
137 struct got_delta_table
*dt
;
138 struct got_delta_instruction
*deltas
;
140 int have_nblocks
= 0;
145 derived_file
= got_opentemp();
146 if (derived_file
== NULL
)
149 result_file
= got_opentemp();
150 if (result_file
== NULL
)
153 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
155 fputc('a', derived_file
);
157 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
158 base_data
[GOT_DELTIFY_MAXCHUNK
+ i
] = 'b';
159 fputc('x', derived_file
);
161 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
162 base_data
[2 * GOT_DELTIFY_MAXCHUNK
+ i
] = 'c';
163 fputc('c', derived_file
);
166 rewind(derived_file
);
168 err
= got_deltify_init_mem(&dt
, base_data
, 0, 3 * GOT_DELTIFY_MAXCHUNK
,
173 for (i
= 0; i
< dt
->nalloc
; i
++) {
174 if (dt
->blocks
[i
].len
> 0)
177 if (have_nblocks
!= dt
->nblocks
) {
178 err
= got_error(GOT_ERR_BAD_DELTA
);
182 err
= got_deltify_file_mem(&deltas
, &ndeltas
, derived_file
, 0,
183 3 * GOT_DELTIFY_MAXCHUNK
, seed
, dt
, base_data
, 0,
184 3 * GOT_DELTIFY_MAXCHUNK
);
189 err
= got_error(GOT_ERR_BAD_DELTA
);
192 /* Copy 'aaaa...' from base file. */
193 if (!(deltas
[0].copy
== 1 && deltas
[0].offset
== 0 &&
194 deltas
[0].len
== GOT_DELTIFY_MAXCHUNK
)) {
195 err
= got_error(GOT_ERR_BAD_DELTA
);
198 /* Copy 'xxxx...' from derived file. */
199 if (!(deltas
[1].copy
== 0 && deltas
[1].offset
== GOT_DELTIFY_MAXCHUNK
&&
200 deltas
[1].len
== GOT_DELTIFY_MAXCHUNK
)) {
201 err
= got_error(GOT_ERR_BAD_DELTA
);
204 /* Copy 'ccccc...' from base file. */
205 if (!(deltas
[2].copy
== 1 &&
206 deltas
[2].offset
== 2 * GOT_DELTIFY_MAXCHUNK
&&
207 deltas
[2].len
== GOT_DELTIFY_MAXCHUNK
)) {
208 err
= got_error(GOT_ERR_BAD_DELTA
);
213 got_deltify_free(dt
);
214 fclose(derived_file
);
216 return (err
== NULL
);
220 deltify_abc_axc_mem_file(void)
222 const struct got_error
*err
= NULL
;
224 FILE *base_file
, *result_file
;
225 uint8_t derived_file
[3 * GOT_DELTIFY_MAXCHUNK
];
226 struct got_delta_table
*dt
;
227 struct got_delta_instruction
*deltas
;
229 int have_nblocks
= 0;
234 base_file
= got_opentemp();
235 if (base_file
== NULL
)
238 result_file
= got_opentemp();
239 if (result_file
== NULL
)
242 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
243 fputc('a', base_file
);
244 derived_file
[i
] = 'a';
246 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
247 fputc('b', base_file
);
248 derived_file
[GOT_DELTIFY_MAXCHUNK
+ i
] = 'x';
250 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
251 fputc('c', base_file
);
252 derived_file
[2 * GOT_DELTIFY_MAXCHUNK
+ i
] = 'c';
257 err
= got_deltify_init(&dt
, base_file
, 0, 3 * GOT_DELTIFY_MAXCHUNK
,
262 for (i
= 0; i
< dt
->nalloc
; i
++) {
263 if (dt
->blocks
[i
].len
> 0)
266 if (have_nblocks
!= dt
->nblocks
) {
267 err
= got_error(GOT_ERR_BAD_DELTA
);
271 err
= got_deltify_mem_file(&deltas
, &ndeltas
, derived_file
, 0,
272 3 * GOT_DELTIFY_MAXCHUNK
, seed
, dt
, base_file
, 0,
273 3 * GOT_DELTIFY_MAXCHUNK
);
278 err
= got_error(GOT_ERR_BAD_DELTA
);
281 /* Copy 'aaaa...' from base file. */
282 if (!(deltas
[0].copy
== 1 && deltas
[0].offset
== 0 &&
283 deltas
[0].len
== GOT_DELTIFY_MAXCHUNK
)) {
284 err
= got_error(GOT_ERR_BAD_DELTA
);
287 /* Copy 'xxxx...' from derived file. */
288 if (!(deltas
[1].copy
== 0 && deltas
[1].offset
== GOT_DELTIFY_MAXCHUNK
&&
289 deltas
[1].len
== GOT_DELTIFY_MAXCHUNK
)) {
290 err
= got_error(GOT_ERR_BAD_DELTA
);
293 /* Copy 'ccccc...' from base file. */
294 if (!(deltas
[2].copy
== 1 &&
295 deltas
[2].offset
== 2 * GOT_DELTIFY_MAXCHUNK
&&
296 deltas
[2].len
== GOT_DELTIFY_MAXCHUNK
)) {
297 err
= got_error(GOT_ERR_BAD_DELTA
);
302 got_deltify_free(dt
);
305 return (err
== NULL
);
309 deltify_abc_axc_mem_mem(void)
311 const struct got_error
*err
= NULL
;
314 uint8_t base_file
[3 * GOT_DELTIFY_MAXCHUNK
];
315 uint8_t derived_file
[3 * GOT_DELTIFY_MAXCHUNK
];
316 struct got_delta_table
*dt
;
317 struct got_delta_instruction
*deltas
;
319 int have_nblocks
= 0;
324 result_file
= got_opentemp();
325 if (result_file
== NULL
)
328 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
330 derived_file
[i
] = 'a';
332 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
333 base_file
[GOT_DELTIFY_MAXCHUNK
+ i
] = 'b';
334 derived_file
[GOT_DELTIFY_MAXCHUNK
+ i
] = 'x';
336 for (i
= 0; i
< GOT_DELTIFY_MAXCHUNK
; i
++) {
337 base_file
[2 * GOT_DELTIFY_MAXCHUNK
+ i
] = 'c';
338 derived_file
[2 * GOT_DELTIFY_MAXCHUNK
+ i
] = 'c';
341 err
= got_deltify_init_mem(&dt
, base_file
, 0, 3 * GOT_DELTIFY_MAXCHUNK
,
346 for (i
= 0; i
< dt
->nalloc
; i
++) {
347 if (dt
->blocks
[i
].len
> 0)
350 if (have_nblocks
!= dt
->nblocks
) {
351 err
= got_error(GOT_ERR_BAD_DELTA
);
355 err
= got_deltify_mem_mem(&deltas
, &ndeltas
, derived_file
, 0,
356 3 * GOT_DELTIFY_MAXCHUNK
, seed
, dt
, base_file
, 0,
357 3 * GOT_DELTIFY_MAXCHUNK
);
362 err
= got_error(GOT_ERR_BAD_DELTA
);
365 /* Copy 'aaaa...' from base file. */
366 if (!(deltas
[0].copy
== 1 && deltas
[0].offset
== 0 &&
367 deltas
[0].len
== GOT_DELTIFY_MAXCHUNK
)) {
368 err
= got_error(GOT_ERR_BAD_DELTA
);
371 /* Copy 'xxxx...' from derived file. */
372 if (!(deltas
[1].copy
== 0 && deltas
[1].offset
== GOT_DELTIFY_MAXCHUNK
&&
373 deltas
[1].len
== GOT_DELTIFY_MAXCHUNK
)) {
374 err
= got_error(GOT_ERR_BAD_DELTA
);
377 /* Copy 'ccccc...' from base file. */
378 if (!(deltas
[2].copy
== 1 &&
379 deltas
[2].offset
== 2 * GOT_DELTIFY_MAXCHUNK
&&
380 deltas
[2].len
== GOT_DELTIFY_MAXCHUNK
)) {
381 err
= got_error(GOT_ERR_BAD_DELTA
);
386 got_deltify_free(dt
);
388 return (err
== NULL
);
393 #define RUN_TEST(expr, name) \
394 { test_ok = (expr); \
395 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
396 failure = (failure || !test_ok); }
401 fprintf(stderr
, "usage: delta_test [-q]\n");
405 main(int argc
, char *argv
[])
411 while ((ch
= getopt(argc
, argv
, "q")) != -1) {
431 if (pledge("stdio rpath wpath cpath unveil", NULL
) == -1)
434 if (unveil(GOT_TMPDIR_STR
, "rwc") != 0)
437 if (unveil(NULL
, NULL
) != 0)
440 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
441 RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
442 RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
443 RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
445 return failure
? 1 : 0;