]>
git.saurik.com Git - apt.git/blob - test/libapt/fileutl_test.cc
3 #include <apt-pkg/error.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/strutl.h>
6 #include <apt-pkg/aptconfiguration.h>
7 #include <apt-pkg/configuration.h>
15 #include <gtest/gtest.h>
17 #include "file-helpers.h"
19 static void TestFileFd(mode_t
const a_umask
, mode_t
const ExpectedFilePermission
,
20 unsigned int const filemode
, APT::Configuration::Compressor
const &compressor
)
23 strprintf(trace
, "TestFileFd: Compressor: %s umask: %#o permission: %#o mode: %d", compressor
.Name
.c_str(), a_umask
, ExpectedFilePermission
, filemode
);
26 static const char* fname
= "apt-filefd-test.txt";
27 if (FileExists(fname
) == true)
28 EXPECT_EQ(0, unlink(fname
));
32 EXPECT_TRUE(f
.Open(fname
, filemode
, compressor
));
33 EXPECT_TRUE(f
.IsOpen());
34 EXPECT_FALSE(f
.Failed());
35 EXPECT_EQ(umask(a_umask
), a_umask
);
37 std::string test
= "This is a test!\n";
38 EXPECT_TRUE(f
.Write(test
.c_str(), test
.size()));
39 EXPECT_TRUE(f
.IsOpen());
40 EXPECT_FALSE(f
.Failed());
43 EXPECT_FALSE(f
.IsOpen());
44 EXPECT_FALSE(f
.Failed());
46 EXPECT_TRUE(f
.Open(fname
, FileFd::ReadOnly
, compressor
));
47 EXPECT_TRUE(f
.IsOpen());
48 EXPECT_FALSE(f
.Failed());
49 EXPECT_FALSE(f
.Eof());
50 EXPECT_NE(0, f
.FileSize());
51 EXPECT_FALSE(f
.Failed());
52 EXPECT_NE(0, f
.ModificationTime());
53 EXPECT_FALSE(f
.Failed());
55 // ensure the memory is as predictably messed up
56 #define APT_INIT_READBACK \
58 memset(readback, 'D', sizeof(readback)*sizeof(readback[0])); \
60 #define EXPECT_N_STR(expect, actual) \
61 EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
64 char const * const expect
= "DDDDDDDDDDDDDDDDDDD";
65 EXPECT_STREQ(expect
,readback
);
66 EXPECT_N_STR(expect
, readback
);
70 char const * const expect
= "This";
71 EXPECT_TRUE(f
.Read(readback
, strlen(expect
)));
72 EXPECT_FALSE(f
.Failed());
73 EXPECT_FALSE(f
.Eof());
74 EXPECT_N_STR(expect
, readback
);
75 EXPECT_EQ(strlen(expect
), f
.Tell());
79 char const * const expect
= "test!\n";
80 EXPECT_TRUE(f
.Skip((test
.size() - f
.Tell()) - strlen(expect
)));
81 EXPECT_TRUE(f
.Read(readback
, strlen(expect
)));
82 EXPECT_FALSE(f
.Failed());
83 EXPECT_FALSE(f
.Eof());
84 EXPECT_N_STR(expect
, readback
);
85 EXPECT_EQ(test
.size(), f
.Tell());
87 // Non-zero backwards seek
90 char const * const expect
= "is";
91 EXPECT_EQ(test
.size(), f
.Tell());
92 EXPECT_TRUE(f
.Seek(5));
93 EXPECT_TRUE(f
.Read(readback
, strlen(expect
)));
94 EXPECT_FALSE(f
.Failed());
95 EXPECT_FALSE(f
.Eof());
96 EXPECT_N_STR(expect
, readback
);
97 EXPECT_EQ(7, f
.Tell());
101 EXPECT_TRUE(f
.Seek(0));
102 EXPECT_FALSE(f
.Eof());
103 EXPECT_TRUE(f
.Read(readback
, 20, true));
104 EXPECT_FALSE(f
.Failed());
105 EXPECT_TRUE(f
.Eof());
106 EXPECT_N_STR(test
.c_str(), readback
);
107 EXPECT_EQ(f
.Size(), f
.Tell());
111 EXPECT_TRUE(f
.Seek(0));
112 EXPECT_FALSE(f
.Eof());
113 EXPECT_TRUE(f
.Read(readback
, test
.size(), true));
114 EXPECT_FALSE(f
.Failed());
115 EXPECT_FALSE(f
.Eof());
116 EXPECT_N_STR(test
.c_str(), readback
);
117 EXPECT_EQ(f
.Size(), f
.Tell());
121 EXPECT_TRUE(f
.Seek(0));
122 EXPECT_FALSE(f
.Eof());
123 unsigned long long actual
;
124 EXPECT_TRUE(f
.Read(readback
, 20, &actual
));
125 EXPECT_FALSE(f
.Failed());
126 EXPECT_TRUE(f
.Eof());
127 EXPECT_EQ(test
.size(), actual
);
128 EXPECT_N_STR(test
.c_str(), readback
);
129 EXPECT_EQ(f
.Size(), f
.Tell());
133 EXPECT_TRUE(f
.Seek(0));
134 EXPECT_FALSE(f
.Eof());
135 f
.ReadLine(readback
, 20);
136 EXPECT_FALSE(f
.Failed());
137 EXPECT_FALSE(f
.Eof());
138 EXPECT_EQ(test
, readback
);
139 EXPECT_EQ(f
.Size(), f
.Tell());
143 EXPECT_TRUE(f
.Seek(0));
144 EXPECT_FALSE(f
.Eof());
145 char const * const expect
= "This";
146 f
.ReadLine(readback
, strlen(expect
) + 1);
147 EXPECT_FALSE(f
.Failed());
148 EXPECT_FALSE(f
.Eof());
149 EXPECT_N_STR(expect
, readback
);
150 EXPECT_EQ(strlen(expect
), f
.Tell());
152 #undef APT_INIT_READBACK
155 EXPECT_FALSE(f
.IsOpen());
156 EXPECT_FALSE(f
.Failed());
158 // regression test for permission bug LP: #1304657
160 EXPECT_EQ(0, stat(fname
, &buf
));
161 EXPECT_EQ(0, unlink(fname
));
162 EXPECT_EQ(ExpectedFilePermission
, buf
.st_mode
& 0777);
165 static void TestFileFd(unsigned int const filemode
)
167 auto const compressors
= APT::Configuration::getCompressors();
168 EXPECT_EQ(7, compressors
.size());
169 bool atLeastOneWasTested
= false;
170 for (auto const &c
: compressors
)
172 if ((filemode
& FileFd::ReadWrite
) == FileFd::ReadWrite
&&
173 (c
.Name
.empty() != true && c
.Binary
.empty() != true))
175 atLeastOneWasTested
= true;
176 TestFileFd(0002, 0664, filemode
, c
);
177 TestFileFd(0022, 0644, filemode
, c
);
178 TestFileFd(0077, 0600, filemode
, c
);
179 TestFileFd(0026, 0640, filemode
, c
);
181 EXPECT_TRUE(atLeastOneWasTested
);
184 TEST(FileUtlTest
, FileFD
)
186 // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries
187 _config
->Set("APT::Compressor::rev::Name", "rev");
188 _config
->Set("APT::Compressor::rev::Extension", ".reversed");
189 _config
->Set("APT::Compressor::rev::Binary", "rev");
190 _config
->Set("APT::Compressor::rev::Cost", 10);
191 auto const compressors
= APT::Configuration::getCompressors(false);
192 EXPECT_EQ(7, compressors
.size());
193 EXPECT_TRUE(std::any_of(compressors
.begin(), compressors
.end(), [](APT::Configuration::Compressor
const &c
) { return c
.Name
== "rev"; }));
195 std::string
const startdir
= SafeGetCWD();
196 EXPECT_FALSE(startdir
.empty());
198 createTemporaryDirectory("filefd", tempdir
);
199 EXPECT_EQ(0, chdir(tempdir
.c_str()));
201 TestFileFd(FileFd::WriteOnly
| FileFd::Create
);
202 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
203 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
);
204 TestFileFd(FileFd::WriteOnly
| FileFd::Atomic
);
205 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
);
206 // short-hands for ReadWrite with these modes
207 TestFileFd(FileFd::WriteEmpty
);
208 TestFileFd(FileFd::WriteAny
);
209 TestFileFd(FileFd::WriteTemp
);
210 TestFileFd(FileFd::WriteAtomic
);
212 EXPECT_EQ(0, chdir(startdir
.c_str()));
213 removeDirectory(tempdir
);
215 TEST(FileUtlTest
, Glob
)
217 std::vector
<std::string
> files
;
219 files
= Glob("*MakeLists.txt");
220 EXPECT_EQ(1, files
.size());
223 files
= Glob("xxxyyyzzz");
224 EXPECT_TRUE(files
.empty());
225 EXPECT_FALSE(_error
->PendingError());
227 // many matches (number is a bit random)
228 files
= Glob("*.cc");
229 EXPECT_LT(10, files
.size());
231 TEST(FileUtlTest
, GetTempDir
)
233 char const * const envtmp
= getenv("TMPDIR");
234 std::string old_tmpdir
;
239 EXPECT_EQ("/tmp", GetTempDir());
241 setenv("TMPDIR", "", 1);
242 EXPECT_EQ("/tmp", GetTempDir());
244 setenv("TMPDIR", "/not-there-no-really-not", 1);
245 EXPECT_EQ("/tmp", GetTempDir());
247 // root can access everything, so /usr will be accepted
250 // here but not accessible for non-roots
251 setenv("TMPDIR", "/usr", 1);
252 EXPECT_EQ("/tmp", GetTempDir());
255 // files are no good for tmpdirs, too
256 setenv("TMPDIR", "/dev/null", 1);
257 EXPECT_EQ("/tmp", GetTempDir());
259 setenv("TMPDIR", "/var/tmp", 1);
260 EXPECT_EQ("/var/tmp", GetTempDir());
263 if (old_tmpdir
.empty() == false)
264 setenv("TMPDIR", old_tmpdir
.c_str(), 1);
266 TEST(FileUtlTest
, Popen
)
272 unsigned long long n
= 0;
273 std::vector
<std::string
> OpenFds
;
275 // count Fds to ensure we don't have a resource leak
276 if(FileExists("/proc/self/fd"))
277 OpenFds
= Glob("/proc/self/fd/*");
280 const char* Args
[10] = {"/bin/echo", "meepmeep", NULL
};
281 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::ReadOnly
));
282 EXPECT_TRUE(Fd
.Read(buf
, sizeof(buf
)-1, &n
));
285 EXPECT_STREQ(buf
, "meepmeep\n");
287 // wait for the child to exit and cleanup
288 EXPECT_TRUE(ExecWait(Child
, "PopenRead"));
289 EXPECT_TRUE(Fd
.Close());
291 // ensure that after a close all is good again
292 if(FileExists("/proc/self/fd"))
293 EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds
.size());
295 // ReadWrite is not supported
296 _error
->PushToStack();
297 EXPECT_FALSE(Popen(Args
, Fd
, Child
, FileFd::ReadWrite
));
298 EXPECT_FALSE(Fd
.IsOpen());
299 EXPECT_FALSE(Fd
.Failed());
300 EXPECT_TRUE(_error
->PendingError());
301 _error
->RevertToStack();
304 Args
[0] = "/bin/bash";
308 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::WriteOnly
));
310 EXPECT_TRUE(Fd
.Write(s
.c_str(), s
.length()));
311 EXPECT_TRUE(Fd
.Close());
312 EXPECT_FALSE(Fd
.IsOpen());
313 EXPECT_FALSE(Fd
.Failed());
314 EXPECT_TRUE(ExecWait(Child
, "PopenWrite"));
316 TEST(FileUtlTest
, flAbsPath
)
318 std::string cwd
= SafeGetCWD();
319 int res
= chdir("/etc/");
321 std::string p
= flAbsPath("passwd");
322 EXPECT_EQ(p
, "/etc/passwd");
324 res
= chdir(cwd
.c_str());
328 static void TestDevNullFileFd(unsigned int const filemode
)
330 SCOPED_TRACE(filemode
);
331 FileFd
f("/dev/null", filemode
);
332 EXPECT_FALSE(f
.Failed());
333 EXPECT_TRUE(f
.IsOpen());
334 EXPECT_TRUE(f
.IsOpen());
336 std::string test
= "This is a test!\n";
337 EXPECT_TRUE(f
.Write(test
.c_str(), test
.size()));
338 EXPECT_TRUE(f
.IsOpen());
339 EXPECT_FALSE(f
.Failed());
342 EXPECT_FALSE(f
.IsOpen());
343 EXPECT_FALSE(f
.Failed());
345 TEST(FileUtlTest
, WorkingWithDevNull
)
347 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
);
348 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
349 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
);
350 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Atomic
);
351 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
);
352 // short-hands for ReadWrite with these modes
353 TestDevNullFileFd(FileFd::WriteEmpty
);
354 TestDevNullFileFd(FileFd::WriteAny
);
355 TestDevNullFileFd(FileFd::WriteTemp
);
356 TestDevNullFileFd(FileFd::WriteAtomic
);
358 constexpr char const * const TESTSTRING
= "This is a test";
359 static void TestFailingAtomicKeepsFile(char const * const label
, std::string
const &filename
)
362 EXPECT_TRUE(FileExists(filename
));
364 EXPECT_TRUE(fd
.Open(filename
, FileFd::ReadOnly
));
366 EXPECT_NE(nullptr, fd
.ReadLine(buffer
, sizeof(buffer
)));
367 EXPECT_STREQ(TESTSTRING
, buffer
);
369 TEST(FileUtlTest
, FailingAtomic
)
372 std::string filename
;
373 createTemporaryFile("failingatomic", fd
, &filename
, TESTSTRING
);
374 TestFailingAtomicKeepsFile("init", filename
);
377 EXPECT_TRUE(f
.Open(filename
, FileFd::ReadWrite
| FileFd::Atomic
));
379 EXPECT_FALSE(f
.Failed());
380 EXPECT_TRUE(f
.IsOpen());
381 TestFailingAtomicKeepsFile("before-fail", filename
);
382 EXPECT_TRUE(f
.Write("Bad file write", 10));
384 EXPECT_TRUE(f
.Failed());
385 TestFailingAtomicKeepsFile("after-fail", filename
);
386 EXPECT_TRUE(f
.Close());
387 TestFailingAtomicKeepsFile("closed", filename
);
389 if (filename
.empty() == false)
390 unlink(filename
.c_str());