2 ac25a292 2018-01-26 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 ac25a292 2018-01-26 stsp #include <sys/queue.h>
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>
24 ac25a292 2018-01-26 stsp #include "got_error.h"
26 ac25a292 2018-01-26 stsp #include "delta.h"
27 885d3e02 2018-01-27 stsp #include "path.h"
29 885d3e02 2018-01-27 stsp #ifndef nitems
30 885d3e02 2018-01-27 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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" },
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)
50 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
52 885d3e02 2018-01-27 stsp char buf[2048];
53 885d3e02 2018-01-27 stsp char *inbuf;
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;
61 885d3e02 2018-01-27 stsp if (deflateInit(&z, 8) != Z_OK)
62 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_IO);
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();
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);
79 885d3e02 2018-01-27 stsp deflateEnd(&z);
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);
86 885d3e02 2018-01-27 stsp fclose(*outfile);
87 885d3e02 2018-01-27 stsp *outfile = NULL;
89 885d3e02 2018-01-27 stsp rewind(*outfile);
94 ac25a292 2018-01-26 stsp delta_combine()
96 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
98 885d3e02 2018-01-27 stsp FILE *result_file;
100 885d3e02 2018-01-27 stsp result_file = got_opentemp();
101 885d3e02 2018-01-27 stsp if (result_file == NULL)
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;
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);
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);
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);
126 885d3e02 2018-01-27 stsp rewind(result_file);
129 885d3e02 2018-01-27 stsp fclose(result_file);
130 885d3e02 2018-01-27 stsp return (err == NULL);
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); }
139 ac25a292 2018-01-26 stsp main(int argc, const char *argv[])
141 b08fe7be 2018-01-26 stsp int test_ok;
142 ac25a292 2018-01-26 stsp int failure = 0;
144 ac25a292 2018-01-26 stsp if (argc != 1) {
145 ac25a292 2018-01-26 stsp fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
149 ac25a292 2018-01-26 stsp RUN_TEST(delta_combine(), "delta_combine");
151 ac25a292 2018-01-26 stsp return failure ? 1 : 0;