test.c 3.5 KB

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