testc_helpers.c 3.2 KB

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