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