fileutl_test.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include <config.h>
  2. #include <apt-pkg/error.h>
  3. #include <apt-pkg/fileutl.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <apt-pkg/aptconfiguration.h>
  6. #include <string>
  7. #include <vector>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <gtest/gtest.h>
  11. #include "file-helpers.h"
  12. static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission,
  13. unsigned int const filemode, APT::Configuration::Compressor const &compressor)
  14. {
  15. std::string trace;
  16. strprintf(trace, "TestFileFd: Compressor: %s umask: %#o permission: %#o mode: %d", compressor.Name.c_str(), a_umask, ExpectedFilePermission, filemode);
  17. SCOPED_TRACE(trace);
  18. static const char* fname = "apt-filefd-test.txt";
  19. if (FileExists(fname) == true)
  20. EXPECT_EQ(0, unlink(fname));
  21. FileFd f;
  22. umask(a_umask);
  23. EXPECT_TRUE(f.Open(fname, filemode, compressor));
  24. EXPECT_TRUE(f.IsOpen());
  25. EXPECT_FALSE(f.Failed());
  26. EXPECT_EQ(umask(a_umask), a_umask);
  27. std::string test = "This is a test!\n";
  28. EXPECT_TRUE(f.Write(test.c_str(), test.size()));
  29. EXPECT_TRUE(f.IsOpen());
  30. EXPECT_FALSE(f.Failed());
  31. f.Close();
  32. EXPECT_FALSE(f.IsOpen());
  33. EXPECT_FALSE(f.Failed());
  34. EXPECT_TRUE(f.Open(fname, FileFd::ReadOnly, compressor));
  35. EXPECT_TRUE(f.IsOpen());
  36. EXPECT_FALSE(f.Failed());
  37. EXPECT_FALSE(f.Eof());
  38. EXPECT_NE(0, f.FileSize());
  39. EXPECT_FALSE(f.Failed());
  40. EXPECT_NE(0, f.ModificationTime());
  41. EXPECT_FALSE(f.Failed());
  42. // ensure the memory is as predictably messed up
  43. #define APT_INIT_READBACK \
  44. char readback[20]; \
  45. memset(readback, 'D', sizeof(readback)/sizeof(readback[0])); \
  46. readback[19] = '\0';
  47. #define EXPECT_N_STR(expect, actual) \
  48. EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
  49. {
  50. APT_INIT_READBACK
  51. char const * const expect = "This";
  52. EXPECT_TRUE(f.Read(readback, strlen(expect)));
  53. EXPECT_FALSE(f.Failed());
  54. EXPECT_FALSE(f.Eof());
  55. EXPECT_N_STR(expect, readback);
  56. EXPECT_EQ(strlen(expect), f.Tell());
  57. }
  58. {
  59. APT_INIT_READBACK
  60. char const * const expect = "test!\n";
  61. EXPECT_TRUE(f.Skip((test.size() - f.Tell()) - strlen(expect)));
  62. EXPECT_TRUE(f.Read(readback, strlen(expect)));
  63. EXPECT_FALSE(f.Failed());
  64. EXPECT_FALSE(f.Eof());
  65. EXPECT_N_STR(expect, readback);
  66. EXPECT_EQ(test.size(), f.Tell());
  67. }
  68. {
  69. APT_INIT_READBACK
  70. EXPECT_TRUE(f.Seek(0));
  71. EXPECT_FALSE(f.Eof());
  72. EXPECT_TRUE(f.Read(readback, 20, true));
  73. EXPECT_FALSE(f.Failed());
  74. EXPECT_TRUE(f.Eof());
  75. EXPECT_N_STR(test.c_str(), readback);
  76. EXPECT_EQ(f.Size(), f.Tell());
  77. }
  78. {
  79. APT_INIT_READBACK
  80. EXPECT_TRUE(f.Seek(0));
  81. EXPECT_FALSE(f.Eof());
  82. EXPECT_TRUE(f.Read(readback, test.size(), true));
  83. EXPECT_FALSE(f.Failed());
  84. EXPECT_FALSE(f.Eof());
  85. EXPECT_N_STR(test.c_str(), readback);
  86. EXPECT_EQ(f.Size(), f.Tell());
  87. }
  88. {
  89. APT_INIT_READBACK
  90. EXPECT_TRUE(f.Seek(0));
  91. EXPECT_FALSE(f.Eof());
  92. unsigned long long actual;
  93. EXPECT_TRUE(f.Read(readback, 20, &actual));
  94. EXPECT_FALSE(f.Failed());
  95. EXPECT_TRUE(f.Eof());
  96. EXPECT_EQ(test.size(), actual);
  97. EXPECT_N_STR(test.c_str(), readback);
  98. EXPECT_EQ(f.Size(), f.Tell());
  99. }
  100. {
  101. APT_INIT_READBACK
  102. EXPECT_TRUE(f.Seek(0));
  103. EXPECT_FALSE(f.Eof());
  104. f.ReadLine(readback, 20);
  105. EXPECT_FALSE(f.Failed());
  106. EXPECT_FALSE(f.Eof());
  107. EXPECT_EQ(test, readback);
  108. EXPECT_EQ(f.Size(), f.Tell());
  109. }
  110. {
  111. APT_INIT_READBACK
  112. EXPECT_TRUE(f.Seek(0));
  113. EXPECT_FALSE(f.Eof());
  114. char const * const expect = "This";
  115. f.ReadLine(readback, strlen(expect) + 1);
  116. EXPECT_FALSE(f.Failed());
  117. EXPECT_FALSE(f.Eof());
  118. EXPECT_N_STR(expect, readback);
  119. EXPECT_EQ(strlen(expect), f.Tell());
  120. }
  121. #undef APT_INIT_READBACK
  122. f.Close();
  123. EXPECT_FALSE(f.IsOpen());
  124. EXPECT_FALSE(f.Failed());
  125. // regression test for permission bug LP: #1304657
  126. struct stat buf;
  127. EXPECT_EQ(0, stat(fname, &buf));
  128. EXPECT_EQ(0, unlink(fname));
  129. EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777);
  130. }
  131. static void TestFileFd(unsigned int const filemode)
  132. {
  133. std::vector<APT::Configuration::Compressor> compressors = APT::Configuration::getCompressors();
  134. // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries
  135. compressors.push_back(APT::Configuration::Compressor("rev", ".reversed", "rev", NULL, NULL, 42));
  136. //compressors.push_back(APT::Configuration::Compressor("cat", ".ident", "cat", NULL, NULL, 42));
  137. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
  138. {
  139. if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite &&
  140. (c->Name.empty() != true && c->Binary.empty() != true))
  141. continue;
  142. TestFileFd(0002, 0664, filemode, *c);
  143. TestFileFd(0022, 0644, filemode, *c);
  144. TestFileFd(0077, 0600, filemode, *c);
  145. TestFileFd(0026, 0640, filemode, *c);
  146. }
  147. }
  148. TEST(FileUtlTest, FileFD)
  149. {
  150. std::string const startdir = SafeGetCWD();
  151. EXPECT_FALSE(startdir.empty());
  152. std::string tempdir;
  153. createTemporaryDirectory("filefd", tempdir);
  154. EXPECT_EQ(0, chdir(tempdir.c_str()));
  155. TestFileFd(FileFd::WriteOnly | FileFd::Create);
  156. TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty);
  157. TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive);
  158. TestFileFd(FileFd::WriteOnly | FileFd::Atomic);
  159. TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic);
  160. // short-hands for ReadWrite with these modes
  161. TestFileFd(FileFd::WriteEmpty);
  162. TestFileFd(FileFd::WriteAny);
  163. TestFileFd(FileFd::WriteTemp);
  164. TestFileFd(FileFd::WriteAtomic);
  165. EXPECT_EQ(0, chdir(startdir.c_str()));
  166. removeDirectory(tempdir);
  167. }
  168. TEST(FileUtlTest, Glob)
  169. {
  170. std::vector<std::string> files;
  171. // normal match
  172. files = Glob("*akefile");
  173. EXPECT_EQ(1, files.size());
  174. // not there
  175. files = Glob("xxxyyyzzz");
  176. EXPECT_TRUE(files.empty());
  177. EXPECT_FALSE(_error->PendingError());
  178. // many matches (number is a bit random)
  179. files = Glob("*.cc");
  180. EXPECT_LT(10, files.size());
  181. }
  182. TEST(FileUtlTest, GetTempDir)
  183. {
  184. char const * const envtmp = getenv("TMPDIR");
  185. std::string old_tmpdir;
  186. if (envtmp != NULL)
  187. old_tmpdir = envtmp;
  188. unsetenv("TMPDIR");
  189. EXPECT_EQ("/tmp", GetTempDir());
  190. setenv("TMPDIR", "", 1);
  191. EXPECT_EQ("/tmp", GetTempDir());
  192. setenv("TMPDIR", "/not-there-no-really-not", 1);
  193. EXPECT_EQ("/tmp", GetTempDir());
  194. setenv("TMPDIR", "/usr", 1);
  195. EXPECT_EQ("/usr", GetTempDir());
  196. unsetenv("TMPDIR");
  197. if (old_tmpdir.empty() == false)
  198. setenv("TMPDIR", old_tmpdir.c_str(), 1);
  199. }
  200. TEST(FileUtlTest, Popen)
  201. {
  202. FileFd Fd;
  203. pid_t Child;
  204. char buf[1024];
  205. std::string s;
  206. unsigned long long n = 0;
  207. std::vector<std::string> OpenFds;
  208. // count Fds to ensure we don't have a resource leak
  209. if(FileExists("/proc/self/fd"))
  210. OpenFds = Glob("/proc/self/fd/*");
  211. // output something
  212. const char* Args[10] = {"/bin/echo", "meepmeep", NULL};
  213. bool res = Popen(Args, Fd, Child, FileFd::ReadOnly);
  214. Fd.Read(buf, sizeof(buf)-1, &n);
  215. buf[n] = 0;
  216. EXPECT_NE(n, 0);
  217. EXPECT_EQ(res, true);
  218. EXPECT_STREQ(buf, "meepmeep\n");
  219. // wait for the child to exit and cleanup
  220. ExecWait(Child, "PopenRead");
  221. Fd.Close();
  222. // ensure that after a close all is good again
  223. if(FileExists("/proc/self/fd"))
  224. EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds.size());
  225. // ReadWrite is not supported
  226. res = Popen(Args, Fd, Child, FileFd::ReadWrite);
  227. EXPECT_EQ(res, false);
  228. _error->Discard();
  229. // write something
  230. Args[0] = "/bin/bash";
  231. Args[1] = "-c";
  232. Args[2] = "read";
  233. Args[3] = NULL;
  234. res = Popen(Args, Fd, Child, FileFd::WriteOnly);
  235. s = "\n";
  236. Fd.Write(s.c_str(), s.size());
  237. Fd.Close();
  238. ExecWait(Child, "PopenWrite");
  239. }
  240. TEST(FileUtlTest, flAbsPath)
  241. {
  242. std::string cwd = SafeGetCWD();
  243. int res = chdir("/bin/");
  244. EXPECT_EQ(res, 0);
  245. std::string p = flAbsPath("ls");
  246. EXPECT_EQ(p, "/bin/ls");
  247. res = chdir(cwd.c_str());
  248. EXPECT_EQ(res, 0);
  249. }