Mercurial > hg > orthanc
annotate UnitTestsSources/StreamTests.cpp @ 4085:da06381f3091
backporting fix from framework branch to mainline
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Jun 2020 15:11:41 +0200 |
parents | 27628b0f6ada |
children |
rev | line source |
---|---|
1525 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1525 | 4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
1525 | 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 | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1 |
4014
27628b0f6ada
merging logging code for plugins and files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3992
diff
changeset
|
35 # include <OrthancFramework.h> |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
36 #endif |
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
37 |
1525 | 38 #include "PrecompiledHeadersUnitTests.h" |
39 #include "gtest/gtest.h" | |
40 | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
41 #include "../Core/SystemToolbox.h" |
1525 | 42 #include "../Core/Toolbox.h" |
43 #include "../Core/OrthancException.h" | |
44 #include "../Core/HttpServer/BufferHttpSender.h" | |
45 #include "../Core/HttpServer/FilesystemHttpSender.h" | |
46 #include "../Core/HttpServer/HttpStreamTranscoder.h" | |
47 #include "../Core/Compression/ZlibCompressor.h" | |
48 #include "../Core/Compression/GzipCompressor.h" | |
49 | |
50 | |
51 using namespace Orthanc; | |
52 | |
53 | |
54 TEST(Gzip, Basic) | |
55 { | |
56 std::string s = "Hello world"; | |
57 | |
58 std::string compressed; | |
59 GzipCompressor c; | |
60 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
61 IBufferCompressor::Compress(compressed, c, s); | |
62 | |
63 std::string uncompressed; | |
64 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
65 ASSERT_EQ(s.size(), uncompressed.size()); | |
66 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
67 } | |
68 | |
69 | |
70 TEST(Gzip, Empty) | |
71 { | |
72 std::string s; | |
73 | |
74 std::string compressed; | |
75 GzipCompressor c; | |
76 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
77 c.SetPrefixWithUncompressedSize(false); | |
78 IBufferCompressor::Compress(compressed, c, s); | |
79 | |
80 std::string uncompressed; | |
81 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 82 ASSERT_TRUE(uncompressed.empty()); |
1525 | 83 } |
84 | |
85 | |
86 TEST(Gzip, BasicWithPrefix) | |
87 { | |
88 std::string s = "Hello world"; | |
89 | |
90 std::string compressed; | |
91 GzipCompressor c; | |
92 c.SetPrefixWithUncompressedSize(true); | |
93 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
94 IBufferCompressor::Compress(compressed, c, s); | |
95 | |
96 std::string uncompressed; | |
97 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
98 ASSERT_EQ(s.size(), uncompressed.size()); | |
99 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
100 } | |
101 | |
102 | |
103 TEST(Gzip, EmptyWithPrefix) | |
104 { | |
105 std::string s; | |
106 | |
107 std::string compressed; | |
108 GzipCompressor c; | |
109 c.SetPrefixWithUncompressedSize(true); | |
110 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
111 IBufferCompressor::Compress(compressed, c, s); | |
112 | |
113 std::string uncompressed; | |
114 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 115 ASSERT_TRUE(uncompressed.empty()); |
1525 | 116 } |
117 | |
118 | |
119 TEST(Zlib, Basic) | |
120 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
121 std::string s = Toolbox::GenerateUuid(); |
1525 | 122 s = s + s + s + s; |
123 | |
124 std::string compressed, compressed2; | |
125 ZlibCompressor c; | |
126 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
127 IBufferCompressor::Compress(compressed, c, s); | |
128 | |
129 std::string uncompressed; | |
130 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
131 ASSERT_EQ(s.size(), uncompressed.size()); | |
132 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
133 } | |
134 | |
135 | |
136 TEST(Zlib, Level) | |
137 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
138 std::string s = Toolbox::GenerateUuid(); |
1525 | 139 s = s + s + s + s; |
140 | |
141 std::string compressed, compressed2; | |
142 ZlibCompressor c; | |
143 c.SetCompressionLevel(9); | |
144 IBufferCompressor::Compress(compressed, c, s); | |
145 | |
146 c.SetCompressionLevel(0); | |
147 IBufferCompressor::Compress(compressed2, c, s); | |
148 | |
149 ASSERT_TRUE(compressed.size() < compressed2.size()); | |
150 } | |
151 | |
152 | |
153 TEST(Zlib, DISABLED_Corrupted) // Disabled because it may result in a crash | |
154 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
155 std::string s = Toolbox::GenerateUuid(); |
1525 | 156 s = s + s + s + s; |
157 | |
158 std::string compressed; | |
159 ZlibCompressor c; | |
160 IBufferCompressor::Compress(compressed, c, s); | |
161 | |
1972 | 162 ASSERT_FALSE(compressed.empty()); |
1525 | 163 compressed[compressed.size() - 1] = 'a'; |
164 std::string u; | |
165 | |
166 ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException); | |
167 } | |
168 | |
169 | |
170 TEST(Zlib, Empty) | |
171 { | |
172 std::string s = ""; | |
173 | |
174 std::string compressed, compressed2; | |
175 ZlibCompressor c; | |
176 IBufferCompressor::Compress(compressed, c, s); | |
177 ASSERT_EQ(compressed, compressed2); | |
178 | |
179 std::string uncompressed; | |
180 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 181 ASSERT_TRUE(uncompressed.empty()); |
1525 | 182 } |
183 | |
184 | |
185 static bool ReadAllStream(std::string& result, | |
186 IHttpStreamAnswer& stream, | |
187 bool allowGzip = false, | |
188 bool allowDeflate = false) | |
189 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
190 stream.SetupHttpCompression(allowGzip, allowDeflate); |
1525 | 191 |
1545 | 192 result.resize(static_cast<size_t>(stream.GetContentLength())); |
1525 | 193 |
194 size_t pos = 0; | |
195 while (stream.ReadNextChunk()) | |
196 { | |
197 size_t s = stream.GetChunkSize(); | |
198 if (pos + s > result.size()) | |
199 { | |
200 return false; | |
201 } | |
202 | |
203 memcpy(&result[pos], stream.GetChunkContent(), s); | |
204 pos += s; | |
205 } | |
206 | |
207 return pos == result.size(); | |
208 } | |
209 | |
210 | |
211 TEST(BufferHttpSender, Basic) | |
212 { | |
213 const std::string s = "Hello world"; | |
214 std::string t; | |
215 | |
216 { | |
217 BufferHttpSender sender; | |
218 sender.SetChunkSize(1); | |
219 ASSERT_TRUE(ReadAllStream(t, sender)); | |
220 ASSERT_EQ(0u, t.size()); | |
221 } | |
222 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
223 for (int cs = 0; cs < 5; cs++) |
1525 | 224 { |
225 BufferHttpSender sender; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
226 sender.SetChunkSize(cs); |
1525 | 227 sender.GetBuffer() = s; |
228 ASSERT_TRUE(ReadAllStream(t, sender)); | |
229 ASSERT_EQ(s, t); | |
230 } | |
231 } | |
232 | |
233 | |
234 TEST(FilesystemHttpSender, Basic) | |
235 { | |
236 const std::string& path = "UnitTestsResults/stream"; | |
237 const std::string s = "Hello world"; | |
238 std::string t; | |
239 | |
240 { | |
2140 | 241 SystemToolbox::WriteFile(s, path); |
1525 | 242 FilesystemHttpSender sender(path); |
243 ASSERT_TRUE(ReadAllStream(t, sender)); | |
244 ASSERT_EQ(s, t); | |
245 } | |
246 | |
247 { | |
2140 | 248 SystemToolbox::WriteFile("", path); |
1525 | 249 FilesystemHttpSender sender(path); |
250 ASSERT_TRUE(ReadAllStream(t, sender)); | |
251 ASSERT_EQ(0u, t.size()); | |
252 } | |
253 } | |
254 | |
255 | |
256 TEST(HttpStreamTranscoder, Basic) | |
257 { | |
258 ZlibCompressor compressor; | |
259 | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
260 const std::string s = "Hello world " + Toolbox::GenerateUuid(); |
1525 | 261 |
262 std::string t; | |
263 IBufferCompressor::Compress(t, compressor, s); | |
264 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
265 for (int cs = 0; cs < 5; cs++) |
1525 | 266 { |
267 BufferHttpSender sender; | |
268 sender.SetChunkSize(cs); | |
269 sender.GetBuffer() = t; | |
270 std::string u; | |
271 ASSERT_TRUE(ReadAllStream(u, sender)); | |
272 | |
273 std::string v; | |
274 IBufferCompressor::Uncompress(v, compressor, u); | |
275 ASSERT_EQ(s, v); | |
276 } | |
277 | |
278 // Pass-through test, no decompression occurs | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
279 for (int cs = 0; cs < 5; cs++) |
1525 | 280 { |
281 BufferHttpSender sender; | |
282 sender.SetChunkSize(cs); | |
283 sender.GetBuffer() = t; | |
284 | |
285 HttpStreamTranscoder transcode(sender, CompressionType_None); | |
286 | |
287 std::string u; | |
288 ASSERT_TRUE(ReadAllStream(u, transcode)); | |
289 | |
290 ASSERT_EQ(t, u); | |
291 } | |
292 | |
293 // Pass-through test, decompression occurs | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
294 for (int cs = 0; cs < 5; cs++) |
1525 | 295 { |
296 BufferHttpSender sender; | |
297 sender.SetChunkSize(cs); | |
298 sender.GetBuffer() = t; | |
299 | |
300 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
301 | |
302 std::string u; | |
303 ASSERT_TRUE(ReadAllStream(u, transcode, false, false)); | |
304 | |
305 ASSERT_EQ(s, u); | |
306 } | |
307 | |
308 // Pass-through test with zlib, no decompression occurs but deflate is sent | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
309 for (int cs = 0; cs < 16; cs++) |
1525 | 310 { |
311 BufferHttpSender sender; | |
312 sender.SetChunkSize(cs); | |
313 sender.GetBuffer() = t; | |
314 | |
315 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
316 | |
317 std::string u; | |
318 ASSERT_TRUE(ReadAllStream(u, transcode, false, true)); | |
319 | |
320 ASSERT_EQ(t.size() - sizeof(uint64_t), u.size()); | |
321 ASSERT_EQ(t.substr(sizeof(uint64_t)), u); | |
322 } | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
323 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
324 for (int cs = 0; cs < 3; cs++) |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
325 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
326 BufferHttpSender sender; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
327 sender.SetChunkSize(cs); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
328 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
329 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
330 std::string u; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
331 ASSERT_TRUE(ReadAllStream(u, transcode, false, true)); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
332 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
333 ASSERT_EQ(0u, u.size()); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
334 } |
1525 | 335 } |