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 <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <err.h>
24 #include <sha1.h>
25 #include <zlib.h>
27 #include "got_error.h"
28 #include "got_object.h"
30 #include "got_lib_object_idset.h"
31 #include "got_lib_sha1.h"
32 #include "got_lib_zbuf.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_object.h"
36 static int verbose;
38 void
39 test_printf(char *fmt, ...)
40 {
41 va_list ap;
43 if (!verbose)
44 return;
46 va_start(ap, fmt);
47 vprintf(fmt, ap);
48 va_end(ap);
49 }
51 static const char *id_str1 = "1111111111111111111111111111111111111111";
52 static const char *id_str2 = "2222222222222222222222222222222222222222";
53 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
54 static struct got_object_id id1, id2, id3;
55 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
56 static int iter_count;
58 static void
59 idset_cb(struct got_object_id *id, void *data, void *arg) {
60 if (iter_count == 0 &&
61 (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
62 abort();
63 if (iter_count == 1 &&
64 (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
65 abort();
66 iter_count++;
67 }
69 static int
70 idset_add_remove_iter(void)
71 {
72 const struct got_error *err = NULL;
73 struct got_object_idset *set;
74 void *existing_data;
76 set = got_object_idset_alloc();
77 if (set == NULL) {
78 err = got_error_from_errno();
79 goto done;
80 }
81 if (got_object_idset_num_elements(set) != 0) {
82 err = got_error(GOT_ERR_BAD_OBJ_DATA);
83 goto done;
84 }
86 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
87 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
88 goto done;
89 }
90 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
91 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
92 goto done;
93 }
94 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
95 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
96 goto done;
97 }
99 err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
100 if (err)
101 goto done;
102 if (existing_data != NULL) {
103 err = got_error(GOT_ERR_BAD_OBJ_DATA);
104 goto done;
106 if (got_object_idset_num_elements(set) != 1) {
107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
108 goto done;
111 if (!got_object_idset_contains(set, &id1)) {
112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
113 goto done;
116 err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
117 if (err)
118 goto done;
119 if (existing_data != NULL) {
120 err = got_error(GOT_ERR_BAD_OBJ_DATA);
121 goto done;
123 err = got_object_idset_add(&existing_data, set, &id2, NULL);
124 if (existing_data == NULL) {
125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
126 goto done;
128 if (err->code != GOT_ERR_OBJ_EXISTS)
129 goto done;
130 err = got_object_idset_add(NULL, set, &id2, NULL);
131 if (err->code != GOT_ERR_OBJ_EXISTS)
132 goto done;
133 err = NULL;
135 if (!got_object_idset_contains(set, &id1)) {
136 err = got_error(GOT_ERR_BAD_OBJ_DATA);
137 goto done;
139 if (!got_object_idset_contains(set, &id2)) {
140 err = got_error(GOT_ERR_BAD_OBJ_DATA);
141 goto done;
143 if (got_object_idset_num_elements(set) != 2) {
144 err = got_error(GOT_ERR_BAD_OBJ_DATA);
145 goto done;
148 err = got_object_idset_add(NULL, set, &id3, (void *)data3);
149 if (err)
150 goto done;
152 if (got_object_idset_get(set, &id1) != (void *)data1) {
153 err = got_error(GOT_ERR_BAD_OBJ_DATA);
154 goto done;
156 if (got_object_idset_get(set, &id2) != (void *)data2) {
157 err = got_error(GOT_ERR_BAD_OBJ_DATA);
158 goto done;
160 if (got_object_idset_get(set, &id3) != (void *)data3) {
161 err = got_error(GOT_ERR_BAD_OBJ_DATA);
162 goto done;
164 if (got_object_idset_num_elements(set) != 3) {
165 err = got_error(GOT_ERR_BAD_OBJ_DATA);
166 goto done;
169 err = got_object_idset_remove(set, &id2);
170 if (err)
171 goto done;
172 if (got_object_idset_num_elements(set) != 2) {
173 err = got_error(GOT_ERR_BAD_OBJ_DATA);
174 goto done;
176 if (got_object_idset_contains(set, &id2)) {
177 err = got_error(GOT_ERR_BAD_OBJ_DATA);
178 goto done;
180 if (got_object_idset_get(set, &id2) != NULL) {
181 err = got_error(GOT_ERR_BAD_OBJ_DATA);
182 goto done;
185 got_object_idset_for_each(set, idset_cb, NULL);
186 done:
187 got_object_idset_free(set);
188 return (err == NULL);
191 #define RUN_TEST(expr, name) \
192 { test_ok = (expr); \
193 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
194 failure = (failure || !test_ok); }
196 void
197 usage(void)
199 fprintf(stderr, "usage: id_test [-v]\n");
202 int
203 main(int argc, char *argv[])
205 int test_ok = 0, failure = 0;
206 int ch;
208 if (pledge("stdio", NULL) == -1)
209 err(1, "pledge");
211 while ((ch = getopt(argc, argv, "v")) != -1) {
212 switch (ch) {
213 case 'v':
214 verbose = 1;
215 break;
216 default:
217 usage();
218 return 1;
221 argc -= optind;
222 argv += optind;
224 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
226 return failure ? 1 : 0;