xref: /freebsd/contrib/file/tests/test.c (revision 81ad6265)
1 /*
2  * Copyright (c) Christos Zoulas 2003.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 
33 #include "magic.h"
34 
35 static const char *prog;
36 
37 static void *
38 xrealloc(void *p, size_t n)
39 {
40 	p = realloc(p, n);
41 	if (p == NULL) {
42 		(void)fprintf(stderr, "%s ERROR slurping file: %s\n",
43 			prog, strerror(errno));
44 		exit(10);
45 	}
46 	return p;
47 }
48 
49 static char *
50 slurp(FILE *fp, size_t *final_len)
51 {
52 	size_t len = 256;
53 	int c;
54 	char *l = (char *)xrealloc(NULL, len), *s = l;
55 
56 	for (c = getc(fp); c != EOF; c = getc(fp)) {
57 		if (s == l + len) {
58 			l = xrealloc(l, len * 2);
59 			len *= 2;
60 		}
61 		*s++ = c;
62 	}
63 	if (s != l && s[-1] == '\n')
64 		s--;
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 = NULL;
81 	int 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_ERROR);
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 		goto bad;
111 	}
112 	if ((result = magic_file(ms, argv[1])) == NULL) {
113 		(void)fprintf(stderr, "%s: ERROR loading file %s: %s\n",
114 		    prog, argv[1], magic_error(ms));
115 		goto bad;
116 	}
117 	fp = fopen(argv[2], "r");
118 	if (fp == NULL) {
119 		(void)fprintf(stderr, "%s: ERROR opening `%s': %s",
120 		    prog, argv[2], strerror(errno));
121 		goto bad;
122 	}
123 	desired = slurp(fp, &desired_len);
124 	fclose(fp);
125 	(void)printf("%s: %s\n", argv[1], result);
126 	if (strcmp(result, desired) != 0) {
127 	    result_len = strlen(result);
128 	    (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n"
129 		"expected (len %zu)\n%s\n", prog, result_len, result,
130 		desired_len, desired);
131 	    goto bad;
132 	}
133 	e = 0;
134 bad:
135 	free(desired);
136 	magic_close(ms);
137 	return e;
138 }
139