]>
Commit | Line | Data |
---|---|---|
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 |
17 | static 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 DK |
55 | char readback[20]; \ |
56 | memset(readback, 'D', sizeof(readback)/sizeof(readback[0])); \ | |
57 | readback[19] = '\0'; | |
f00832cc DK |
58 | #define EXPECT_N_STR(expect, actual) \ |
59 | EXPECT_EQ(0, strncmp(expect, actual, strlen(expect))); | |
60 | ||
f7feb041 | 61 | { |
bb93178b | 62 | APT_INIT_READBACK |
f7feb041 | 63 | char const * const expect = "This"; |
f00832cc DK |
64 | EXPECT_TRUE(f.Read(readback, strlen(expect))); |
65 | EXPECT_FALSE(f.Failed()); | |
66 | EXPECT_FALSE(f.Eof()); | |
67 | EXPECT_N_STR(expect, readback); | |
68 | EXPECT_EQ(strlen(expect), f.Tell()); | |
f7feb041 DK |
69 | } |
70 | { | |
bb93178b DK |
71 | APT_INIT_READBACK |
72 | char const * const expect = "test!\n"; | |
f00832cc DK |
73 | EXPECT_TRUE(f.Skip((test.size() - f.Tell()) - strlen(expect))); |
74 | EXPECT_TRUE(f.Read(readback, strlen(expect))); | |
75 | EXPECT_FALSE(f.Failed()); | |
76 | EXPECT_FALSE(f.Eof()); | |
77 | EXPECT_N_STR(expect, readback); | |
78 | EXPECT_EQ(test.size(), f.Tell()); | |
f7feb041 | 79 | } |
bb93178b DK |
80 | { |
81 | APT_INIT_READBACK | |
f00832cc DK |
82 | EXPECT_TRUE(f.Seek(0)); |
83 | EXPECT_FALSE(f.Eof()); | |
84 | EXPECT_TRUE(f.Read(readback, 20, true)); | |
85 | EXPECT_FALSE(f.Failed()); | |
86 | EXPECT_TRUE(f.Eof()); | |
87 | EXPECT_N_STR(test.c_str(), readback); | |
88 | EXPECT_EQ(f.Size(), f.Tell()); | |
bb93178b DK |
89 | } |
90 | { | |
91 | APT_INIT_READBACK | |
f00832cc DK |
92 | EXPECT_TRUE(f.Seek(0)); |
93 | EXPECT_FALSE(f.Eof()); | |
94 | EXPECT_TRUE(f.Read(readback, test.size(), true)); | |
95 | EXPECT_FALSE(f.Failed()); | |
96 | EXPECT_FALSE(f.Eof()); | |
97 | EXPECT_N_STR(test.c_str(), readback); | |
98 | EXPECT_EQ(f.Size(), f.Tell()); | |
bb93178b DK |
99 | } |
100 | { | |
101 | APT_INIT_READBACK | |
f00832cc DK |
102 | EXPECT_TRUE(f.Seek(0)); |
103 | EXPECT_FALSE(f.Eof()); | |
bb93178b | 104 | unsigned long long actual; |
f00832cc DK |
105 | EXPECT_TRUE(f.Read(readback, 20, &actual)); |
106 | EXPECT_FALSE(f.Failed()); | |
107 | EXPECT_TRUE(f.Eof()); | |
108 | EXPECT_EQ(test.size(), actual); | |
109 | EXPECT_N_STR(test.c_str(), readback); | |
110 | EXPECT_EQ(f.Size(), f.Tell()); | |
bb93178b DK |
111 | } |
112 | { | |
113 | APT_INIT_READBACK | |
f00832cc DK |
114 | EXPECT_TRUE(f.Seek(0)); |
115 | EXPECT_FALSE(f.Eof()); | |
bb93178b | 116 | f.ReadLine(readback, 20); |
f00832cc DK |
117 | EXPECT_FALSE(f.Failed()); |
118 | EXPECT_FALSE(f.Eof()); | |
119 | EXPECT_EQ(test, readback); | |
120 | EXPECT_EQ(f.Size(), f.Tell()); | |
bb93178b DK |
121 | } |
122 | { | |
123 | APT_INIT_READBACK | |
f00832cc DK |
124 | EXPECT_TRUE(f.Seek(0)); |
125 | EXPECT_FALSE(f.Eof()); | |
bb93178b DK |
126 | char const * const expect = "This"; |
127 | f.ReadLine(readback, strlen(expect) + 1); | |
f00832cc DK |
128 | EXPECT_FALSE(f.Failed()); |
129 | EXPECT_FALSE(f.Eof()); | |
130 | EXPECT_N_STR(expect, readback); | |
131 | EXPECT_EQ(strlen(expect), f.Tell()); | |
bb93178b DK |
132 | } |
133 | #undef APT_INIT_READBACK | |
f7feb041 DK |
134 | |
135 | f.Close(); | |
f00832cc DK |
136 | EXPECT_FALSE(f.IsOpen()); |
137 | EXPECT_FALSE(f.Failed()); | |
f7feb041 DK |
138 | |
139 | // regression test for permission bug LP: #1304657 | |
f00832cc DK |
140 | struct stat buf; |
141 | EXPECT_EQ(0, stat(fname, &buf)); | |
142 | EXPECT_EQ(0, unlink(fname)); | |
143 | EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777); | |
f22b65b4 MV |
144 | } |
145 | ||
f00832cc | 146 | static void TestFileFd(unsigned int const filemode) |
488011fa | 147 | { |
bb93178b DK |
148 | std::vector<APT::Configuration::Compressor> compressors = APT::Configuration::getCompressors(); |
149 | ||
150 | // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries | |
151 | compressors.push_back(APT::Configuration::Compressor("rev", ".reversed", "rev", NULL, NULL, 42)); | |
152 | //compressors.push_back(APT::Configuration::Compressor("cat", ".ident", "cat", NULL, NULL, 42)); | |
153 | ||
f7feb041 DK |
154 | for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c) |
155 | { | |
156 | if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite && | |
157 | (c->Name.empty() != true && c->Binary.empty() != true)) | |
158 | continue; | |
f00832cc DK |
159 | TestFileFd(0002, 0664, filemode, *c); |
160 | TestFileFd(0022, 0644, filemode, *c); | |
161 | TestFileFd(0077, 0600, filemode, *c); | |
162 | TestFileFd(0026, 0640, filemode, *c); | |
f7feb041 | 163 | } |
f7feb041 | 164 | } |
488011fa | 165 | |
f00832cc DK |
166 | TEST(FileUtlTest, FileFD) |
167 | { | |
168 | std::string const startdir = SafeGetCWD(); | |
169 | EXPECT_FALSE(startdir.empty()); | |
170 | std::string tempdir; | |
171 | createTemporaryDirectory("filefd", tempdir); | |
172 | EXPECT_EQ(0, chdir(tempdir.c_str())); | |
173 | ||
174 | TestFileFd(FileFd::WriteOnly | FileFd::Create); | |
175 | TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty); | |
176 | TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive); | |
177 | TestFileFd(FileFd::WriteOnly | FileFd::Atomic); | |
178 | TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic); | |
179 | // short-hands for ReadWrite with these modes | |
180 | TestFileFd(FileFd::WriteEmpty); | |
181 | TestFileFd(FileFd::WriteAny); | |
182 | TestFileFd(FileFd::WriteTemp); | |
183 | TestFileFd(FileFd::WriteAtomic); | |
184 | ||
185 | EXPECT_EQ(0, chdir(startdir.c_str())); | |
186 | removeDirectory(tempdir); | |
187 | } | |
188 | TEST(FileUtlTest, Glob) | |
f7feb041 | 189 | { |
f7feb041 | 190 | std::vector<std::string> files; |
488011fa | 191 | // normal match |
f00832cc DK |
192 | files = Glob("*akefile"); |
193 | EXPECT_EQ(1, files.size()); | |
488011fa MV |
194 | |
195 | // not there | |
196 | files = Glob("xxxyyyzzz"); | |
f00832cc DK |
197 | EXPECT_TRUE(files.empty()); |
198 | EXPECT_FALSE(_error->PendingError()); | |
488011fa MV |
199 | |
200 | // many matches (number is a bit random) | |
201 | files = Glob("*.cc"); | |
f00832cc DK |
202 | EXPECT_LT(10, files.size()); |
203 | } | |
204 | TEST(FileUtlTest, GetTempDir) | |
205 | { | |
206 | char const * const envtmp = getenv("TMPDIR"); | |
207 | std::string old_tmpdir; | |
208 | if (envtmp != NULL) | |
209 | old_tmpdir = envtmp; | |
488011fa | 210 | |
a077861a | 211 | unsetenv("TMPDIR"); |
f00832cc | 212 | EXPECT_EQ("/tmp", GetTempDir()); |
a077861a MV |
213 | |
214 | setenv("TMPDIR", "", 1); | |
f00832cc | 215 | EXPECT_EQ("/tmp", GetTempDir()); |
a077861a MV |
216 | |
217 | setenv("TMPDIR", "/not-there-no-really-not", 1); | |
f00832cc | 218 | EXPECT_EQ("/tmp", GetTempDir()); |
a077861a | 219 | |
0d303f17 | 220 | // here but not accessible for non-roots |
a077861a | 221 | setenv("TMPDIR", "/usr", 1); |
0d303f17 DK |
222 | EXPECT_EQ("/tmp", GetTempDir()); |
223 | ||
224 | // files are no good for tmpdirs, too | |
225 | setenv("TMPDIR", "/dev/null", 1); | |
226 | EXPECT_EQ("/tmp", GetTempDir()); | |
227 | ||
228 | setenv("TMPDIR", "/var/tmp", 1); | |
229 | EXPECT_EQ("/var/tmp", GetTempDir()); | |
a077861a | 230 | |
f00832cc DK |
231 | unsetenv("TMPDIR"); |
232 | if (old_tmpdir.empty() == false) | |
233 | setenv("TMPDIR", old_tmpdir.c_str(), 1); | |
488011fa | 234 | } |
7ad2a347 MV |
235 | TEST(FileUtlTest, Popen) |
236 | { | |
237 | FileFd Fd; | |
238 | pid_t Child; | |
239 | char buf[1024]; | |
240 | std::string s; | |
241 | unsigned long long n = 0; | |
242 | std::vector<std::string> OpenFds; | |
243 | ||
244 | // count Fds to ensure we don't have a resource leak | |
245 | if(FileExists("/proc/self/fd")) | |
246 | OpenFds = Glob("/proc/self/fd/*"); | |
247 | ||
248 | // output something | |
249 | const char* Args[10] = {"/bin/echo", "meepmeep", NULL}; | |
250 | bool res = Popen(Args, Fd, Child, FileFd::ReadOnly); | |
251 | Fd.Read(buf, sizeof(buf)-1, &n); | |
252 | buf[n] = 0; | |
253 | EXPECT_NE(n, 0); | |
254 | EXPECT_EQ(res, true); | |
255 | EXPECT_STREQ(buf, "meepmeep\n"); | |
256 | ||
257 | // wait for the child to exit and cleanup | |
258 | ExecWait(Child, "PopenRead"); | |
259 | Fd.Close(); | |
260 | ||
261 | // ensure that after a close all is good again | |
262 | if(FileExists("/proc/self/fd")) | |
263 | EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds.size()); | |
264 | ||
265 | ||
266 | // ReadWrite is not supported | |
267 | res = Popen(Args, Fd, Child, FileFd::ReadWrite); | |
268 | EXPECT_EQ(res, false); | |
269 | _error->Discard(); | |
270 | ||
271 | // write something | |
272 | Args[0] = "/bin/bash"; | |
273 | Args[1] = "-c"; | |
274 | Args[2] = "read"; | |
275 | Args[3] = NULL; | |
276 | res = Popen(Args, Fd, Child, FileFd::WriteOnly); | |
277 | s = "\n"; | |
278 | Fd.Write(s.c_str(), s.size()); | |
279 | Fd.Close(); | |
280 | ExecWait(Child, "PopenWrite"); | |
281 | } | |
53ac87ac MV |
282 | TEST(FileUtlTest, flAbsPath) |
283 | { | |
a111a024 | 284 | std::string cwd = SafeGetCWD(); |
53ac87ac MV |
285 | int res = chdir("/bin/"); |
286 | EXPECT_EQ(res, 0); | |
287 | std::string p = flAbsPath("ls"); | |
288 | EXPECT_EQ(p, "/bin/ls"); | |
a111a024 MV |
289 | |
290 | res = chdir(cwd.c_str()); | |
291 | EXPECT_EQ(res, 0); | |
53ac87ac | 292 | } |