]>
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)));
63 char const * const expect
= "This";
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());
72 char const * const expect
= "test!\n";
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());
82 EXPECT_TRUE(f
.Seek(0));
83 EXPECT_FALSE(f
.Eof());
84 EXPECT_TRUE(f
.Read(readback
, 20, true));
85 EXPECT_FALSE(f
.Failed());
87 EXPECT_N_STR(test
.c_str(), readback
);
88 EXPECT_EQ(f
.Size(), f
.Tell());
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());
102 EXPECT_TRUE(f
.Seek(0));
103 EXPECT_FALSE(f
.Eof());
104 unsigned long long actual
;
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());
114 EXPECT_TRUE(f
.Seek(0));
115 EXPECT_FALSE(f
.Eof());
116 f
.ReadLine(readback
, 20);
117 EXPECT_FALSE(f
.Failed());
118 EXPECT_FALSE(f
.Eof());
119 EXPECT_EQ(test
, readback
);
120 EXPECT_EQ(f
.Size(), f
.Tell());
124 EXPECT_TRUE(f
.Seek(0));
125 EXPECT_FALSE(f
.Eof());
126 char const * const expect
= "This";
127 f
.ReadLine(readback
, strlen(expect
) + 1);
128 EXPECT_FALSE(f
.Failed());
129 EXPECT_FALSE(f
.Eof());
130 EXPECT_N_STR(expect
, readback
);
131 EXPECT_EQ(strlen(expect
), f
.Tell());
133 #undef APT_INIT_READBACK
136 EXPECT_FALSE(f
.IsOpen());
137 EXPECT_FALSE(f
.Failed());
139 // regression test for permission bug LP: #1304657
141 EXPECT_EQ(0, stat(fname
, &buf
));
142 EXPECT_EQ(0, unlink(fname
));
143 EXPECT_EQ(ExpectedFilePermission
, buf
.st_mode
& 0777);
146 static void TestFileFd(unsigned int const filemode
)
148 std::vector
<APT::Configuration::Compressor
> compressors
= APT::Configuration::getCompressors();
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));
154 for (std::vector
<APT::Configuration::Compressor
>::const_iterator c
= compressors
.begin(); c
!= compressors
.end(); ++c
)
156 if ((filemode
& FileFd::ReadWrite
) == FileFd::ReadWrite
&&
157 (c
->Name
.empty() != true && c
->Binary
.empty() != true))
159 TestFileFd(0002, 0664, filemode
, *c
);
160 TestFileFd(0022, 0644, filemode
, *c
);
161 TestFileFd(0077, 0600, filemode
, *c
);
162 TestFileFd(0026, 0640, filemode
, *c
);
166 TEST(FileUtlTest
, FileFD
)
168 std::string
const startdir
= SafeGetCWD();
169 EXPECT_FALSE(startdir
.empty());
171 createTemporaryDirectory("filefd", tempdir
);
172 EXPECT_EQ(0, chdir(tempdir
.c_str()));
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
);
185 EXPECT_EQ(0, chdir(startdir
.c_str()));
186 removeDirectory(tempdir
);
188 TEST(FileUtlTest
, Glob
)
190 std::vector
<std::string
> files
;
192 files
= Glob("*akefile");
193 EXPECT_EQ(1, files
.size());
196 files
= Glob("xxxyyyzzz");
197 EXPECT_TRUE(files
.empty());
198 EXPECT_FALSE(_error
->PendingError());
200 // many matches (number is a bit random)
201 files
= Glob("*.cc");
202 EXPECT_LT(10, files
.size());
204 TEST(FileUtlTest
, GetTempDir
)
206 char const * const envtmp
= getenv("TMPDIR");
207 std::string old_tmpdir
;
212 EXPECT_EQ("/tmp", GetTempDir());
214 setenv("TMPDIR", "", 1);
215 EXPECT_EQ("/tmp", GetTempDir());
217 setenv("TMPDIR", "/not-there-no-really-not", 1);
218 EXPECT_EQ("/tmp", GetTempDir());
220 setenv("TMPDIR", "/usr", 1);
221 EXPECT_EQ("/usr", GetTempDir());
224 if (old_tmpdir
.empty() == false)
225 setenv("TMPDIR", old_tmpdir
.c_str(), 1);
227 TEST(FileUtlTest
, Popen
)
233 unsigned long long n
= 0;
234 std::vector
<std::string
> OpenFds
;
236 // count Fds to ensure we don't have a resource leak
237 if(FileExists("/proc/self/fd"))
238 OpenFds
= Glob("/proc/self/fd/*");
241 const char* Args
[10] = {"/bin/echo", "meepmeep", NULL
};
242 bool res
= Popen(Args
, Fd
, Child
, FileFd::ReadOnly
);
243 Fd
.Read(buf
, sizeof(buf
)-1, &n
);
246 EXPECT_EQ(res
, true);
247 EXPECT_STREQ(buf
, "meepmeep\n");
249 // wait for the child to exit and cleanup
250 ExecWait(Child
, "PopenRead");
253 // ensure that after a close all is good again
254 if(FileExists("/proc/self/fd"))
255 EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds
.size());
258 // ReadWrite is not supported
259 res
= Popen(Args
, Fd
, Child
, FileFd::ReadWrite
);
260 EXPECT_EQ(res
, false);
264 Args
[0] = "/bin/bash";
268 res
= Popen(Args
, Fd
, Child
, FileFd::WriteOnly
);
270 Fd
.Write(s
.c_str(), s
.size());
272 ExecWait(Child
, "PopenWrite");
274 TEST(FileUtlTest
, flAbsPath
)
276 std::string cwd
= SafeGetCWD();
277 int res
= chdir("/bin/");
279 std::string p
= flAbsPath("ls");
280 EXPECT_EQ(p
, "/bin/ls");
282 res
= chdir(cwd
.c_str());