test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include "magic.h"
  33. static const char *prog;
  34. static void *
  35. xrealloc(void *p, size_t n)
  36. {
  37. p = realloc(p, n);
  38. if (p == NULL) {
  39. (void)fprintf(stderr, "%s ERROR slurping file: %s\n",
  40. prog, strerror(errno));
  41. exit(10);
  42. }
  43. return p;
  44. }
  45. static char *
  46. slurp(FILE *fp, size_t *final_len)
  47. {
  48. size_t len = 256;
  49. int c;
  50. char *l = xrealloc(NULL, len), *s = l;
  51. for (c = getc(fp); c != EOF; c = getc(fp)) {
  52. if (s == l + len) {
  53. s = l + len;
  54. len *= 2;
  55. l = xrealloc(l, len);
  56. }
  57. *s++ = c;
  58. }
  59. if (s != l && s[-1] == '\n')
  60. s--;
  61. if (s == l + len) {
  62. l = xrealloc(l, len + 1);
  63. s = l + len;
  64. }
  65. *s++ = '\0';
  66. *final_len = s - l;
  67. return xrealloc(l, s - l);
  68. }
  69. int
  70. main(int argc, char **argv)
  71. {
  72. struct magic_set *ms = NULL;
  73. const char *result;
  74. size_t result_len, desired_len;
  75. char *desired = NULL;
  76. int e = EXIT_FAILURE, flags, c;
  77. FILE *fp;
  78. prog = strrchr(argv[0], '/');
  79. if (prog)
  80. prog++;
  81. else
  82. prog = argv[0];
  83. if (argc == 1)
  84. return 0;
  85. flags = 0;
  86. while ((c = getopt(argc, argv, "ek")) != -1)
  87. switch (c) {
  88. case 'e':
  89. flags |= MAGIC_ERROR;
  90. break;
  91. case 'k':
  92. flags |= MAGIC_CONTINUE;
  93. break;
  94. default:
  95. goto usage;
  96. }
  97. argc -= optind;
  98. argv += optind;
  99. if (argc != 2) {
  100. usage:
  101. (void)fprintf(stderr,
  102. "Usage: %s [-ek] TEST-FILE RESULT\n", prog);
  103. goto bad;
  104. }
  105. ms = magic_open(flags);
  106. if (ms == NULL) {
  107. (void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n",
  108. prog, strerror(errno));
  109. return e;
  110. }
  111. if (magic_load(ms, NULL) == -1) {
  112. (void)fprintf(stderr, "%s: ERROR loading with NULL file: %s\n",
  113. prog, magic_error(ms));
  114. goto bad;
  115. }
  116. if ((result = magic_file(ms, argv[0])) == NULL) {
  117. (void)fprintf(stderr, "%s: ERROR loading file %s: %s\n",
  118. prog, argv[1], magic_error(ms));
  119. goto bad;
  120. }
  121. fp = fopen(argv[1], "r");
  122. if (fp == NULL) {
  123. (void)fprintf(stderr, "%s: ERROR opening `%s': %s",
  124. prog, argv[1], strerror(errno));
  125. goto bad;
  126. }
  127. desired = slurp(fp, &desired_len);
  128. fclose(fp);
  129. (void)printf("%s: %s\n", argv[0], result);
  130. if (strcmp(result, desired) != 0) {
  131. result_len = strlen(result);
  132. (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n"
  133. "expected (len %zu)\n%s\n", prog, result_len, result,
  134. desired_len, desired);
  135. goto bad;
  136. }
  137. e = 0;
  138. bad:
  139. free(desired);
  140. if (ms)
  141. magic_close(ms);
  142. return e;
  143. }