Blame


1 ddb547b4 2018-06-04 stsp /*
2 ddb547b4 2018-06-04 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 ddb547b4 2018-06-04 stsp *
4 ddb547b4 2018-06-04 stsp * Permission to use, copy, modify, and distribute this software for any
5 ddb547b4 2018-06-04 stsp * purpose with or without fee is hereby granted, provided that the above
6 ddb547b4 2018-06-04 stsp * copyright notice and this permission notice appear in all copies.
7 ddb547b4 2018-06-04 stsp *
8 ddb547b4 2018-06-04 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ddb547b4 2018-06-04 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ddb547b4 2018-06-04 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ddb547b4 2018-06-04 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ddb547b4 2018-06-04 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ddb547b4 2018-06-04 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ddb547b4 2018-06-04 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ddb547b4 2018-06-04 stsp */
16 ddb547b4 2018-06-04 stsp
17 ddb547b4 2018-06-04 stsp #include <sys/queue.h>
18 ddb547b4 2018-06-04 stsp
19 ddb547b4 2018-06-04 stsp #include <stdarg.h>
20 ddb547b4 2018-06-04 stsp #include <stdlib.h>
21 ddb547b4 2018-06-04 stsp #include <stdio.h>
22 ddb547b4 2018-06-04 stsp #include <unistd.h>
23 ddb547b4 2018-06-04 stsp #include <err.h>
24 ddb547b4 2018-06-04 stsp #include <sha1.h>
25 ddb547b4 2018-06-04 stsp #include <zlib.h>
26 ddb547b4 2018-06-04 stsp
27 ddb547b4 2018-06-04 stsp #include "got_error.h"
28 ddb547b4 2018-06-04 stsp #include "got_object.h"
29 ddb547b4 2018-06-04 stsp
30 ddb547b4 2018-06-04 stsp #include "got_lib_object_idset.h"
31 ddb547b4 2018-06-04 stsp #include "got_lib_sha1.h"
32 ddb547b4 2018-06-04 stsp #include "got_lib_zbuf.h"
33 ddb547b4 2018-06-04 stsp #include "got_lib_delta.h"
34 ddb547b4 2018-06-04 stsp #include "got_lib_object.h"
35 ddb547b4 2018-06-04 stsp
36 ddb547b4 2018-06-04 stsp static int verbose;
37 ddb547b4 2018-06-04 stsp
38 ddb547b4 2018-06-04 stsp void
39 ddb547b4 2018-06-04 stsp test_printf(char *fmt, ...)
40 ddb547b4 2018-06-04 stsp {
41 ddb547b4 2018-06-04 stsp va_list ap;
42 ddb547b4 2018-06-04 stsp
43 ddb547b4 2018-06-04 stsp if (!verbose)
44 ddb547b4 2018-06-04 stsp return;
45 ddb547b4 2018-06-04 stsp
46 ddb547b4 2018-06-04 stsp va_start(ap, fmt);
47 ddb547b4 2018-06-04 stsp vprintf(fmt, ap);
48 ddb547b4 2018-06-04 stsp va_end(ap);
49 ddb547b4 2018-06-04 stsp }
50 ddb547b4 2018-06-04 stsp
51 ddb547b4 2018-06-04 stsp static const char *id_str1 = "1111111111111111111111111111111111111111";
52 ddb547b4 2018-06-04 stsp static const char *id_str2 = "2222222222222222222222222222222222222222";
53 ddb547b4 2018-06-04 stsp static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
54 ddb547b4 2018-06-04 stsp static struct got_object_id id1, id2, id3;
55 ddb547b4 2018-06-04 stsp static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
56 ddb547b4 2018-06-04 stsp static int iter_count;
57 ddb547b4 2018-06-04 stsp
58 ddb547b4 2018-06-04 stsp static void
59 917bfd05 2018-06-10 stsp idset_cb(struct got_object_id *id, void *data, void *arg) {
60 ddb547b4 2018-06-04 stsp if (iter_count == 0 &&
61 ddb547b4 2018-06-04 stsp (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
62 ddb547b4 2018-06-04 stsp abort();
63 ddb547b4 2018-06-04 stsp if (iter_count == 1 &&
64 ddb547b4 2018-06-04 stsp (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
65 ddb547b4 2018-06-04 stsp abort();
66 ddb547b4 2018-06-04 stsp iter_count++;
67 ddb547b4 2018-06-04 stsp }
68 ddb547b4 2018-06-04 stsp
69 ddb547b4 2018-06-04 stsp static int
70 ddb547b4 2018-06-04 stsp idset_add_remove_iter(void)
71 ddb547b4 2018-06-04 stsp {
72 ddb547b4 2018-06-04 stsp const struct got_error *err = NULL;
73 ddb547b4 2018-06-04 stsp struct got_object_idset *set;
74 d5a90aac 2018-06-04 stsp void *existing_data;
75 ddb547b4 2018-06-04 stsp
76 ddb547b4 2018-06-04 stsp set = got_object_idset_alloc();
77 ddb547b4 2018-06-04 stsp if (set == NULL) {
78 ddb547b4 2018-06-04 stsp err = got_error_from_errno();
79 ddb547b4 2018-06-04 stsp goto done;
80 ddb547b4 2018-06-04 stsp }
81 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 0) {
82 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
83 c6f420bf 2018-06-04 stsp goto done;
84 c6f420bf 2018-06-04 stsp }
85 ddb547b4 2018-06-04 stsp
86 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
87 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
88 ddb547b4 2018-06-04 stsp goto done;
89 ddb547b4 2018-06-04 stsp }
90 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
91 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
92 ddb547b4 2018-06-04 stsp goto done;
93 ddb547b4 2018-06-04 stsp }
94 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
95 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
96 ddb547b4 2018-06-04 stsp goto done;
97 ddb547b4 2018-06-04 stsp }
98 ddb547b4 2018-06-04 stsp
99 d5a90aac 2018-06-04 stsp err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
100 ddb547b4 2018-06-04 stsp if (err)
101 ddb547b4 2018-06-04 stsp goto done;
102 d5a90aac 2018-06-04 stsp if (existing_data != NULL) {
103 d5a90aac 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
104 d5a90aac 2018-06-04 stsp goto done;
105 d5a90aac 2018-06-04 stsp }
106 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 1) {
107 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
108 c6f420bf 2018-06-04 stsp goto done;
109 c6f420bf 2018-06-04 stsp }
110 ddb547b4 2018-06-04 stsp
111 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
112 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
113 ddb547b4 2018-06-04 stsp goto done;
114 ddb547b4 2018-06-04 stsp }
115 ddb547b4 2018-06-04 stsp
116 d5a90aac 2018-06-04 stsp err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
117 ddb547b4 2018-06-04 stsp if (err)
118 ddb547b4 2018-06-04 stsp goto done;
119 d5a90aac 2018-06-04 stsp if (existing_data != NULL) {
120 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
121 c6f420bf 2018-06-04 stsp goto done;
122 c6f420bf 2018-06-04 stsp }
123 d5a90aac 2018-06-04 stsp err = got_object_idset_add(&existing_data, set, &id2, NULL);
124 d5a90aac 2018-06-04 stsp if (existing_data == NULL) {
125 d5a90aac 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
126 d5a90aac 2018-06-04 stsp goto done;
127 d5a90aac 2018-06-04 stsp }
128 ddb547b4 2018-06-04 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
129 ddb547b4 2018-06-04 stsp goto done;
130 d5a90aac 2018-06-04 stsp err = got_object_idset_add(NULL, set, &id2, NULL);
131 d5a90aac 2018-06-04 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
132 d5a90aac 2018-06-04 stsp goto done;
133 ddb547b4 2018-06-04 stsp err = NULL;
134 ddb547b4 2018-06-04 stsp
135 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
136 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
137 ddb547b4 2018-06-04 stsp goto done;
138 ddb547b4 2018-06-04 stsp }
139 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id2)) {
140 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
141 ddb547b4 2018-06-04 stsp goto done;
142 ddb547b4 2018-06-04 stsp }
143 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
144 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
145 c6f420bf 2018-06-04 stsp goto done;
146 c6f420bf 2018-06-04 stsp }
147 ddb547b4 2018-06-04 stsp
148 d5a90aac 2018-06-04 stsp err = got_object_idset_add(NULL, set, &id3, (void *)data3);
149 ddb547b4 2018-06-04 stsp if (err)
150 ddb547b4 2018-06-04 stsp goto done;
151 ddb547b4 2018-06-04 stsp
152 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id1) != (void *)data1) {
153 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
154 ddb547b4 2018-06-04 stsp goto done;
155 ddb547b4 2018-06-04 stsp }
156 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != (void *)data2) {
157 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
158 ddb547b4 2018-06-04 stsp goto done;
159 ddb547b4 2018-06-04 stsp }
160 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id3) != (void *)data3) {
161 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
162 ddb547b4 2018-06-04 stsp goto done;
163 ddb547b4 2018-06-04 stsp }
164 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 3) {
165 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
166 c6f420bf 2018-06-04 stsp goto done;
167 c6f420bf 2018-06-04 stsp }
168 ddb547b4 2018-06-04 stsp
169 ddb547b4 2018-06-04 stsp err = got_object_idset_remove(set, &id2);
170 ddb547b4 2018-06-04 stsp if (err)
171 ddb547b4 2018-06-04 stsp goto done;
172 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
173 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
174 c6f420bf 2018-06-04 stsp goto done;
175 c6f420bf 2018-06-04 stsp }
176 ddb547b4 2018-06-04 stsp if (got_object_idset_contains(set, &id2)) {
177 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
178 ddb547b4 2018-06-04 stsp goto done;
179 ddb547b4 2018-06-04 stsp }
180 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != NULL) {
181 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
182 ddb547b4 2018-06-04 stsp goto done;
183 ddb547b4 2018-06-04 stsp }
184 ddb547b4 2018-06-04 stsp
185 917bfd05 2018-06-10 stsp got_object_idset_for_each(set, idset_cb, NULL);
186 ddb547b4 2018-06-04 stsp done:
187 c6f420bf 2018-06-04 stsp got_object_idset_free(set);
188 ddb547b4 2018-06-04 stsp return (err == NULL);
189 ddb547b4 2018-06-04 stsp }
190 ddb547b4 2018-06-04 stsp
191 ddb547b4 2018-06-04 stsp #define RUN_TEST(expr, name) \
192 ddb547b4 2018-06-04 stsp { test_ok = (expr); \
193 ddb547b4 2018-06-04 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
194 ddb547b4 2018-06-04 stsp failure = (failure || !test_ok); }
195 ddb547b4 2018-06-04 stsp
196 ddb547b4 2018-06-04 stsp void
197 ddb547b4 2018-06-04 stsp usage(void)
198 ddb547b4 2018-06-04 stsp {
199 ddb547b4 2018-06-04 stsp fprintf(stderr, "usage: id_test [-v]\n");
200 ddb547b4 2018-06-04 stsp }
201 ddb547b4 2018-06-04 stsp
202 ddb547b4 2018-06-04 stsp int
203 ddb547b4 2018-06-04 stsp main(int argc, char *argv[])
204 ddb547b4 2018-06-04 stsp {
205 ddb547b4 2018-06-04 stsp int test_ok = 0, failure = 0;
206 ddb547b4 2018-06-04 stsp int ch;
207 ddb547b4 2018-06-04 stsp
208 ddb547b4 2018-06-04 stsp if (pledge("stdio", NULL) == -1)
209 ddb547b4 2018-06-04 stsp err(1, "pledge");
210 ddb547b4 2018-06-04 stsp
211 ddb547b4 2018-06-04 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
212 ddb547b4 2018-06-04 stsp switch (ch) {
213 ddb547b4 2018-06-04 stsp case 'v':
214 ddb547b4 2018-06-04 stsp verbose = 1;
215 ddb547b4 2018-06-04 stsp break;
216 ddb547b4 2018-06-04 stsp default:
217 ddb547b4 2018-06-04 stsp usage();
218 ddb547b4 2018-06-04 stsp return 1;
219 ddb547b4 2018-06-04 stsp }
220 ddb547b4 2018-06-04 stsp }
221 ddb547b4 2018-06-04 stsp argc -= optind;
222 ddb547b4 2018-06-04 stsp argv += optind;
223 ddb547b4 2018-06-04 stsp
224 ddb547b4 2018-06-04 stsp RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
225 ddb547b4 2018-06-04 stsp
226 ddb547b4 2018-06-04 stsp return failure ? 1 : 0;
227 ddb547b4 2018-06-04 stsp }