test.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 + len)
  58. l = (char *)xrealloc(l, len + 1);
  59. *s++ = '\0';
  60. *final_len = s - l;
  61. l = (char *)xrealloc(l, s - l);
  62. return l;
  63. }
  64. int
  65. main(int argc, char **argv)
  66. {
  67. struct magic_set *ms;
  68. const char *result;
  69. size_t result_len, desired_len;
  70. char *desired = NULL;
  71. int e = EXIT_FAILURE;
  72. FILE *fp;
  73. prog = strrchr(argv[0], '/');
  74. if (prog)
  75. prog++;
  76. else
  77. prog = argv[0];
  78. ms = magic_open(MAGIC_NONE);
  79. if (ms == NULL) {
  80. (void)fprintf(stderr, "%s: ERROR opening MAGIC_NONE: %s\n",
  81. prog, strerror(errno));
  82. return e;
  83. }
  84. if (magic_load(ms, NULL) == -1) {
  85. (void)fprintf(stderr, "%s: ERROR loading with NULL file: %s\n",
  86. prog, magic_error(ms));
  87. goto bad;
  88. }
  89. if (argc == 1) {
  90. e = 0;
  91. goto bad;
  92. }
  93. if (argc != 3) {
  94. (void)fprintf(stderr, "Usage: %s TEST-FILE RESULT\n", prog);
  95. magic_close(ms);
  96. goto bad;
  97. }
  98. if ((result = magic_file(ms, argv[1])) == NULL) {
  99. (void)fprintf(stderr, "%s: ERROR loading file %s: %s\n",
  100. prog, argv[1], magic_error(ms));
  101. goto bad;
  102. }
  103. fp = fopen(argv[2], "r");
  104. if (fp == NULL) {
  105. (void)fprintf(stderr, "%s: ERROR opening `%s': %s",
  106. prog, argv[2], strerror(errno));
  107. goto bad;
  108. }
  109. desired = slurp(fp, &desired_len);
  110. fclose(fp);
  111. (void)printf("%s: %s\n", argv[1], result);
  112. if (strcmp(result, desired) != 0) {
  113. result_len = strlen(result);
  114. (void)fprintf(stderr, "%s: ERROR: result was (len %zu)\n%s\n"
  115. "expected (len %zu)\n%s\n", prog, result_len, result,
  116. desired_len, desired);
  117. goto bad;
  118. }
  119. e = 0;
  120. bad:
  121. free(desired);
  122. magic_close(ms);
  123. return e;
  124. }