Blame


1 8704c7ce 2021-03-10 stsp /*
2 8704c7ce 2021-03-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 8704c7ce 2021-03-10 stsp *
4 8704c7ce 2021-03-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 8704c7ce 2021-03-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 8704c7ce 2021-03-10 stsp * copyright notice and this permission notice appear in all copies.
7 8704c7ce 2021-03-10 stsp *
8 8704c7ce 2021-03-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8704c7ce 2021-03-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8704c7ce 2021-03-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8704c7ce 2021-03-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8704c7ce 2021-03-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8704c7ce 2021-03-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8704c7ce 2021-03-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 8704c7ce 2021-03-10 stsp */
16 8704c7ce 2021-03-10 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 8704c7ce 2021-03-10 stsp #include <stdio.h>
19 8704c7ce 2021-03-10 stsp #include <stdlib.h>
20 8704c7ce 2021-03-10 stsp #include <string.h>
21 8704c7ce 2021-03-10 stsp #include <err.h>
22 8704c7ce 2021-03-10 stsp #include <unistd.h>
23 8704c7ce 2021-03-10 stsp #include <getopt.h>
24 8704c7ce 2021-03-10 stsp
25 8704c7ce 2021-03-10 stsp #include "got_error.h"
26 8704c7ce 2021-03-10 stsp #include "got_opentemp.h"
27 8704c7ce 2021-03-10 stsp
28 8704c7ce 2021-03-10 stsp #include "got_lib_deltify.h"
29 8704c7ce 2021-03-10 stsp
30 8704c7ce 2021-03-10 stsp #ifndef nitems
31 8704c7ce 2021-03-10 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
32 8704c7ce 2021-03-10 stsp #endif
33 8704c7ce 2021-03-10 stsp
34 8704c7ce 2021-03-10 stsp static int
35 8704c7ce 2021-03-10 stsp deltify_abc_axc(void)
36 8704c7ce 2021-03-10 stsp {
37 8704c7ce 2021-03-10 stsp const struct got_error *err = NULL;
38 8704c7ce 2021-03-10 stsp size_t i;
39 8704c7ce 2021-03-10 stsp FILE *base_file, *derived_file, *result_file;
40 8704c7ce 2021-03-10 stsp struct got_delta_table *dt;
41 8704c7ce 2021-03-10 stsp struct got_delta_instruction *deltas;
42 8704c7ce 2021-03-10 stsp int ndeltas;
43 8704c7ce 2021-03-10 stsp int have_nblocks = 0;
44 cc524d36 2022-05-31 thomas uint32_t seed;
45 8704c7ce 2021-03-10 stsp
46 cc524d36 2022-05-31 thomas seed = arc4random();
47 cc524d36 2022-05-31 thomas
48 8704c7ce 2021-03-10 stsp base_file = got_opentemp();
49 8704c7ce 2021-03-10 stsp if (base_file == NULL)
50 8704c7ce 2021-03-10 stsp return 1;
51 8704c7ce 2021-03-10 stsp
52 8704c7ce 2021-03-10 stsp derived_file = got_opentemp();
53 8704c7ce 2021-03-10 stsp if (derived_file == NULL)
54 8704c7ce 2021-03-10 stsp return 1;
55 8704c7ce 2021-03-10 stsp
56 8704c7ce 2021-03-10 stsp result_file = got_opentemp();
57 8704c7ce 2021-03-10 stsp if (result_file == NULL)
58 8704c7ce 2021-03-10 stsp return 1;
59 8704c7ce 2021-03-10 stsp
60 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
61 8704c7ce 2021-03-10 stsp fputc('a', base_file);
62 8704c7ce 2021-03-10 stsp fputc('a', derived_file);
63 8704c7ce 2021-03-10 stsp }
64 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
65 8704c7ce 2021-03-10 stsp fputc('b', base_file);
66 8704c7ce 2021-03-10 stsp fputc('x', derived_file);
67 8704c7ce 2021-03-10 stsp }
68 8704c7ce 2021-03-10 stsp for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
69 8704c7ce 2021-03-10 stsp fputc('c', base_file);
70 8704c7ce 2021-03-10 stsp fputc('c', derived_file);
71 8704c7ce 2021-03-10 stsp }
72 8704c7ce 2021-03-10 stsp
73 8704c7ce 2021-03-10 stsp rewind(base_file);
74 8704c7ce 2021-03-10 stsp rewind(derived_file);
75 8704c7ce 2021-03-10 stsp
76 cc524d36 2022-05-31 thomas err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
77 cc524d36 2022-05-31 thomas seed);
78 8704c7ce 2021-03-10 stsp if (err)
79 8704c7ce 2021-03-10 stsp goto done;
80 8704c7ce 2021-03-10 stsp
81 8704c7ce 2021-03-10 stsp for (i = 0; i < dt->nalloc; i++) {
82 8704c7ce 2021-03-10 stsp if (dt->blocks[i].len > 0)
83 8704c7ce 2021-03-10 stsp have_nblocks++;
84 8704c7ce 2021-03-10 stsp }
85 8704c7ce 2021-03-10 stsp if (have_nblocks != dt->nblocks) {
86 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
87 8704c7ce 2021-03-10 stsp goto done;
88 8704c7ce 2021-03-10 stsp }
89 8704c7ce 2021-03-10 stsp
90 8704c7ce 2021-03-10 stsp err = got_deltify(&deltas, &ndeltas, derived_file, 0,
91 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
92 f34b169e 2021-06-18 stsp 3 * GOT_DELTIFY_MAXCHUNK);
93 8704c7ce 2021-03-10 stsp if (err)
94 8704c7ce 2021-03-10 stsp goto done;
95 8704c7ce 2021-03-10 stsp
96 8704c7ce 2021-03-10 stsp if (ndeltas != 3) {
97 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
98 8704c7ce 2021-03-10 stsp goto done;
99 8704c7ce 2021-03-10 stsp }
100 8704c7ce 2021-03-10 stsp /* Copy 'aaaa...' from base file. */
101 8704c7ce 2021-03-10 stsp if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
102 8704c7ce 2021-03-10 stsp deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
103 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
104 8704c7ce 2021-03-10 stsp goto done;
105 8704c7ce 2021-03-10 stsp }
106 8704c7ce 2021-03-10 stsp /* Copy 'xxxx...' from derived file. */
107 8704c7ce 2021-03-10 stsp if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
108 8704c7ce 2021-03-10 stsp deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
109 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
110 8704c7ce 2021-03-10 stsp goto done;
111 8704c7ce 2021-03-10 stsp }
112 8704c7ce 2021-03-10 stsp /* Copy 'ccccc...' from base file. */
113 8704c7ce 2021-03-10 stsp if (!(deltas[2].copy == 1 &&
114 8704c7ce 2021-03-10 stsp deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
115 8704c7ce 2021-03-10 stsp deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
116 8704c7ce 2021-03-10 stsp err = got_error(GOT_ERR_BAD_DELTA);
117 8704c7ce 2021-03-10 stsp goto done;
118 8704c7ce 2021-03-10 stsp }
119 8704c7ce 2021-03-10 stsp
120 8704c7ce 2021-03-10 stsp done:
121 8704c7ce 2021-03-10 stsp got_deltify_free(dt);
122 8704c7ce 2021-03-10 stsp fclose(base_file);
123 8704c7ce 2021-03-10 stsp fclose(derived_file);
124 2b0ae357 2022-01-10 thomas fclose(result_file);
125 2b0ae357 2022-01-10 thomas return (err == NULL);
126 2b0ae357 2022-01-10 thomas }
127 2b0ae357 2022-01-10 thomas
128 2b0ae357 2022-01-10 thomas static int
129 2b0ae357 2022-01-10 thomas deltify_abc_axc_file_mem(void)
130 2b0ae357 2022-01-10 thomas {
131 2b0ae357 2022-01-10 thomas const struct got_error *err = NULL;
132 2b0ae357 2022-01-10 thomas size_t i;
133 2b0ae357 2022-01-10 thomas uint8_t base_data[3 * GOT_DELTIFY_MAXCHUNK];
134 2b0ae357 2022-01-10 thomas FILE *derived_file, *result_file;
135 2b0ae357 2022-01-10 thomas struct got_delta_table *dt;
136 2b0ae357 2022-01-10 thomas struct got_delta_instruction *deltas;
137 2b0ae357 2022-01-10 thomas int ndeltas;
138 2b0ae357 2022-01-10 thomas int have_nblocks = 0;
139 cc524d36 2022-05-31 thomas uint32_t seed;
140 2b0ae357 2022-01-10 thomas
141 cc524d36 2022-05-31 thomas seed = arc4random();
142 cc524d36 2022-05-31 thomas
143 2b0ae357 2022-01-10 thomas derived_file = got_opentemp();
144 2b0ae357 2022-01-10 thomas if (derived_file == NULL)
145 2b0ae357 2022-01-10 thomas return 1;
146 2b0ae357 2022-01-10 thomas
147 2b0ae357 2022-01-10 thomas result_file = got_opentemp();
148 2b0ae357 2022-01-10 thomas if (result_file == NULL)
149 2b0ae357 2022-01-10 thomas return 1;
150 2b0ae357 2022-01-10 thomas
151 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
152 2b0ae357 2022-01-10 thomas base_data[i] = 'a';
153 2b0ae357 2022-01-10 thomas fputc('a', derived_file);
154 2b0ae357 2022-01-10 thomas }
155 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
156 2b0ae357 2022-01-10 thomas base_data[GOT_DELTIFY_MAXCHUNK + i] = 'b';
157 2b0ae357 2022-01-10 thomas fputc('x', derived_file);
158 2b0ae357 2022-01-10 thomas }
159 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
160 2b0ae357 2022-01-10 thomas base_data[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
161 2b0ae357 2022-01-10 thomas fputc('c', derived_file);
162 2b0ae357 2022-01-10 thomas }
163 2b0ae357 2022-01-10 thomas
164 2b0ae357 2022-01-10 thomas rewind(derived_file);
165 2b0ae357 2022-01-10 thomas
166 cc524d36 2022-05-31 thomas err = got_deltify_init_mem(&dt, base_data, 0, 3 * GOT_DELTIFY_MAXCHUNK,
167 cc524d36 2022-05-31 thomas seed);
168 2b0ae357 2022-01-10 thomas if (err)
169 2b0ae357 2022-01-10 thomas goto done;
170 2b0ae357 2022-01-10 thomas
171 2b0ae357 2022-01-10 thomas for (i = 0; i < dt->nalloc; i++) {
172 2b0ae357 2022-01-10 thomas if (dt->blocks[i].len > 0)
173 2b0ae357 2022-01-10 thomas have_nblocks++;
174 2b0ae357 2022-01-10 thomas }
175 2b0ae357 2022-01-10 thomas if (have_nblocks != dt->nblocks) {
176 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
177 2b0ae357 2022-01-10 thomas goto done;
178 2b0ae357 2022-01-10 thomas }
179 2b0ae357 2022-01-10 thomas
180 2b0ae357 2022-01-10 thomas err = got_deltify_file_mem(&deltas, &ndeltas, derived_file, 0,
181 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_data, 0,
182 2b0ae357 2022-01-10 thomas 3 * GOT_DELTIFY_MAXCHUNK);
183 2b0ae357 2022-01-10 thomas if (err)
184 2b0ae357 2022-01-10 thomas goto done;
185 2b0ae357 2022-01-10 thomas
186 2b0ae357 2022-01-10 thomas if (ndeltas != 3) {
187 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
188 2b0ae357 2022-01-10 thomas goto done;
189 2b0ae357 2022-01-10 thomas }
190 2b0ae357 2022-01-10 thomas /* Copy 'aaaa...' from base file. */
191 2b0ae357 2022-01-10 thomas if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
192 2b0ae357 2022-01-10 thomas deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
193 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
194 2b0ae357 2022-01-10 thomas goto done;
195 2b0ae357 2022-01-10 thomas }
196 2b0ae357 2022-01-10 thomas /* Copy 'xxxx...' from derived file. */
197 2b0ae357 2022-01-10 thomas if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
198 2b0ae357 2022-01-10 thomas deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
199 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
200 2b0ae357 2022-01-10 thomas goto done;
201 2b0ae357 2022-01-10 thomas }
202 2b0ae357 2022-01-10 thomas /* Copy 'ccccc...' from base file. */
203 2b0ae357 2022-01-10 thomas if (!(deltas[2].copy == 1 &&
204 2b0ae357 2022-01-10 thomas deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
205 2b0ae357 2022-01-10 thomas deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
206 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
207 2b0ae357 2022-01-10 thomas goto done;
208 2b0ae357 2022-01-10 thomas }
209 2b0ae357 2022-01-10 thomas
210 2b0ae357 2022-01-10 thomas done:
211 2b0ae357 2022-01-10 thomas got_deltify_free(dt);
212 2b0ae357 2022-01-10 thomas fclose(derived_file);
213 2b0ae357 2022-01-10 thomas fclose(result_file);
214 2b0ae357 2022-01-10 thomas return (err == NULL);
215 2b0ae357 2022-01-10 thomas }
216 2b0ae357 2022-01-10 thomas
217 2b0ae357 2022-01-10 thomas static int
218 2b0ae357 2022-01-10 thomas deltify_abc_axc_mem_file(void)
219 2b0ae357 2022-01-10 thomas {
220 2b0ae357 2022-01-10 thomas const struct got_error *err = NULL;
221 2b0ae357 2022-01-10 thomas size_t i;
222 2b0ae357 2022-01-10 thomas FILE *base_file, *result_file;
223 2b0ae357 2022-01-10 thomas uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
224 2b0ae357 2022-01-10 thomas struct got_delta_table *dt;
225 2b0ae357 2022-01-10 thomas struct got_delta_instruction *deltas;
226 2b0ae357 2022-01-10 thomas int ndeltas;
227 2b0ae357 2022-01-10 thomas int have_nblocks = 0;
228 cc524d36 2022-05-31 thomas uint32_t seed;
229 2b0ae357 2022-01-10 thomas
230 cc524d36 2022-05-31 thomas seed = arc4random();
231 cc524d36 2022-05-31 thomas
232 2b0ae357 2022-01-10 thomas base_file = got_opentemp();
233 2b0ae357 2022-01-10 thomas if (base_file == NULL)
234 2b0ae357 2022-01-10 thomas return 1;
235 2b0ae357 2022-01-10 thomas
236 2b0ae357 2022-01-10 thomas result_file = got_opentemp();
237 2b0ae357 2022-01-10 thomas if (result_file == NULL)
238 2b0ae357 2022-01-10 thomas return 1;
239 2b0ae357 2022-01-10 thomas
240 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
241 2b0ae357 2022-01-10 thomas fputc('a', base_file);
242 2b0ae357 2022-01-10 thomas derived_file[i] = 'a';
243 2b0ae357 2022-01-10 thomas }
244 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
245 2b0ae357 2022-01-10 thomas fputc('b', base_file);
246 2b0ae357 2022-01-10 thomas derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
247 2b0ae357 2022-01-10 thomas }
248 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
249 2b0ae357 2022-01-10 thomas fputc('c', base_file);
250 2b0ae357 2022-01-10 thomas derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
251 2b0ae357 2022-01-10 thomas }
252 2b0ae357 2022-01-10 thomas
253 2b0ae357 2022-01-10 thomas rewind(base_file);
254 2b0ae357 2022-01-10 thomas
255 cc524d36 2022-05-31 thomas err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
256 cc524d36 2022-05-31 thomas seed);
257 2b0ae357 2022-01-10 thomas if (err)
258 2b0ae357 2022-01-10 thomas goto done;
259 2b0ae357 2022-01-10 thomas
260 2b0ae357 2022-01-10 thomas for (i = 0; i < dt->nalloc; i++) {
261 2b0ae357 2022-01-10 thomas if (dt->blocks[i].len > 0)
262 2b0ae357 2022-01-10 thomas have_nblocks++;
263 2b0ae357 2022-01-10 thomas }
264 2b0ae357 2022-01-10 thomas if (have_nblocks != dt->nblocks) {
265 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
266 2b0ae357 2022-01-10 thomas goto done;
267 2b0ae357 2022-01-10 thomas }
268 2b0ae357 2022-01-10 thomas
269 2b0ae357 2022-01-10 thomas err = got_deltify_mem_file(&deltas, &ndeltas, derived_file, 0,
270 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
271 2b0ae357 2022-01-10 thomas 3 * GOT_DELTIFY_MAXCHUNK);
272 2b0ae357 2022-01-10 thomas if (err)
273 2b0ae357 2022-01-10 thomas goto done;
274 2b0ae357 2022-01-10 thomas
275 2b0ae357 2022-01-10 thomas if (ndeltas != 3) {
276 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
277 2b0ae357 2022-01-10 thomas goto done;
278 2b0ae357 2022-01-10 thomas }
279 2b0ae357 2022-01-10 thomas /* Copy 'aaaa...' from base file. */
280 2b0ae357 2022-01-10 thomas if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
281 2b0ae357 2022-01-10 thomas deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
282 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
283 2b0ae357 2022-01-10 thomas goto done;
284 2b0ae357 2022-01-10 thomas }
285 2b0ae357 2022-01-10 thomas /* Copy 'xxxx...' from derived file. */
286 2b0ae357 2022-01-10 thomas if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
287 2b0ae357 2022-01-10 thomas deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
288 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
289 2b0ae357 2022-01-10 thomas goto done;
290 2b0ae357 2022-01-10 thomas }
291 2b0ae357 2022-01-10 thomas /* Copy 'ccccc...' from base file. */
292 2b0ae357 2022-01-10 thomas if (!(deltas[2].copy == 1 &&
293 2b0ae357 2022-01-10 thomas deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
294 2b0ae357 2022-01-10 thomas deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
295 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
296 2b0ae357 2022-01-10 thomas goto done;
297 2b0ae357 2022-01-10 thomas }
298 2b0ae357 2022-01-10 thomas
299 2b0ae357 2022-01-10 thomas done:
300 2b0ae357 2022-01-10 thomas got_deltify_free(dt);
301 2b0ae357 2022-01-10 thomas fclose(base_file);
302 8704c7ce 2021-03-10 stsp fclose(result_file);
303 8704c7ce 2021-03-10 stsp return (err == NULL);
304 8704c7ce 2021-03-10 stsp }
305 8704c7ce 2021-03-10 stsp
306 2b0ae357 2022-01-10 thomas static int
307 2b0ae357 2022-01-10 thomas deltify_abc_axc_mem_mem(void)
308 2b0ae357 2022-01-10 thomas {
309 2b0ae357 2022-01-10 thomas const struct got_error *err = NULL;
310 2b0ae357 2022-01-10 thomas size_t i;
311 2b0ae357 2022-01-10 thomas FILE *result_file;
312 2b0ae357 2022-01-10 thomas uint8_t base_file[3 * GOT_DELTIFY_MAXCHUNK];
313 2b0ae357 2022-01-10 thomas uint8_t derived_file[3 * GOT_DELTIFY_MAXCHUNK];
314 2b0ae357 2022-01-10 thomas struct got_delta_table *dt;
315 2b0ae357 2022-01-10 thomas struct got_delta_instruction *deltas;
316 2b0ae357 2022-01-10 thomas int ndeltas;
317 2b0ae357 2022-01-10 thomas int have_nblocks = 0;
318 cc524d36 2022-05-31 thomas uint32_t seed;
319 2b0ae357 2022-01-10 thomas
320 cc524d36 2022-05-31 thomas seed = arc4random();
321 cc524d36 2022-05-31 thomas
322 2b0ae357 2022-01-10 thomas result_file = got_opentemp();
323 2b0ae357 2022-01-10 thomas if (result_file == NULL)
324 2b0ae357 2022-01-10 thomas return 1;
325 2b0ae357 2022-01-10 thomas
326 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
327 2b0ae357 2022-01-10 thomas base_file[i] = 'a';
328 2b0ae357 2022-01-10 thomas derived_file[i] = 'a';
329 2b0ae357 2022-01-10 thomas }
330 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
331 2b0ae357 2022-01-10 thomas base_file[GOT_DELTIFY_MAXCHUNK + i] = 'b';
332 2b0ae357 2022-01-10 thomas derived_file[GOT_DELTIFY_MAXCHUNK + i] = 'x';
333 2b0ae357 2022-01-10 thomas }
334 2b0ae357 2022-01-10 thomas for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
335 2b0ae357 2022-01-10 thomas base_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
336 2b0ae357 2022-01-10 thomas derived_file[2 * GOT_DELTIFY_MAXCHUNK + i] = 'c';
337 2b0ae357 2022-01-10 thomas }
338 2b0ae357 2022-01-10 thomas
339 cc524d36 2022-05-31 thomas err = got_deltify_init_mem(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK,
340 cc524d36 2022-05-31 thomas seed);
341 2b0ae357 2022-01-10 thomas if (err)
342 2b0ae357 2022-01-10 thomas goto done;
343 2b0ae357 2022-01-10 thomas
344 2b0ae357 2022-01-10 thomas for (i = 0; i < dt->nalloc; i++) {
345 2b0ae357 2022-01-10 thomas if (dt->blocks[i].len > 0)
346 2b0ae357 2022-01-10 thomas have_nblocks++;
347 2b0ae357 2022-01-10 thomas }
348 2b0ae357 2022-01-10 thomas if (have_nblocks != dt->nblocks) {
349 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
350 2b0ae357 2022-01-10 thomas goto done;
351 2b0ae357 2022-01-10 thomas }
352 2b0ae357 2022-01-10 thomas
353 2b0ae357 2022-01-10 thomas err = got_deltify_mem_mem(&deltas, &ndeltas, derived_file, 0,
354 cc524d36 2022-05-31 thomas 3 * GOT_DELTIFY_MAXCHUNK, seed, dt, base_file, 0,
355 2b0ae357 2022-01-10 thomas 3 * GOT_DELTIFY_MAXCHUNK);
356 2b0ae357 2022-01-10 thomas if (err)
357 2b0ae357 2022-01-10 thomas goto done;
358 2b0ae357 2022-01-10 thomas
359 2b0ae357 2022-01-10 thomas if (ndeltas != 3) {
360 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
361 2b0ae357 2022-01-10 thomas goto done;
362 2b0ae357 2022-01-10 thomas }
363 2b0ae357 2022-01-10 thomas /* Copy 'aaaa...' from base file. */
364 2b0ae357 2022-01-10 thomas if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
365 2b0ae357 2022-01-10 thomas deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
366 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
367 2b0ae357 2022-01-10 thomas goto done;
368 2b0ae357 2022-01-10 thomas }
369 2b0ae357 2022-01-10 thomas /* Copy 'xxxx...' from derived file. */
370 2b0ae357 2022-01-10 thomas if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
371 2b0ae357 2022-01-10 thomas deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
372 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
373 2b0ae357 2022-01-10 thomas goto done;
374 2b0ae357 2022-01-10 thomas }
375 2b0ae357 2022-01-10 thomas /* Copy 'ccccc...' from base file. */
376 2b0ae357 2022-01-10 thomas if (!(deltas[2].copy == 1 &&
377 2b0ae357 2022-01-10 thomas deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
378 2b0ae357 2022-01-10 thomas deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
379 2b0ae357 2022-01-10 thomas err = got_error(GOT_ERR_BAD_DELTA);
380 2b0ae357 2022-01-10 thomas goto done;
381 2b0ae357 2022-01-10 thomas }
382 2b0ae357 2022-01-10 thomas
383 2b0ae357 2022-01-10 thomas done:
384 2b0ae357 2022-01-10 thomas got_deltify_free(dt);
385 2b0ae357 2022-01-10 thomas fclose(result_file);
386 2b0ae357 2022-01-10 thomas return (err == NULL);
387 2b0ae357 2022-01-10 thomas }
388 2b0ae357 2022-01-10 thomas
389 8704c7ce 2021-03-10 stsp static int quiet;
390 8704c7ce 2021-03-10 stsp
391 8704c7ce 2021-03-10 stsp #define RUN_TEST(expr, name) \
392 8704c7ce 2021-03-10 stsp { test_ok = (expr); \
393 8704c7ce 2021-03-10 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
394 8704c7ce 2021-03-10 stsp failure = (failure || !test_ok); }
395 8704c7ce 2021-03-10 stsp
396 8704c7ce 2021-03-10 stsp static void
397 8704c7ce 2021-03-10 stsp usage(void)
398 8704c7ce 2021-03-10 stsp {
399 8704c7ce 2021-03-10 stsp fprintf(stderr, "usage: delta_test [-q]\n");
400 8704c7ce 2021-03-10 stsp }
401 8704c7ce 2021-03-10 stsp
402 8704c7ce 2021-03-10 stsp int
403 8704c7ce 2021-03-10 stsp main(int argc, char *argv[])
404 8704c7ce 2021-03-10 stsp {
405 8704c7ce 2021-03-10 stsp int test_ok;
406 8704c7ce 2021-03-10 stsp int failure = 0;
407 8704c7ce 2021-03-10 stsp int ch;
408 8704c7ce 2021-03-10 stsp
409 8704c7ce 2021-03-10 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
410 8704c7ce 2021-03-10 stsp switch (ch) {
411 8704c7ce 2021-03-10 stsp case 'q':
412 8704c7ce 2021-03-10 stsp quiet = 1;
413 8704c7ce 2021-03-10 stsp break;
414 8704c7ce 2021-03-10 stsp default:
415 8704c7ce 2021-03-10 stsp usage();
416 8704c7ce 2021-03-10 stsp return 1;
417 8704c7ce 2021-03-10 stsp }
418 8704c7ce 2021-03-10 stsp }
419 8704c7ce 2021-03-10 stsp
420 8704c7ce 2021-03-10 stsp argc -= optind;
421 8704c7ce 2021-03-10 stsp argv += optind;
422 8704c7ce 2021-03-10 stsp
423 8704c7ce 2021-03-10 stsp if (argc != 0) {
424 8704c7ce 2021-03-10 stsp usage();
425 8704c7ce 2021-03-10 stsp return 1;
426 8704c7ce 2021-03-10 stsp }
427 8704c7ce 2021-03-10 stsp
428 8704c7ce 2021-03-10 stsp #ifndef PROFILE
429 8704c7ce 2021-03-10 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
430 8704c7ce 2021-03-10 stsp err(1, "pledge");
431 8704c7ce 2021-03-10 stsp #endif
432 8704c7ce 2021-03-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
433 8704c7ce 2021-03-10 stsp err(1, "unveil");
434 8704c7ce 2021-03-10 stsp
435 8704c7ce 2021-03-10 stsp if (unveil(NULL, NULL) != 0)
436 8704c7ce 2021-03-10 stsp err(1, "unveil");
437 8704c7ce 2021-03-10 stsp
438 8704c7ce 2021-03-10 stsp RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
439 2b0ae357 2022-01-10 thomas RUN_TEST(deltify_abc_axc_file_mem(), "deltify_abc_axc_file_mem");
440 2b0ae357 2022-01-10 thomas RUN_TEST(deltify_abc_axc_mem_file(), "deltify_abc_axc_mem_file");
441 2b0ae357 2022-01-10 thomas RUN_TEST(deltify_abc_axc_mem_mem(), "deltify_abc_axc_mem_mem");
442 8704c7ce 2021-03-10 stsp
443 8704c7ce 2021-03-10 stsp return failure ? 1 : 0;
444 8704c7ce 2021-03-10 stsp }