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 static const char *id_str1 = "1111111111111111111111111111111111111111";
42 static const char *id_str2 = "2222222222222222222222222222222222222222";
43 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
44 static struct got_object_id id1, id2, id3;
45 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
47 static const struct got_error *
48 idset_cb(struct got_object_id *id, void *data, void *arg) {
49 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
50 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
51 return NULL;
52 abort();
53 return NULL; /* not reached */
54 }
56 static int
57 idset_add_remove_iter(void)
58 {
59 const struct got_error *err = NULL;
60 struct got_object_idset *set;
62 set = got_object_idset_alloc();
63 if (set == NULL) {
64 err = got_error_from_errno("got_object_idset_alloc");
65 goto done;
66 }
67 if (got_object_idset_num_elements(set) != 0) {
68 err = got_error(GOT_ERR_BAD_OBJ_DATA);
69 goto done;
70 }
72 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
73 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
74 goto done;
75 }
76 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
77 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 goto done;
79 }
80 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
81 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
82 goto done;
83 }
85 err = got_object_idset_add(set, &id1, (void *)data1);
86 if (err)
87 goto done;
88 if (got_object_idset_num_elements(set) != 1) {
89 err = got_error(GOT_ERR_BAD_OBJ_DATA);
90 goto done;
91 }
93 if (!got_object_idset_contains(set, &id1)) {
94 err = got_error(GOT_ERR_BAD_OBJ_DATA);
95 goto done;
96 }
98 err = got_object_idset_add(set, &id2, (void *)data2);
99 if (err)
100 goto done;
102 if (!got_object_idset_contains(set, &id1)) {
103 err = got_error(GOT_ERR_BAD_OBJ_DATA);
104 goto done;
106 if (!got_object_idset_contains(set, &id2)) {
107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
108 goto done;
110 if (got_object_idset_num_elements(set) != 2) {
111 err = got_error(GOT_ERR_BAD_OBJ_DATA);
112 goto done;
115 err = got_object_idset_add(set, &id3, (void *)data3);
116 if (err)
117 goto done;
119 if (got_object_idset_get(set, &id1) != (void *)data1) {
120 err = got_error(GOT_ERR_BAD_OBJ_DATA);
121 goto done;
123 if (got_object_idset_get(set, &id2) != (void *)data2) {
124 err = got_error(GOT_ERR_BAD_OBJ_DATA);
125 goto done;
127 if (got_object_idset_get(set, &id3) != (void *)data3) {
128 err = got_error(GOT_ERR_BAD_OBJ_DATA);
129 goto done;
131 if (got_object_idset_num_elements(set) != 3) {
132 err = got_error(GOT_ERR_BAD_OBJ_DATA);
133 goto done;
136 err = got_object_idset_remove(NULL, set, &id2);
137 if (err)
138 goto done;
139 if (got_object_idset_num_elements(set) != 2) {
140 err = got_error(GOT_ERR_BAD_OBJ_DATA);
141 goto done;
143 if (got_object_idset_contains(set, &id2)) {
144 err = got_error(GOT_ERR_BAD_OBJ_DATA);
145 goto done;
147 if (got_object_idset_get(set, &id2) != NULL) {
148 err = got_error(GOT_ERR_BAD_OBJ_DATA);
149 goto done;
152 got_object_idset_for_each(set, idset_cb, NULL);
153 done:
154 got_object_idset_free(set);
155 return (err == NULL);
158 #define RUN_TEST(expr, name) \
159 { test_ok = (expr); \
160 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
161 failure = (failure || !test_ok); }
163 static void
164 usage(void)
166 fprintf(stderr, "usage: id_test [-v] [-q]\n");
169 int
170 main(int argc, char *argv[])
172 int test_ok = 0, failure = 0;
173 int ch;
175 #ifndef PROFILE
176 if (pledge("stdio", NULL) == -1)
177 err(1, "pledge");
178 #endif
180 while ((ch = getopt(argc, argv, "qv")) != -1) {
181 switch (ch) {
182 case 'q':
183 quiet = 1;
184 verbose = 0;
185 break;
186 case 'v':
187 verbose = 1;
188 quiet = 0;
189 break;
190 default:
191 usage();
192 return 1;
195 argc -= optind;
196 argv += optind;
198 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
200 return failure ? 1 : 0;