]>
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>
13 #include <gtest/gtest.h>
15 #include "file-helpers.h"
17 static void TestFileFd(mode_t
const a_umask
, mode_t
const ExpectedFilePermission
,
18 unsigned int const filemode
, APT::Configuration::Compressor
const &compressor
)
21 strprintf(trace
, "TestFileFd: Compressor: %s umask: %#o permission: %#o mode: %d", compressor
.Name
.c_str(), a_umask
, ExpectedFilePermission
, filemode
);
24 static const char* fname
= "apt-filefd-test.txt";
25 if (FileExists(fname
) == true)
26 EXPECT_EQ(0, unlink(fname
));
30 EXPECT_TRUE(f
.Open(fname
, filemode
, compressor
));
31 EXPECT_TRUE(f
.IsOpen());
32 EXPECT_FALSE(f
.Failed());
33 EXPECT_EQ(umask(a_umask
), a_umask
);
35 std::string test
= "This is a test!\n";
36 EXPECT_TRUE(f
.Write(test
.c_str(), test
.size()));
37 EXPECT_TRUE(f
.IsOpen());
38 EXPECT_FALSE(f
.Failed());
41 EXPECT_FALSE(f
.IsOpen());
42 EXPECT_FALSE(f
.Failed());
44 EXPECT_TRUE(f
.Open(fname
, FileFd::ReadOnly
, compressor
));
45 EXPECT_TRUE(f
.IsOpen());
46 EXPECT_FALSE(f
.Failed());
47 EXPECT_FALSE(f
.Eof());
48 EXPECT_NE(0, f
.FileSize());
49 EXPECT_FALSE(f
.Failed());
50 EXPECT_NE(0, f
.ModificationTime());
51 EXPECT_FALSE(f
.Failed());
53 // ensure the memory is as predictably messed up
54 #define APT_INIT_READBACK \
56 memset(readback, 'D', sizeof(readback)*sizeof(readback[0])); \
58 #define EXPECT_N_STR(expect, actual) \
59 EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
62 char const * const expect
= "DDDDDDDDDDDDDDDDDDD";
63 EXPECT_STREQ(expect
,readback
);
64 EXPECT_N_STR(expect
, readback
);
68 char const * const expect
= "This";
69 EXPECT_TRUE(f
.Read(readback
, strlen(expect
)));
70 EXPECT_FALSE(f
.Failed());
71 EXPECT_FALSE(f
.Eof());
72 EXPECT_N_STR(expect
, readback
);
73 EXPECT_EQ(strlen(expect
), f
.Tell());
77 char const * const expect
= "test!\n";
78 EXPECT_TRUE(f
.Skip((test
.size() - f
.Tell()) - strlen(expect
)));
79 EXPECT_TRUE(f
.Read(readback
, strlen(expect
)));
80 EXPECT_FALSE(f
.Failed());
81 EXPECT_FALSE(f
.Eof());
82 EXPECT_N_STR(expect
, readback
);
83 EXPECT_EQ(test
.size(), f
.Tell());
85 // Non-zero backwards seek
88 char const * const expect
= "is";
89 EXPECT_EQ(test
.size(), f
.Tell());
90 EXPECT_TRUE(f
.Seek(5));
91 EXPECT_TRUE(f
.Read(readback
, strlen(expect
)));
92 EXPECT_FALSE(f
.Failed());
93 EXPECT_FALSE(f
.Eof());
94 EXPECT_N_STR(expect
, readback
);
95 EXPECT_EQ(7, f
.Tell());
99 EXPECT_TRUE(f
.Seek(0));
100 EXPECT_FALSE(f
.Eof());
101 EXPECT_TRUE(f
.Read(readback
, 20, true));
102 EXPECT_FALSE(f
.Failed());
103 EXPECT_TRUE(f
.Eof());
104 EXPECT_N_STR(test
.c_str(), readback
);
105 EXPECT_EQ(f
.Size(), f
.Tell());
109 EXPECT_TRUE(f
.Seek(0));
110 EXPECT_FALSE(f
.Eof());
111 EXPECT_TRUE(f
.Read(readback
, test
.size(), true));
112 EXPECT_FALSE(f
.Failed());
113 EXPECT_FALSE(f
.Eof());
114 EXPECT_N_STR(test
.c_str(), readback
);
115 EXPECT_EQ(f
.Size(), f
.Tell());
119 EXPECT_TRUE(f
.Seek(0));
120 EXPECT_FALSE(f
.Eof());
121 unsigned long long actual
;
122 EXPECT_TRUE(f
.Read(readback
, 20, &actual
));
123 EXPECT_FALSE(f
.Failed());
124 EXPECT_TRUE(f
.Eof());
125 EXPECT_EQ(test
.size(), actual
);
126 EXPECT_N_STR(test
.c_str(), readback
);
127 EXPECT_EQ(f
.Size(), f
.Tell());
131 EXPECT_TRUE(f
.Seek(0));
132 EXPECT_FALSE(f
.Eof());
133 f
.ReadLine(readback
, 20);
134 EXPECT_FALSE(f
.Failed());
135 EXPECT_FALSE(f
.Eof());
136 EXPECT_EQ(test
, readback
);
137 EXPECT_EQ(f
.Size(), f
.Tell());
141 EXPECT_TRUE(f
.Seek(0));
142 EXPECT_FALSE(f
.Eof());
143 char const * const expect
= "This";
144 f
.ReadLine(readback
, strlen(expect
) + 1);
145 EXPECT_FALSE(f
.Failed());
146 EXPECT_FALSE(f
.Eof());
147 EXPECT_N_STR(expect
, readback
);
148 EXPECT_EQ(strlen(expect
), f
.Tell());
150 #undef APT_INIT_READBACK
153 EXPECT_FALSE(f
.IsOpen());
154 EXPECT_FALSE(f
.Failed());
156 // regression test for permission bug LP: #1304657
158 EXPECT_EQ(0, stat(fname
, &buf
));
159 EXPECT_EQ(0, unlink(fname
));
160 EXPECT_EQ(ExpectedFilePermission
, buf
.st_mode
& 0777);
163 static void TestFileFd(unsigned int const filemode
)
165 std::vector
<APT::Configuration::Compressor
> compressors
= APT::Configuration::getCompressors();
167 // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries
168 compressors
.push_back(APT::Configuration::Compressor("rev", ".reversed", "rev", NULL
, NULL
, 42));
169 //compressors.push_back(APT::Configuration::Compressor("cat", ".ident", "cat", NULL, NULL, 42));
171 for (std::vector
<APT::Configuration::Compressor
>::const_iterator c
= compressors
.begin(); c
!= compressors
.end(); ++c
)
173 if ((filemode
& FileFd::ReadWrite
) == FileFd::ReadWrite
&&
174 (c
->Name
.empty() != true && c
->Binary
.empty() != 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
);
183 TEST(FileUtlTest
, FileFD
)
185 std::string
const startdir
= SafeGetCWD();
186 EXPECT_FALSE(startdir
.empty());
188 createTemporaryDirectory("filefd", tempdir
);
189 EXPECT_EQ(0, chdir(tempdir
.c_str()));
191 TestFileFd(FileFd::WriteOnly
| FileFd::Create
);
192 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
193 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
);
194 TestFileFd(FileFd::WriteOnly
| FileFd::Atomic
);
195 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
);
196 // short-hands for ReadWrite with these modes
197 TestFileFd(FileFd::WriteEmpty
);
198 TestFileFd(FileFd::WriteAny
);
199 TestFileFd(FileFd::WriteTemp
);
200 TestFileFd(FileFd::WriteAtomic
);
202 EXPECT_EQ(0, chdir(startdir
.c_str()));
203 removeDirectory(tempdir
);
205 TEST(FileUtlTest
, Glob
)
207 std::vector
<std::string
> files
;
209 files
= Glob("*akefile");
210 EXPECT_EQ(1, files
.size());
213 files
= Glob("xxxyyyzzz");
214 EXPECT_TRUE(files
.empty());
215 EXPECT_FALSE(_error
->PendingError());
217 // many matches (number is a bit random)
218 files
= Glob("*.cc");
219 EXPECT_LT(10, files
.size());
221 TEST(FileUtlTest
, GetTempDir
)
223 char const * const envtmp
= getenv("TMPDIR");
224 std::string old_tmpdir
;
229 EXPECT_EQ("/tmp", GetTempDir());
231 setenv("TMPDIR", "", 1);
232 EXPECT_EQ("/tmp", GetTempDir());
234 setenv("TMPDIR", "/not-there-no-really-not", 1);
235 EXPECT_EQ("/tmp", GetTempDir());
237 // root can access everything, so /usr will be accepted
240 // here but not accessible for non-roots
241 setenv("TMPDIR", "/usr", 1);
242 EXPECT_EQ("/tmp", GetTempDir());
245 // files are no good for tmpdirs, too
246 setenv("TMPDIR", "/dev/null", 1);
247 EXPECT_EQ("/tmp", GetTempDir());
249 setenv("TMPDIR", "/var/tmp", 1);
250 EXPECT_EQ("/var/tmp", GetTempDir());
253 if (old_tmpdir
.empty() == false)
254 setenv("TMPDIR", old_tmpdir
.c_str(), 1);
256 TEST(FileUtlTest
, Popen
)
262 unsigned long long n
= 0;
263 std::vector
<std::string
> OpenFds
;
265 // count Fds to ensure we don't have a resource leak
266 if(FileExists("/proc/self/fd"))
267 OpenFds
= Glob("/proc/self/fd/*");
270 const char* Args
[10] = {"/bin/echo", "meepmeep", NULL
};
271 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::ReadOnly
));
272 EXPECT_TRUE(Fd
.Read(buf
, sizeof(buf
)-1, &n
));
275 EXPECT_STREQ(buf
, "meepmeep\n");
277 // wait for the child to exit and cleanup
278 EXPECT_TRUE(ExecWait(Child
, "PopenRead"));
279 EXPECT_TRUE(Fd
.Close());
281 // ensure that after a close all is good again
282 if(FileExists("/proc/self/fd"))
283 EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds
.size());
285 // ReadWrite is not supported
286 _error
->PushToStack();
287 EXPECT_FALSE(Popen(Args
, Fd
, Child
, FileFd::ReadWrite
));
288 EXPECT_FALSE(Fd
.IsOpen());
289 EXPECT_FALSE(Fd
.Failed());
290 EXPECT_TRUE(_error
->PendingError());
291 _error
->RevertToStack();
294 Args
[0] = "/bin/bash";
298 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::WriteOnly
));
300 EXPECT_TRUE(Fd
.Write(s
.c_str(), s
.length()));
301 EXPECT_TRUE(Fd
.Close());
302 EXPECT_FALSE(Fd
.IsOpen());
303 EXPECT_FALSE(Fd
.Failed());
304 EXPECT_TRUE(ExecWait(Child
, "PopenWrite"));
306 TEST(FileUtlTest
, flAbsPath
)
308 std::string cwd
= SafeGetCWD();
309 int res
= chdir("/etc/");
311 std::string p
= flAbsPath("passwd");
312 EXPECT_EQ(p
, "/etc/passwd");
314 res
= chdir(cwd
.c_str());
318 static void TestDevNullFileFd(unsigned int const filemode
)
320 FileFd
f("/dev/null", filemode
);
321 EXPECT_FALSE(f
.Failed());
322 EXPECT_TRUE(f
.IsOpen());
323 EXPECT_TRUE(f
.IsOpen());
325 std::string test
= "This is a test!\n";
326 EXPECT_TRUE(f
.Write(test
.c_str(), test
.size()));
327 EXPECT_TRUE(f
.IsOpen());
328 EXPECT_FALSE(f
.Failed());
331 EXPECT_FALSE(f
.IsOpen());
332 EXPECT_FALSE(f
.Failed());
334 TEST(FileUtlTest
, WorkingWithDevNull
)
336 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
);
337 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
338 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
);
339 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Atomic
);
340 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
);
341 // short-hands for ReadWrite with these modes
342 TestDevNullFileFd(FileFd::WriteEmpty
);
343 TestDevNullFileFd(FileFd::WriteAny
);
344 TestDevNullFileFd(FileFd::WriteTemp
);
345 TestDevNullFileFd(FileFd::WriteAtomic
);