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 885d3e02 2018-01-27 stsp #include <zlib.h>
23 ac25a292 2018-01-26 stsp
24 ac25a292 2018-01-26 stsp #include "got_error.h"
25 ac25a292 2018-01-26 stsp
26 ac25a292 2018-01-26 stsp #include "delta.h"
27 885d3e02 2018-01-27 stsp #include "path.h"
28 ac25a292 2018-01-26 stsp
29 885d3e02 2018-01-27 stsp #ifndef nitems
30 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
31 885d3e02 2018-01-27 stsp #endif
32 885d3e02 2018-01-27 stsp
33 885d3e02 2018-01-27 stsp struct delta_test {
34 885d3e02 2018-01-27 stsp const char *base;
35 885d3e02 2018-01-27 stsp const char *delta;
36 885d3e02 2018-01-27 stsp size_t delta_len;
37 885d3e02 2018-01-27 stsp const char *expected;
38 885d3e02 2018-01-27 stsp } delta_tests[] = {
39 885d3e02 2018-01-27 stsp /* base len 0, target len 4, append 4 'x' */
40 885d3e02 2018-01-27 stsp { "", "\x00\x04\x04xxxx", 7, "xxxx" },
41 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 0 from base, append 4 'x' */
42 885d3e02 2018-01-27 stsp { "aabbccdd", "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx" },
43 885d3e02 2018-01-27 stsp /* copy 4 bytes at offset 4 from base, append 4 'x' */
44 885d3e02 2018-01-27 stsp { "aabbccdd", "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx" },
45 885d3e02 2018-01-27 stsp };
46 885d3e02 2018-01-27 stsp
47 885d3e02 2018-01-27 stsp static const struct got_error *
48 885d3e02 2018-01-27 stsp compress_to_file(FILE **outfile, const char *input, size_t inlen)
49 885d3e02 2018-01-27 stsp {
50 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
51 885d3e02 2018-01-27 stsp z_stream z;
52 885d3e02 2018-01-27 stsp char buf[2048];
53 885d3e02 2018-01-27 stsp char *inbuf;
54 885d3e02 2018-01-27 stsp int ret;
55 885d3e02 2018-01-27 stsp size_t n;
56 885d3e02 2018-01-27 stsp
57 885d3e02 2018-01-27 stsp memset(&z, 0, sizeof(z));
58 885d3e02 2018-01-27 stsp z.zalloc = Z_NULL;
59 885d3e02 2018-01-27 stsp z.zfree = Z_NULL;
60 885d3e02 2018-01-27 stsp
61 885d3e02 2018-01-27 stsp if (deflateInit(&z, 8) != Z_OK)
62 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_IO);
63 885d3e02 2018-01-27 stsp
64 885d3e02 2018-01-27 stsp *outfile = got_opentemp();
65 885d3e02 2018-01-27 stsp if (*outfile == NULL)
66 885d3e02 2018-01-27 stsp return got_error_from_errno();
67 885d3e02 2018-01-27 stsp
68 885d3e02 2018-01-27 stsp z.next_in = (Bytef *)input;
69 885d3e02 2018-01-27 stsp z.avail_in = inlen;
70 885d3e02 2018-01-27 stsp z.next_out = buf;
71 885d3e02 2018-01-27 stsp z.avail_out = sizeof(buf);
72 885d3e02 2018-01-27 stsp /* Our output buffer is large enough so one round should be enough. */
73 885d3e02 2018-01-27 stsp ret = deflate(&z, Z_FINISH);
74 885d3e02 2018-01-27 stsp if (ret != Z_STREAM_END || z.avail_out == 0) {
75 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_COMPRESSION);
76 885d3e02 2018-01-27 stsp goto done;
77 885d3e02 2018-01-27 stsp }
78 885d3e02 2018-01-27 stsp
79 885d3e02 2018-01-27 stsp deflateEnd(&z);
80 885d3e02 2018-01-27 stsp
81 885d3e02 2018-01-27 stsp n = fwrite(buf, 1, z.avail_out, *outfile);
82 885d3e02 2018-01-27 stsp if (n != z.avail_out)
83 885d3e02 2018-01-27 stsp err = got_ferror(*outfile, GOT_ERR_IO);
84 885d3e02 2018-01-27 stsp done:
85 885d3e02 2018-01-27 stsp if (err) {
86 885d3e02 2018-01-27 stsp fclose(*outfile);
87 885d3e02 2018-01-27 stsp *outfile = NULL;
88 885d3e02 2018-01-27 stsp } else
89 885d3e02 2018-01-27 stsp rewind(*outfile);
90 885d3e02 2018-01-27 stsp return err;
91 885d3e02 2018-01-27 stsp }
92 885d3e02 2018-01-27 stsp
93 ac25a292 2018-01-26 stsp static int
94 ac25a292 2018-01-26 stsp delta_combine()
95 ac25a292 2018-01-26 stsp {
96 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
97 885d3e02 2018-01-27 stsp int i;
98 885d3e02 2018-01-27 stsp FILE *result_file;
99 885d3e02 2018-01-27 stsp
100 885d3e02 2018-01-27 stsp result_file = got_opentemp();
101 885d3e02 2018-01-27 stsp if (result_file == NULL)
102 885d3e02 2018-01-27 stsp return 1;
103 885d3e02 2018-01-27 stsp
104 885d3e02 2018-01-27 stsp for (i = 0; i < nitems(delta_tests); i++) {
105 885d3e02 2018-01-27 stsp struct delta_test *dt = &delta_tests[i];
106 885d3e02 2018-01-27 stsp FILE *base_file;
107 885d3e02 2018-01-27 stsp char buf[1024];
108 885d3e02 2018-01-27 stsp size_t n, len, result_len;
109 885d3e02 2018-01-27 stsp
110 885d3e02 2018-01-27 stsp len = strlen(dt->base);
111 885d3e02 2018-01-27 stsp err = compress_to_file(&base_file, dt->base, len);
112 885d3e02 2018-01-27 stsp if (err)
113 885d3e02 2018-01-27 stsp break;
114 885d3e02 2018-01-27 stsp
115 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, dt->delta, dt->delta_len,
116 885d3e02 2018-01-27 stsp result_file);
117 885d3e02 2018-01-27 stsp fclose(base_file);
118 885d3e02 2018-01-27 stsp if (err)
119 885d3e02 2018-01-27 stsp break;
120 885d3e02 2018-01-27 stsp result_len = strlen(dt->expected);
121 885d3e02 2018-01-27 stsp n = fread(buf, result_len, 1, result_file);
122 885d3e02 2018-01-27 stsp if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
123 885d3e02 2018-01-27 stsp err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
124 885d3e02 2018-01-27 stsp break;
125 885d3e02 2018-01-27 stsp }
126 885d3e02 2018-01-27 stsp rewind(result_file);
127 885d3e02 2018-01-27 stsp }
128 885d3e02 2018-01-27 stsp
129 885d3e02 2018-01-27 stsp fclose(result_file);
130 885d3e02 2018-01-27 stsp return (err == NULL);
131 ac25a292 2018-01-26 stsp }
132 ac25a292 2018-01-26 stsp
133 b08fe7be 2018-01-26 stsp #define RUN_TEST(expr, name) \
134 b08fe7be 2018-01-26 stsp { test_ok = (expr); \
135 b08fe7be 2018-01-26 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
136 b08fe7be 2018-01-26 stsp failure = (failure || !test_ok); }
137 b08fe7be 2018-01-26 stsp
138 ac25a292 2018-01-26 stsp int
139 ac25a292 2018-01-26 stsp main(int argc, const char *argv[])
140 ac25a292 2018-01-26 stsp {
141 b08fe7be 2018-01-26 stsp int test_ok;
142 ac25a292 2018-01-26 stsp int failure = 0;
143 ac25a292 2018-01-26 stsp
144 ac25a292 2018-01-26 stsp if (argc != 1) {
145 ac25a292 2018-01-26 stsp fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
146 ac25a292 2018-01-26 stsp return 1;
147 ac25a292 2018-01-26 stsp }
148 ac25a292 2018-01-26 stsp
149 ac25a292 2018-01-26 stsp RUN_TEST(delta_combine(), "delta_combine");
150 ac25a292 2018-01-26 stsp
151 ac25a292 2018-01-26 stsp return failure ? 1 : 0;
152 ac25a292 2018-01-26 stsp }