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 <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 static const char *id_str1 = "1111111111111111111111111111111111111111";
41 static const char *id_str2 = "2222222222222222222222222222222222222222";
42 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
43 static struct got_object_id id1, id2, id3;
44 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
46 static const struct got_error *
47 idset_cb(struct got_object_id *id, void *data, void *arg) {
48 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
49 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
50 return NULL;
51 abort();
52 return NULL; /* not reached */
53 }
55 static int
56 idset_add_remove_iter(void)
57 {
58 const struct got_error *err = NULL;
59 struct got_object_idset *set;
61 set = got_object_idset_alloc();
62 if (set == NULL) {
63 err = got_error_from_errno("got_object_idset_alloc");
64 goto done;
65 }
66 if (got_object_idset_num_elements(set) != 0) {
67 err = got_error(GOT_ERR_BAD_OBJ_DATA);
68 goto done;
69 }
71 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
72 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
73 goto done;
74 }
75 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
76 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
77 goto done;
78 }
79 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
80 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
81 goto done;
82 }
84 err = got_object_idset_add(set, &id1, (void *)data1);
85 if (err)
86 goto done;
87 if (got_object_idset_num_elements(set) != 1) {
88 err = got_error(GOT_ERR_BAD_OBJ_DATA);
89 goto done;
90 }
92 if (!got_object_idset_contains(set, &id1)) {
93 err = got_error(GOT_ERR_BAD_OBJ_DATA);
94 goto done;
95 }
97 err = got_object_idset_add(set, &id2, (void *)data2);
98 if (err)
99 goto done;
101 if (!got_object_idset_contains(set, &id1)) {
102 err = got_error(GOT_ERR_BAD_OBJ_DATA);
103 goto done;
105 if (!got_object_idset_contains(set, &id2)) {
106 err = got_error(GOT_ERR_BAD_OBJ_DATA);
107 goto done;
109 if (got_object_idset_num_elements(set) != 2) {
110 err = got_error(GOT_ERR_BAD_OBJ_DATA);
111 goto done;
114 err = got_object_idset_add(set, &id3, (void *)data3);
115 if (err)
116 goto done;
118 if (got_object_idset_get(set, &id1) != (void *)data1) {
119 err = got_error(GOT_ERR_BAD_OBJ_DATA);
120 goto done;
122 if (got_object_idset_get(set, &id2) != (void *)data2) {
123 err = got_error(GOT_ERR_BAD_OBJ_DATA);
124 goto done;
126 if (got_object_idset_get(set, &id3) != (void *)data3) {
127 err = got_error(GOT_ERR_BAD_OBJ_DATA);
128 goto done;
130 if (got_object_idset_num_elements(set) != 3) {
131 err = got_error(GOT_ERR_BAD_OBJ_DATA);
132 goto done;
135 err = got_object_idset_remove(NULL, set, &id2);
136 if (err)
137 goto done;
138 if (got_object_idset_num_elements(set) != 2) {
139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
140 goto done;
142 if (got_object_idset_contains(set, &id2)) {
143 err = got_error(GOT_ERR_BAD_OBJ_DATA);
144 goto done;
146 if (got_object_idset_get(set, &id2) != NULL) {
147 err = got_error(GOT_ERR_BAD_OBJ_DATA);
148 goto done;
151 got_object_idset_for_each(set, idset_cb, NULL);
152 done:
153 got_object_idset_free(set);
154 return (err == NULL);
157 #define RUN_TEST(expr, name) \
158 { test_ok = (expr); \
159 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
160 failure = (failure || !test_ok); }
162 static void
163 usage(void)
165 fprintf(stderr, "usage: id_test [-v] [-q]\n");
168 int
169 main(int argc, char *argv[])
171 int test_ok = 0, failure = 0;
172 int ch;
174 #ifndef PROFILE
175 if (pledge("stdio", NULL) == -1)
176 err(1, "pledge");
177 #endif
179 while ((ch = getopt(argc, argv, "vq")) != -1) {
180 switch (ch) {
181 case 'v':
182 verbose = 1;
183 quiet = 0;
184 break;
185 case 'q':
186 quiet = 1;
187 verbose = 0;
188 break;
189 default:
190 usage();
191 return 1;
194 argc -= optind;
195 argv += optind;
197 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
199 return failure ? 1 : 0;