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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 ac25a292 2018-01-26 stsp #include <stdio.h>
20 ac25a292 2018-01-26 stsp #include <stdlib.h>
21 885d3e02 2018-01-27 stsp #include <string.h>
22 f8352b2a 2018-03-12 stsp #include <err.h>
23 f8352b2a 2018-03-12 stsp #include <unistd.h>
24 7fb414ae 2020-08-08 stsp #include <getopt.h>
26 ac25a292 2018-01-26 stsp #include "got_error.h"
27 511a516b 2018-05-19 stsp #include "got_opentemp.h"
28 324d37e7 2019-05-11 stsp #include "got_path.h"
30 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
32 885d3e02 2018-01-27 stsp #ifndef nitems
33 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 fa8129f7 2022-03-22 thomas const struct delta_test {
37 885d3e02 2018-01-27 stsp const char *base;
38 89817b30 2018-11-11 stsp size_t base_len;
39 885d3e02 2018-01-27 stsp const char *delta;
40 885d3e02 2018-01-27 stsp size_t delta_len;
41 885d3e02 2018-01-27 stsp const char *expected;
42 89817b30 2018-11-11 stsp size_t result_len;
43 885d3e02 2018-01-27 stsp } delta_tests[] = {
44 885d3e02 2018-01-27 stsp /* base len 0, target len 4, append 4 'x' */
45 89817b30 2018-11-11 stsp { "", 0, "\x00\x04\x04xxxx", 7, "xxxx", 4 },
46 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 0 from base, append 4 'x' */
47 89817b30 2018-11-11 stsp { "aabbccdd", 8, "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx", 8 },
48 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 4 from base, append 4 'x' */
49 89817b30 2018-11-11 stsp { "aabbccdd", 8, "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx", 8 },
50 89817b30 2018-11-11 stsp /* git 48fb7deb5 Fix big left-shifts of unsigned char, 2009-06-17) */
51 89817b30 2018-11-11 stsp { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
52 53509745 2018-11-11 stsp 16, "\x10\x10\xff\xff\xff\xff\xff\x10\00\00", 10 , NULL, 0 },
53 53509745 2018-11-11 stsp /* libgit2 9844d38be delta: fix out-of-bounds read of delta */
54 53509745 2018-11-11 stsp { "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
55 53509745 2018-11-11 stsp 16, "\x10\x70\xff", 3, NULL, 0}
59 2ff12563 2018-09-15 stsp delta_apply(void)
61 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
63 885d3e02 2018-01-27 stsp FILE *result_file;
65 885d3e02 2018-01-27 stsp result_file = got_opentemp();
66 885d3e02 2018-01-27 stsp if (result_file == NULL)
69 885d3e02 2018-01-27 stsp for (i = 0; i < nitems(delta_tests); i++) {
70 fa8129f7 2022-03-22 thomas const struct delta_test *dt = &delta_tests[i];
71 885d3e02 2018-01-27 stsp FILE *base_file;
72 885d3e02 2018-01-27 stsp char buf[1024];
73 89817b30 2018-11-11 stsp size_t n, result_len;
75 f8aea23e 2018-02-11 stsp base_file = got_opentemp();
76 f8aea23e 2018-02-11 stsp if (base_file == NULL) {
77 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
81 89817b30 2018-11-11 stsp n = fwrite(dt->base, 1, dt->base_len, base_file);
82 89817b30 2018-11-11 stsp if (n != dt->base_len) {
83 f8aea23e 2018-02-11 stsp err = got_ferror(base_file, GOT_ERR_IO);
86 f8aea23e 2018-02-11 stsp rewind(base_file);
88 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, dt->delta, dt->delta_len,
89 89817b30 2018-11-11 stsp result_file, &result_len);
90 56b63ca4 2021-01-22 stsp if (fclose(base_file) == EOF && err == NULL)
91 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
92 89817b30 2018-11-11 stsp if (dt->expected == NULL) {
93 89817b30 2018-11-11 stsp /* Invalid delta, expect an error. */
94 89817b30 2018-11-11 stsp if (err == NULL)
95 89817b30 2018-11-11 stsp err = got_error(GOT_ERR_EXPECTED);
96 89817b30 2018-11-11 stsp else if (err->code == GOT_ERR_BAD_DELTA)
101 89817b30 2018-11-11 stsp if (result_len != dt->result_len) {
102 89817b30 2018-11-11 stsp err = got_ferror(result_file,
103 89817b30 2018-11-11 stsp GOT_ERR_BAD_DELTA);
106 89817b30 2018-11-11 stsp n = fread(buf, result_len, 1, result_file);
107 89817b30 2018-11-11 stsp if (n != 1 ||
108 89817b30 2018-11-11 stsp strncmp(buf, dt->expected, result_len) != 0) {
109 89817b30 2018-11-11 stsp err = got_ferror(result_file,
110 89817b30 2018-11-11 stsp GOT_ERR_BAD_DELTA);
114 885d3e02 2018-01-27 stsp rewind(result_file);
117 885d3e02 2018-01-27 stsp fclose(result_file);
118 885d3e02 2018-01-27 stsp return (err == NULL);
121 7fb414ae 2020-08-08 stsp static int quiet;
123 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
124 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
125 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
126 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
128 7fb414ae 2020-08-08 stsp static void
129 7fb414ae 2020-08-08 stsp usage(void)
131 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: delta_test [-q]\n");
135 7fb414ae 2020-08-08 stsp main(int argc, char *argv[])
137 b08fe7be 2018-01-26 stsp int test_ok;
138 ac25a292 2018-01-26 stsp int failure = 0;
141 7fb414ae 2020-08-08 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
142 7fb414ae 2020-08-08 stsp switch (ch) {
152 7fb414ae 2020-08-08 stsp argc -= optind;
153 7fb414ae 2020-08-08 stsp argv += optind;
155 7fb414ae 2020-08-08 stsp if (argc != 0) {
160 2ff12563 2018-09-15 stsp #ifndef PROFILE
161 c89f2770 2019-01-04 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
162 f8352b2a 2018-03-12 stsp err(1, "pledge");
164 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
165 c89f2770 2019-01-04 stsp err(1, "unveil");
167 c89f2770 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
168 c89f2770 2019-01-04 stsp err(1, "unveil");
170 6df54056 2018-03-03 stsp RUN_TEST(delta_apply(), "delta_apply");
172 ac25a292 2018-01-26 stsp return failure ? 1 : 0;