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>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_lib_object_idset.h"
32 #include "got_lib_sha1.h"
33 #include "got_lib_zbuf.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_object.h"
37 static int verbose;
39 void
40 test_printf(char *fmt, ...)
41 {
42 va_list ap;
44 if (!verbose)
45 return;
47 va_start(ap, fmt);
48 vprintf(fmt, ap);
49 va_end(ap);
50 }
52 static const char *id_str1 = "1111111111111111111111111111111111111111";
53 static const char *id_str2 = "2222222222222222222222222222222222222222";
54 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
55 static struct got_object_id id1, id2, id3;
56 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
57 static int iter_count;
59 static void
60 idset_cb(struct got_object_id *id, void *data, void *arg) {
61 if (iter_count == 0 &&
62 (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
63 abort();
64 if (iter_count == 1 &&
65 (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
66 abort();
67 iter_count++;
68 }
70 static int
71 idset_add_remove_iter(void)
72 {
73 const struct got_error *err = NULL;
74 struct got_object_idset *set;
75 void *existing_data;
77 set = got_object_idset_alloc();
78 if (set == NULL) {
79 err = got_error_from_errno();
80 goto done;
81 }
82 if (got_object_idset_num_elements(set) != 0) {
83 err = got_error(GOT_ERR_BAD_OBJ_DATA);
84 goto done;
85 }
87 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
88 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
89 goto done;
90 }
91 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
92 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
93 goto done;
94 }
95 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
96 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
97 goto done;
98 }
100 err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
101 if (err)
102 goto done;
103 if (existing_data != NULL) {
104 err = got_error(GOT_ERR_BAD_OBJ_DATA);
105 goto done;
107 if (got_object_idset_num_elements(set) != 1) {
108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
109 goto done;
112 if (!got_object_idset_contains(set, &id1)) {
113 err = got_error(GOT_ERR_BAD_OBJ_DATA);
114 goto done;
117 err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
118 if (err)
119 goto done;
120 if (existing_data != NULL) {
121 err = got_error(GOT_ERR_BAD_OBJ_DATA);
122 goto done;
124 err = got_object_idset_add(&existing_data, set, &id2, NULL);
125 if (existing_data == NULL) {
126 err = got_error(GOT_ERR_BAD_OBJ_DATA);
127 goto done;
129 if (err->code != GOT_ERR_OBJ_EXISTS)
130 goto done;
131 err = got_object_idset_add(NULL, set, &id2, NULL);
132 if (err->code != GOT_ERR_OBJ_EXISTS)
133 goto done;
134 err = NULL;
136 if (!got_object_idset_contains(set, &id1)) {
137 err = got_error(GOT_ERR_BAD_OBJ_DATA);
138 goto done;
140 if (!got_object_idset_contains(set, &id2)) {
141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
142 goto done;
144 if (got_object_idset_num_elements(set) != 2) {
145 err = got_error(GOT_ERR_BAD_OBJ_DATA);
146 goto done;
149 err = got_object_idset_add(NULL, set, &id3, (void *)data3);
150 if (err)
151 goto done;
153 if (got_object_idset_get(set, &id1) != (void *)data1) {
154 err = got_error(GOT_ERR_BAD_OBJ_DATA);
155 goto done;
157 if (got_object_idset_get(set, &id2) != (void *)data2) {
158 err = got_error(GOT_ERR_BAD_OBJ_DATA);
159 goto done;
161 if (got_object_idset_get(set, &id3) != (void *)data3) {
162 err = got_error(GOT_ERR_BAD_OBJ_DATA);
163 goto done;
165 if (got_object_idset_num_elements(set) != 3) {
166 err = got_error(GOT_ERR_BAD_OBJ_DATA);
167 goto done;
170 err = got_object_idset_remove(NULL, set, &id2);
171 if (err)
172 goto done;
173 if (got_object_idset_num_elements(set) != 2) {
174 err = got_error(GOT_ERR_BAD_OBJ_DATA);
175 goto done;
177 if (got_object_idset_contains(set, &id2)) {
178 err = got_error(GOT_ERR_BAD_OBJ_DATA);
179 goto done;
181 if (got_object_idset_get(set, &id2) != NULL) {
182 err = got_error(GOT_ERR_BAD_OBJ_DATA);
183 goto done;
186 got_object_idset_for_each(set, idset_cb, NULL);
187 done:
188 got_object_idset_free(set);
189 return (err == NULL);
192 #define RUN_TEST(expr, name) \
193 { test_ok = (expr); \
194 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
195 failure = (failure || !test_ok); }
197 void
198 usage(void)
200 fprintf(stderr, "usage: id_test [-v]\n");
203 int
204 main(int argc, char *argv[])
206 int test_ok = 0, failure = 0;
207 int ch;
209 if (pledge("stdio", NULL) == -1)
210 err(1, "pledge");
212 while ((ch = getopt(argc, argv, "v")) != -1) {
213 switch (ch) {
214 case 'v':
215 verbose = 1;
216 break;
217 default:
218 usage();
219 return 1;
222 argc -= optind;
223 argv += optind;
225 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
227 return failure ? 1 : 0;