2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 56e0773d 2019-11-28 stsp #include <limits.h>
20 ddb547b4 2018-06-04 stsp #include <stdarg.h>
21 ddb547b4 2018-06-04 stsp #include <stdlib.h>
22 ddb547b4 2018-06-04 stsp #include <stdio.h>
23 ddb547b4 2018-06-04 stsp #include <unistd.h>
24 ddb547b4 2018-06-04 stsp #include <err.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 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
33 63581804 2018-07-09 stsp #include "got_lib_inflate.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;
38 7fb414ae 2020-08-08 stsp static int quiet;
40 ddb547b4 2018-06-04 stsp static const char *id_str1 = "1111111111111111111111111111111111111111";
41 ddb547b4 2018-06-04 stsp static const char *id_str2 = "2222222222222222222222222222222222222222";
42 ddb547b4 2018-06-04 stsp static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
43 ddb547b4 2018-06-04 stsp static struct got_object_id id1, id2, id3;
44 ddb547b4 2018-06-04 stsp static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
46 cb103d04 2018-11-07 stsp static const struct got_error *
47 917bfd05 2018-06-10 stsp idset_cb(struct got_object_id *id, void *data, void *arg) {
48 b36429ab 2018-11-05 stsp if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
49 b36429ab 2018-11-05 stsp (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
50 cb103d04 2018-11-07 stsp return NULL;
52 cb103d04 2018-11-07 stsp return NULL; /* not reached */
56 ddb547b4 2018-06-04 stsp idset_add_remove_iter(void)
58 ddb547b4 2018-06-04 stsp const struct got_error *err = NULL;
59 ddb547b4 2018-06-04 stsp struct got_object_idset *set;
61 ddb547b4 2018-06-04 stsp set = got_object_idset_alloc();
62 ddb547b4 2018-06-04 stsp if (set == NULL) {
63 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_idset_alloc");
66 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 0) {
67 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
71 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id1, id_str1, GOT_HASH_SHA1)) {
72 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
75 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id2, id_str2, GOT_HASH_SHA1)) {
76 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
79 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id3, id_str3, GOT_HASH_SHA1)) {
80 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
84 b36429ab 2018-11-05 stsp err = got_object_idset_add(set, &id1, (void *)data1);
87 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 1) {
88 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
92 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
93 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
97 b36429ab 2018-11-05 stsp err = got_object_idset_add(set, &id2, (void *)data2);
101 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
102 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
105 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id2)) {
106 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
109 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
110 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
114 b36429ab 2018-11-05 stsp err = got_object_idset_add(set, &id3, (void *)data3);
118 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id1) != (void *)data1) {
119 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
122 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != (void *)data2) {
123 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
126 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id3) != (void *)data3) {
127 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
130 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 3) {
131 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
135 50bc349d 2018-06-22 stsp err = got_object_idset_remove(NULL, set, &id2);
138 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
139 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
142 ddb547b4 2018-06-04 stsp if (got_object_idset_contains(set, &id2)) {
143 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
146 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != NULL) {
147 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
151 917bfd05 2018-06-10 stsp got_object_idset_for_each(set, idset_cb, NULL);
153 c6f420bf 2018-06-04 stsp got_object_idset_free(set);
154 ddb547b4 2018-06-04 stsp return (err == NULL);
157 ddb547b4 2018-06-04 stsp #define RUN_TEST(expr, name) \
158 ddb547b4 2018-06-04 stsp { test_ok = (expr); \
159 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
160 ddb547b4 2018-06-04 stsp failure = (failure || !test_ok); }
162 ef20f542 2022-06-26 thomas static void
163 ddb547b4 2018-06-04 stsp usage(void)
165 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: id_test [-v] [-q]\n");
169 ddb547b4 2018-06-04 stsp main(int argc, char *argv[])
171 ddb547b4 2018-06-04 stsp int test_ok = 0, failure = 0;
174 2ff12563 2018-09-15 stsp #ifndef PROFILE
175 ddb547b4 2018-06-04 stsp if (pledge("stdio", NULL) == -1)
176 ddb547b4 2018-06-04 stsp err(1, "pledge");
179 f7065961 2022-10-27 thomas while ((ch = getopt(argc, argv, "qv")) != -1) {
180 ddb547b4 2018-06-04 stsp switch (ch) {
183 7fb414ae 2020-08-08 stsp verbose = 0;
185 f7065961 2022-10-27 thomas case 'v':
186 f7065961 2022-10-27 thomas verbose = 1;
187 f7065961 2022-10-27 thomas quiet = 0;
194 ddb547b4 2018-06-04 stsp argc -= optind;
195 ddb547b4 2018-06-04 stsp argv += optind;
197 ddb547b4 2018-06-04 stsp RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
199 ddb547b4 2018-06-04 stsp return failure ? 1 : 0;