]>
git.saurik.com Git - apt.git/blob - test/libapt/fileutl_test.cc
b4226171696f29110dfd71ca231ab05d3065910f
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 // here but not accessible for non-roots
226 setenv("TMPDIR", "/usr", 1);
227 EXPECT_EQ("/tmp", GetTempDir());
229 // files are no good for tmpdirs, too
230 setenv("TMPDIR", "/dev/null", 1);
231 EXPECT_EQ("/tmp", GetTempDir());
233 setenv("TMPDIR", "/var/tmp", 1);
234 EXPECT_EQ("/var/tmp", GetTempDir());
237 if (old_tmpdir
.empty() == false)
238 setenv("TMPDIR", old_tmpdir
.c_str(), 1);
240 TEST(FileUtlTest
, Popen
)
246 unsigned long long n
= 0;
247 std::vector
<std::string
> OpenFds
;
249 // count Fds to ensure we don't have a resource leak
250 if(FileExists("/proc/self/fd"))
251 OpenFds
= Glob("/proc/self/fd/*");
254 const char* Args
[10] = {"/bin/echo", "meepmeep", NULL
};
255 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::ReadOnly
));
256 EXPECT_TRUE(Fd
.Read(buf
, sizeof(buf
)-1, &n
));
259 EXPECT_STREQ(buf
, "meepmeep\n");
261 // wait for the child to exit and cleanup
262 EXPECT_TRUE(ExecWait(Child
, "PopenRead"));
263 EXPECT_TRUE(Fd
.Close());
265 // ensure that after a close all is good again
266 if(FileExists("/proc/self/fd"))
267 EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds
.size());
269 // ReadWrite is not supported
270 _error
->PushToStack();
271 EXPECT_FALSE(Popen(Args
, Fd
, Child
, FileFd::ReadWrite
));
272 EXPECT_FALSE(Fd
.IsOpen());
273 EXPECT_FALSE(Fd
.Failed());
274 EXPECT_TRUE(_error
->PendingError());
275 _error
->RevertToStack();
278 Args
[0] = "/bin/bash";
282 EXPECT_TRUE(Popen(Args
, Fd
, Child
, FileFd::WriteOnly
));
284 EXPECT_TRUE(Fd
.Write(s
.c_str(), s
.length()));
285 EXPECT_TRUE(Fd
.Close());
286 EXPECT_FALSE(Fd
.IsOpen());
287 EXPECT_FALSE(Fd
.Failed());
288 EXPECT_TRUE(ExecWait(Child
, "PopenWrite"));
290 TEST(FileUtlTest
, flAbsPath
)
292 std::string cwd
= SafeGetCWD();
293 int res
= chdir("/etc/");
295 std::string p
= flAbsPath("passwd");
296 EXPECT_EQ(p
, "/etc/passwd");
298 res
= chdir(cwd
.c_str());