testc_helpers.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 char *str) {
  17. faux_file_t *f = NULL;
  18. ssize_t bytes_written = 0;
  19. assert(fn);
  20. assert(str);
  21. if (!fn || !str)
  22. return -1;
  23. f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 00644);
  24. if (!f)
  25. return -1;
  26. bytes_written = faux_file_write_block(f, str, strlen(str));
  27. faux_file_close(f);
  28. if (bytes_written < 0)
  29. return -1;
  30. return bytes_written;
  31. }
  32. char *faux_testc_tmpfile_deploy(const char *str) {
  33. char *template = NULL;
  34. int fd = -1;
  35. faux_file_t *f = NULL;
  36. ssize_t bytes_written = 0;
  37. char *env_tmpdir = NULL;
  38. assert(str);
  39. if (!str)
  40. return NULL;
  41. env_tmpdir = getenv(FAUX_TESTC_TMPDIR_ENV);
  42. if (env_tmpdir)
  43. template = faux_str_sprintf("%s/tmpfile-XXXXXX", env_tmpdir);
  44. else
  45. template = faux_str_sprintf("/tmp/testc-tmpfile-XXXXXX");
  46. assert(template);
  47. if (!template)
  48. return NULL;
  49. fd = mkstemp(template);
  50. if (fd < 0)
  51. return NULL;
  52. f = faux_file_fdopen(fd);
  53. if (!f)
  54. return NULL;
  55. bytes_written = faux_file_write_block(f, str, strlen(str));
  56. faux_file_close(f);
  57. if (bytes_written < 0)
  58. return NULL;
  59. return template;
  60. }
  61. #define CHUNK_SIZE 1024
  62. int faux_testc_file_cmp(const char *first_file, const char *second_file) {
  63. int ret = -1; // Pessimistic retval
  64. faux_file_t *f = NULL;
  65. faux_file_t *s = NULL;
  66. char buf_f[CHUNK_SIZE];
  67. char buf_s[CHUNK_SIZE];
  68. ssize_t readed_f = 0;
  69. ssize_t readed_s = 0;
  70. assert(first_file);
  71. assert(second_file);
  72. if (!first_file || !second_file)
  73. return -1;
  74. f = faux_file_open(first_file, O_RDONLY, 0);
  75. s = faux_file_open(second_file, O_RDONLY, 0);
  76. if (!f || !s)
  77. goto cmp_error;
  78. do {
  79. readed_f = faux_file_read_block(f, buf_f, CHUNK_SIZE);
  80. readed_s = faux_file_read_block(s, buf_s, CHUNK_SIZE);
  81. if (readed_f != readed_s)
  82. goto cmp_error;
  83. if (readed_f < 0)
  84. goto cmp_error;
  85. if (0 == readed_f)
  86. break; // EOF
  87. if (memcmp(buf_f, buf_s, readed_f) != 0)
  88. goto cmp_error;
  89. } while (CHUNK_SIZE == readed_f); // Not full chunk so EOF is near
  90. ret = 0; // Equal
  91. cmp_error:
  92. faux_file_close(f);
  93. faux_file_close(s);
  94. return ret;
  95. }