Blame


1 ac25a292 2018-01-26 stsp /*
2 ac25a292 2018-01-26 stsp * Copyright (c) 2018 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 ac25a292 2018-01-26 stsp
25 ac25a292 2018-01-26 stsp #include "got_error.h"
26 511a516b 2018-05-19 stsp #include "got_opentemp.h"
27 ac25a292 2018-01-26 stsp
28 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
29 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
30 ac25a292 2018-01-26 stsp
31 885d3e02 2018-01-27 stsp #ifndef nitems
32 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
33 885d3e02 2018-01-27 stsp #endif
34 885d3e02 2018-01-27 stsp
35 885d3e02 2018-01-27 stsp struct delta_test {
36 885d3e02 2018-01-27 stsp const char *base;
37 885d3e02 2018-01-27 stsp const char *delta;
38 885d3e02 2018-01-27 stsp size_t delta_len;
39 885d3e02 2018-01-27 stsp const char *expected;
40 885d3e02 2018-01-27 stsp } delta_tests[] = {
41 885d3e02 2018-01-27 stsp /* base len 0, target len 4, append 4 'x' */
42 885d3e02 2018-01-27 stsp { "", "\x00\x04\x04xxxx", 7, "xxxx" },
43 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 0 from base, append 4 'x' */
44 885d3e02 2018-01-27 stsp { "aabbccdd", "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx" },
45 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 4 from base, append 4 'x' */
46 885d3e02 2018-01-27 stsp { "aabbccdd", "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx" },
47 885d3e02 2018-01-27 stsp };
48 885d3e02 2018-01-27 stsp
49 ac25a292 2018-01-26 stsp static int
50 6df54056 2018-03-03 stsp delta_apply()
51 ac25a292 2018-01-26 stsp {
52 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
53 885d3e02 2018-01-27 stsp int i;
54 885d3e02 2018-01-27 stsp FILE *result_file;
55 885d3e02 2018-01-27 stsp
56 885d3e02 2018-01-27 stsp result_file = got_opentemp();
57 885d3e02 2018-01-27 stsp if (result_file == NULL)
58 885d3e02 2018-01-27 stsp return 1;
59 885d3e02 2018-01-27 stsp
60 885d3e02 2018-01-27 stsp for (i = 0; i < nitems(delta_tests); i++) {
61 885d3e02 2018-01-27 stsp struct delta_test *dt = &delta_tests[i];
62 885d3e02 2018-01-27 stsp FILE *base_file;
63 885d3e02 2018-01-27 stsp char buf[1024];
64 885d3e02 2018-01-27 stsp size_t n, len, result_len;
65 885d3e02 2018-01-27 stsp
66 885d3e02 2018-01-27 stsp len = strlen(dt->base);
67 f8aea23e 2018-02-11 stsp base_file = got_opentemp();
68 f8aea23e 2018-02-11 stsp if (base_file == NULL) {
69 f8aea23e 2018-02-11 stsp err = got_error_from_errno();
70 885d3e02 2018-01-27 stsp break;
71 f8aea23e 2018-02-11 stsp }
72 885d3e02 2018-01-27 stsp
73 f8aea23e 2018-02-11 stsp n = fwrite(dt->base, 1, len, base_file);
74 f8aea23e 2018-02-11 stsp if (n != len) {
75 f8aea23e 2018-02-11 stsp err = got_ferror(base_file, GOT_ERR_IO);
76 f8aea23e 2018-02-11 stsp break;
77 f8aea23e 2018-02-11 stsp }
78 f8aea23e 2018-02-11 stsp rewind(base_file);
79 f8aea23e 2018-02-11 stsp
80 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, dt->delta, dt->delta_len,
81 b29656e2 2018-03-16 stsp result_file, &len);
82 885d3e02 2018-01-27 stsp fclose(base_file);
83 885d3e02 2018-01-27 stsp if (err)
84 885d3e02 2018-01-27 stsp break;
85 885d3e02 2018-01-27 stsp result_len = strlen(dt->expected);
86 b29656e2 2018-03-16 stsp if (result_len != len) {
87 b29656e2 2018-03-16 stsp err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
88 b29656e2 2018-03-16 stsp break;
89 b29656e2 2018-03-16 stsp }
90 885d3e02 2018-01-27 stsp n = fread(buf, result_len, 1, result_file);
91 885d3e02 2018-01-27 stsp if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
92 885d3e02 2018-01-27 stsp err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
93 885d3e02 2018-01-27 stsp break;
94 885d3e02 2018-01-27 stsp }
95 885d3e02 2018-01-27 stsp rewind(result_file);
96 885d3e02 2018-01-27 stsp }
97 885d3e02 2018-01-27 stsp
98 885d3e02 2018-01-27 stsp fclose(result_file);
99 885d3e02 2018-01-27 stsp return (err == NULL);
100 ac25a292 2018-01-26 stsp }
101 ac25a292 2018-01-26 stsp
102 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
103 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
104 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
105 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
106 b08fe7be 2018-01-26 stsp
107 ac25a292 2018-01-26 stsp int
108 ac25a292 2018-01-26 stsp main(int argc, const char *argv[])
109 ac25a292 2018-01-26 stsp {
110 b08fe7be 2018-01-26 stsp int test_ok;
111 ac25a292 2018-01-26 stsp int failure = 0;
112 ac25a292 2018-01-26 stsp
113 ac25a292 2018-01-26 stsp if (argc != 1) {
114 ac25a292 2018-01-26 stsp fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
115 ac25a292 2018-01-26 stsp return 1;
116 ac25a292 2018-01-26 stsp }
117 ac25a292 2018-01-26 stsp
118 f8352b2a 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
119 f8352b2a 2018-03-12 stsp err(1, "pledge");
120 f8352b2a 2018-03-12 stsp
121 6df54056 2018-03-03 stsp RUN_TEST(delta_apply(), "delta_apply");
122 ac25a292 2018-01-26 stsp
123 ac25a292 2018-01-26 stsp return failure ? 1 : 0;
124 ac25a292 2018-01-26 stsp }