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) {
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;
75 set = got_object_idset_alloc();
76 if (set == NULL) {
77 err = got_error_from_errno();
78 goto done;
79 }
81 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
82 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
83 goto done;
84 }
85 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
86 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
87 goto done;
88 }
89 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
90 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
91 goto done;
92 }
94 err = got_object_idset_add(set, &id1, (void *)data1);
95 if (err)
96 goto done;
98 if (!got_object_idset_contains(set, &id1)) {
99 err = got_error(GOT_ERR_BAD_OBJ_DATA);
100 goto done;
103 err = got_object_idset_add(set, &id2, (void *)data2);
104 if (err)
105 goto done;
106 err = got_object_idset_add(set, &id2, NULL);
107 if (err->code != GOT_ERR_OBJ_EXISTS)
108 goto done;
109 err = NULL;
111 if (!got_object_idset_contains(set, &id1)) {
112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
113 goto done;
115 if (!got_object_idset_contains(set, &id2)) {
116 err = got_error(GOT_ERR_BAD_OBJ_DATA);
117 goto done;
120 err = got_object_idset_add(set, &id3, (void *)data3);
121 if (err)
122 goto done;
124 if (got_object_idset_get_data(set, &id1) != (void *)data1) {
125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
126 goto done;
128 if (got_object_idset_get_data(set, &id2) != (void *)data2) {
129 err = got_error(GOT_ERR_BAD_OBJ_DATA);
130 goto done;
132 if (got_object_idset_get_data(set, &id3) != (void *)data3) {
133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
134 goto done;
137 err = got_object_idset_remove(set, &id2);
138 if (err)
139 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_get_data(set, &id2) != NULL) {
145 err = got_error(GOT_ERR_BAD_OBJ_DATA);
146 goto done;
149 got_object_idset_for_each(set, idset_cb);
150 got_object_idset_free(set);
151 done:
152 return (err == NULL);
155 #define RUN_TEST(expr, name) \
156 { test_ok = (expr); \
157 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
158 failure = (failure || !test_ok); }
160 void
161 usage(void)
163 fprintf(stderr, "usage: id_test [-v]\n");
166 int
167 main(int argc, char *argv[])
169 int test_ok = 0, failure = 0;
170 int ch;
172 if (pledge("stdio", NULL) == -1)
173 err(1, "pledge");
175 while ((ch = getopt(argc, argv, "v")) != -1) {
176 switch (ch) {
177 case 'v':
178 verbose = 1;
179 break;
180 default:
181 usage();
182 return 1;
185 argc -= optind;
186 argv += optind;
188 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
190 return failure ? 1 : 0;