comparison OrthancFramework/UnitTestsSources/ZipTests.cpp @ 4672:d9942d48fea7

ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 02 Jun 2021 17:35:39 +0200
parents b12faca76a52
children 863383e7e582
comparison
equal deleted inserted replaced
4671:42e1f5bde40b 4672:d9942d48fea7
28 #include <gtest/gtest.h> 28 #include <gtest/gtest.h>
29 29
30 #include "../Sources/Compression/HierarchicalZipWriter.h" 30 #include "../Sources/Compression/HierarchicalZipWriter.h"
31 #include "../Sources/Compression/ZipReader.h" 31 #include "../Sources/Compression/ZipReader.h"
32 #include "../Sources/OrthancException.h" 32 #include "../Sources/OrthancException.h"
33 #include "../Sources/SystemToolbox.h"
33 #include "../Sources/TemporaryFile.h" 34 #include "../Sources/TemporaryFile.h"
34 #include "../Sources/Toolbox.h" 35 #include "../Sources/Toolbox.h"
35 36
36 37
37 using namespace Orthanc; 38 using namespace Orthanc;
187 { 188 {
188 TemporaryFile f; 189 TemporaryFile f;
189 190
190 { 191 {
191 Orthanc::ZipWriter w; 192 Orthanc::ZipWriter w;
193 ASSERT_EQ(0, w.GetArchiveSize());
194
192 w.SetOutputPath(f.GetPath().c_str()); 195 w.SetOutputPath(f.GetPath().c_str());
193 w.Open(); 196 w.Open();
194 w.OpenFile("world/hello"); 197 w.OpenFile("world/hello");
195 w.Write("Hello world"); 198 w.Write("Hello world");
199
200 ASSERT_EQ(w.GetArchiveSize(), SystemToolbox::GetFileSize(f.GetPath()));
196 } 201 }
197 202
198 ASSERT_TRUE(ZipReader::IsZipFile(f.GetPath())); 203 ASSERT_TRUE(ZipReader::IsZipFile(f.GetPath()));
199 204
200 std::unique_ptr<ZipReader> reader(ZipReader::CreateFromFile(f.GetPath())); 205 std::unique_ptr<ZipReader> reader(ZipReader::CreateFromFile(f.GetPath()));
223 228
224 for (int i = 0; i < 2; i++) 229 for (int i = 0; i < 2; i++)
225 { 230 {
226 { 231 {
227 Orthanc::ZipWriter w; 232 Orthanc::ZipWriter w;
228 w.SetZip64(i == 0); 233 w.SetMemoryOutput(memory, (i == 0) /* ZIP64? */);
229 w.SetMemoryOutput(memory);
230 w.Open(); 234 w.Open();
231 235
236 w.OpenFile("world/hello");
237 w.Write("Hello");
238 w.CancelStream();
239 }
240
241 ASSERT_THROW(ZipReader::CreateFromMemory(memory), Orthanc::OrthancException);
242
243 memory.clear();
244 uint64_t archiveSize;
245
246 {
247 Orthanc::ZipWriter w;
248 ASSERT_EQ(0, w.GetArchiveSize());
249
250 w.SetMemoryOutput(memory, (i == 0) /* ZIP64? */);
251 w.Open();
252
253 ASSERT_EQ(0, w.GetArchiveSize());
254
232 w.OpenFile("world/hello"); 255 w.OpenFile("world/hello");
233 w.Write(large); 256 w.Write(large);
234 w.OpenFile("world/hello2"); 257 w.OpenFile("world/hello2");
235 w.Write(large); 258 w.Write(large);
236 w.OpenFile("world/hello3"); 259 w.OpenFile("world/hello3");
237 w.Write("Hello world"); 260 w.Write("Hello world");
238 w.OpenFile("world/hello4"); 261 w.OpenFile("world/hello4");
239 w.Write(large); 262 w.Write(large);
240 } 263
241 264 ASSERT_TRUE(memory.empty());
265
266 uint64_t s1 = w.GetArchiveSize();
267 ASSERT_NE(0, s1);
268
269 w.Close();
270 archiveSize = w.GetArchiveSize();
271
272 ASSERT_NE(archiveSize, s1);
273 ASSERT_EQ(archiveSize, w.GetArchiveSize());
274 }
275
276 ASSERT_EQ(archiveSize, memory.size());
277
242 std::unique_ptr<ZipReader> reader(ZipReader::CreateFromMemory(memory)); 278 std::unique_ptr<ZipReader> reader(ZipReader::CreateFromMemory(memory));
243 279
244 ASSERT_EQ(4u, reader->GetFilesCount()); 280 ASSERT_EQ(4u, reader->GetFilesCount());
245 281
246 { 282 {