Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <zlib.h>
24 #include "got_error.h"
26 #include "delta.h"
27 #include "path.h"
29 #ifndef nitems
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
31 #endif
33 struct delta_test {
34 const char *base;
35 const char *delta;
36 size_t delta_len;
37 const char *expected;
38 } delta_tests[] = {
39 /* base len 0, target len 4, append 4 'x' */
40 { "", "\x00\x04\x04xxxx", 7, "xxxx" },
41 /* copy 4 bytes at offset 0 from base, append 4 'x' */
42 { "aabbccdd", "\x08\x08\x90\x04\x04xxxx", 9, "aabbxxxx" },
43 /* copy 4 bytes at offset 4 from base, append 4 'x' */
44 { "aabbccdd", "\x08\x08\x91\x04\x04\x04xxxx", 10, "ccddxxxx" },
45 };
47 static const struct got_error *
48 compress_to_file(FILE **outfile, const char *input, size_t inlen)
49 {
50 const struct got_error *err = NULL;
51 z_stream z;
52 char buf[2048];
53 char *inbuf;
54 int ret;
55 size_t n;
57 memset(&z, 0, sizeof(z));
58 z.zalloc = Z_NULL;
59 z.zfree = Z_NULL;
61 if (deflateInit(&z, 8) != Z_OK)
62 return got_error(GOT_ERR_IO);
64 *outfile = got_opentemp();
65 if (*outfile == NULL)
66 return got_error_from_errno();
68 z.next_in = (Bytef *)input;
69 z.avail_in = inlen;
70 z.next_out = buf;
71 z.avail_out = sizeof(buf);
72 /* Our output buffer is large enough so one round should be enough. */
73 ret = deflate(&z, Z_FINISH);
74 if (ret != Z_STREAM_END || z.avail_out == 0) {
75 err = got_error(GOT_ERR_COMPRESSION);
76 goto done;
77 }
79 deflateEnd(&z);
81 n = fwrite(buf, 1, z.avail_out, *outfile);
82 if (n != z.avail_out)
83 err = got_ferror(*outfile, GOT_ERR_IO);
84 done:
85 if (err) {
86 fclose(*outfile);
87 *outfile = NULL;
88 } else
89 rewind(*outfile);
90 return err;
91 }
93 static int
94 delta_combine()
95 {
96 const struct got_error *err = NULL;
97 int i;
98 FILE *result_file;
100 result_file = got_opentemp();
101 if (result_file == NULL)
102 return 1;
104 for (i = 0; i < nitems(delta_tests); i++) {
105 struct delta_test *dt = &delta_tests[i];
106 FILE *base_file;
107 char buf[1024];
108 size_t n, len, result_len;
110 len = strlen(dt->base);
111 err = compress_to_file(&base_file, dt->base, len);
112 if (err)
113 break;
115 err = got_delta_apply(base_file, dt->delta, dt->delta_len,
116 result_file);
117 fclose(base_file);
118 if (err)
119 break;
120 result_len = strlen(dt->expected);
121 n = fread(buf, result_len, 1, result_file);
122 if (n != 1 || strncmp(buf, dt->expected, result_len) != 0) {
123 err = got_ferror(result_file, GOT_ERR_BAD_DELTA);
124 break;
126 rewind(result_file);
129 fclose(result_file);
130 return (err == NULL);
133 #define RUN_TEST(expr, name) \
134 { test_ok = (expr); \
135 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
136 failure = (failure || !test_ok); }
138 int
139 main(int argc, const char *argv[])
141 int test_ok;
142 int failure = 0;
144 if (argc != 1) {
145 fprintf(stderr, "usage: delta_test [REPO_PATH]\n");
146 return 1;
149 RUN_TEST(delta_combine(), "delta_combine");
151 return failure ? 1 : 0;