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