diff options
author | Narayan Kamath | 2017-12-21 06:54:52 -0600 |
---|---|---|
committer | Narayan Kamath | 2017-12-21 06:55:59 -0600 |
commit | f37bb8e45b302091932a7a169c26146a1254aaff (patch) | |
tree | 04fbe720d3c4e0cbdd3a8ec45288fc62a839b61b /libziparchive/zip_archive_test.cc | |
parent | df9fbc7d75ba4148c4849924bf945fd44f01abb3 (diff) | |
download | platform-system-core-f37bb8e45b302091932a7a169c26146a1254aaff.tar.gz platform-system-core-f37bb8e45b302091932a7a169c26146a1254aaff.tar.xz platform-system-core-f37bb8e45b302091932a7a169c26146a1254aaff.zip |
zip_archive: Fix tests broken by 1f93d71022cca7bb6bb9eec49.
Moving to std::hash changed iteration order but these tests should
not have relied on hash_map iteration order anyway.
Test: zip_archive_test
Change-Id: I712bf2307c8770f03ea6f074bfc506a40cdcb066
Diffstat (limited to 'libziparchive/zip_archive_test.cc')
-rw-r--r-- | libziparchive/zip_archive_test.cc | 131 |
1 files changed, 27 insertions, 104 deletions
diff --git a/libziparchive/zip_archive_test.cc b/libziparchive/zip_archive_test.cc index cb72f0e9b..466be4a3c 100644 --- a/libziparchive/zip_archive_test.cc +++ b/libziparchive/zip_archive_test.cc | |||
@@ -64,11 +64,6 @@ static int32_t OpenArchiveWrapper(const std::string& name, ZipArchiveHandle* han | |||
64 | return OpenArchive(abs_path.c_str(), handle); | 64 | return OpenArchive(abs_path.c_str(), handle); |
65 | } | 65 | } |
66 | 66 | ||
67 | static void AssertNameEquals(const std::string& name_str, const ZipString& name) { | ||
68 | ASSERT_EQ(name_str.size(), name.name_length); | ||
69 | ASSERT_EQ(0, memcmp(name_str.c_str(), name.name, name.name_length)); | ||
70 | } | ||
71 | |||
72 | static void SetZipString(ZipString* zip_str, const std::string& str) { | 67 | static void SetZipString(ZipString* zip_str, const std::string& str) { |
73 | zip_str->name = reinterpret_cast<const uint8_t*>(str.c_str()); | 68 | zip_str->name = reinterpret_cast<const uint8_t*>(str.c_str()); |
74 | zip_str->name_length = str.size(); | 69 | zip_str->name_length = str.size(); |
@@ -117,132 +112,60 @@ TEST(ziparchive, OpenDoNotAssumeFdOwnership) { | |||
117 | close(fd); | 112 | close(fd); |
118 | } | 113 | } |
119 | 114 | ||
120 | TEST(ziparchive, Iteration) { | 115 | static void AssertIterationOrder(const ZipString* prefix, const ZipString* suffix, |
116 | const std::vector<std::string>& expected_names_sorted) { | ||
121 | ZipArchiveHandle handle; | 117 | ZipArchiveHandle handle; |
122 | ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); | 118 | ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); |
123 | 119 | ||
124 | void* iteration_cookie; | 120 | void* iteration_cookie; |
125 | ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, nullptr, nullptr)); | 121 | ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, prefix, suffix)); |
126 | 122 | ||
127 | ZipEntry data; | 123 | ZipEntry data; |
128 | ZipString name; | 124 | std::vector<std::string> names; |
129 | |||
130 | // b/c.txt | ||
131 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
132 | AssertNameEquals("b/c.txt", name); | ||
133 | |||
134 | // b/d.txt | ||
135 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
136 | AssertNameEquals("b/d.txt", name); | ||
137 | |||
138 | // a.txt | ||
139 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
140 | AssertNameEquals("a.txt", name); | ||
141 | 125 | ||
142 | // b.txt | 126 | ZipString name; |
143 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | 127 | for (size_t i = 0; i < expected_names_sorted.size(); ++i) { |
144 | AssertNameEquals("b.txt", name); | 128 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); |
145 | 129 | names.push_back(std::string(reinterpret_cast<const char*>(name.name), name.name_length)); | |
146 | // b/ | 130 | } |
147 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
148 | AssertNameEquals("b/", name); | ||
149 | 131 | ||
150 | // End of iteration. | 132 | // End of iteration. |
151 | ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); | 133 | ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); |
152 | |||
153 | CloseArchive(handle); | 134 | CloseArchive(handle); |
154 | } | ||
155 | 135 | ||
156 | TEST(ziparchive, IterationWithPrefix) { | 136 | // Assert that the names are as expected. |
157 | ZipArchiveHandle handle; | 137 | std::sort(names.begin(), names.end()); |
158 | ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); | 138 | ASSERT_EQ(expected_names_sorted, names); |
159 | 139 | } | |
160 | void* iteration_cookie; | ||
161 | ZipString prefix("b/"); | ||
162 | ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, nullptr)); | ||
163 | |||
164 | ZipEntry data; | ||
165 | ZipString name; | ||
166 | |||
167 | // b/c.txt | ||
168 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
169 | AssertNameEquals("b/c.txt", name); | ||
170 | 140 | ||
171 | // b/d.txt | 141 | TEST(ziparchive, Iteration) { |
172 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | 142 | static const std::vector<std::string> kExpectedMatchesSorted = {"a.txt", "b.txt", "b/", "b/c.txt", |
173 | AssertNameEquals("b/d.txt", name); | 143 | "b/d.txt"}; |
174 | 144 | ||
175 | // b/ | 145 | AssertIterationOrder(nullptr, nullptr, kExpectedMatchesSorted); |
176 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | 146 | } |
177 | AssertNameEquals("b/", name); | ||
178 | 147 | ||
179 | // End of iteration. | 148 | TEST(ziparchive, IterationWithPrefix) { |
180 | ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); | 149 | ZipString prefix("b/"); |
150 | static const std::vector<std::string> kExpectedMatchesSorted = {"b/", "b/c.txt", "b/d.txt"}; | ||
181 | 151 | ||
182 | CloseArchive(handle); | 152 | AssertIterationOrder(&prefix, nullptr, kExpectedMatchesSorted); |
183 | } | 153 | } |
184 | 154 | ||
185 | TEST(ziparchive, IterationWithSuffix) { | 155 | TEST(ziparchive, IterationWithSuffix) { |
186 | ZipArchiveHandle handle; | ||
187 | ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); | ||
188 | |||
189 | void* iteration_cookie; | ||
190 | ZipString suffix(".txt"); | 156 | ZipString suffix(".txt"); |
191 | ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, nullptr, &suffix)); | 157 | static const std::vector<std::string> kExpectedMatchesSorted = {"a.txt", "b.txt", "b/c.txt", |
192 | 158 | "b/d.txt"}; | |
193 | ZipEntry data; | ||
194 | ZipString name; | ||
195 | |||
196 | // b/c.txt | ||
197 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
198 | AssertNameEquals("b/c.txt", name); | ||
199 | 159 | ||
200 | // b/d.txt | 160 | AssertIterationOrder(nullptr, &suffix, kExpectedMatchesSorted); |
201 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
202 | AssertNameEquals("b/d.txt", name); | ||
203 | |||
204 | // a.txt | ||
205 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
206 | AssertNameEquals("a.txt", name); | ||
207 | |||
208 | // b.txt | ||
209 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
210 | AssertNameEquals("b.txt", name); | ||
211 | |||
212 | // End of iteration. | ||
213 | ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); | ||
214 | |||
215 | CloseArchive(handle); | ||
216 | } | 161 | } |
217 | 162 | ||
218 | TEST(ziparchive, IterationWithPrefixAndSuffix) { | 163 | TEST(ziparchive, IterationWithPrefixAndSuffix) { |
219 | ZipArchiveHandle handle; | ||
220 | ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); | ||
221 | |||
222 | void* iteration_cookie; | ||
223 | ZipString prefix("b"); | 164 | ZipString prefix("b"); |
224 | ZipString suffix(".txt"); | 165 | ZipString suffix(".txt"); |
225 | ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix)); | 166 | static const std::vector<std::string> kExpectedMatchesSorted = {"b.txt", "b/c.txt", "b/d.txt"}; |
226 | |||
227 | ZipEntry data; | ||
228 | ZipString name; | ||
229 | 167 | ||
230 | // b/c.txt | 168 | AssertIterationOrder(&prefix, &suffix, kExpectedMatchesSorted); |
231 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
232 | AssertNameEquals("b/c.txt", name); | ||
233 | |||
234 | // b/d.txt | ||
235 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
236 | AssertNameEquals("b/d.txt", name); | ||
237 | |||
238 | // b.txt | ||
239 | ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); | ||
240 | AssertNameEquals("b.txt", name); | ||
241 | |||
242 | // End of iteration. | ||
243 | ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); | ||
244 | |||
245 | CloseArchive(handle); | ||
246 | } | 169 | } |
247 | 170 | ||
248 | TEST(ziparchive, IterationWithBadPrefixAndSuffix) { | 171 | TEST(ziparchive, IterationWithBadPrefixAndSuffix) { |