2 ddb547b4 2018-06-04 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 ddb547b4 2018-06-04 stsp * Permission to use, copy, modify, and distribute this software for any
5 ddb547b4 2018-06-04 stsp * purpose with or without fee is hereby granted, provided that the above
6 ddb547b4 2018-06-04 stsp * copyright notice and this permission notice appear in all copies.
8 ddb547b4 2018-06-04 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ddb547b4 2018-06-04 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ddb547b4 2018-06-04 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ddb547b4 2018-06-04 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ddb547b4 2018-06-04 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ddb547b4 2018-06-04 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ddb547b4 2018-06-04 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ddb547b4 2018-06-04 stsp #include <sys/queue.h>
19 ddb547b4 2018-06-04 stsp #include <stdarg.h>
20 ddb547b4 2018-06-04 stsp #include <stdlib.h>
21 ddb547b4 2018-06-04 stsp #include <stdio.h>
22 ddb547b4 2018-06-04 stsp #include <unistd.h>
23 ddb547b4 2018-06-04 stsp #include <err.h>
24 ddb547b4 2018-06-04 stsp #include <sha1.h>
25 ddb547b4 2018-06-04 stsp #include <zlib.h>
26 788c352e 2018-06-16 stsp #include <time.h>
28 ddb547b4 2018-06-04 stsp #include "got_error.h"
29 ddb547b4 2018-06-04 stsp #include "got_object.h"
31 ddb547b4 2018-06-04 stsp #include "got_lib_object_idset.h"
32 ddb547b4 2018-06-04 stsp #include "got_lib_sha1.h"
33 ddb547b4 2018-06-04 stsp #include "got_lib_zbuf.h"
34 ddb547b4 2018-06-04 stsp #include "got_lib_delta.h"
35 ddb547b4 2018-06-04 stsp #include "got_lib_object.h"
37 ddb547b4 2018-06-04 stsp static int verbose;
40 ddb547b4 2018-06-04 stsp test_printf(char *fmt, ...)
44 ddb547b4 2018-06-04 stsp if (!verbose)
47 ddb547b4 2018-06-04 stsp va_start(ap, fmt);
48 ddb547b4 2018-06-04 stsp vprintf(fmt, ap);
52 ddb547b4 2018-06-04 stsp static const char *id_str1 = "1111111111111111111111111111111111111111";
53 ddb547b4 2018-06-04 stsp static const char *id_str2 = "2222222222222222222222222222222222222222";
54 ddb547b4 2018-06-04 stsp static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
55 ddb547b4 2018-06-04 stsp static struct got_object_id id1, id2, id3;
56 ddb547b4 2018-06-04 stsp static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
57 ddb547b4 2018-06-04 stsp static int iter_count;
60 917bfd05 2018-06-10 stsp idset_cb(struct got_object_id *id, void *data, void *arg) {
61 ddb547b4 2018-06-04 stsp if (iter_count == 0 &&
62 ddb547b4 2018-06-04 stsp (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
64 ddb547b4 2018-06-04 stsp if (iter_count == 1 &&
65 ddb547b4 2018-06-04 stsp (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
67 ddb547b4 2018-06-04 stsp iter_count++;
71 ddb547b4 2018-06-04 stsp idset_add_remove_iter(void)
73 ddb547b4 2018-06-04 stsp const struct got_error *err = NULL;
74 ddb547b4 2018-06-04 stsp struct got_object_idset *set;
75 d5a90aac 2018-06-04 stsp void *existing_data;
77 ddb547b4 2018-06-04 stsp set = got_object_idset_alloc();
78 ddb547b4 2018-06-04 stsp if (set == NULL) {
79 ddb547b4 2018-06-04 stsp err = got_error_from_errno();
82 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 0) {
83 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
87 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
88 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
91 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
92 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
95 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
96 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
100 d5a90aac 2018-06-04 stsp err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
103 d5a90aac 2018-06-04 stsp if (existing_data != NULL) {
104 d5a90aac 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
107 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 1) {
108 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
112 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
113 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
117 d5a90aac 2018-06-04 stsp err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
120 d5a90aac 2018-06-04 stsp if (existing_data != NULL) {
121 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
124 d5a90aac 2018-06-04 stsp err = got_object_idset_add(&existing_data, set, &id2, NULL);
125 d5a90aac 2018-06-04 stsp if (existing_data == NULL) {
126 d5a90aac 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
129 ddb547b4 2018-06-04 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
131 d5a90aac 2018-06-04 stsp err = got_object_idset_add(NULL, set, &id2, NULL);
132 d5a90aac 2018-06-04 stsp if (err->code != GOT_ERR_OBJ_EXISTS)
134 ddb547b4 2018-06-04 stsp err = NULL;
136 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
137 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
140 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id2)) {
141 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
144 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
145 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
149 d5a90aac 2018-06-04 stsp err = got_object_idset_add(NULL, set, &id3, (void *)data3);
153 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id1) != (void *)data1) {
154 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
157 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != (void *)data2) {
158 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
161 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id3) != (void *)data3) {
162 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
165 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 3) {
166 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
170 50bc349d 2018-06-22 stsp err = got_object_idset_remove(NULL, set, &id2);
173 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
174 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
177 ddb547b4 2018-06-04 stsp if (got_object_idset_contains(set, &id2)) {
178 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != NULL) {
182 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
186 917bfd05 2018-06-10 stsp got_object_idset_for_each(set, idset_cb, NULL);
188 c6f420bf 2018-06-04 stsp got_object_idset_free(set);
189 ddb547b4 2018-06-04 stsp return (err == NULL);
192 ddb547b4 2018-06-04 stsp #define RUN_TEST(expr, name) \
193 ddb547b4 2018-06-04 stsp { test_ok = (expr); \
194 ddb547b4 2018-06-04 stsp printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
195 ddb547b4 2018-06-04 stsp failure = (failure || !test_ok); }
198 ddb547b4 2018-06-04 stsp usage(void)
200 ddb547b4 2018-06-04 stsp fprintf(stderr, "usage: id_test [-v]\n");
204 ddb547b4 2018-06-04 stsp main(int argc, char *argv[])
206 ddb547b4 2018-06-04 stsp int test_ok = 0, failure = 0;
209 ddb547b4 2018-06-04 stsp if (pledge("stdio", NULL) == -1)
210 ddb547b4 2018-06-04 stsp err(1, "pledge");
212 ddb547b4 2018-06-04 stsp while ((ch = getopt(argc, argv, "v")) != -1) {
213 ddb547b4 2018-06-04 stsp switch (ch) {
215 ddb547b4 2018-06-04 stsp verbose = 1;
222 ddb547b4 2018-06-04 stsp argc -= optind;
223 ddb547b4 2018-06-04 stsp argv += optind;
225 ddb547b4 2018-06-04 stsp RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
227 ddb547b4 2018-06-04 stsp return failure ? 1 : 0;