Blame


1 ac25a292 2018-01-26 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 ac25a292 2018-01-26 stsp *
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.
7 ac25a292 2018-01-26 stsp *
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.
15 ac25a292 2018-01-26 stsp */
16 ac25a292 2018-01-26 stsp
17 ac25a292 2018-01-26 stsp #include <sys/queue.h>
18 ac25a292 2018-01-26 stsp
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>
25 ac25a292 2018-01-26 stsp
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"
29 ac25a292 2018-01-26 stsp
30 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
31 ac25a292 2018-01-26 stsp
32 885d3e02 2018-01-27 stsp #ifndef nitems
33 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 885d3e02 2018-01-27 stsp #endif
35 885d3e02 2018-01-27 stsp
36 d58ddaf3 2022-03-17 naddy 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}
56 885d3e02 2018-01-27 stsp };
57 885d3e02 2018-01-27 stsp
58 ac25a292 2018-01-26 stsp static int
59 2ff12563 2018-09-15 stsp delta_apply(void)
60 ac25a292 2018-01-26 stsp {
61 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
62 6059809a 2020-12-17 stsp size_t i;
63 885d3e02 2018-01-27 stsp FILE *result_file;
64 885d3e02 2018-01-27 stsp
65 885d3e02 2018-01-27 stsp result_file = got_opentemp();
66 885d3e02 2018-01-27 stsp if (result_file == NULL)
67 885d3e02 2018-01-27 stsp return 1;
68 885d3e02 2018-01-27 stsp
69 885d3e02 2018-01-27 stsp for (i = 0; i < nitems(delta_tests); i++) {
70 d58ddaf3 2022-03-17 naddy 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;
74 885d3e02 2018-01-27 stsp
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");
78 885d3e02 2018-01-27 stsp break;
79 f8aea23e 2018-02-11 stsp }
80 885d3e02 2018-01-27 stsp
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);
84 f8aea23e 2018-02-11 stsp break;
85 f8aea23e 2018-02-11 stsp }
86 f8aea23e 2018-02-11 stsp rewind(base_file);
87 f8aea23e 2018-02-11 stsp
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)
97 89817b30 2018-11-11 stsp err = NULL;
98 89817b30 2018-11-11 stsp } else {
99 89817b30 2018-11-11 stsp if (err)
100 89817b30 2018-11-11 stsp break;
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);
104 89817b30 2018-11-11 stsp break;
105 89817b30 2018-11-11 stsp }
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);
111 89817b30 2018-11-11 stsp break;
112 89817b30 2018-11-11 stsp }
113 b29656e2 2018-03-16 stsp }
114 885d3e02 2018-01-27 stsp rewind(result_file);
115 885d3e02 2018-01-27 stsp }
116 885d3e02 2018-01-27 stsp
117 885d3e02 2018-01-27 stsp fclose(result_file);
118 885d3e02 2018-01-27 stsp return (err == NULL);
119 ac25a292 2018-01-26 stsp }
120 ac25a292 2018-01-26 stsp
121 7fb414ae 2020-08-08 stsp static int quiet;
122 7fb414ae 2020-08-08 stsp
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); }
127 b08fe7be 2018-01-26 stsp
128 7fb414ae 2020-08-08 stsp static void
129 7fb414ae 2020-08-08 stsp usage(void)
130 7fb414ae 2020-08-08 stsp {
131 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: delta_test [-q]\n");
132 7fb414ae 2020-08-08 stsp }
133 7fb414ae 2020-08-08 stsp
134 ac25a292 2018-01-26 stsp int
135 7fb414ae 2020-08-08 stsp main(int argc, char *argv[])
136 ac25a292 2018-01-26 stsp {
137 b08fe7be 2018-01-26 stsp int test_ok;
138 ac25a292 2018-01-26 stsp int failure = 0;
139 7fb414ae 2020-08-08 stsp int ch;
140 ac25a292 2018-01-26 stsp
141 7fb414ae 2020-08-08 stsp while ((ch = getopt(argc, argv, "q")) != -1) {
142 7fb414ae 2020-08-08 stsp switch (ch) {
143 7fb414ae 2020-08-08 stsp case 'q':
144 7fb414ae 2020-08-08 stsp quiet = 1;
145 7fb414ae 2020-08-08 stsp break;
146 7fb414ae 2020-08-08 stsp default:
147 7fb414ae 2020-08-08 stsp usage();
148 7fb414ae 2020-08-08 stsp return 1;
149 7fb414ae 2020-08-08 stsp }
150 7fb414ae 2020-08-08 stsp }
151 7fb414ae 2020-08-08 stsp
152 7fb414ae 2020-08-08 stsp argc -= optind;
153 7fb414ae 2020-08-08 stsp argv += optind;
154 7fb414ae 2020-08-08 stsp
155 7fb414ae 2020-08-08 stsp if (argc != 0) {
156 7fb414ae 2020-08-08 stsp usage();
157 ac25a292 2018-01-26 stsp return 1;
158 ac25a292 2018-01-26 stsp }
159 ac25a292 2018-01-26 stsp
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");
163 2ff12563 2018-09-15 stsp #endif
164 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
165 c89f2770 2019-01-04 stsp err(1, "unveil");
166 f8352b2a 2018-03-12 stsp
167 c89f2770 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
168 c89f2770 2019-01-04 stsp err(1, "unveil");
169 c89f2770 2019-01-04 stsp
170 6df54056 2018-03-03 stsp RUN_TEST(delta_apply(), "delta_apply");
171 ac25a292 2018-01-26 stsp
172 ac25a292 2018-01-26 stsp return failure ? 1 : 0;
173 ac25a292 2018-01-26 stsp }