]>
git.saurik.com Git - apt.git/blob - test/libapt/fileutl_test.cc
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 bool res
= Popen(Args
, Fd
, Child
, FileFd::ReadOnly
);
256 Fd
.Read(buf
, sizeof(buf
)-1, &n
);
259 EXPECT_EQ(res
, true);
260 EXPECT_STREQ(buf
, "meepmeep\n");
262 // wait for the child to exit and cleanup
263 ExecWait(Child
, "PopenRead");
266 // ensure that after a close all is good again
267 if(FileExists("/proc/self/fd"))
268 EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds
.size());
271 // ReadWrite is not supported
272 res
= Popen(Args
, Fd
, Child
, FileFd::ReadWrite
);
273 EXPECT_EQ(res
, false);
277 Args
[0] = "/bin/bash";
281 res
= Popen(Args
, Fd
, Child
, FileFd::WriteOnly
);
283 Fd
.Write(s
.c_str(), s
.size());
285 ExecWait(Child
, "PopenWrite");
287 TEST(FileUtlTest
, flAbsPath
)
289 std::string cwd
= SafeGetCWD();
290 int res
= chdir("/bin/");
292 std::string p
= flAbsPath("ls");
293 EXPECT_EQ(p
, "/bin/ls");
295 res
= chdir(cwd
.c_str());