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