xref: /netbsd-src/external/bsd/file/dist/tests/test.c (revision f4748aaa01faf324805f9747191535eb6600f82c)
1 /*	$NetBSD: test.c,v 1.1.1.7 2022/09/24 20:07:55 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Christos Zoulas 2003.
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 
35 #include "magic.h"
36 
37 static const char *prog;
38 
39 static void *
40 xrealloc(void *p, size_t n)
41 {
42 	p = realloc(p, n);
43 	if (p == NULL) {
44 		(void)fprintf(stderr, "%s ERROR slurping file: %s\n",
45 			prog, strerror(errno));
46 		exit(10);
47 	}
48 	return p;
49 }
50 
51 static char *
52 slurp(FILE *fp, size_t *final_len)
53 {
54 	size_t len = 256;
55 	int c;
56 	char *l = (char *)xrealloc(NULL, len), *s = l;
57 
58 	for (c = getc(fp); c != EOF; c = getc(fp)) {
59 		if (s == l + len) {
60 			l = xrealloc(l, len * 2);
61 			len *= 2;
62 		}
63 		*s++ = c;
64 	}
65 	if (s != l && s[-1] == '\n')
66 		s--;
67 	if (s == l + len)
68 		l = (char *)xrealloc(l, len + 1);
69 	*s++ = '\0';
70 
71 	*final_len = s - l;
72 	l = (char *)xrealloc(l, s - l);
73 	return l;
74 }
75 
76 int
77 main(int argc, char **argv)
78 {
79 	struct magic_set *ms;
80 	const char *result;
81 	size_t result_len, desired_len;
82 	char *desired = NULL;
83 	int e = EXIT_FAILURE;
84 	FILE *fp;
85 
86 
87 	prog = strrchr(argv[0], '/');
88 	if (prog)
89 		prog++;
90 	else
91 		prog = argv[0];
92 
93 	ms = magic_open(MAGIC_ERROR);
94 	if (ms == NULL) {
95 		(void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n",
96 		    prog, strerror(errno));
97 		return e;
98 	}
99 	if (magic_load(ms, NULL) == -1) {
100 		(void)fprintf(stderr, "%s: ERROR loading with NULL file: %s\n",
101 		    prog, magic_error(ms));
102 		goto bad;
103 	}
104 
105 	if (argc == 1) {
106 		e = 0;
107 		goto bad;
108 	}
109 
110 	if (argc != 3) {
111 		(void)fprintf(stderr, "Usage: %s TEST-FILE RESULT\n", prog);
112 		goto bad;
113 	}
114 	if ((result = magic_file(ms, argv[1])) == NULL) {
115 		(void)fprintf(stderr, "%s: ERROR loading file %s: %s\n",
116 		    prog, argv[1], magic_error(ms));
117 		goto bad;
118 	}
119 	fp = fopen(argv[2], "r");
120 	if (fp == NULL) {
121 		(void)fprintf(stderr, "%s: ERROR opening `%s': %s",
122 		    prog, argv[2], strerror(errno));
123 		goto bad;
124 	}
125 	desired = slurp(fp, &desired_len);
126 	fclose(fp);
127 	(void)printf("%s: %s\n", argv[1], result);
128 	if (strcmp(result, desired) != 0) {
129 	    result_len = strlen(result);
130 	    (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n"
131 		"expected (len %zu)\n%s\n", prog, result_len, result,
132 		desired_len, desired);
133 	    goto bad;
134 	}
135 	e = 0;
136 bad:
137 	free(desired);
138 	magic_close(ms);
139 	return e;
140 }
141