]>
Commit | Line | Data |
---|---|---|
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 | { | |
86 | APT_INIT_READBACK | |
87 | EXPECT_TRUE(f.Seek(0)); | |
88 | EXPECT_FALSE(f.Eof()); | |
89 | EXPECT_TRUE(f.Read(readback, 20, true)); | |
90 | EXPECT_FALSE(f.Failed()); | |
91 | EXPECT_TRUE(f.Eof()); | |
92 | EXPECT_N_STR(test.c_str(), readback); | |
93 | EXPECT_EQ(f.Size(), f.Tell()); | |
94 | } | |
95 | { | |
96 | APT_INIT_READBACK | |
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()); | |
104 | } | |
105 | { | |
106 | APT_INIT_READBACK | |
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()); | |
116 | } | |
117 | { | |
118 | APT_INIT_READBACK | |
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()); | |
126 | } | |
127 | { | |
128 | APT_INIT_READBACK | |
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()); | |
137 | } | |
138 | #undef APT_INIT_READBACK | |
139 | ||
140 | f.Close(); | |
141 | EXPECT_FALSE(f.IsOpen()); | |
142 | EXPECT_FALSE(f.Failed()); | |
143 | ||
144 | // regression test for permission bug LP: #1304657 | |
145 | struct stat buf; | |
146 | EXPECT_EQ(0, stat(fname, &buf)); | |
147 | EXPECT_EQ(0, unlink(fname)); | |
148 | EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777); | |
149 | } | |
150 | ||
151 | static void TestFileFd(unsigned int const filemode) | |
152 | { | |
153 | std::vector<APT::Configuration::Compressor> compressors = APT::Configuration::getCompressors(); | |
154 | ||
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)); | |
158 | ||
159 | for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c) | |
160 | { | |
161 | if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite && | |
162 | (c->Name.empty() != true && c->Binary.empty() != true)) | |
163 | continue; | |
164 | TestFileFd(0002, 0664, filemode, *c); | |
165 | TestFileFd(0022, 0644, filemode, *c); | |
166 | TestFileFd(0077, 0600, filemode, *c); | |
167 | TestFileFd(0026, 0640, filemode, *c); | |
168 | } | |
169 | } | |
170 | ||
171 | TEST(FileUtlTest, FileFD) | |
172 | { | |
173 | std::string const startdir = SafeGetCWD(); | |
174 | EXPECT_FALSE(startdir.empty()); | |
175 | std::string tempdir; | |
176 | createTemporaryDirectory("filefd", tempdir); | |
177 | EXPECT_EQ(0, chdir(tempdir.c_str())); | |
178 | ||
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); | |
189 | ||
190 | EXPECT_EQ(0, chdir(startdir.c_str())); | |
191 | removeDirectory(tempdir); | |
192 | } | |
193 | TEST(FileUtlTest, Glob) | |
194 | { | |
195 | std::vector<std::string> files; | |
196 | // normal match | |
197 | files = Glob("*akefile"); | |
198 | EXPECT_EQ(1, files.size()); | |
199 | ||
200 | // not there | |
201 | files = Glob("xxxyyyzzz"); | |
202 | EXPECT_TRUE(files.empty()); | |
203 | EXPECT_FALSE(_error->PendingError()); | |
204 | ||
205 | // many matches (number is a bit random) | |
206 | files = Glob("*.cc"); | |
207 | EXPECT_LT(10, files.size()); | |
208 | } | |
209 | TEST(FileUtlTest, GetTempDir) | |
210 | { | |
211 | char const * const envtmp = getenv("TMPDIR"); | |
212 | std::string old_tmpdir; | |
213 | if (envtmp != NULL) | |
214 | old_tmpdir = envtmp; | |
215 | ||
216 | unsetenv("TMPDIR"); | |
217 | EXPECT_EQ("/tmp", GetTempDir()); | |
218 | ||
219 | setenv("TMPDIR", "", 1); | |
220 | EXPECT_EQ("/tmp", GetTempDir()); | |
221 | ||
222 | setenv("TMPDIR", "/not-there-no-really-not", 1); | |
223 | EXPECT_EQ("/tmp", GetTempDir()); | |
224 | ||
225 | // here but not accessible for non-roots | |
226 | setenv("TMPDIR", "/usr", 1); | |
227 | EXPECT_EQ("/tmp", GetTempDir()); | |
228 | ||
229 | // files are no good for tmpdirs, too | |
230 | setenv("TMPDIR", "/dev/null", 1); | |
231 | EXPECT_EQ("/tmp", GetTempDir()); | |
232 | ||
233 | setenv("TMPDIR", "/var/tmp", 1); | |
234 | EXPECT_EQ("/var/tmp", GetTempDir()); | |
235 | ||
236 | unsetenv("TMPDIR"); | |
237 | if (old_tmpdir.empty() == false) | |
238 | setenv("TMPDIR", old_tmpdir.c_str(), 1); | |
239 | } | |
240 | TEST(FileUtlTest, Popen) | |
241 | { | |
242 | FileFd Fd; | |
243 | pid_t Child; | |
244 | char buf[1024]; | |
245 | std::string s; | |
246 | unsigned long long n = 0; | |
247 | std::vector<std::string> OpenFds; | |
248 | ||
249 | // count Fds to ensure we don't have a resource leak | |
250 | if(FileExists("/proc/self/fd")) | |
251 | OpenFds = Glob("/proc/self/fd/*"); | |
252 | ||
253 | // output something | |
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)); | |
257 | buf[n] = 0; | |
258 | EXPECT_NE(n, 0); | |
259 | EXPECT_STREQ(buf, "meepmeep\n"); | |
260 | ||
261 | // wait for the child to exit and cleanup | |
262 | EXPECT_TRUE(ExecWait(Child, "PopenRead")); | |
263 | EXPECT_TRUE(Fd.Close()); | |
264 | ||
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()); | |
268 | ||
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(); | |
276 | ||
277 | // write something | |
278 | Args[0] = "/bin/bash"; | |
279 | Args[1] = "-c"; | |
280 | Args[2] = "read"; | |
281 | Args[3] = NULL; | |
282 | EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly)); | |
283 | s = "\n"; | |
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")); | |
289 | } | |
290 | TEST(FileUtlTest, flAbsPath) | |
291 | { | |
292 | std::string cwd = SafeGetCWD(); | |
293 | int res = chdir("/etc/"); | |
294 | EXPECT_EQ(res, 0); | |
295 | std::string p = flAbsPath("passwd"); | |
296 | EXPECT_EQ(p, "/etc/passwd"); | |
297 | ||
298 | res = chdir(cwd.c_str()); | |
299 | EXPECT_EQ(res, 0); | |
300 | } |