testc_helpers.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /** @file testc_helpers.c
  2. * @brief Testc helper functions
  3. *
  4. * This file implements helpers for writing tests for 'testc' utility.
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <errno.h>
  12. #include "faux/ctype.h"
  13. #include "faux/str.h"
  14. #include "faux/file.h"
  15. #include "faux/testc_helpers.h"
  16. ssize_t faux_testc_file_deploy(const char *fn, const void *buf, size_t len)
  17. {
  18. faux_file_t *f = NULL;
  19. ssize_t bytes_written = 0;
  20. assert(fn);
  21. assert(buf);
  22. if (!fn || !buf)
  23. return -1;
  24. f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 00644);
  25. if (!f)
  26. return -1;
  27. bytes_written = faux_file_write_block(f, buf, len);
  28. faux_file_close(f);
  29. if (bytes_written < 0)
  30. return -1;
  31. return bytes_written;
  32. }
  33. ssize_t faux_testc_file_deploy_str(const char *fn, const char *str)
  34. {
  35. assert(str);
  36. if (!str)
  37. return -1;
  38. return faux_testc_file_deploy(fn, str, strlen(str));
  39. }
  40. char *faux_testc_tmpfile_deploy(const void *buf, size_t len)
  41. {
  42. char *template = NULL;
  43. int fd = -1;
  44. faux_file_t *f = NULL;
  45. ssize_t bytes_written = 0;
  46. char *env_tmpdir = NULL;
  47. assert(buf);
  48. if (!buf)
  49. return NULL;
  50. env_tmpdir = getenv(FAUX_TESTC_TMPDIR_ENV);
  51. if (env_tmpdir)
  52. template = faux_str_sprintf("%s/tmpfile-XXXXXX", env_tmpdir);
  53. else
  54. template = faux_str_sprintf("/tmp/testc-tmpfile-XXXXXX");
  55. assert(template);
  56. if (!template)
  57. return NULL;
  58. fd = mkstemp(template);
  59. if (fd < 0)
  60. return NULL;
  61. f = faux_file_fdopen(fd);
  62. if (!f)
  63. return NULL;
  64. bytes_written = faux_file_write_block(f, buf, len);
  65. faux_file_close(f);
  66. if (bytes_written < 0)
  67. return NULL;
  68. return template;
  69. }
  70. char *faux_testc_tmpfile_deploy_str(const char *str)
  71. {
  72. assert(str);
  73. if (!str)
  74. return NULL;
  75. return faux_testc_tmpfile_deploy(str, strlen(str));
  76. }
  77. #define CHUNK_SIZE 1024
  78. int faux_testc_file_cmp(const char *first_file, const char *second_file)
  79. {
  80. int ret = -1; // Pessimistic retval
  81. faux_file_t *f = NULL;
  82. faux_file_t *s = NULL;
  83. char buf_f[CHUNK_SIZE];
  84. char buf_s[CHUNK_SIZE];
  85. ssize_t readed_f = 0;
  86. ssize_t readed_s = 0;
  87. assert(first_file);
  88. assert(second_file);
  89. if (!first_file || !second_file)
  90. return -1;
  91. f = faux_file_open(first_file, O_RDONLY, 0);
  92. s = faux_file_open(second_file, O_RDONLY, 0);
  93. if (!f || !s)
  94. goto cmp_error;
  95. do {
  96. readed_f = faux_file_read_block(f, buf_f, CHUNK_SIZE);
  97. readed_s = faux_file_read_block(s, buf_s, CHUNK_SIZE);
  98. if (readed_f != readed_s)
  99. goto cmp_error;
  100. if (readed_f < 0)
  101. goto cmp_error;
  102. if (0 == readed_f)
  103. break; // EOF
  104. if (memcmp(buf_f, buf_s, readed_f) != 0)
  105. goto cmp_error;
  106. } while (CHUNK_SIZE == readed_f); // Not full chunk so EOF is near
  107. ret = 0; // Equal
  108. cmp_error:
  109. faux_file_close(f);
  110. faux_file_close(s);
  111. return ret;
  112. }
  113. bool_t faux_testc_fill_rnd(void *buf, size_t len)
  114. {
  115. char *b = (char *)buf;
  116. size_t pos = 0;
  117. assert(buf);
  118. if (!buf)
  119. return BOOL_FALSE;
  120. if (0 == len)
  121. return BOOL_FALSE;
  122. for (pos = 0; pos < len; pos++) {
  123. b[pos] = (char)random();
  124. }
  125. return BOOL_TRUE;
  126. }
  127. char *faux_testc_rnd_buf(size_t len)
  128. {
  129. char *buf = NULL;
  130. if (0 == len)
  131. return NULL;
  132. buf = faux_malloc(len);
  133. assert(buf);
  134. if (!buf)
  135. return NULL;
  136. if (!faux_testc_fill_rnd(buf, len)) {
  137. faux_free(buf);
  138. return NULL;
  139. }
  140. return buf;
  141. }