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 */
18 #include <limits.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_inflate.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_object.h"
37 static int verbose;
38 static int quiet;
40 void
41 test_printf(char *fmt, ...)
42 {
43 va_list ap;
45 if (!verbose)
46 return;
48 va_start(ap, fmt);
49 vprintf(fmt, ap);
50 va_end(ap);
51 }
53 static const char *id_str1 = "1111111111111111111111111111111111111111";
54 static const char *id_str2 = "2222222222222222222222222222222222222222";
55 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
56 static struct got_object_id id1, id2, id3;
57 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
59 static const struct got_error *
60 idset_cb(struct got_object_id *id, void *data, void *arg) {
61 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
62 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
63 return NULL;
64 abort();
65 return NULL; /* not reached */
66 }
68 static int
69 idset_add_remove_iter(void)
70 {
71 const struct got_error *err = NULL;
72 struct got_object_idset *set;
74 set = got_object_idset_alloc();
75 if (set == NULL) {
76 err = got_error_from_errno("got_object_idset_alloc");
77 goto done;
78 }
79 if (got_object_idset_num_elements(set) != 0) {
80 err = got_error(GOT_ERR_BAD_OBJ_DATA);
81 goto done;
82 }
84 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
85 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
86 goto done;
87 }
88 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
89 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
90 goto done;
91 }
92 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
93 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
94 goto done;
95 }
97 err = got_object_idset_add(set, &id1, (void *)data1);
98 if (err)
99 goto done;
100 if (got_object_idset_num_elements(set) != 1) {
101 err = got_error(GOT_ERR_BAD_OBJ_DATA);
102 goto done;
105 if (!got_object_idset_contains(set, &id1)) {
106 err = got_error(GOT_ERR_BAD_OBJ_DATA);
107 goto done;
110 err = got_object_idset_add(set, &id2, (void *)data2);
111 if (err)
112 goto done;
114 if (!got_object_idset_contains(set, &id1)) {
115 err = got_error(GOT_ERR_BAD_OBJ_DATA);
116 goto done;
118 if (!got_object_idset_contains(set, &id2)) {
119 err = got_error(GOT_ERR_BAD_OBJ_DATA);
120 goto done;
122 if (got_object_idset_num_elements(set) != 2) {
123 err = got_error(GOT_ERR_BAD_OBJ_DATA);
124 goto done;
127 err = got_object_idset_add(set, &id3, (void *)data3);
128 if (err)
129 goto done;
131 if (got_object_idset_get(set, &id1) != (void *)data1) {
132 err = got_error(GOT_ERR_BAD_OBJ_DATA);
133 goto done;
135 if (got_object_idset_get(set, &id2) != (void *)data2) {
136 err = got_error(GOT_ERR_BAD_OBJ_DATA);
137 goto done;
139 if (got_object_idset_get(set, &id3) != (void *)data3) {
140 err = got_error(GOT_ERR_BAD_OBJ_DATA);
141 goto done;
143 if (got_object_idset_num_elements(set) != 3) {
144 err = got_error(GOT_ERR_BAD_OBJ_DATA);
145 goto done;
148 err = got_object_idset_remove(NULL, set, &id2);
149 if (err)
150 goto done;
151 if (got_object_idset_num_elements(set) != 2) {
152 err = got_error(GOT_ERR_BAD_OBJ_DATA);
153 goto done;
155 if (got_object_idset_contains(set, &id2)) {
156 err = got_error(GOT_ERR_BAD_OBJ_DATA);
157 goto done;
159 if (got_object_idset_get(set, &id2) != NULL) {
160 err = got_error(GOT_ERR_BAD_OBJ_DATA);
161 goto done;
164 got_object_idset_for_each(set, idset_cb, NULL);
165 done:
166 got_object_idset_free(set);
167 return (err == NULL);
170 #define RUN_TEST(expr, name) \
171 { test_ok = (expr); \
172 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
173 failure = (failure || !test_ok); }
175 void
176 usage(void)
178 fprintf(stderr, "usage: id_test [-v] [-q]\n");
181 int
182 main(int argc, char *argv[])
184 int test_ok = 0, failure = 0;
185 int ch;
187 #ifndef PROFILE
188 if (pledge("stdio", NULL) == -1)
189 err(1, "pledge");
190 #endif
192 while ((ch = getopt(argc, argv, "vq")) != -1) {
193 switch (ch) {
194 case 'v':
195 verbose = 1;
196 quiet = 0;
197 break;
198 case 'q':
199 quiet = 1;
200 verbose = 0;
201 break;
202 default:
203 usage();
204 return 1;
207 argc -= optind;
208 argv += optind;
210 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
212 return failure ? 1 : 0;