]>
git.saurik.com Git - apt.git/blob - test/libapt/fileutl_test.cc
f3a3dd08ebbf5a22cd1cb9919b405f569eb86dcd
3 #include <apt-pkg/error.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/aptconfiguration.h>
15 static void assertStringEquals(char const * const expect
, char const * const got
, unsigned long const line
) {
16 if (strncmp(expect
, got
, strlen(expect
)) == 0)
18 OutputAssertEqual(expect
, "==", got
, line
);
20 #define strequals(x,y) assertStringEquals(x, y, __LINE__)
23 TestFileFd(mode_t
const a_umask
, mode_t
const ExpectedFilePermission
, unsigned int const filemode
, APT::Configuration::Compressor
const &compressor
)
27 static const char* fname
= "apt-filefd-test.txt";
28 if (FileExists(fname
) == true)
29 equals(unlink(fname
), 0);
32 equals(f
.Open(fname
, filemode
, compressor
), true);
33 equals(f
.IsOpen(), true);
34 equals(f
.Failed(), false);
35 equals(umask(a_umask
), a_umask
);
37 std::string test
= "This is a test!\n";
38 equals(f
.Write(test
.c_str(), test
.size()), true);
39 equals(f
.IsOpen(), true);
40 equals(f
.Failed(), false);
43 equals(f
.IsOpen(), false);
44 equals(f
.Failed(), false);
46 equals(f
.Open(fname
, FileFd::ReadOnly
, compressor
), true);
47 equals(f
.IsOpen(), true);
48 equals(f
.Failed(), false);
49 equals(f
.Eof(), false);
50 equalsNot(f
.FileSize(), 0);
51 equals(f
.Failed(), false);
52 equalsNot(f
.ModificationTime(), 0);
53 equals(f
.Failed(), false);
55 // ensure the memory is as predictably messed up
56 # define APT_INIT_READBACK \
58 memset(readback, 'D', sizeof(readback)/sizeof(readback[0])); \
62 char const * const expect
= "This";
63 equals(f
.Read(readback
, strlen(expect
)), true);
64 equals(f
.Failed(), false);
65 equals(f
.Eof(), false);
66 strequals(expect
, readback
);
67 equals(strlen(expect
), f
.Tell());
71 char const * const expect
= "test!\n";
72 equals(f
.Skip((test
.size() - f
.Tell()) - strlen(expect
)), true);
73 equals(f
.Read(readback
, strlen(expect
)), true);
74 equals(f
.Failed(), false);
75 equals(f
.Eof(), false);
76 strequals(expect
, readback
);
77 equals(test
.size(), f
.Tell());
81 equals(f
.Seek(0), true);
82 equals(f
.Eof(), false);
83 equals(f
.Read(readback
, 20, true), true);
84 equals(f
.Failed(), false);
85 equals(f
.Eof(), true);
86 strequals(test
.c_str(), readback
);
87 equals(f
.Size(), f
.Tell());
91 equals(f
.Seek(0), true);
92 equals(f
.Eof(), false);
93 equals(f
.Read(readback
, test
.size(), true), true);
94 equals(f
.Failed(), false);
95 equals(f
.Eof(), false);
96 strequals(test
.c_str(), readback
);
97 equals(f
.Size(), f
.Tell());
101 equals(f
.Seek(0), true);
102 equals(f
.Eof(), false);
103 unsigned long long actual
;
104 equals(f
.Read(readback
, 20, &actual
), true);
105 equals(f
.Failed(), false);
106 equals(f
.Eof(), true);
107 equals(test
.size(), actual
);
108 strequals(test
.c_str(), readback
);
109 equals(f
.Size(), f
.Tell());
113 equals(f
.Seek(0), true);
114 equals(f
.Eof(), false);
115 f
.ReadLine(readback
, 20);
116 equals(f
.Failed(), false);
117 equals(f
.Eof(), false);
118 equals(test
, readback
);
119 equals(f
.Size(), f
.Tell());
123 equals(f
.Seek(0), true);
124 equals(f
.Eof(), false);
125 char const * const expect
= "This";
126 f
.ReadLine(readback
, strlen(expect
) + 1);
127 equals(f
.Failed(), false);
128 equals(f
.Eof(), false);
129 strequals(expect
, readback
);
130 equals(strlen(expect
), f
.Tell());
132 #undef APT_INIT_READBACK
135 equals(f
.IsOpen(), false);
136 equals(f
.Failed(), false);
138 // regression test for permission bug LP: #1304657
139 if (stat(fname
, &buf
) < 0)
141 _error
->Errno("stat", "failed to stat");
144 equals(unlink(fname
), 0);
145 equals(buf
.st_mode
& 0777, ExpectedFilePermission
);
149 static bool TestFileFd(unsigned int const filemode
)
151 std::vector
<APT::Configuration::Compressor
> compressors
= APT::Configuration::getCompressors();
153 // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries
154 compressors
.push_back(APT::Configuration::Compressor("rev", ".reversed", "rev", NULL
, NULL
, 42));
155 //compressors.push_back(APT::Configuration::Compressor("cat", ".ident", "cat", NULL, NULL, 42));
157 for (std::vector
<APT::Configuration::Compressor
>::const_iterator c
= compressors
.begin(); c
!= compressors
.end(); ++c
)
159 if ((filemode
& FileFd::ReadWrite
) == FileFd::ReadWrite
&&
160 (c
->Name
.empty() != true && c
->Binary
.empty() != true))
162 if (TestFileFd(0002, 0664, filemode
, *c
) == false ||
163 TestFileFd(0022, 0644, filemode
, *c
) == false ||
164 TestFileFd(0077, 0600, filemode
, *c
) == false ||
165 TestFileFd(0026, 0640, filemode
, *c
) == false)
167 _error
->DumpErrors();
174 int main(int const argc
, char const * const * const argv
)
176 std::string startdir
;
177 if (argc
> 1 && DirectoryExists(argv
[1]) == true) {
178 startdir
= SafeGetCWD();
179 equals(chdir(argv
[1]), 0);
181 if (TestFileFd(FileFd::WriteOnly
| FileFd::Create
) == false ||
182 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
) == false ||
183 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Exclusive
) == false ||
184 TestFileFd(FileFd::WriteOnly
| FileFd::Atomic
) == false ||
185 TestFileFd(FileFd::WriteOnly
| FileFd::Create
| FileFd::Atomic
) == false ||
186 // short-hands for ReadWrite with these modes
187 TestFileFd(FileFd::WriteEmpty
) == false ||
188 TestFileFd(FileFd::WriteAny
) == false ||
189 TestFileFd(FileFd::WriteTemp
) == false ||
190 TestFileFd(FileFd::WriteAtomic
) == false)
194 if (startdir
.empty() == false)
195 equals(chdir(startdir
.c_str()), 0);
197 std::vector
<std::string
> files
;
199 files
= Glob("*.lst");
200 if (files
.size() != 1)
202 _error
->DumpErrors();
207 files
= Glob("xxxyyyzzz");
208 if (files
.size() != 0 || _error
->PendingError())
210 _error
->DumpErrors();
214 // many matches (number is a bit random)
215 files
= Glob("*.cc");
216 if (files
.size() < 10)
218 _error
->DumpErrors();
224 equals(GetTempDir(), "/tmp");
226 setenv("TMPDIR", "", 1);
227 equals(GetTempDir(), "/tmp");
229 setenv("TMPDIR", "/not-there-no-really-not", 1);
230 equals(GetTempDir(), "/tmp");
232 setenv("TMPDIR", "/usr", 1);
233 equals(GetTempDir(), "/usr");