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 <zlib.h>
25 #include <time.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_inflate.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_object.h"
36 static int verbose;
37 static int quiet;
39 static const char *id_str1 = "1111111111111111111111111111111111111111";
40 static const char *id_str2 = "2222222222222222222222222222222222222222";
41 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
42 static struct got_object_id id1, id2, id3;
43 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
45 static const struct got_error *
46 idset_cb(struct got_object_id *id, void *data, void *arg) {
47 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
48 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
49 return NULL;
50 abort();
51 return NULL; /* not reached */
52 }
54 static int
55 idset_add_remove_iter(void)
56 {
57 const struct got_error *err = NULL;
58 struct got_object_idset *set;
60 set = got_object_idset_alloc();
61 if (set == NULL) {
62 err = got_error_from_errno("got_object_idset_alloc");
63 goto done;
64 }
65 if (got_object_idset_num_elements(set) != 0) {
66 err = got_error(GOT_ERR_BAD_OBJ_DATA);
67 goto done;
68 }
70 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
71 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
72 goto done;
73 }
74 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
75 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
76 goto done;
77 }
78 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
79 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
80 goto done;
81 }
83 err = got_object_idset_add(set, &id1, (void *)data1);
84 if (err)
85 goto done;
86 if (got_object_idset_num_elements(set) != 1) {
87 err = got_error(GOT_ERR_BAD_OBJ_DATA);
88 goto done;
89 }
91 if (!got_object_idset_contains(set, &id1)) {
92 err = got_error(GOT_ERR_BAD_OBJ_DATA);
93 goto done;
94 }
96 err = got_object_idset_add(set, &id2, (void *)data2);
97 if (err)
98 goto done;
100 if (!got_object_idset_contains(set, &id1)) {
101 err = got_error(GOT_ERR_BAD_OBJ_DATA);
102 goto done;
104 if (!got_object_idset_contains(set, &id2)) {
105 err = got_error(GOT_ERR_BAD_OBJ_DATA);
106 goto done;
108 if (got_object_idset_num_elements(set) != 2) {
109 err = got_error(GOT_ERR_BAD_OBJ_DATA);
110 goto done;
113 err = got_object_idset_add(set, &id3, (void *)data3);
114 if (err)
115 goto done;
117 if (got_object_idset_get(set, &id1) != (void *)data1) {
118 err = got_error(GOT_ERR_BAD_OBJ_DATA);
119 goto done;
121 if (got_object_idset_get(set, &id2) != (void *)data2) {
122 err = got_error(GOT_ERR_BAD_OBJ_DATA);
123 goto done;
125 if (got_object_idset_get(set, &id3) != (void *)data3) {
126 err = got_error(GOT_ERR_BAD_OBJ_DATA);
127 goto done;
129 if (got_object_idset_num_elements(set) != 3) {
130 err = got_error(GOT_ERR_BAD_OBJ_DATA);
131 goto done;
134 err = got_object_idset_remove(NULL, set, &id2);
135 if (err)
136 goto done;
137 if (got_object_idset_num_elements(set) != 2) {
138 err = got_error(GOT_ERR_BAD_OBJ_DATA);
139 goto done;
141 if (got_object_idset_contains(set, &id2)) {
142 err = got_error(GOT_ERR_BAD_OBJ_DATA);
143 goto done;
145 if (got_object_idset_get(set, &id2) != NULL) {
146 err = got_error(GOT_ERR_BAD_OBJ_DATA);
147 goto done;
150 got_object_idset_for_each(set, idset_cb, NULL);
151 done:
152 got_object_idset_free(set);
153 return (err == NULL);
156 #define RUN_TEST(expr, name) \
157 { test_ok = (expr); \
158 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
159 failure = (failure || !test_ok); }
161 static void
162 usage(void)
164 fprintf(stderr, "usage: id_test [-v] [-q]\n");
167 int
168 main(int argc, char *argv[])
170 int test_ok = 0, failure = 0;
171 int ch;
173 #ifndef PROFILE
174 if (pledge("stdio", NULL) == -1)
175 err(1, "pledge");
176 #endif
178 while ((ch = getopt(argc, argv, "vq")) != -1) {
179 switch (ch) {
180 case 'v':
181 verbose = 1;
182 quiet = 0;
183 break;
184 case 'q':
185 quiet = 1;
186 verbose = 0;
187 break;
188 default:
189 usage();
190 return 1;
193 argc -= optind;
194 argv += optind;
196 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
198 return failure ? 1 : 0;