]> git.saurik.com Git - apt.git/blob - test/libapt/fileutl_test.cc
cachefile: Only set members that were initialized successfully
[apt.git] / test / libapt / fileutl_test.cc
1 #include <config.h>
2
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
8 #include <string>
9 #include <vector>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <gtest/gtest.h>
14
15 #include "file-helpers.h"
16
17 static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission,
18 unsigned int const filemode, APT::Configuration::Compressor const &compressor)
19 {
20 std::string trace;
21 strprintf(trace, "TestFileFd: Compressor: %s umask: %#o permission: %#o mode: %d", compressor.Name.c_str(), a_umask, ExpectedFilePermission, filemode);
22 SCOPED_TRACE(trace);
23
24 static const char* fname = "apt-filefd-test.txt";
25 if (FileExists(fname) == true)
26 EXPECT_EQ(0, unlink(fname));
27
28 FileFd f;
29 umask(a_umask);
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);
34
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());
39
40 f.Close();
41 EXPECT_FALSE(f.IsOpen());
42 EXPECT_FALSE(f.Failed());
43
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());
52
53 // ensure the memory is as predictably messed up
54 #define APT_INIT_READBACK \
55 char readback[20]; \
56 memset(readback, 'D', sizeof(readback)*sizeof(readback[0])); \
57 readback[19] = '\0';
58 #define EXPECT_N_STR(expect, actual) \
59 EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
60 {
61 APT_INIT_READBACK
62 char const * const expect = "DDDDDDDDDDDDDDDDDDD";
63 EXPECT_STREQ(expect,readback);
64 EXPECT_N_STR(expect, readback);
65 }
66 {
67 APT_INIT_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());
74 }
75 {
76 APT_INIT_READBACK
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());
84 }
85 // Non-zero backwards seek
86 {
87 APT_INIT_READBACK
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());
96 }
97 {
98 APT_INIT_READBACK
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());
106 }
107 {
108 APT_INIT_READBACK
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());
116 }
117 {
118 APT_INIT_READBACK
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());
128 }
129 {
130 APT_INIT_READBACK
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());
138 }
139 {
140 APT_INIT_READBACK
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());
149 }
150 #undef APT_INIT_READBACK
151
152 f.Close();
153 EXPECT_FALSE(f.IsOpen());
154 EXPECT_FALSE(f.Failed());
155
156 // regression test for permission bug LP: #1304657
157 struct stat buf;
158 EXPECT_EQ(0, stat(fname, &buf));
159 EXPECT_EQ(0, unlink(fname));
160 EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777);
161 }
162
163 static void TestFileFd(unsigned int const filemode)
164 {
165 std::vector<APT::Configuration::Compressor> compressors = APT::Configuration::getCompressors();
166
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));
170
171 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
172 {
173 if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite &&
174 (c->Name.empty() != true && c->Binary.empty() != true))
175 continue;
176 TestFileFd(0002, 0664, filemode, *c);
177 TestFileFd(0022, 0644, filemode, *c);
178 TestFileFd(0077, 0600, filemode, *c);
179 TestFileFd(0026, 0640, filemode, *c);
180 }
181 }
182
183 TEST(FileUtlTest, FileFD)
184 {
185 std::string const startdir = SafeGetCWD();
186 EXPECT_FALSE(startdir.empty());
187 std::string tempdir;
188 createTemporaryDirectory("filefd", tempdir);
189 EXPECT_EQ(0, chdir(tempdir.c_str()));
190
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);
201
202 EXPECT_EQ(0, chdir(startdir.c_str()));
203 removeDirectory(tempdir);
204 }
205 TEST(FileUtlTest, Glob)
206 {
207 std::vector<std::string> files;
208 // normal match
209 files = Glob("*akefile");
210 EXPECT_EQ(1, files.size());
211
212 // not there
213 files = Glob("xxxyyyzzz");
214 EXPECT_TRUE(files.empty());
215 EXPECT_FALSE(_error->PendingError());
216
217 // many matches (number is a bit random)
218 files = Glob("*.cc");
219 EXPECT_LT(10, files.size());
220 }
221 TEST(FileUtlTest, GetTempDir)
222 {
223 char const * const envtmp = getenv("TMPDIR");
224 std::string old_tmpdir;
225 if (envtmp != NULL)
226 old_tmpdir = envtmp;
227
228 unsetenv("TMPDIR");
229 EXPECT_EQ("/tmp", GetTempDir());
230
231 setenv("TMPDIR", "", 1);
232 EXPECT_EQ("/tmp", GetTempDir());
233
234 setenv("TMPDIR", "/not-there-no-really-not", 1);
235 EXPECT_EQ("/tmp", GetTempDir());
236
237 // root can access everything, so /usr will be accepted
238 if (geteuid() != 0)
239 {
240 // here but not accessible for non-roots
241 setenv("TMPDIR", "/usr", 1);
242 EXPECT_EQ("/tmp", GetTempDir());
243 }
244
245 // files are no good for tmpdirs, too
246 setenv("TMPDIR", "/dev/null", 1);
247 EXPECT_EQ("/tmp", GetTempDir());
248
249 setenv("TMPDIR", "/var/tmp", 1);
250 EXPECT_EQ("/var/tmp", GetTempDir());
251
252 unsetenv("TMPDIR");
253 if (old_tmpdir.empty() == false)
254 setenv("TMPDIR", old_tmpdir.c_str(), 1);
255 }
256 TEST(FileUtlTest, Popen)
257 {
258 FileFd Fd;
259 pid_t Child;
260 char buf[1024];
261 std::string s;
262 unsigned long long n = 0;
263 std::vector<std::string> OpenFds;
264
265 // count Fds to ensure we don't have a resource leak
266 if(FileExists("/proc/self/fd"))
267 OpenFds = Glob("/proc/self/fd/*");
268
269 // output something
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));
273 buf[n] = 0;
274 EXPECT_NE(n, 0);
275 EXPECT_STREQ(buf, "meepmeep\n");
276
277 // wait for the child to exit and cleanup
278 EXPECT_TRUE(ExecWait(Child, "PopenRead"));
279 EXPECT_TRUE(Fd.Close());
280
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());
284
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();
292
293 // write something
294 Args[0] = "/bin/bash";
295 Args[1] = "-c";
296 Args[2] = "read";
297 Args[3] = NULL;
298 EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly));
299 s = "\n";
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"));
305 }
306 TEST(FileUtlTest, flAbsPath)
307 {
308 std::string cwd = SafeGetCWD();
309 int res = chdir("/etc/");
310 EXPECT_EQ(res, 0);
311 std::string p = flAbsPath("passwd");
312 EXPECT_EQ(p, "/etc/passwd");
313
314 res = chdir(cwd.c_str());
315 EXPECT_EQ(res, 0);
316 }
317
318 static void TestDevNullFileFd(unsigned int const filemode)
319 {
320 FileFd f("/dev/null", filemode);
321 EXPECT_FALSE(f.Failed());
322 EXPECT_TRUE(f.IsOpen());
323 EXPECT_TRUE(f.IsOpen());
324
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());
329
330 f.Close();
331 EXPECT_FALSE(f.IsOpen());
332 EXPECT_FALSE(f.Failed());
333 }
334 TEST(FileUtlTest, WorkingWithDevNull)
335 {
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);
346 }