test-util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright (c) 2020 Red Hat, Inc.
  4. * Author: Sergio Correia <scorreia@redhat.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _XOPEN_SOURCE 500L
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <ftw.h>
  26. #include "test-util.h"
  27. void
  28. assert_func(const char* filename,
  29. int lineno,
  30. const char* funcname,
  31. const char* expr,
  32. const char* fmt,
  33. ...)
  34. {
  35. char buffer[MAX_BUF_LEN] = {};
  36. if (fmt) {
  37. va_list ap;
  38. va_start(ap, fmt);
  39. vsnprintf(buffer, MAX_BUF_LEN, fmt, ap);
  40. va_end(ap);
  41. buffer[strcspn(buffer, "\r\n")] = '\0';
  42. }
  43. fprintf(stderr, "%s:%d: assertion '%s' failed in %s(). %s\n", filename,
  44. lineno,
  45. expr,
  46. funcname,
  47. buffer);
  48. abort();
  49. }
  50. static int
  51. nftw_remove_callback(const char* path, const struct stat* stat,
  52. int type, struct FTW* ftw)
  53. {
  54. return remove(path);
  55. }
  56. char*
  57. create_tempdir(void)
  58. {
  59. char template[] = "/tmp/tang.test.XXXXXX";
  60. char *tmpdir = mkdtemp(template);
  61. return strdup(tmpdir);
  62. }
  63. int
  64. remove_tempdir(const char* path)
  65. {
  66. return nftw(path, nftw_remove_callback, FOPEN_MAX, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
  67. }