fileutl_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <apt-pkg/configuration.h>
  7. #include <algorithm>
  8. #include <string>
  9. #include <vector>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <gtest/gtest.h>
  13. #include "file-helpers.h"
  14. static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission,
  15. unsigned int const filemode, APT::Configuration::Compressor const &compressor)
  16. {
  17. std::string trace;
  18. strprintf(trace, "TestFileFd: Compressor: %s umask: %#o permission: %#o mode: %d", compressor.Name.c_str(), a_umask, ExpectedFilePermission, filemode);
  19. SCOPED_TRACE(trace);
  20. static const char* fname = "apt-filefd-test.txt";
  21. if (FileExists(fname) == true)
  22. EXPECT_EQ(0, unlink(fname));
  23. FileFd f;
  24. umask(a_umask);
  25. EXPECT_TRUE(f.Open(fname, filemode, compressor));
  26. EXPECT_TRUE(f.IsOpen());
  27. EXPECT_FALSE(f.Failed());
  28. EXPECT_EQ(umask(a_umask), a_umask);
  29. std::string test = "This is a test!\n";
  30. EXPECT_TRUE(f.Write(test.c_str(), test.size()));
  31. EXPECT_TRUE(f.IsOpen());
  32. EXPECT_FALSE(f.Failed());
  33. f.Close();
  34. EXPECT_FALSE(f.IsOpen());
  35. EXPECT_FALSE(f.Failed());
  36. EXPECT_TRUE(f.Open(fname, FileFd::ReadOnly, compressor));
  37. EXPECT_TRUE(f.IsOpen());
  38. EXPECT_FALSE(f.Failed());
  39. EXPECT_FALSE(f.Eof());
  40. EXPECT_NE(0, f.FileSize());
  41. EXPECT_FALSE(f.Failed());
  42. EXPECT_NE(0, f.ModificationTime());
  43. EXPECT_FALSE(f.Failed());
  44. // ensure the memory is as predictably messed up
  45. #define APT_INIT_READBACK \
  46. char readback[20]; \
  47. memset(readback, 'D', sizeof(readback)*sizeof(readback[0])); \
  48. readback[19] = '\0';
  49. #define EXPECT_N_STR(expect, actual) \
  50. EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
  51. {
  52. APT_INIT_READBACK
  53. char const * const expect = "DDDDDDDDDDDDDDDDDDD";
  54. EXPECT_STREQ(expect,readback);
  55. EXPECT_N_STR(expect, readback);
  56. }
  57. {
  58. APT_INIT_READBACK
  59. char const * const expect = "This";
  60. EXPECT_TRUE(f.Read(readback, strlen(expect)));
  61. EXPECT_FALSE(f.Failed());
  62. EXPECT_FALSE(f.Eof());
  63. EXPECT_N_STR(expect, readback);
  64. EXPECT_EQ(strlen(expect), f.Tell());
  65. }
  66. {
  67. APT_INIT_READBACK
  68. char const * const expect = "test!\n";
  69. EXPECT_TRUE(f.Skip((test.size() - f.Tell()) - strlen(expect)));
  70. EXPECT_TRUE(f.Read(readback, strlen(expect)));
  71. EXPECT_FALSE(f.Failed());
  72. EXPECT_FALSE(f.Eof());
  73. EXPECT_N_STR(expect, readback);
  74. EXPECT_EQ(test.size(), f.Tell());
  75. }
  76. // Non-zero backwards seek
  77. {
  78. APT_INIT_READBACK
  79. char const * const expect = "is";
  80. EXPECT_EQ(test.size(), f.Tell());
  81. EXPECT_TRUE(f.Seek(5));
  82. EXPECT_TRUE(f.Read(readback, strlen(expect)));
  83. EXPECT_FALSE(f.Failed());
  84. EXPECT_FALSE(f.Eof());
  85. EXPECT_N_STR(expect, readback);
  86. EXPECT_EQ(7, f.Tell());
  87. }
  88. {
  89. APT_INIT_READBACK
  90. EXPECT_TRUE(f.Seek(0));
  91. EXPECT_FALSE(f.Eof());
  92. EXPECT_TRUE(f.Read(readback, 20, true));
  93. EXPECT_FALSE(f.Failed());
  94. EXPECT_TRUE(f.Eof());
  95. EXPECT_N_STR(test.c_str(), readback);
  96. EXPECT_EQ(f.Size(), f.Tell());
  97. }
  98. {
  99. APT_INIT_READBACK
  100. EXPECT_TRUE(f.Seek(0));
  101. EXPECT_FALSE(f.Eof());
  102. EXPECT_TRUE(f.Read(readback, test.size(), true));
  103. EXPECT_FALSE(f.Failed());
  104. EXPECT_FALSE(f.Eof());
  105. EXPECT_N_STR(test.c_str(), readback);
  106. EXPECT_EQ(f.Size(), f.Tell());
  107. }
  108. {
  109. APT_INIT_READBACK
  110. EXPECT_TRUE(f.Seek(0));
  111. EXPECT_FALSE(f.Eof());
  112. unsigned long long actual;
  113. EXPECT_TRUE(f.Read(readback, 20, &actual));
  114. EXPECT_FALSE(f.Failed());
  115. EXPECT_TRUE(f.Eof());
  116. EXPECT_EQ(test.size(), actual);
  117. EXPECT_N_STR(test.c_str(), readback);
  118. EXPECT_EQ(f.Size(), f.Tell());
  119. }
  120. {
  121. APT_INIT_READBACK
  122. EXPECT_TRUE(f.Seek(0));
  123. EXPECT_FALSE(f.Eof());
  124. f.ReadLine(readback, 20);
  125. EXPECT_FALSE(f.Failed());
  126. EXPECT_FALSE(f.Eof());
  127. EXPECT_EQ(test, readback);
  128. EXPECT_EQ(f.Size(), f.Tell());
  129. }
  130. {
  131. APT_INIT_READBACK
  132. EXPECT_TRUE(f.Seek(0));
  133. EXPECT_FALSE(f.Eof());
  134. char const * const expect = "This";
  135. f.ReadLine(readback, strlen(expect) + 1);
  136. EXPECT_FALSE(f.Failed());
  137. EXPECT_FALSE(f.Eof());
  138. EXPECT_N_STR(expect, readback);
  139. EXPECT_EQ(strlen(expect), f.Tell());
  140. }
  141. #undef APT_INIT_READBACK
  142. f.Close();
  143. EXPECT_FALSE(f.IsOpen());
  144. EXPECT_FALSE(f.Failed());
  145. // regression test for permission bug LP: #1304657
  146. struct stat buf;
  147. EXPECT_EQ(0, stat(fname, &buf));
  148. EXPECT_EQ(0, unlink(fname));
  149. EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777);
  150. }
  151. static void TestFileFd(unsigned int const filemode)
  152. {
  153. auto const compressors = APT::Configuration::getCompressors();
  154. EXPECT_EQ(7, compressors.size());
  155. bool atLeastOneWasTested = false;
  156. for (auto const &c: compressors)
  157. {
  158. if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite &&
  159. (c.Name.empty() != true && c.Binary.empty() != true))
  160. continue;
  161. atLeastOneWasTested = true;
  162. TestFileFd(0002, 0664, filemode, c);
  163. TestFileFd(0022, 0644, filemode, c);
  164. TestFileFd(0077, 0600, filemode, c);
  165. TestFileFd(0026, 0640, filemode, c);
  166. }
  167. EXPECT_TRUE(atLeastOneWasTested);
  168. }
  169. TEST(FileUtlTest, FileFD)
  170. {
  171. // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries
  172. _config->Set("APT::Compressor::rev::Name", "rev");
  173. _config->Set("APT::Compressor::rev::Extension", ".reversed");
  174. _config->Set("APT::Compressor::rev::Binary", "rev");
  175. _config->Set("APT::Compressor::rev::Cost", 10);
  176. auto const compressors = APT::Configuration::getCompressors(false);
  177. EXPECT_EQ(7, compressors.size());
  178. EXPECT_TRUE(std::any_of(compressors.begin(), compressors.end(), [](APT::Configuration::Compressor const &c) { return c.Name == "rev"; }));
  179. std::string const startdir = SafeGetCWD();
  180. EXPECT_FALSE(startdir.empty());
  181. std::string tempdir;
  182. createTemporaryDirectory("filefd", tempdir);
  183. EXPECT_EQ(0, chdir(tempdir.c_str()));
  184. TestFileFd(FileFd::WriteOnly | FileFd::Create);
  185. TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty);
  186. TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive);
  187. TestFileFd(FileFd::WriteOnly | FileFd::Atomic);
  188. TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic);
  189. // short-hands for ReadWrite with these modes
  190. TestFileFd(FileFd::WriteEmpty);
  191. TestFileFd(FileFd::WriteAny);
  192. TestFileFd(FileFd::WriteTemp);
  193. TestFileFd(FileFd::WriteAtomic);
  194. EXPECT_EQ(0, chdir(startdir.c_str()));
  195. removeDirectory(tempdir);
  196. }
  197. TEST(FileUtlTest, Glob)
  198. {
  199. std::vector<std::string> files;
  200. // normal match
  201. files = Glob("*MakeLists.txt");
  202. EXPECT_EQ(1, files.size());
  203. // not there
  204. files = Glob("xxxyyyzzz");
  205. EXPECT_TRUE(files.empty());
  206. EXPECT_FALSE(_error->PendingError());
  207. // many matches (number is a bit random)
  208. files = Glob("*.cc");
  209. EXPECT_LT(10, files.size());
  210. }
  211. TEST(FileUtlTest, GetTempDir)
  212. {
  213. char const * const envtmp = getenv("TMPDIR");
  214. std::string old_tmpdir;
  215. if (envtmp != NULL)
  216. old_tmpdir = envtmp;
  217. unsetenv("TMPDIR");
  218. EXPECT_EQ("/tmp", GetTempDir());
  219. setenv("TMPDIR", "", 1);
  220. EXPECT_EQ("/tmp", GetTempDir());
  221. setenv("TMPDIR", "/not-there-no-really-not", 1);
  222. EXPECT_EQ("/tmp", GetTempDir());
  223. // root can access everything, so /usr will be accepted
  224. if (geteuid() != 0)
  225. {
  226. // here but not accessible for non-roots
  227. setenv("TMPDIR", "/usr", 1);
  228. EXPECT_EQ("/tmp", GetTempDir());
  229. }
  230. // files are no good for tmpdirs, too
  231. setenv("TMPDIR", "/dev/null", 1);
  232. EXPECT_EQ("/tmp", GetTempDir());
  233. setenv("TMPDIR", "/var/tmp", 1);
  234. EXPECT_EQ("/var/tmp", GetTempDir());
  235. unsetenv("TMPDIR");
  236. if (old_tmpdir.empty() == false)
  237. setenv("TMPDIR", old_tmpdir.c_str(), 1);
  238. }
  239. TEST(FileUtlTest, Popen)
  240. {
  241. FileFd Fd;
  242. pid_t Child;
  243. char buf[1024];
  244. std::string s;
  245. unsigned long long n = 0;
  246. std::vector<std::string> OpenFds;
  247. // count Fds to ensure we don't have a resource leak
  248. if(FileExists("/proc/self/fd"))
  249. OpenFds = Glob("/proc/self/fd/*");
  250. // output something
  251. const char* Args[10] = {"/bin/echo", "meepmeep", NULL};
  252. EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::ReadOnly));
  253. EXPECT_TRUE(Fd.Read(buf, sizeof(buf)-1, &n));
  254. buf[n] = 0;
  255. EXPECT_NE(n, 0);
  256. EXPECT_STREQ(buf, "meepmeep\n");
  257. // wait for the child to exit and cleanup
  258. EXPECT_TRUE(ExecWait(Child, "PopenRead"));
  259. EXPECT_TRUE(Fd.Close());
  260. // ensure that after a close all is good again
  261. if(FileExists("/proc/self/fd"))
  262. EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds.size());
  263. // ReadWrite is not supported
  264. _error->PushToStack();
  265. EXPECT_FALSE(Popen(Args, Fd, Child, FileFd::ReadWrite));
  266. EXPECT_FALSE(Fd.IsOpen());
  267. EXPECT_FALSE(Fd.Failed());
  268. EXPECT_TRUE(_error->PendingError());
  269. _error->RevertToStack();
  270. // write something
  271. Args[0] = "/bin/bash";
  272. Args[1] = "-c";
  273. Args[2] = "read";
  274. Args[3] = NULL;
  275. EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly));
  276. s = "\n";
  277. EXPECT_TRUE(Fd.Write(s.c_str(), s.length()));
  278. EXPECT_TRUE(Fd.Close());
  279. EXPECT_FALSE(Fd.IsOpen());
  280. EXPECT_FALSE(Fd.Failed());
  281. EXPECT_TRUE(ExecWait(Child, "PopenWrite"));
  282. }
  283. TEST(FileUtlTest, flAbsPath)
  284. {
  285. std::string cwd = SafeGetCWD();
  286. int res = chdir("/etc/");
  287. EXPECT_EQ(res, 0);
  288. std::string p = flAbsPath("passwd");
  289. EXPECT_EQ(p, "/etc/passwd");
  290. res = chdir(cwd.c_str());
  291. EXPECT_EQ(res, 0);
  292. }
  293. static void TestDevNullFileFd(unsigned int const filemode)
  294. {
  295. SCOPED_TRACE(filemode);
  296. FileFd f("/dev/null", filemode);
  297. EXPECT_FALSE(f.Failed());
  298. EXPECT_TRUE(f.IsOpen());
  299. EXPECT_TRUE(f.IsOpen());
  300. std::string test = "This is a test!\n";
  301. EXPECT_TRUE(f.Write(test.c_str(), test.size()));
  302. EXPECT_TRUE(f.IsOpen());
  303. EXPECT_FALSE(f.Failed());
  304. f.Close();
  305. EXPECT_FALSE(f.IsOpen());
  306. EXPECT_FALSE(f.Failed());
  307. }
  308. TEST(FileUtlTest, WorkingWithDevNull)
  309. {
  310. TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create);
  311. TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty);
  312. TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive);
  313. TestDevNullFileFd(FileFd::WriteOnly | FileFd::Atomic);
  314. TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic);
  315. // short-hands for ReadWrite with these modes
  316. TestDevNullFileFd(FileFd::WriteEmpty);
  317. TestDevNullFileFd(FileFd::WriteAny);
  318. TestDevNullFileFd(FileFd::WriteTemp);
  319. TestDevNullFileFd(FileFd::WriteAtomic);
  320. }
  321. constexpr char const * const TESTSTRING = "This is a test";
  322. static void TestFailingAtomicKeepsFile(char const * const label, std::string const &filename)
  323. {
  324. SCOPED_TRACE(label);
  325. EXPECT_TRUE(FileExists(filename));
  326. FileFd fd;
  327. EXPECT_TRUE(fd.Open(filename, FileFd::ReadOnly));
  328. char buffer[50];
  329. EXPECT_NE(nullptr, fd.ReadLine(buffer, sizeof(buffer)));
  330. EXPECT_STREQ(TESTSTRING, buffer);
  331. }
  332. TEST(FileUtlTest, FailingAtomic)
  333. {
  334. FileFd fd;
  335. std::string filename;
  336. createTemporaryFile("failingatomic", fd, &filename, TESTSTRING);
  337. TestFailingAtomicKeepsFile("init", filename);
  338. FileFd f;
  339. EXPECT_TRUE(f.Open(filename, FileFd::ReadWrite | FileFd::Atomic));
  340. f.EraseOnFailure();
  341. EXPECT_FALSE(f.Failed());
  342. EXPECT_TRUE(f.IsOpen());
  343. TestFailingAtomicKeepsFile("before-fail", filename);
  344. EXPECT_TRUE(f.Write("Bad file write", 10));
  345. f.OpFail();
  346. EXPECT_TRUE(f.Failed());
  347. TestFailingAtomicKeepsFile("after-fail", filename);
  348. EXPECT_TRUE(f.Close());
  349. TestFailingAtomicKeepsFile("closed", filename);
  350. if (filename.empty() == false)
  351. unlink(filename.c_str());
  352. }