test.c 3.8 KB

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