TestLibCMkTemp.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <LibCore/File.h>
  9. #include <LibTest/TestCase.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/mman.h>
  15. #include <sys/wait.h>
  16. #include <unistd.h>
  17. TEST_CASE(test_mktemp_unique_filename)
  18. {
  19. u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  20. EXPECT(ptr != MAP_FAILED);
  21. if (fork() == 0) {
  22. char path[] = "/tmp/test.mktemp.XXXXXX";
  23. auto temp_path = String::formatted("{}", mktemp(path));
  24. EXPECT(temp_path.characters());
  25. unlink(path);
  26. memcpy(&ptr[0], temp_path.characters(), temp_path.length());
  27. exit(EXIT_SUCCESS);
  28. } else {
  29. wait(NULL);
  30. auto path1 = String::formatted("{}", reinterpret_cast<const char*>(ptr));
  31. char path[] = "/tmp/test.mktemp.XXXXXX";
  32. auto path2 = String::formatted("{}", mktemp(path));
  33. EXPECT(path2.characters());
  34. unlink(path);
  35. EXPECT_NE(path1, path2);
  36. }
  37. munmap(ptr, sizeof(*ptr));
  38. }
  39. TEST_CASE(test_mkdtemp_unique_filename)
  40. {
  41. u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  42. EXPECT_NE(ptr, MAP_FAILED);
  43. if (fork() == 0) {
  44. char path[] = "/tmp/test.mkdtemp.XXXXXX";
  45. auto temp_path = String::formatted("{}", mkdtemp(path));
  46. EXPECT(temp_path.characters());
  47. rmdir(path);
  48. memcpy(&ptr[0], temp_path.characters(), temp_path.length());
  49. exit(EXIT_SUCCESS);
  50. } else {
  51. wait(NULL);
  52. auto path1 = String::formatted("{}", reinterpret_cast<const char*>(ptr));
  53. char path[] = "/tmp/test.mkdtemp.XXXXXX";
  54. auto path2 = String::formatted("{}", mkdtemp(path));
  55. EXPECT(path2.characters());
  56. rmdir(path);
  57. EXPECT_NE(path1, path2);
  58. }
  59. munmap(ptr, sizeof(*ptr));
  60. }
  61. TEST_CASE(test_mkstemp_unique_filename)
  62. {
  63. u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  64. EXPECT_NE(ptr, MAP_FAILED);
  65. if (fork() == 0) {
  66. char path[] = "/tmp/test.mkstemp.XXXXXX";
  67. auto fd = mkstemp(path);
  68. EXPECT_NE(fd, -1);
  69. auto temp_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
  70. EXPECT(temp_path.characters());
  71. close(fd);
  72. unlink(path);
  73. memcpy(&ptr[0], temp_path.characters(), temp_path.length());
  74. exit(EXIT_SUCCESS);
  75. } else {
  76. wait(NULL);
  77. auto path1 = String::formatted("{}", reinterpret_cast<const char*>(ptr));
  78. char path[] = "/tmp/test.mkstemp.XXXXXX";
  79. auto fd = mkstemp(path);
  80. EXPECT(fd != -1);
  81. auto path2 = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
  82. EXPECT(path2.characters());
  83. close(fd);
  84. unlink(path);
  85. EXPECT_NE(path1, path2);
  86. }
  87. munmap(ptr, sizeof(*ptr));
  88. }