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.
 
 
 
 
 
 

396 lines
12 KiB

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