]>
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());
87 EXPECT_TRUE(f
.Seek(0));
88 EXPECT_FALSE(f
.Eof());
89 EXPECT_TRUE(f
.Read(readback
, 20, true));
90 EXPECT_FALSE(f
.Failed());
92 EXPECT_N_STR(test
.c_str(), readback
);
93 EXPECT_EQ(f
.Size(), f
.Tell());
97 EXPECT_TRUE(f
.Seek(0));
98 EXPECT_FALSE(f
.Eof());
99 EXPECT_TRUE(f
.Read(readback
, test
.size(), true));
100 EXPECT_FALSE(f
.Failed());
101 EXPECT_FALSE(f
.Eof());
102 EXPECT_N_STR(test
.c_str(), readback
);
103 EXPECT_EQ(f
.Size(), f
.Tell());
107 EXPECT_TRUE(f
.Seek(0));
108 EXPECT_FALSE(f
.Eof());
109 unsigned long long actual
;
110 EXPECT_TRUE(f
.Read(readback
, 20, &actual
));
111 EXPECT_FALSE(f
.Failed());
112 EXPECT_TRUE(f
.Eof());
113 EXPECT_EQ(test
.size(), actual
);
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 f
.ReadLine(readback
, 20);
122 EXPECT_FALSE(f
.Failed());
123 EXPECT_FALSE(f
.Eof());
124 EXPECT_EQ(test
, readback
);
125 EXPECT_EQ(f
.Size(), f
.Tell());
129 EXPECT_TRUE(f
.Seek(0));
130 EXPECT_FALSE(f
.Eof());
131 char const * const expect
= "This";
132 f
.ReadLine(readback
, strlen(expect
) + 1);
133 EXPECT_FALSE(f
.Failed());
134 EXPECT_FALSE(f
.Eof());
135 EXPECT_N_STR(expect
, readback
);
136 EXPECT_EQ(strlen(expect
), f
.Tell());
138 #undef APT_INIT_READBACK
141 EXPECT_FALSE(f
.IsOpen());
142 EXPECT_FALSE(f
.Failed());
144 // regression test for permission bug LP: #1304657
146 EXPECT_EQ(0, stat(fname
, &buf
));
147 EXPECT_EQ(0, unlink(fname
));
148 EXPECT_EQ(ExpectedFilePermission
, buf
.st_mode
& 0777);
151 static void TestFileFd(unsigned int const filemode
)
153 std::vector
<APT::Configuration::Compressor
> compressors
= APT::Configuration::getCompressors();
155 // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries
156 compressors
.push_back(APT::Configuration::Compressor("rev", ".reversed", "rev", NULL
, NULL
, 42));
157 //compressors.push_back(APT::Configuration::Compressor("cat", ".ident", "cat", NULL, NULL, 42));
159 for (std::vector
<APT::Configuration::Compressor
>::const_iterator c
= compressors
.begin(); c
!= compressors
.end(); ++c
)
161 if ((filemode
& FileFd::ReadWrite
) == FileFd::ReadWrite
&&
162 (c
->Name
.empty() != true && c
->Binary
.empty() != 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
);
171 TEST(FileUtlTest
, FileFD
)
173 std::string
const startdir
= SafeGetCWD();
174 EXPECT_FALSE(startdir
.empty());
176 createTemporaryDirectory("filefd", tempdir
);
177 EXPECT_EQ(0, chdir(tempdir
.c_str()));
179 TestFileFd(FileFd::WriteOnly
| FileFd::Create
);
180 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
181 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
);
182 TestFileFd(FileFd::WriteOnly
| FileFd::Atomic
);
183 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
);
184 // short-hands for ReadWrite with these modes
185 TestFileFd(FileFd::WriteEmpty
);
186 TestFileFd(FileFd::WriteAny
);
187 TestFileFd(FileFd::WriteTemp
);
188 TestFileFd(FileFd::WriteAtomic
);
190 EXPECT_EQ(0, chdir(startdir
.c_str()));
191 removeDirectory(tempdir
);
193 TEST(FileUtlTest
, Glob
)
195 std::vector
<std::string
> files
;
197 files
= Glob("*akefile");
198 EXPECT_EQ(1, files
.size());
201 files
= Glob("xxxyyyzzz");
202 EXPECT_TRUE(files
.empty());
203 EXPECT_FALSE(_error
->PendingError());
205 // many matches (number is a bit random)
206 files
= Glob("*.cc");
207 EXPECT_LT(10, files
.size());
209 TEST(FileUtlTest
, GetTempDir
)
211 char const * const envtmp
= getenv("TMPDIR");
212 std::string old_tmpdir
;
217 EXPECT_EQ("/tmp", GetTempDir());
219 setenv("TMPDIR", "", 1);
220 EXPECT_EQ("/tmp", GetTempDir());
222 setenv("TMPDIR", "/not-there-no-really-not", 1);
223 EXPECT_EQ("/tmp", GetTempDir());
225 // root can access everything, so /usr will be accepted
228 // here but not accessible for non-roots
229 setenv("TMPDIR", "/usr", 1);
230 EXPECT_EQ("/tmp", GetTempDir());
233 // files are no good for tmpdirs, too
234 setenv("TMPDIR", "/dev/null", 1);
235 EXPECT_EQ("/tmp", GetTempDir());
237 setenv("TMPDIR", "/var/tmp", 1);
238 EXPECT_EQ("/var/tmp", GetTempDir());
241 if (old_tmpdir
.empty() == false)
242 setenv("TMPDIR", old_tmpdir
.c_str(), 1);
244 TEST(FileUtlTest
, Popen
)
250 unsigned long long n
= 0;
251 std::vector
<std::string
> OpenFds
;
253 // count Fds to ensure we don't have a resource leak
254 if(FileExists("/proc/self/fd"))
255 OpenFds
= Glob("/proc/self/fd/*");
258 const char* Args
[10] = {"/bin/echo", "meepmeep", NULL
};
259 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::ReadOnly
));
260 EXPECT_TRUE(Fd
.Read(buf
, sizeof(buf
)-1, &n
));
263 EXPECT_STREQ(buf
, "meepmeep\n");
265 // wait for the child to exit and cleanup
266 EXPECT_TRUE(ExecWait(Child
, "PopenRead"));
267 EXPECT_TRUE(Fd
.Close());
269 // ensure that after a close all is good again
270 if(FileExists("/proc/self/fd"))
271 EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds
.size());
273 // ReadWrite is not supported
274 _error
->PushToStack();
275 EXPECT_FALSE(Popen(Args
, Fd
, Child
, FileFd::ReadWrite
));
276 EXPECT_FALSE(Fd
.IsOpen());
277 EXPECT_FALSE(Fd
.Failed());
278 EXPECT_TRUE(_error
->PendingError());
279 _error
->RevertToStack();
282 Args
[0] = "/bin/bash";
286 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::WriteOnly
));
288 EXPECT_TRUE(Fd
.Write(s
.c_str(), s
.length()));
289 EXPECT_TRUE(Fd
.Close());
290 EXPECT_FALSE(Fd
.IsOpen());
291 EXPECT_FALSE(Fd
.Failed());
292 EXPECT_TRUE(ExecWait(Child
, "PopenWrite"));
294 TEST(FileUtlTest
, flAbsPath
)
296 std::string cwd
= SafeGetCWD();
297 int res
= chdir("/etc/");
299 std::string p
= flAbsPath("passwd");
300 EXPECT_EQ(p
, "/etc/passwd");
302 res
= chdir(cwd
.c_str());
306 static void TestDevNullFileFd(unsigned int const filemode
)
308 FileFd
f("/dev/null", filemode
);
309 EXPECT_FALSE(f
.Failed());
310 EXPECT_TRUE(f
.IsOpen());
311 EXPECT_TRUE(f
.IsOpen());
313 std::string test
= "This is a test!\n";
314 EXPECT_TRUE(f
.Write(test
.c_str(), test
.size()));
315 EXPECT_TRUE(f
.IsOpen());
316 EXPECT_FALSE(f
.Failed());
319 EXPECT_FALSE(f
.IsOpen());
320 EXPECT_FALSE(f
.Failed());
322 TEST(FileUtlTest
, WorkingWithDevNull
)
324 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
);
325 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
326 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
);
327 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Atomic
);
328 TestDevNullFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
);
329 // short-hands for ReadWrite with these modes
330 TestDevNullFileFd(FileFd::WriteEmpty
);
331 TestDevNullFileFd(FileFd::WriteAny
);
332 TestDevNullFileFd(FileFd::WriteTemp
);
333 TestDevNullFileFd(FileFd::WriteAtomic
);