comparison OrthancFramework/UnitTestsSources/ZipTests.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents UnitTestsSources/ZipTests.cpp@27628b0f6ada
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1
35 # include <OrthancFramework.h>
36 #endif
37
38 #include "PrecompiledHeadersUnitTests.h"
39 #include "gtest/gtest.h"
40
41 #include "../Core/OrthancException.h"
42 #include "../Core/Compression/ZipWriter.h"
43 #include "../Core/Compression/HierarchicalZipWriter.h"
44 #include "../Core/Toolbox.h"
45
46
47 using namespace Orthanc;
48
49 TEST(ZipWriter, Basic)
50 {
51 Orthanc::ZipWriter w;
52 w.SetOutputPath("UnitTestsResults/hello.zip");
53 w.Open();
54 w.OpenFile("world/hello");
55 w.Write("Hello world");
56 }
57
58
59 TEST(ZipWriter, Basic64)
60 {
61 Orthanc::ZipWriter w;
62 w.SetOutputPath("UnitTestsResults/hello64.zip");
63 w.SetZip64(true);
64 w.Open();
65 w.OpenFile("world/hello");
66 w.Write("Hello world");
67 }
68
69
70 TEST(ZipWriter, Exceptions)
71 {
72 Orthanc::ZipWriter w;
73 ASSERT_THROW(w.Open(), Orthanc::OrthancException);
74 w.SetOutputPath("UnitTestsResults/hello3.zip");
75 w.Open();
76 ASSERT_THROW(w.Write("hello world"), Orthanc::OrthancException);
77 }
78
79
80 TEST(ZipWriter, Append)
81 {
82 {
83 Orthanc::ZipWriter w;
84 w.SetAppendToExisting(false);
85 w.SetOutputPath("UnitTestsResults/append.zip");
86 w.Open();
87 w.OpenFile("world/hello");
88 w.Write("Hello world 1");
89 }
90
91 {
92 Orthanc::ZipWriter w;
93 w.SetAppendToExisting(true);
94 w.SetOutputPath("UnitTestsResults/append.zip");
95 w.Open();
96 w.OpenFile("world/appended");
97 w.Write("Hello world 2");
98 }
99 }
100
101
102
103
104
105 namespace Orthanc
106 {
107 // The namespace is necessary
108 // http://code.google.com/p/googletest/wiki/AdvancedGuide#Private_Class_Members
109
110 TEST(HierarchicalZipWriter, Index)
111 {
112 HierarchicalZipWriter::Index i;
113 ASSERT_EQ("hello", i.OpenFile("hello"));
114 ASSERT_EQ("hello-2", i.OpenFile("hello"));
115 ASSERT_EQ("coucou", i.OpenFile("coucou"));
116 ASSERT_EQ("hello-3", i.OpenFile("hello"));
117
118 i.OpenDirectory("coucou");
119
120 ASSERT_EQ("coucou-2/world", i.OpenFile("world"));
121 ASSERT_EQ("coucou-2/world-2", i.OpenFile("world"));
122
123 i.OpenDirectory("world");
124
125 ASSERT_EQ("coucou-2/world-3/hello", i.OpenFile("hello"));
126 ASSERT_EQ("coucou-2/world-3/hello-2", i.OpenFile("hello"));
127
128 i.CloseDirectory();
129
130 ASSERT_EQ("coucou-2/world-4", i.OpenFile("world"));
131
132 i.CloseDirectory();
133
134 ASSERT_EQ("coucou-3", i.OpenFile("coucou"));
135
136 ASSERT_THROW(i.CloseDirectory(), OrthancException);
137 }
138
139
140 TEST(HierarchicalZipWriter, Filenames)
141 {
142 ASSERT_EQ("trE hell", HierarchicalZipWriter::Index::KeepAlphanumeric(" ÊtrE hellô "));
143
144 // The "^" character is considered as a space in DICOM
145 ASSERT_EQ("Hel lo world", HierarchicalZipWriter::Index::KeepAlphanumeric(" Hel^^ ^\r\n\t^^lo \t <world> "));
146 }
147 }
148
149
150 TEST(HierarchicalZipWriter, Basic)
151 {
152 static const std::string SPACES = " ";
153
154 HierarchicalZipWriter w("UnitTestsResults/hello2.zip");
155
156 w.SetCompressionLevel(0);
157
158 // Inside "/"
159 w.OpenFile("hello");
160 w.Write(SPACES + "hello\n");
161 w.OpenFile("hello");
162 w.Write(SPACES + "hello-2\n");
163 w.OpenDirectory("hello");
164
165 // Inside "/hello-3"
166 w.OpenFile("hello");
167 w.Write(SPACES + "hello\n");
168 w.OpenDirectory("hello");
169
170 w.SetCompressionLevel(9);
171
172 // Inside "/hello-3/hello-2"
173 w.OpenFile("hello");
174 w.Write(SPACES + "hello\n");
175 w.OpenFile("hello");
176 w.Write(SPACES + "hello-2\n");
177 w.CloseDirectory();
178
179 // Inside "/hello-3"
180 w.OpenFile("hello");
181 w.Write(SPACES + "hello-3\n");
182
183 /**
184
185 TO CHECK THE CONTENT OF THE "hello2.zip" FILE:
186
187 # unzip -v hello2.zip
188
189 => There must be 6 files. The first 3 files must have a negative
190 compression ratio.
191
192 **/
193 }