xref: /Universal-ctags/libreadtags/tests/test-api-tagsOpen.c (revision 0e675dcc44e62ca4628330a3e983e7b45eb28fd2)
1 /*
2 *   Copyright (c) 2020, Masatake YAMATO
3 *
4 *   This source code is released into the public domain.
5 *
6 *   Testing tagsOpen() API function
7 */
8 
9 #include "readtags.h"
10 
11 #include <errno.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 
18 static int
check_info(tagFileInfo * info,tagFileInfo * expected)19 check_info (tagFileInfo *info, tagFileInfo *expected)
20 {
21 
22 	fprintf (stderr, "inspecting info->file.format...");
23 	if (info->file.format != expected->file.format)
24 	{
25 		fprintf (stderr, "unexpected result: %d\n", info->file.format);
26 		return 1;
27 	}
28 	fprintf (stderr, "ok\n");
29 
30 	fprintf (stderr, "inspecting info->file.sort...");
31 	if (info->file.sort != expected->file.sort)
32 	{
33 		fprintf (stderr, "unexpected result: %d\n", info->file.sort);
34 		return 1;
35 	}
36 	fprintf (stderr, "ok\n");
37 
38 	fprintf (stderr, "inspecting info->program.author...");
39 	if (strcmp (info->program.author, expected->program.author))
40 	{
41 		fprintf (stderr, "unexpected result: %s\n", info->program.author);
42 		return 1;
43 	}
44 	fprintf (stderr, "ok\n");
45 
46 	fprintf (stderr, "inspecting info->program.name...");
47 	if (strcmp (info->program.name, expected->program.name))
48 	{
49 		fprintf (stderr, "unexpected result: %s\n", info->program.name);
50 		return 1;
51 	}
52 	fprintf (stderr, "ok\n");
53 
54 	fprintf (stderr, "inspecting info->program.url...");
55 	if (strcmp (info->program.url, expected->program.url))
56 	{
57 		fprintf (stderr, "unexpected result: %s\n", info->program.url);
58 		return 1;
59 	}
60 	fprintf (stderr, "ok\n");
61 
62 	fprintf (stderr, "inspecting info->program.version...");
63 	if (strcmp (info->program.version, expected->program.version))
64 	{
65 		fprintf (stderr, "unexpected result: %s\n", expected->program.version);
66 		return 1;
67 	}
68 	fprintf (stderr, "ok\n");
69 
70 	return 0;
71 }
72 
73 int
main(void)74 main (void)
75 {
76 	char *srcdir = getenv ("srcdir");
77 	if (srcdir)
78 	{
79 		if (chdir (srcdir) == -1)
80 		{
81 			perror ("chdir");
82 			return 99;
83 		}
84 	}
85 
86 	tagFile *t;
87 	tagFileInfo info;
88 
89 	const char *tags0 = "./no-such-file.tags";
90 	fprintf (stderr, "opening no-existing tags file...");
91 	t = tagsOpen (tags0, &info);
92 	if (! (t == NULL
93 		   && info.status.opened == 0
94 		   && info.status.error_number != 0))
95 	{
96 		fprintf (stderr, "unexpected result (t: %p, opened: %d, error_number: %d)\n",
97 				 t, info.status.opened, info.status.error_number);
98 		return 1;
99 	}
100 	fprintf (stderr, "ok\n");
101 
102 	fprintf (stderr, "opening no-existing tags file with NULL tagFileInfo...");
103 	t = tagsOpen (tags0, NULL);
104 	if (t != NULL)
105 	{
106 		fprintf (stderr, "unexpected result (t: %p)\n", t);
107 		return 1;
108 	}
109 	fprintf (stderr, "ok\n");
110 
111 	tagFileInfo expected1 = {
112 		.file.format = 2,
113 		.file.sort = TAG_SORTED,
114 		.program.author = "Darren Hiebert",
115 		.program.name = "Exuberant Ctags",
116 		.program.url = "http://ctags.sourceforge.net",
117 		.program.version = "5.8",
118 	};
119 	const char *tags1 = "./api-tagsOpen-ectags.tags";
120 	fprintf (stderr, "opening an existing tags file...");
121 	t = tagsOpen (tags1, &info);
122 	if (t == NULL
123 		|| info.status.opened == 0)
124 	{
125 		fprintf (stderr, "unexpected result (t: %p, opened: %d)\n",
126 				 t, info.status.opened);
127 		return 1;
128 	}
129 	fprintf (stderr, "ok\n");
130 
131 
132 	if (check_info (&info, &expected1) != 0)
133 		return 1;
134 
135 	fprintf (stderr, "closing the tag file...");
136 	if (tagsClose (t) != TagSuccess)
137 	{
138 		fprintf (stderr, "unexpected result\n");
139 		return 1;
140 	}
141 	fprintf (stderr, "ok\n");
142 
143 	fprintf (stderr, "opening an existing tags file with NULL tagFileInfo...");
144 	t = tagsOpen (tags1, NULL);
145 	if (t == NULL)
146 	{
147 		fprintf (stderr, "unexpected result\n");
148 		return 1;
149 	}
150 	fprintf (stderr, "ok\n");
151 
152 	fprintf (stderr, "closing the tag file...");
153 	if (tagsClose (t) != TagSuccess)
154 	{
155 		fprintf (stderr, "unexpected result\n");
156 		return 1;
157 	}
158 	fprintf (stderr, "ok\n");
159 
160 	fprintf (stderr, "opening a / (an error is expected)...");
161 	info.status.error_number = 0;
162 	t = tagsOpen ("/", &info);
163 	if (t != NULL)
164 	{
165 		fprintf (stderr, "unexpected result (!NULL)\n");
166 		return 1;
167 	}
168 	else if (info.status.opened)
169 	{
170 		fprintf (stderr, "unexpected result (opened != 0)\n");
171 		return 1;
172 	}
173 	else if (info.status.error_number == 0)
174 	{
175 		fprintf (stderr, "no error\n");
176 		return 1;
177 	}
178 	fprintf (stderr, "ok (errno: %d)\n", info.status.error_number);
179 
180 	fprintf (stderr, "closing the unopened tag file...");
181 	if (tagsClose (t) == TagSuccess)
182 	{
183 		fprintf (stderr, "unexpected result\n");
184 		return 1;
185 	}
186 	fprintf (stderr, "ok\n");
187 
188 	fprintf (stderr, "opening an broken tags file (format: unexpected number)...");
189 	t = tagsOpen ("./api-tagsOpen-wrong-format-num.tags", &info);
190 	if (t)
191 	{
192 		fprintf (stderr, "opened well unexpectedly (NULL)\n");
193 		return 1;
194 	}
195 	else if (info.status.error_number != TagErrnoUnexpectedFormat)
196 	{
197 		fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedFormat)\n");
198 	}
199 	fprintf (stderr, "ok\n");
200 	fprintf (stderr, "closing the unopened tag file...");
201 	if (tagsClose (t) == TagSuccess)
202 	{
203 		fprintf (stderr, "unexpected result\n");
204 		return 1;
205 	}
206 	fprintf (stderr, "ok\n");
207 
208 	fprintf (stderr, "opening an broken tags file (format: not a number)...");
209 	t = tagsOpen ("./api-tagsOpen-wrong-format-nonum.tags", &info);
210 	if (t)
211 	{
212 		fprintf (stderr, "opened well unexpectedly (NULL)\n");
213 		return 1;
214 	}
215 	else if (info.status.error_number != TagErrnoUnexpectedFormat)
216 	{
217 		fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedFormat)\n");
218 	}
219 	fprintf (stderr, "ok\n");
220 	fprintf (stderr, "closing the unopened tag file...");
221 	if (tagsClose (t) == TagSuccess)
222 	{
223 		fprintf (stderr, "unexpected result\n");
224 		return 1;
225 	}
226 	fprintf (stderr, "ok\n");
227 
228 	fprintf (stderr, "opening an broken tags file (sort: unexpected number)...");
229 	t = tagsOpen ("./api-tagsOpen-wrong-sort-method-num.tags", &info);
230 	if (t)
231 	{
232 		fprintf (stderr, "opened well unexpectedly (NULL)\n");
233 		return 1;
234 	}
235 	else if (info.status.error_number != TagErrnoUnexpectedSortedMethod)
236 	{
237 		fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedSortedMethod)\n");
238 	}
239 	fprintf (stderr, "ok\n");
240 	fprintf (stderr, "closing the unopened tag file...");
241 	if (tagsClose (t) == TagSuccess)
242 	{
243 		fprintf (stderr, "unexpected result\n");
244 		return 1;
245 	}
246 	fprintf (stderr, "ok\n");
247 
248 	fprintf (stderr, "opening an broken tags file (sort: not a number)...");
249 	t = tagsOpen ("./api-tagsOpen-wrong-sort-method-nonum.tags", &info);
250 	if (t)
251 	{
252 		fprintf (stderr, "opened well unexpectedly (NULL)\n");
253 		return 1;
254 	}
255 	else if (info.status.error_number != TagErrnoUnexpectedSortedMethod)
256 	{
257 		fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedSortedMethod)\n");
258 	}
259 	fprintf (stderr, "ok\n");
260 	fprintf (stderr, "closing the unopened tag file...");
261 	if (tagsClose (t) == TagSuccess)
262 	{
263 		fprintf (stderr, "unexpected result\n");
264 		return 1;
265 	}
266 	fprintf (stderr, "ok\n");
267 
268 	const char* broken_PROGRAM_AUTHOR [6] = {
269 		"Universal Ctags Team",
270 		"Universal Ctags Team",
271 		"",
272 		NULL,
273 		NULL,
274 		NULL,
275 	};
276 	for (int i = 0; i < 6; i++)
277 	{
278 		char tagf_name_tmpl [] = "./api-tagsOpen-incomplete-program-author-%d.tags";
279 		char tagf_name [sizeof (tagf_name_tmpl) - 1];
280 		fprintf (stderr, "opening a tags file with incomplete PROGRAM_AUTHOR field [trimming level: %d]...", i);
281 		snprintf (tagf_name, sizeof (tagf_name), tagf_name_tmpl, i);
282 		t = tagsOpen (tagf_name, &info);
283 		if (t == NULL)
284 		{
285 			fprintf (stderr, "unexpected error: %d %s\n", info.status.error_number,
286 					 info.status.error_number > 0
287 					 ? strerror (info.status.error_number)
288 					 : "");
289 			return 1;
290 		}
291 		if (!((broken_PROGRAM_AUTHOR [i] == info.program.author)
292 			  || (broken_PROGRAM_AUTHOR [i]
293 				  && info.program.author
294 				  && strcmp (broken_PROGRAM_AUTHOR [i], info.program.author)) == 0))
295 		{
296 			fprintf (stderr, "unexpected value: %s (!= %s)\n",
297 					 info.program.author? info.program.author: "(null)",
298 					 broken_PROGRAM_AUTHOR [i]? broken_PROGRAM_AUTHOR [i]: "(null)");
299 			return 1;
300 		}
301 		fprintf (stderr, "closing the unopened tag file...");
302 		if (tagsClose (t) == TagFailure)
303 		{
304 			fprintf (stderr, "successful unexpectedly\n");
305 			return 1;
306 		}
307 		fprintf (stderr, "ok\n");
308 	}
309 
310 	return 0;
311 }
312