2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 ac25a292 2018-01-26 stsp * Permission to use, copy, modify, and distribute this software for any
5 ac25a292 2018-01-26 stsp * purpose with or without fee is hereby granted, provided that the above
6 ac25a292 2018-01-26 stsp * copyright notice and this permission notice appear in all copies.
8 ac25a292 2018-01-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ac25a292 2018-01-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ac25a292 2018-01-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ac25a292 2018-01-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ac25a292 2018-01-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ac25a292 2018-01-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ac25a292 2018-01-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 09e05ed5 2023-03-08 thomas #include "got_compat.h"
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 ac25a292 2018-01-26 stsp #include <stdio.h>
22 ac25a292 2018-01-26 stsp #include <stdlib.h>
23 885d3e02 2018-01-27 stsp #include <string.h>
24 f8352b2a 2018-03-12 stsp #include <err.h>
25 f8352b2a 2018-03-12 stsp #include <unistd.h>
26 7fb414ae 2020-08-08 stsp #include <getopt.h>
28 ac25a292 2018-01-26 stsp #include "got_error.h"
29 511a516b 2018-05-19 stsp #include "got_opentemp.h"
30 324d37e7 2019-05-11 stsp #include "got_path.h"
32 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
34 885d3e02 2018-01-27 stsp #ifndef nitems
35 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
38 fa8129f7 2022-03-22 thomas const struct delta_test {
39 885d3e02 2018-01-27 stsp const char *base;
40 89817b30 2018-11-11 stsp size_t base_len;
41 885d3e02 2018-01-27 stsp const char *delta;
42 885d3e02 2018-01-27 stsp size_t delta_len;
43 885d3e02 2018-01-27 stsp const char *expected;
44 89817b30 2018-11-11 stsp size_t result_len;
45 885d3e02 2018-01-27 stsp } delta_tests[] = {
46 885d3e02 2018-01-27 stsp /* base len 0, target len 4, append 4 'x' */
47 89817b30 2018-11-11 stsp { "", 0, "\x00\x04\x04xxxx", 7, "xxxx", 4 },
48 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 0 from base, append 4 'x' */
49 89817b30 2018-11-11 stsp { "aabbccdd", 8, "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx", 8 },
50 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 4 from base, append 4 'x' */
51 89817b30 2018-11-11 stsp { "aabbccdd", 8, "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx", 8 },
52 89817b30 2018-11-11 stsp /* git 48fb7deb5 Fix big left-shifts of unsigned char, 2009-06-17) */
53 89817b30 2018-11-11 stsp { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
54 53509745 2018-11-11 stsp 16, "\x10\x10\xff\xff\xff\xff\xff\x10\00\00", 10 , NULL, 0 },
55 53509745 2018-11-11 stsp /* libgit2 9844d38be delta: fix out-of-bounds read of delta */
56 53509745 2018-11-11 stsp { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
57 53509745 2018-11-11 stsp 16, "\x10\x70\xff", 3, NULL, 0}
61 2ff12563 2018-09-15 stsp delta_apply(void)
63 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
65 885d3e02 2018-01-27 stsp FILE *result_file;
67 885d3e02 2018-01-27 stsp result_file = got_opentemp();
68 885d3e02 2018-01-27 stsp if (result_file == NULL)
71 885d3e02 2018-01-27 stsp for (i = 0; i < nitems(delta_tests); i++) {
72 fa8129f7 2022-03-22 thomas const struct delta_test *dt = &delta_tests[i];
73 885d3e02 2018-01-27 stsp FILE *base_file;
74 885d3e02 2018-01-27 stsp char buf[1024];
75 89817b30 2018-11-11 stsp size_t n, result_len;
77 f8aea23e 2018-02-11 stsp base_file = got_opentemp();
78 f8aea23e 2018-02-11 stsp if (base_file == NULL) {
79 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
83 89817b30 2018-11-11 stsp n = fwrite(dt->base, 1, dt->base_len, base_file);
84 89817b30 2018-11-11 stsp if (n != dt->base_len) {
85 f8aea23e 2018-02-11 stsp err = got_ferror(base_file, GOT_ERR_IO);
88 f8aea23e 2018-02-11 stsp rewind(base_file);
90 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, dt->delta, dt->delta_len,
91 89817b30 2018-11-11 stsp result_file, &result_len);
92 56b63ca4 2021-01-22 stsp if (fclose(base_file) == EOF && err == NULL)
93 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
94 89817b30 2018-11-11 stsp if (dt->expected == NULL) {
95 89817b30 2018-11-11 stsp /* Invalid delta, expect an error. */
96 89817b30 2018-11-11 stsp if (err == NULL)
97 89817b30 2018-11-11 stsp err = got_error(GOT_ERR_EXPECTED);
98 89817b30 2018-11-11 stsp else if (err->code == GOT_ERR_BAD_DELTA)
103 89817b30 2018-11-11 stsp if (result_len != dt->result_len) {
104 89817b30 2018-11-11 stsp err = got_ferror(result_file,
105 89817b30 2018-11-11 stsp GOT_ERR_BAD_DELTA);
108 89817b30 2018-11-11 stsp n = fread(buf, result_len, 1, result_file);
109 89817b30 2018-11-11 stsp if (n != 1 ||
110 89817b30 2018-11-11 stsp strncmp(buf, dt->expected, result_len) != 0) {
111 89817b30 2018-11-11 stsp err = got_ferror(result_file,
112 89817b30 2018-11-11 stsp GOT_ERR_BAD_DELTA);
116 885d3e02 2018-01-27 stsp rewind(result_file);
119 885d3e02 2018-01-27 stsp fclose(result_file);
120 885d3e02 2018-01-27 stsp return (err == NULL);
123 7fb414ae 2020-08-08 stsp static int quiet;
125 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
126 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
127 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
128 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
130 7fb414ae 2020-08-08 stsp static void
131 7fb414ae 2020-08-08 stsp usage(void)
133 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: delta_test [-q]\n");
137 7fb414ae 2020-08-08 stsp main(int argc, char *argv[])
139 b08fe7be 2018-01-26 stsp int test_ok;
140 ac25a292 2018-01-26 stsp int failure = 0;
143 7fb414ae 2020-08-08 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
144 7fb414ae 2020-08-08 stsp switch (ch) {
154 7fb414ae 2020-08-08 stsp argc -= optind;
155 7fb414ae 2020-08-08 stsp argv += optind;
157 7fb414ae 2020-08-08 stsp if (argc != 0) {
162 2ff12563 2018-09-15 stsp #ifndef PROFILE
163 c89f2770 2019-01-04 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
164 f8352b2a 2018-03-12 stsp err(1, "pledge");
166 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
167 c89f2770 2019-01-04 stsp err(1, "unveil");
169 c89f2770 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
170 c89f2770 2019-01-04 stsp err(1, "unveil");
172 6df54056 2018-03-03 stsp RUN_TEST(delta_apply(), "delta_apply");
174 ac25a292 2018-01-26 stsp return failure ? 1 : 0;