Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <err.h>
23 #include <unistd.h>
24 #include <getopt.h>
26 #include "got_error.h"
27 #include "got_opentemp.h"
29 #include "got_lib_deltify.h"
31 #ifndef nitems
32 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
33 #endif
35 static int
36 deltify_abc_axc(void)
37 {
38 const struct got_error *err = NULL;
39 size_t i;
40 FILE *base_file, *derived_file, *result_file;
41 struct got_delta_table *dt;
42 struct got_delta_instruction *deltas;
43 int ndeltas;
44 int have_nblocks = 0;
46 base_file = got_opentemp();
47 if (base_file == NULL)
48 return 1;
50 derived_file = got_opentemp();
51 if (derived_file == NULL)
52 return 1;
54 result_file = got_opentemp();
55 if (result_file == NULL)
56 return 1;
58 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
59 fputc('a', base_file);
60 fputc('a', derived_file);
61 }
62 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
63 fputc('b', base_file);
64 fputc('x', derived_file);
65 }
66 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
67 fputc('c', base_file);
68 fputc('c', derived_file);
69 }
71 rewind(base_file);
72 rewind(derived_file);
74 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
75 if (err)
76 goto done;
78 for (i = 0; i < dt->nalloc; i++) {
79 if (dt->blocks[i].len > 0)
80 have_nblocks++;
81 }
82 if (have_nblocks != dt->nblocks) {
83 err = got_error(GOT_ERR_BAD_DELTA);
84 goto done;
85 }
87 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
88 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 3 * GOT_DELTIFY_MAXCHUNK);
89 if (err)
90 goto done;
92 if (ndeltas != 3) {
93 err = got_error(GOT_ERR_BAD_DELTA);
94 goto done;
95 }
96 /* Copy 'aaaa...' from base file. */
97 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
98 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
99 err = got_error(GOT_ERR_BAD_DELTA);
100 goto done;
102 /* Copy 'xxxx...' from derived file. */
103 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
104 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
105 err = got_error(GOT_ERR_BAD_DELTA);
106 goto done;
108 /* Copy 'ccccc...' from base file. */
109 if (!(deltas[2].copy == 1 &&
110 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
111 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
112 err = got_error(GOT_ERR_BAD_DELTA);
113 goto done;
116 done:
117 got_deltify_free(dt);
118 fclose(base_file);
119 fclose(derived_file);
120 fclose(result_file);
121 return (err == NULL);
124 static int quiet;
126 #define RUN_TEST(expr, name) \
127 { test_ok = (expr); \
128 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
129 failure = (failure || !test_ok); }
131 static void
132 usage(void)
134 fprintf(stderr, "usage: delta_test [-q]\n");
137 int
138 main(int argc, char *argv[])
140 int test_ok;
141 int failure = 0;
142 int ch;
144 while ((ch = getopt(argc, argv, "q")) != -1) {
145 switch (ch) {
146 case 'q':
147 quiet = 1;
148 break;
149 default:
150 usage();
151 return 1;
155 argc -= optind;
156 argv += optind;
158 if (argc != 0) {
159 usage();
160 return 1;
163 #ifndef PROFILE
164 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
165 err(1, "pledge");
166 #endif
167 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
168 err(1, "unveil");
170 if (unveil(NULL, NULL) != 0)
171 err(1, "unveil");
173 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
175 return failure ? 1 : 0;