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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <err.h>
21 #include <unistd.h>
22 #include <getopt.h>
24 #include "got_error.h"
25 #include "got_opentemp.h"
27 #include "got_lib_deltify.h"
29 #ifndef nitems
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
31 #endif
33 static int
34 deltify_abc_axc(void)
35 {
36 const struct got_error *err = NULL;
37 size_t i;
38 FILE *base_file, *derived_file, *result_file;
39 struct got_delta_table *dt;
40 struct got_delta_instruction *deltas;
41 int ndeltas;
42 int have_nblocks = 0;
44 base_file = got_opentemp();
45 if (base_file == NULL)
46 return 1;
48 derived_file = got_opentemp();
49 if (derived_file == NULL)
50 return 1;
52 result_file = got_opentemp();
53 if (result_file == NULL)
54 return 1;
56 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
57 fputc('a', base_file);
58 fputc('a', derived_file);
59 }
60 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
61 fputc('b', base_file);
62 fputc('x', derived_file);
63 }
64 for (i = 0; i < GOT_DELTIFY_MAXCHUNK; i++) {
65 fputc('c', base_file);
66 fputc('c', derived_file);
67 }
69 rewind(base_file);
70 rewind(derived_file);
72 err = got_deltify_init(&dt, base_file, 0, 3 * GOT_DELTIFY_MAXCHUNK);
73 if (err)
74 goto done;
76 for (i = 0; i < dt->nalloc; i++) {
77 if (dt->blocks[i].len > 0)
78 have_nblocks++;
79 }
80 if (have_nblocks != dt->nblocks) {
81 err = got_error(GOT_ERR_BAD_DELTA);
82 goto done;
83 }
85 err = got_deltify(&deltas, &ndeltas, derived_file, 0,
86 3 * GOT_DELTIFY_MAXCHUNK, dt, base_file, 0,
87 3 * GOT_DELTIFY_MAXCHUNK);
88 if (err)
89 goto done;
91 if (ndeltas != 3) {
92 err = got_error(GOT_ERR_BAD_DELTA);
93 goto done;
94 }
95 /* Copy 'aaaa...' from base file. */
96 if (!(deltas[0].copy == 1 && deltas[0].offset == 0 &&
97 deltas[0].len == GOT_DELTIFY_MAXCHUNK)) {
98 err = got_error(GOT_ERR_BAD_DELTA);
99 goto done;
101 /* Copy 'xxxx...' from derived file. */
102 if (!(deltas[1].copy == 0 && deltas[1].offset == GOT_DELTIFY_MAXCHUNK &&
103 deltas[1].len == GOT_DELTIFY_MAXCHUNK)) {
104 err = got_error(GOT_ERR_BAD_DELTA);
105 goto done;
107 /* Copy 'ccccc...' from base file. */
108 if (!(deltas[2].copy == 1 &&
109 deltas[2].offset == 2 * GOT_DELTIFY_MAXCHUNK &&
110 deltas[2].len == GOT_DELTIFY_MAXCHUNK)) {
111 err = got_error(GOT_ERR_BAD_DELTA);
112 goto done;
115 done:
116 got_deltify_free(dt);
117 fclose(base_file);
118 fclose(derived_file);
119 fclose(result_file);
120 return (err == NULL);
123 static int quiet;
125 #define RUN_TEST(expr, name) \
126 { test_ok = (expr); \
127 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
128 failure = (failure || !test_ok); }
130 static void
131 usage(void)
133 fprintf(stderr, "usage: delta_test [-q]\n");
136 int
137 main(int argc, char *argv[])
139 int test_ok;
140 int failure = 0;
141 int ch;
143 while ((ch = getopt(argc, argv, "q")) != -1) {
144 switch (ch) {
145 case 'q':
146 quiet = 1;
147 break;
148 default:
149 usage();
150 return 1;
154 argc -= optind;
155 argv += optind;
157 if (argc != 0) {
158 usage();
159 return 1;
162 #ifndef PROFILE
163 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
164 err(1, "pledge");
165 #endif
166 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
167 err(1, "unveil");
169 if (unveil(NULL, NULL) != 0)
170 err(1, "unveil");
172 RUN_TEST(deltify_abc_axc(), "deltify_abc_axc");
174 return failure ? 1 : 0;