xref: /netbsd-src/external/bsd/file/dist/tests/test.c (revision 25f16eeab3cef3574b7944f484c90bae9f02e957)
1 /*	$NetBSD: test.c,v 1.1.1.6 2021/04/09 18:58:02 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 + len)
66 		l = (char *)xrealloc(l, len + 1);
67 	*s++ = '\0';
68 
69 	*final_len = s - l;
70 	l = (char *)xrealloc(l, s - l);
71 	return l;
72 }
73 
74 int
75 main(int argc, char **argv)
76 {
77 	struct magic_set *ms;
78 	const char *result;
79 	size_t result_len, desired_len;
80 	char *desired;
81 	int i, e = EXIT_FAILURE;
82 	FILE *fp;
83 
84 
85 	prog = strrchr(argv[0], '/');
86 	if (prog)
87 		prog++;
88 	else
89 		prog = argv[0];
90 
91 	ms = magic_open(MAGIC_NONE);
92 	if (ms == NULL) {
93 		(void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n",
94 		    prog, strerror(errno));
95 		return e;
96 	}
97 	if (magic_load(ms, NULL) == -1) {
98 		(void)fprintf(stderr, "%s: ERROR loading with NULL file: %s\n",
99 		    prog, magic_error(ms));
100 		goto bad;
101 	}
102 
103 	if (argc == 1) {
104 		e = 0;
105 		goto bad;
106 	}
107 
108 	if (argc != 3) {
109 		(void)fprintf(stderr, "Usage: %s TEST-FILE RESULT\n", prog);
110 		magic_close(ms);
111 		goto bad;
112 	}
113 	if ((result = magic_file(ms, argv[1])) == NULL) {
114 		(void)fprintf(stderr, "%s: ERROR loading file %s: %s\n",
115 		    prog, argv[1], magic_error(ms));
116 		goto bad;
117 	}
118 	fp = fopen(argv[2], "r");
119 	if (fp == NULL) {
120 		(void)fprintf(stderr, "%s: ERROR opening `%s': %s",
121 		    prog, argv[2], strerror(errno));
122 		goto bad;
123 	}
124 	desired = slurp(fp, &desired_len);
125 	fclose(fp);
126 	(void)printf("%s: %s\n", argv[1], result);
127 	if (strcmp(result, desired) != 0) {
128 	    result_len = strlen(result);
129 	    (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n"
130 		"expected (len %zu)\n%s\n", prog, result_len, result,
131 		desired_len, desired);
132 	    goto bad;
133 	}
134 	e = 0;
135 bad:
136 	magic_close(ms);
137 	return e;
138 }
139