]> git.saurik.com Git - apt.git/blame - test/libapt/fileutl_test.cc
pass versioned provides to external solvers in EDSP
[apt.git] / test / libapt / fileutl_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
488011fa
MV
3#include <apt-pkg/error.h>
4#include <apt-pkg/fileutl.h>
f00832cc 5#include <apt-pkg/strutl.h>
f7feb041 6#include <apt-pkg/aptconfiguration.h>
488011fa 7
488011fa
MV
8#include <string>
9#include <vector>
488011fa 10#include <stdlib.h>
f7feb041 11#include <string.h>
488011fa 12
f00832cc 13#include <gtest/gtest.h>
488011fa 14
f00832cc 15#include "file-helpers.h"
f7feb041 16
f00832cc
DK
17static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission,
18 unsigned int const filemode, APT::Configuration::Compressor const &compressor)
f22b65b4 19{
f00832cc
DK
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
bb93178b
DK
24 static const char* fname = "apt-filefd-test.txt";
25 if (FileExists(fname) == true)
f00832cc 26 EXPECT_EQ(0, unlink(fname));
f22b65b4 27
f00832cc 28 FileFd f;
f22b65b4 29 umask(a_umask);
f00832cc
DK
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);
f7feb041 34
bb93178b 35 std::string test = "This is a test!\n";
f00832cc
DK
36 EXPECT_TRUE(f.Write(test.c_str(), test.size()));
37 EXPECT_TRUE(f.IsOpen());
38 EXPECT_FALSE(f.Failed());
f7feb041 39
f22b65b4 40 f.Close();
f00832cc
DK
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());
f7feb041 52
bb93178b 53 // ensure the memory is as predictably messed up
f00832cc 54#define APT_INIT_READBACK \
bb93178b 55 char readback[20]; \
ad5ffe1f 56 memset(readback, 'D', sizeof(readback)*sizeof(readback[0])); \
bb93178b 57 readback[19] = '\0';
f00832cc
DK
58#define EXPECT_N_STR(expect, actual) \
59 EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
ad5ffe1f
DK
60 {
61 APT_INIT_READBACK
62 char const * const expect = "DDDDDDDDDDDDDDDDDDD";
63 EXPECT_STREQ(expect,readback);
64 EXPECT_N_STR(expect, readback);
65 }
f7feb041 66 {
bb93178b 67 APT_INIT_READBACK
f7feb041 68 char const * const expect = "This";
f00832cc
DK
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());
f7feb041
DK
74 }
75 {
bb93178b
DK
76 APT_INIT_READBACK
77 char const * const expect = "test!\n";
f00832cc
DK
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());
f7feb041 84 }
5fafaf27
JAK
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 }
bb93178b
DK
97 {
98 APT_INIT_READBACK
f00832cc
DK
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());
bb93178b
DK
106 }
107 {
108 APT_INIT_READBACK
f00832cc
DK
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());
bb93178b
DK
116 }
117 {
118 APT_INIT_READBACK
f00832cc
DK
119 EXPECT_TRUE(f.Seek(0));
120 EXPECT_FALSE(f.Eof());
bb93178b 121 unsigned long long actual;
f00832cc
DK
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());
bb93178b
DK
128 }
129 {
130 APT_INIT_READBACK
f00832cc
DK
131 EXPECT_TRUE(f.Seek(0));
132 EXPECT_FALSE(f.Eof());
bb93178b 133 f.ReadLine(readback, 20);
f00832cc
DK
134 EXPECT_FALSE(f.Failed());
135 EXPECT_FALSE(f.Eof());
136 EXPECT_EQ(test, readback);
137 EXPECT_EQ(f.Size(), f.Tell());
bb93178b
DK
138 }
139 {
140 APT_INIT_READBACK
f00832cc
DK
141 EXPECT_TRUE(f.Seek(0));
142 EXPECT_FALSE(f.Eof());
bb93178b
DK
143 char const * const expect = "This";
144 f.ReadLine(readback, strlen(expect) + 1);
f00832cc
DK
145 EXPECT_FALSE(f.Failed());
146 EXPECT_FALSE(f.Eof());
147 EXPECT_N_STR(expect, readback);
148 EXPECT_EQ(strlen(expect), f.Tell());
bb93178b
DK
149 }
150#undef APT_INIT_READBACK
f7feb041
DK
151
152 f.Close();
f00832cc
DK
153 EXPECT_FALSE(f.IsOpen());
154 EXPECT_FALSE(f.Failed());
f7feb041
DK
155
156 // regression test for permission bug LP: #1304657
f00832cc
DK
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);
f22b65b4
MV
161}
162
f00832cc 163static void TestFileFd(unsigned int const filemode)
488011fa 164{
bb93178b
DK
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
f7feb041
DK
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;
f00832cc
DK
176 TestFileFd(0002, 0664, filemode, *c);
177 TestFileFd(0022, 0644, filemode, *c);
178 TestFileFd(0077, 0600, filemode, *c);
179 TestFileFd(0026, 0640, filemode, *c);
f7feb041 180 }
f7feb041 181}
488011fa 182
f00832cc
DK
183TEST(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}
205TEST(FileUtlTest, Glob)
f7feb041 206{
f7feb041 207 std::vector<std::string> files;
488011fa 208 // normal match
f00832cc
DK
209 files = Glob("*akefile");
210 EXPECT_EQ(1, files.size());
488011fa
MV
211
212 // not there
213 files = Glob("xxxyyyzzz");
f00832cc
DK
214 EXPECT_TRUE(files.empty());
215 EXPECT_FALSE(_error->PendingError());
488011fa
MV
216
217 // many matches (number is a bit random)
218 files = Glob("*.cc");
f00832cc
DK
219 EXPECT_LT(10, files.size());
220}
221TEST(FileUtlTest, GetTempDir)
222{
223 char const * const envtmp = getenv("TMPDIR");
224 std::string old_tmpdir;
225 if (envtmp != NULL)
226 old_tmpdir = envtmp;
488011fa 227
a077861a 228 unsetenv("TMPDIR");
f00832cc 229 EXPECT_EQ("/tmp", GetTempDir());
a077861a
MV
230
231 setenv("TMPDIR", "", 1);
f00832cc 232 EXPECT_EQ("/tmp", GetTempDir());
a077861a
MV
233
234 setenv("TMPDIR", "/not-there-no-really-not", 1);
f00832cc 235 EXPECT_EQ("/tmp", GetTempDir());
a077861a 236
dbbe1e63
PT
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 }
0d303f17
DK
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());
a077861a 251
f00832cc
DK
252 unsetenv("TMPDIR");
253 if (old_tmpdir.empty() == false)
254 setenv("TMPDIR", old_tmpdir.c_str(), 1);
488011fa 255}
7ad2a347
MV
256TEST(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};
23fb7b2c
DK
271 EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::ReadOnly));
272 EXPECT_TRUE(Fd.Read(buf, sizeof(buf)-1, &n));
7ad2a347
MV
273 buf[n] = 0;
274 EXPECT_NE(n, 0);
7ad2a347
MV
275 EXPECT_STREQ(buf, "meepmeep\n");
276
277 // wait for the child to exit and cleanup
23fb7b2c
DK
278 EXPECT_TRUE(ExecWait(Child, "PopenRead"));
279 EXPECT_TRUE(Fd.Close());
7ad2a347
MV
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
7ad2a347 285 // ReadWrite is not supported
23fb7b2c
DK
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();
7ad2a347
MV
292
293 // write something
294 Args[0] = "/bin/bash";
295 Args[1] = "-c";
296 Args[2] = "read";
297 Args[3] = NULL;
23fb7b2c 298 EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly));
7ad2a347 299 s = "\n";
23fb7b2c
DK
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"));
7ad2a347 305}
53ac87ac
MV
306TEST(FileUtlTest, flAbsPath)
307{
a111a024 308 std::string cwd = SafeGetCWD();
20cf708a 309 int res = chdir("/etc/");
53ac87ac 310 EXPECT_EQ(res, 0);
20cf708a
JAK
311 std::string p = flAbsPath("passwd");
312 EXPECT_EQ(p, "/etc/passwd");
a111a024
MV
313
314 res = chdir(cwd.c_str());
315 EXPECT_EQ(res, 0);
53ac87ac 316}
cd46d4eb
DK
317
318static 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}
334TEST(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}