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, 0,
89 3 * GOT_DELTIFY_MAXCHUNK);
90 if (err)
91 goto done;
93 if (ndeltas != 3) {
94 err = got_error(GOT_ERR_BAD_DELTA);
95 goto done;
96 }
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);
101 goto done;
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);
107 goto done;
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);
114 goto done;
117 done:
118 got_deltify_free(dt);
119 fclose(base_file);
120 fclose(derived_file);
121 fclose(result_file);
122 return (err == NULL);
125 static int quiet;
127 #define RUN_TEST(expr, name) \
128 { test_ok = (expr); \
129 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
130 failure = (failure || !test_ok); }
132 static void
133 usage(void)
135 fprintf(stderr, "usage: delta_test [-q]\n");
138 int
139 main(int argc, char *argv[])
141 int test_ok;
142 int failure = 0;
143 int ch;
145 while ((ch = getopt(argc, argv, "q")) != -1) {
146 switch (ch) {
147 case 'q':
148 quiet = 1;
149 break;
150 default:
151 usage();
152 return 1;
156 argc -= optind;
157 argv += optind;
159 if (argc != 0) {
160 usage();
161 return 1;
164 #ifndef PROFILE
165 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
166 err(1, "pledge");
167 #endif
168 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
169 err(1, "unveil");
171 if (unveil(NULL, NULL) != 0)
172 err(1, "unveil");
174 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
176 return failure ? 1 : 0;