You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

84 lines
2.5 KiB

  1. #include <apt-pkg/fileutl.h>
  2. #include <string>
  3. #include <fcntl.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <gtest/gtest.h>
  8. #include "file-helpers.h"
  9. void helperCreateTemporaryDirectory(std::string const &id, std::string &dir)
  10. {
  11. std::string const strtempdir = GetTempDir().append("/apt-tests-").append(id).append(".XXXXXX");
  12. char * tempdir = strdup(strtempdir.c_str());
  13. ASSERT_STREQ(tempdir, mkdtemp(tempdir));
  14. dir = tempdir;
  15. free(tempdir);
  16. }
  17. void helperRemoveDirectory(std::string const &dir)
  18. {
  19. // basic sanity check to avoid removing random directories based on earlier failures
  20. if (dir.find("/apt-tests-") == std::string::npos || dir.find_first_of("*?") != std::string::npos)
  21. FAIL() << "Directory '" << dir << "' seems invalid. It is therefore not removed!";
  22. else
  23. ASSERT_EQ(0, system(std::string("rm -rf ").append(dir).c_str()));
  24. }
  25. void helperCreateFile(std::string const &dir, std::string const &name)
  26. {
  27. std::string file = dir;
  28. file.append("/");
  29. file.append(name);
  30. int const fd = creat(file.c_str(), 0600);
  31. ASSERT_NE(-1, fd);
  32. close(fd);
  33. }
  34. void helperCreateDirectory(std::string const &dir, std::string const &name)
  35. {
  36. std::string file = dir;
  37. file.append("/");
  38. file.append(name);
  39. ASSERT_TRUE(CreateDirectory(dir, file));
  40. }
  41. void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname)
  42. {
  43. std::string target = dir;
  44. target.append("/");
  45. target.append(targetname);
  46. std::string link = dir;
  47. link.append("/");
  48. link.append(linkname);
  49. ASSERT_EQ(0, symlink(target.c_str(), link.c_str()));
  50. }
  51. void helperCreateTemporaryFile(std::string const &id, FileFd &fd, std::string * const filename, char const * const content)
  52. {
  53. std::string name("apt-test-");
  54. name.append(id);
  55. size_t const giventmp = name.find(".XXXXXX.");
  56. if (giventmp == std::string::npos)
  57. name.append(".XXXXXX");
  58. char * tempfile = strdup(name.c_str());
  59. ASSERT_STRNE(NULL, tempfile);
  60. int tempfile_fd;
  61. if (giventmp == std::string::npos)
  62. tempfile_fd = mkstemp(tempfile);
  63. else
  64. tempfile_fd = mkstemps(tempfile, name.length() - (giventmp + 7));
  65. ASSERT_NE(-1, tempfile_fd);
  66. if (filename != NULL)
  67. *filename = tempfile;
  68. else
  69. unlink(tempfile);
  70. free(tempfile);
  71. EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite, true));
  72. if (content != NULL)
  73. {
  74. ASSERT_TRUE(fd.Write(content, strlen(content)));
  75. fd.Seek(0);
  76. }
  77. }