2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.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"
40 test_printf(char *fmt, ...)
52 static const char *id_str1 = "1111111111111111111111111111111111111111";
53 static const char *id_str2 = "2222222222222222222222222222222222222222";
54 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
55 static struct got_object_id id1, id2, id3;
56 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
57 static int iter_count;
60 idset_cb(struct got_object_id *id, void *data, void *arg) {
61 if (iter_count == 0 &&
62 (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
64 if (iter_count == 1 &&
65 (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
71 idset_add_remove_iter(void)
73 const struct got_error *err = NULL;
74 struct got_object_idset *set;
77 set = got_object_idset_alloc();
79 err = got_error_from_errno();
82 if (got_object_idset_num_elements(set) != 0) {
83 err = got_error(GOT_ERR_BAD_OBJ_DATA);
87 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
88 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
91 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
92 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
95 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
96 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
100 err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
103 if (existing_data != NULL) {
104 err = got_error(GOT_ERR_BAD_OBJ_DATA);
107 if (got_object_idset_num_elements(set) != 1) {
108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
112 if (!got_object_idset_contains(set, &id1)) {
113 err = got_error(GOT_ERR_BAD_OBJ_DATA);
117 err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
120 if (existing_data != NULL) {
121 err = got_error(GOT_ERR_BAD_OBJ_DATA);
124 err = got_object_idset_add(&existing_data, set, &id2, NULL);
125 if (existing_data == NULL) {
126 err = got_error(GOT_ERR_BAD_OBJ_DATA);
129 if (err->code != GOT_ERR_OBJ_EXISTS)
131 err = got_object_idset_add(NULL, set, &id2, NULL);
132 if (err->code != GOT_ERR_OBJ_EXISTS)
136 if (!got_object_idset_contains(set, &id1)) {
137 err = got_error(GOT_ERR_BAD_OBJ_DATA);
140 if (!got_object_idset_contains(set, &id2)) {
141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
144 if (got_object_idset_num_elements(set) != 2) {
145 err = got_error(GOT_ERR_BAD_OBJ_DATA);
149 err = got_object_idset_add(NULL, set, &id3, (void *)data3);
153 if (got_object_idset_get(set, &id1) != (void *)data1) {
154 err = got_error(GOT_ERR_BAD_OBJ_DATA);
157 if (got_object_idset_get(set, &id2) != (void *)data2) {
158 err = got_error(GOT_ERR_BAD_OBJ_DATA);
161 if (got_object_idset_get(set, &id3) != (void *)data3) {
162 err = got_error(GOT_ERR_BAD_OBJ_DATA);
165 if (got_object_idset_num_elements(set) != 3) {
166 err = got_error(GOT_ERR_BAD_OBJ_DATA);
170 err = got_object_idset_remove(NULL, set, &id2);
173 if (got_object_idset_num_elements(set) != 2) {
174 err = got_error(GOT_ERR_BAD_OBJ_DATA);
177 if (got_object_idset_contains(set, &id2)) {
178 err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 if (got_object_idset_get(set, &id2) != NULL) {
182 err = got_error(GOT_ERR_BAD_OBJ_DATA);
186 got_object_idset_for_each(set, idset_cb, NULL);
188 got_object_idset_free(set);
189 return (err == NULL);
192 #define RUN_TEST(expr, name) \
193 { test_ok = (expr); \
194 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
195 failure = (failure || !test_ok); }
200 fprintf(stderr, "usage: id_test [-v]\n");
204 main(int argc, char *argv[])
206 int test_ok = 0, failure = 0;
209 if (pledge("stdio", NULL) == -1)
212 while ((ch = getopt(argc, argv, "v")) != -1) {
225 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
227 return failure ? 1 : 0;