tst-mkostemps.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Test application for mkstemp/mkstemps/mkostemp/mkostemps
  3. * Copyright (C) 2015 by Romain Naour <romain.naour@openwide.fr>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <errno.h>
  13. #define assert(x) \
  14. if (!(x)) \
  15. { \
  16. fputs ("test failed: " #x "\n", stderr); \
  17. retval = 1; \
  18. goto the_end; \
  19. }
  20. int
  21. main (int argc, char *argv[])
  22. {
  23. char name[256];
  24. char name_suffix[256];
  25. FILE *fp = NULL;
  26. int retval = 0;
  27. int fd;
  28. int flags = O_RDONLY | O_CLOEXEC;
  29. struct stat sb_f1;
  30. struct stat sb_f2;
  31. /* mkstemp test */
  32. sprintf(name, "/tmp/%s-uClibc-test.XXXXXX", __FILE__);
  33. fd = mkstemp(name);
  34. fstat(fd, &sb_f1);
  35. assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
  36. stat(name, &sb_f2);
  37. assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
  38. assert (sb_f1.st_ino == sb_f2.st_ino)
  39. close(fd);
  40. unlink (name);
  41. /* mkstemps test */
  42. sprintf(name_suffix, "/tmp/%s-uClibc-test.XXXXXX.txt", __FILE__);
  43. fd = mkstemps(name_suffix, 4);
  44. fstat(fd, &sb_f1);
  45. assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
  46. stat(name_suffix, &sb_f2);
  47. assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
  48. assert (sb_f1.st_ino == sb_f2.st_ino)
  49. close(fd);
  50. unlink (name_suffix);
  51. /* mkostemp test */
  52. sprintf(name, "/tmp/%s-uClibc-test.XXXXXX", __FILE__);
  53. fd = mkostemp(name, flags);
  54. fstat(fd, &sb_f1);
  55. assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
  56. stat(name, &sb_f2);
  57. assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
  58. assert (sb_f1.st_ino == sb_f2.st_ino)
  59. assert (sb_f1.st_mode == sb_f2.st_mode)
  60. close(fd);
  61. unlink (name);
  62. /* mkostemps test */
  63. sprintf(name_suffix, "/tmp/%s-uClibc-test.XXXXXX.txt", __FILE__);
  64. fd = mkostemps(name_suffix, 4, flags);
  65. fstat(fd, &sb_f1);
  66. assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
  67. stat(name_suffix, &sb_f2);
  68. assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
  69. assert (sb_f1.st_ino == sb_f2.st_ino)
  70. assert (sb_f1.st_mode == sb_f2.st_mode)
  71. close(fd);
  72. unlink (name_suffix);
  73. /* suffixlen = 0 */
  74. sprintf(name_suffix, "/tmp/%s-uClibc-test.XXXXXX", __FILE__);
  75. fd = mkostemps(name_suffix, 0, flags);
  76. fstat(fd, &sb_f1);
  77. assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
  78. stat(name_suffix, &sb_f2);
  79. assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
  80. assert (sb_f1.st_ino == sb_f2.st_ino)
  81. assert (sb_f1.st_mode == sb_f2.st_mode)
  82. close(fd);
  83. unlink (name_suffix);
  84. /* stress tests */
  85. /* template len < 6 */
  86. sprintf(name, "XXXXX");
  87. fd = mkstemp(name);
  88. assert(fd == -1);
  89. assert(errno == EINVAL);
  90. /* suffixlen < 0 */
  91. sprintf(name_suffix, "/tmp/%s-uClibc-test.XXXXXX.txt", __FILE__);
  92. fd = mkostemps(name_suffix, -1, flags);
  93. assert(fd == -1);
  94. assert(errno == EINVAL);
  95. /* Missing one X */
  96. sprintf(name_suffix, "/tmp/%s-uClibc-test.XXXXX.txt", __FILE__);
  97. fd = mkostemps(name_suffix, 4, flags);
  98. assert(fd == -1);
  99. assert(errno == EINVAL);
  100. /* wrong suffixlen */
  101. sprintf(name_suffix, "/tmp/%s-uClibc-test.XXXXXX.txt", __FILE__);
  102. fd = mkostemps(name_suffix, 2, flags);
  103. assert(fd == -1);
  104. assert(errno == EINVAL);
  105. the_end:
  106. if (fp != NULL)
  107. assert (fclose (fp) == 0);
  108. unlink (name);
  109. unlink (name_suffix);
  110. return retval;
  111. }