Blob


1 /*
2 * Copyright (c) 2018, 2019 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 <limits.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <err.h>
25 #include <sha1.h>
26 #include <zlib.h>
27 #include <time.h>
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_object_idset.h"
33 #include "got_lib_sha1.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_object.h"
38 static int verbose;
39 static int quiet;
41 void
42 test_printf(char *fmt, ...)
43 {
44 va_list ap;
46 if (!verbose)
47 return;
49 va_start(ap, fmt);
50 vprintf(fmt, ap);
51 va_end(ap);
52 }
54 static const char *id_str1 = "1111111111111111111111111111111111111111";
55 static const char *id_str2 = "2222222222222222222222222222222222222222";
56 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
57 static struct got_object_id id1, id2, id3;
58 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
60 static const struct got_error *
61 idset_cb(struct got_object_id *id, void *data, void *arg) {
62 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
63 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
64 return NULL;
65 abort();
66 return NULL; /* not reached */
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("got_object_idset_alloc");
78 goto done;
79 }
80 if (got_object_idset_num_elements(set) != 0) {
81 err = got_error(GOT_ERR_BAD_OBJ_DATA);
82 goto done;
83 }
85 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
86 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
87 goto done;
88 }
89 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
90 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
91 goto done;
92 }
93 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
94 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
95 goto done;
96 }
98 err = got_object_idset_add(set, &id1, (void *)data1);
99 if (err)
100 goto done;
101 if (got_object_idset_num_elements(set) != 1) {
102 err = got_error(GOT_ERR_BAD_OBJ_DATA);
103 goto done;
106 if (!got_object_idset_contains(set, &id1)) {
107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
108 goto done;
111 err = got_object_idset_add(set, &id2, (void *)data2);
112 if (err)
113 goto done;
115 if (!got_object_idset_contains(set, &id1)) {
116 err = got_error(GOT_ERR_BAD_OBJ_DATA);
117 goto done;
119 if (!got_object_idset_contains(set, &id2)) {
120 err = got_error(GOT_ERR_BAD_OBJ_DATA);
121 goto done;
123 if (got_object_idset_num_elements(set) != 2) {
124 err = got_error(GOT_ERR_BAD_OBJ_DATA);
125 goto done;
128 err = got_object_idset_add(set, &id3, (void *)data3);
129 if (err)
130 goto done;
132 if (got_object_idset_get(set, &id1) != (void *)data1) {
133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
134 goto done;
136 if (got_object_idset_get(set, &id2) != (void *)data2) {
137 err = got_error(GOT_ERR_BAD_OBJ_DATA);
138 goto done;
140 if (got_object_idset_get(set, &id3) != (void *)data3) {
141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
142 goto done;
144 if (got_object_idset_num_elements(set) != 3) {
145 err = got_error(GOT_ERR_BAD_OBJ_DATA);
146 goto done;
149 err = got_object_idset_remove(NULL, set, &id2);
150 if (err)
151 goto done;
152 if (got_object_idset_num_elements(set) != 2) {
153 err = got_error(GOT_ERR_BAD_OBJ_DATA);
154 goto done;
156 if (got_object_idset_contains(set, &id2)) {
157 err = got_error(GOT_ERR_BAD_OBJ_DATA);
158 goto done;
160 if (got_object_idset_get(set, &id2) != NULL) {
161 err = got_error(GOT_ERR_BAD_OBJ_DATA);
162 goto done;
165 got_object_idset_for_each(set, idset_cb, NULL);
166 done:
167 got_object_idset_free(set);
168 return (err == NULL);
171 #define RUN_TEST(expr, name) \
172 { test_ok = (expr); \
173 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
174 failure = (failure || !test_ok); }
176 void
177 usage(void)
179 fprintf(stderr, "usage: id_test [-v] [-q]\n");
182 int
183 main(int argc, char *argv[])
185 int test_ok = 0, failure = 0;
186 int ch;
188 #ifndef PROFILE
189 if (pledge("stdio", NULL) == -1)
190 err(1, "pledge");
191 #endif
193 while ((ch = getopt(argc, argv, "vq")) != -1) {
194 switch (ch) {
195 case 'v':
196 verbose = 1;
197 quiet = 0;
198 break;
199 case 'q':
200 quiet = 1;
201 verbose = 0;
202 break;
203 default:
204 usage();
205 return 1;
208 argc -= optind;
209 argv += optind;
211 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
213 return failure ? 1 : 0;