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