test.c revision 1.1.1.4
1/*	$NetBSD: test.c,v 1.1.1.4 2017/09/08 13:22:45 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 "magic.h"
34
35static void *
36xrealloc(void *p, size_t n)
37{
38	p = realloc(p, n);
39	if (p == NULL) {
40		(void)fprintf(stderr, "ERROR slurping file: out of memory\n");
41		exit(10);
42	}
43	return p;
44}
45
46static char *
47slurp(FILE *fp, size_t *final_len)
48{
49	size_t len = 256;
50	int c;
51	char *l = (char *)xrealloc(NULL, len), *s = l;
52
53	for (c = getc(fp); c != EOF; c = getc(fp)) {
54		if (s == l + len) {
55			l = (char *)xrealloc(l, len * 2);
56			len *= 2;
57		}
58		*s++ = c;
59	}
60	if (s == l + len)
61		l = (char *)xrealloc(l, len + 1);
62	*s++ = '\0';
63
64	*final_len = s - l;
65	l = (char *)xrealloc(l, s - l);
66	return l;
67}
68
69int
70main(int argc, char **argv)
71{
72	struct magic_set *ms;
73	const char *result;
74	char *desired;
75	size_t desired_len;
76	int i;
77	FILE *fp;
78
79	ms = magic_open(MAGIC_NONE);
80	if (ms == NULL) {
81		(void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory\n");
82		return 10;
83	}
84	if (magic_load(ms, NULL) == -1) {
85		(void)fprintf(stderr, "ERROR loading with NULL file: %s\n",
86		    magic_error(ms));
87		return 11;
88	}
89
90	if (argc > 1) {
91		if (argc != 3) {
92			(void)fprintf(stderr, "Usage: test TEST-FILE RESULT\n");
93		} else {
94			if ((result = magic_file(ms, argv[1])) == NULL) {
95				(void)fprintf(stderr, "ERROR loading file %s: %s\n", argv[1], magic_error(ms));
96				return 12;
97			} else {
98				fp = fopen(argv[2], "r");
99				if (fp == NULL) {
100					(void)fprintf(stderr, "ERROR opening `%s': ", argv[2]);
101					perror(NULL);
102					return 13;
103				}
104				desired = slurp(fp, &desired_len);
105				fclose(fp);
106				(void)printf("%s: %s\n", argv[1], result);
107                                if (strcmp(result, desired) != 0) {
108					(void)fprintf(stderr, "Error: result was\n%s\nexpected:\n%s\n", result, desired);
109					return 1;
110                                }
111			}
112		}
113	}
114
115	magic_close(ms);
116	return 0;
117}
118