summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChristos Zoulas <christos@zoulas.com>2022-10-09 16:45:12 +0000
committerChristos Zoulas <christos@zoulas.com>2022-10-09 16:45:12 +0000
commitb74150490be904801378b2712fe0d532e1700124 (patch)
treedbb2c647e313ef217793fa045d099d343316ad50 /tests
parent3503cc0c92100fec9d20fec7379984b5dd899dcd (diff)
downloadfile-git-b74150490be904801378b2712fe0d532e1700124.tar.gz
Add multiple magic with continuation test
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am30
-rw-r--r--tests/multiple-A.magic2
-rw-r--r--tests/multiple-B.magic2
-rw-r--r--tests/multiple.flags1
-rw-r--r--tests/multiple.result1
-rw-r--r--tests/multiple.testfile1
-rw-r--r--tests/test.c52
7 files changed, 66 insertions, 23 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c581258a..831f7a96 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -57,6 +57,11 @@ jsonlines1.testfile \
jsonlines1.result \
matilde.arm.result \
matilde.arm.testfile \
+multiple-A.magic \
+multiple-B.magic \
+multiple.flags \
+multiple.result \
+multiple.testfile \
pcjr.result \
pcjr.testfile \
pgp-binary-key-v2-phil.result \
@@ -127,14 +132,27 @@ zstd-v0.8-FF.testfile
T = $(top_srcdir)/tests
check-local:
- MAGIC=$(top_builddir)/magic/magic ./test
set -e; \
for i in $T/*.testfile; do \
- echo Running test: $$i; \
- if [ -f $${i%%.testfile}.magic ]; then \
- m=$${i%%.testfile}.magic; \
- else \
+ t=$${i%%.testfile}; \
+ echo Running test: $$t; \
+ m=; \
+ for j in $$(eval echo $${t}\*.magic); do \
+ if [ -f "$$j" ]; then \
+ if [ -z "$$m" ]; then \
+ m=$$j; \
+ else \
+ m=$$m:$$j; \
+ fi \
+ fi \
+ done; \
+ if [ -z "$$m" ]; then \
m=$(top_builddir)/magic/magic; \
fi; \
- TZ=UTC MAGIC=$$m ./test $$i $${i%%.testfile}.result; \
+ f=-e; \
+ if [ -f $${t}.flags ]; then \
+ f=$$f$$(cat $${t}.flags); \
+ fi; \
+ echo TZ=UTC MAGIC=$$m ./test $$f $$i $${t}.result; \
+ TZ=UTC MAGIC=$$m ./test $$f $$i $${t}.result; \
done
diff --git a/tests/multiple-A.magic b/tests/multiple-A.magic
new file mode 100644
index 00000000..7709bb2c
--- /dev/null
+++ b/tests/multiple-A.magic
@@ -0,0 +1,2 @@
+0 search {\\rt1 RTF1.0
+16 search ViVa2 Viva File 2.0
diff --git a/tests/multiple-B.magic b/tests/multiple-B.magic
new file mode 100644
index 00000000..ccc452d7
--- /dev/null
+++ b/tests/multiple-B.magic
@@ -0,0 +1,2 @@
+6 search ABCD ABCD File
+10 search TesT Test File 1.0
diff --git a/tests/multiple.flags b/tests/multiple.flags
new file mode 100644
index 00000000..b68fde2a
--- /dev/null
+++ b/tests/multiple.flags
@@ -0,0 +1 @@
+k
diff --git a/tests/multiple.result b/tests/multiple.result
new file mode 100644
index 00000000..8a8c5dfa
--- /dev/null
+++ b/tests/multiple.result
@@ -0,0 +1 @@
+RTF1.0\012- Viva File 2.0\012- ABCD File\012- Test File 1.0, ASCII text, with no line terminators
diff --git a/tests/multiple.testfile b/tests/multiple.testfile
new file mode 100644
index 00000000..388979a6
--- /dev/null
+++ b/tests/multiple.testfile
@@ -0,0 +1 @@
+{\rt1 ABCDTesT xxViVa2 \ No newline at end of file
diff --git a/tests/test.c b/tests/test.c
index 8ff9614e..a942ba5c 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -27,6 +27,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
#include <errno.h>
@@ -76,11 +77,11 @@ slurp(FILE *fp, size_t *final_len)
int
main(int argc, char **argv)
{
- struct magic_set *ms;
+ struct magic_set *ms = NULL;
const char *result;
size_t result_len, desired_len;
char *desired = NULL;
- int e = EXIT_FAILURE;
+ int e = EXIT_FAILURE, flags, c;
FILE *fp;
@@ -90,7 +91,32 @@ main(int argc, char **argv)
else
prog = argv[0];
- ms = magic_open(MAGIC_ERROR);
+ if (argc == 1)
+ return 0;
+
+ flags = 0;
+ while ((c = getopt(argc, argv, "ek")) != -1)
+ switch (c) {
+ case 'e':
+ flags |= MAGIC_ERROR;
+ break;
+ case 'k':
+ flags |= MAGIC_CONTINUE;
+ break;
+ default:
+ goto usage;
+ }
+
+ argc -= optind;
+ argv += optind;
+ if (argc != 2) {
+usage:
+ (void)fprintf(stderr,
+ "Usage: %s [-ek] TEST-FILE RESULT\n", prog);
+ goto bad;
+ }
+
+ ms = magic_open(flags);
if (ms == NULL) {
(void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n",
prog, strerror(errno));
@@ -102,29 +128,20 @@ main(int argc, char **argv)
goto bad;
}
- if (argc == 1) {
- e = 0;
- goto bad;
- }
-
- if (argc != 3) {
- (void)fprintf(stderr, "Usage: %s TEST-FILE RESULT\n", prog);
- goto bad;
- }
- if ((result = magic_file(ms, argv[1])) == NULL) {
+ if ((result = magic_file(ms, argv[0])) == NULL) {
(void)fprintf(stderr, "%s: ERROR loading file %s: %s\n",
prog, argv[1], magic_error(ms));
goto bad;
}
- fp = fopen(argv[2], "r");
+ fp = fopen(argv[1], "r");
if (fp == NULL) {
(void)fprintf(stderr, "%s: ERROR opening `%s': %s",
- prog, argv[2], strerror(errno));
+ prog, argv[1], strerror(errno));
goto bad;
}
desired = slurp(fp, &desired_len);
fclose(fp);
- (void)printf("%s: %s\n", argv[1], result);
+ (void)printf("%s: %s\n", argv[0], result);
if (strcmp(result, desired) != 0) {
result_len = strlen(result);
(void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n"
@@ -135,6 +152,7 @@ main(int argc, char **argv)
e = 0;
bad:
free(desired);
- magic_close(ms);
+ if (ms)
+ magic_close(ms);
return e;
}