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