Blame


1 ddb547b4 2018-06-04 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 ddb547b4 2018-06-04 stsp *
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.
7 ddb547b4 2018-06-04 stsp *
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.
15 ddb547b4 2018-06-04 stsp */
16 ddb547b4 2018-06-04 stsp
17 ddb547b4 2018-06-04 stsp #include <sys/queue.h>
18 ddb547b4 2018-06-04 stsp
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 <sha1.h>
26 ddb547b4 2018-06-04 stsp #include <zlib.h>
27 788c352e 2018-06-16 stsp #include <time.h>
28 ddb547b4 2018-06-04 stsp
29 ddb547b4 2018-06-04 stsp #include "got_error.h"
30 ddb547b4 2018-06-04 stsp #include "got_object.h"
31 ddb547b4 2018-06-04 stsp
32 ddb547b4 2018-06-04 stsp #include "got_lib_object_idset.h"
33 ddb547b4 2018-06-04 stsp #include "got_lib_sha1.h"
34 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 ddb547b4 2018-06-04 stsp #include "got_lib_delta.h"
36 ddb547b4 2018-06-04 stsp #include "got_lib_object.h"
37 ddb547b4 2018-06-04 stsp
38 ddb547b4 2018-06-04 stsp static int verbose;
39 7fb414ae 2020-08-08 stsp static int quiet;
40 ddb547b4 2018-06-04 stsp
41 ddb547b4 2018-06-04 stsp void
42 ddb547b4 2018-06-04 stsp test_printf(char *fmt, ...)
43 ddb547b4 2018-06-04 stsp {
44 ddb547b4 2018-06-04 stsp va_list ap;
45 ddb547b4 2018-06-04 stsp
46 ddb547b4 2018-06-04 stsp if (!verbose)
47 ddb547b4 2018-06-04 stsp return;
48 ddb547b4 2018-06-04 stsp
49 ddb547b4 2018-06-04 stsp va_start(ap, fmt);
50 ddb547b4 2018-06-04 stsp vprintf(fmt, ap);
51 ddb547b4 2018-06-04 stsp va_end(ap);
52 ddb547b4 2018-06-04 stsp }
53 ddb547b4 2018-06-04 stsp
54 ddb547b4 2018-06-04 stsp static const char *id_str1 = "1111111111111111111111111111111111111111";
55 ddb547b4 2018-06-04 stsp static const char *id_str2 = "2222222222222222222222222222222222222222";
56 ddb547b4 2018-06-04 stsp static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
57 ddb547b4 2018-06-04 stsp static struct got_object_id id1, id2, id3;
58 ddb547b4 2018-06-04 stsp static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
59 ddb547b4 2018-06-04 stsp
60 cb103d04 2018-11-07 stsp static const struct got_error *
61 917bfd05 2018-06-10 stsp idset_cb(struct got_object_id *id, void *data, void *arg) {
62 b36429ab 2018-11-05 stsp if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
63 b36429ab 2018-11-05 stsp (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
64 cb103d04 2018-11-07 stsp return NULL;
65 b36429ab 2018-11-05 stsp abort();
66 cb103d04 2018-11-07 stsp return NULL; /* not reached */
67 ddb547b4 2018-06-04 stsp }
68 ddb547b4 2018-06-04 stsp
69 ddb547b4 2018-06-04 stsp static int
70 ddb547b4 2018-06-04 stsp idset_add_remove_iter(void)
71 ddb547b4 2018-06-04 stsp {
72 ddb547b4 2018-06-04 stsp const struct got_error *err = NULL;
73 ddb547b4 2018-06-04 stsp struct got_object_idset *set;
74 ddb547b4 2018-06-04 stsp
75 ddb547b4 2018-06-04 stsp set = got_object_idset_alloc();
76 ddb547b4 2018-06-04 stsp if (set == NULL) {
77 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_idset_alloc");
78 ddb547b4 2018-06-04 stsp goto done;
79 ddb547b4 2018-06-04 stsp }
80 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 0) {
81 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
82 c6f420bf 2018-06-04 stsp goto done;
83 c6f420bf 2018-06-04 stsp }
84 ddb547b4 2018-06-04 stsp
85 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
86 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
87 ddb547b4 2018-06-04 stsp goto done;
88 ddb547b4 2018-06-04 stsp }
89 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
90 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
91 ddb547b4 2018-06-04 stsp goto done;
92 ddb547b4 2018-06-04 stsp }
93 ddb547b4 2018-06-04 stsp if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
94 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
95 ddb547b4 2018-06-04 stsp goto done;
96 ddb547b4 2018-06-04 stsp }
97 ddb547b4 2018-06-04 stsp
98 b36429ab 2018-11-05 stsp err = got_object_idset_add(set, &id1, (void *)data1);
99 ddb547b4 2018-06-04 stsp if (err)
100 ddb547b4 2018-06-04 stsp goto done;
101 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 1) {
102 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
103 c6f420bf 2018-06-04 stsp goto done;
104 c6f420bf 2018-06-04 stsp }
105 ddb547b4 2018-06-04 stsp
106 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
107 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
108 ddb547b4 2018-06-04 stsp goto done;
109 ddb547b4 2018-06-04 stsp }
110 ddb547b4 2018-06-04 stsp
111 b36429ab 2018-11-05 stsp err = got_object_idset_add(set, &id2, (void *)data2);
112 ddb547b4 2018-06-04 stsp if (err)
113 ddb547b4 2018-06-04 stsp goto done;
114 ddb547b4 2018-06-04 stsp
115 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id1)) {
116 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
117 ddb547b4 2018-06-04 stsp goto done;
118 ddb547b4 2018-06-04 stsp }
119 ddb547b4 2018-06-04 stsp if (!got_object_idset_contains(set, &id2)) {
120 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
121 ddb547b4 2018-06-04 stsp goto done;
122 ddb547b4 2018-06-04 stsp }
123 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
124 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
125 c6f420bf 2018-06-04 stsp goto done;
126 c6f420bf 2018-06-04 stsp }
127 ddb547b4 2018-06-04 stsp
128 b36429ab 2018-11-05 stsp err = got_object_idset_add(set, &id3, (void *)data3);
129 ddb547b4 2018-06-04 stsp if (err)
130 ddb547b4 2018-06-04 stsp goto done;
131 ddb547b4 2018-06-04 stsp
132 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id1) != (void *)data1) {
133 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
134 ddb547b4 2018-06-04 stsp goto done;
135 ddb547b4 2018-06-04 stsp }
136 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != (void *)data2) {
137 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
138 ddb547b4 2018-06-04 stsp goto done;
139 ddb547b4 2018-06-04 stsp }
140 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id3) != (void *)data3) {
141 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
142 ddb547b4 2018-06-04 stsp goto done;
143 ddb547b4 2018-06-04 stsp }
144 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 3) {
145 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
146 c6f420bf 2018-06-04 stsp goto done;
147 c6f420bf 2018-06-04 stsp }
148 ddb547b4 2018-06-04 stsp
149 50bc349d 2018-06-22 stsp err = got_object_idset_remove(NULL, set, &id2);
150 ddb547b4 2018-06-04 stsp if (err)
151 ddb547b4 2018-06-04 stsp goto done;
152 c6f420bf 2018-06-04 stsp if (got_object_idset_num_elements(set) != 2) {
153 c6f420bf 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
154 c6f420bf 2018-06-04 stsp goto done;
155 c6f420bf 2018-06-04 stsp }
156 ddb547b4 2018-06-04 stsp if (got_object_idset_contains(set, &id2)) {
157 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
158 ddb547b4 2018-06-04 stsp goto done;
159 ddb547b4 2018-06-04 stsp }
160 45b73774 2018-06-04 stsp if (got_object_idset_get(set, &id2) != NULL) {
161 ddb547b4 2018-06-04 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
162 ddb547b4 2018-06-04 stsp goto done;
163 ddb547b4 2018-06-04 stsp }
164 ddb547b4 2018-06-04 stsp
165 917bfd05 2018-06-10 stsp got_object_idset_for_each(set, idset_cb, NULL);
166 ddb547b4 2018-06-04 stsp done:
167 c6f420bf 2018-06-04 stsp got_object_idset_free(set);
168 ddb547b4 2018-06-04 stsp return (err == NULL);
169 ddb547b4 2018-06-04 stsp }
170 ddb547b4 2018-06-04 stsp
171 ddb547b4 2018-06-04 stsp #define RUN_TEST(expr, name) \
172 ddb547b4 2018-06-04 stsp { test_ok = (expr); \
173 7fb414ae 2020-08-08 stsp if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
174 ddb547b4 2018-06-04 stsp failure = (failure || !test_ok); }
175 ddb547b4 2018-06-04 stsp
176 ddb547b4 2018-06-04 stsp void
177 ddb547b4 2018-06-04 stsp usage(void)
178 ddb547b4 2018-06-04 stsp {
179 7fb414ae 2020-08-08 stsp fprintf(stderr, "usage: id_test [-v] [-q]\n");
180 ddb547b4 2018-06-04 stsp }
181 ddb547b4 2018-06-04 stsp
182 ddb547b4 2018-06-04 stsp int
183 ddb547b4 2018-06-04 stsp main(int argc, char *argv[])
184 ddb547b4 2018-06-04 stsp {
185 ddb547b4 2018-06-04 stsp int test_ok = 0, failure = 0;
186 ddb547b4 2018-06-04 stsp int ch;
187 ddb547b4 2018-06-04 stsp
188 2ff12563 2018-09-15 stsp #ifndef PROFILE
189 ddb547b4 2018-06-04 stsp if (pledge("stdio", NULL) == -1)
190 ddb547b4 2018-06-04 stsp err(1, "pledge");
191 2ff12563 2018-09-15 stsp #endif
192 ddb547b4 2018-06-04 stsp
193 7fb414ae 2020-08-08 stsp while ((ch = getopt(argc, argv, "vq")) != -1) {
194 ddb547b4 2018-06-04 stsp switch (ch) {
195 ddb547b4 2018-06-04 stsp case 'v':
196 ddb547b4 2018-06-04 stsp verbose = 1;
197 7fb414ae 2020-08-08 stsp quiet = 0;
198 ddb547b4 2018-06-04 stsp break;
199 7fb414ae 2020-08-08 stsp case 'q':
200 7fb414ae 2020-08-08 stsp quiet = 1;
201 7fb414ae 2020-08-08 stsp verbose = 0;
202 7fb414ae 2020-08-08 stsp break;
203 ddb547b4 2018-06-04 stsp default:
204 ddb547b4 2018-06-04 stsp usage();
205 ddb547b4 2018-06-04 stsp return 1;
206 ddb547b4 2018-06-04 stsp }
207 ddb547b4 2018-06-04 stsp }
208 ddb547b4 2018-06-04 stsp argc -= optind;
209 ddb547b4 2018-06-04 stsp argv += optind;
210 ddb547b4 2018-06-04 stsp
211 ddb547b4 2018-06-04 stsp RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
212 ddb547b4 2018-06-04 stsp
213 ddb547b4 2018-06-04 stsp return failure ? 1 : 0;
214 ddb547b4 2018-06-04 stsp }