Mercurial > hg > orthanc
annotate OrthancFramework/UnitTestsSources/StreamTests.cpp @ 4831:7053502fbf97
added copyright UCLouvain
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Nov 2021 19:01:11 +0100 |
parents | d9473bd5ed43 |
children | 43e613a7756b |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4325
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
4831
7053502fbf97
added copyright UCLouvain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
6 * Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1525 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
11 * the License, or (at your option) any later version. |
1525 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
16 * Lesser General Public License for more details. |
1525 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4063
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
1525 | 21 **/ |
22 | |
23 | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
24 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1 |
4063
e00f3d089991
shared library of orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4062
diff
changeset
|
25 // Must be the first to be sure to use the Orthanc framework shared library |
4014
27628b0f6ada
merging logging code for plugins and files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3992
diff
changeset
|
26 # include <OrthancFramework.h> |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
27 #endif |
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
28 |
4062 | 29 #include <gtest/gtest.h> |
1525 | 30 |
4045 | 31 #include "../Sources/Toolbox.h" |
32 #include "../Sources/OrthancException.h" | |
33 #include "../Sources/HttpServer/BufferHttpSender.h" | |
34 #include "../Sources/HttpServer/HttpStreamTranscoder.h" | |
35 #include "../Sources/Compression/ZlibCompressor.h" | |
36 #include "../Sources/Compression/GzipCompressor.h" | |
1525 | 37 |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
38 #if ORTHANC_SANDBOXED != 1 |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
39 # include "../Sources/HttpServer/FilesystemHttpSender.h" |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
40 # include "../Sources/SystemToolbox.h" |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
41 #endif |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
42 |
1525 | 43 |
44 using namespace Orthanc; | |
45 | |
46 | |
47 TEST(Gzip, Basic) | |
48 { | |
49 std::string s = "Hello world"; | |
50 | |
51 std::string compressed; | |
52 GzipCompressor c; | |
53 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
54 IBufferCompressor::Compress(compressed, c, s); | |
55 | |
56 std::string uncompressed; | |
57 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
58 ASSERT_EQ(s.size(), uncompressed.size()); | |
59 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
60 } | |
61 | |
62 | |
63 TEST(Gzip, Empty) | |
64 { | |
65 std::string s; | |
66 | |
67 std::string compressed; | |
68 GzipCompressor c; | |
69 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
70 c.SetPrefixWithUncompressedSize(false); | |
71 IBufferCompressor::Compress(compressed, c, s); | |
72 | |
73 std::string uncompressed; | |
74 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 75 ASSERT_TRUE(uncompressed.empty()); |
1525 | 76 } |
77 | |
78 | |
79 TEST(Gzip, BasicWithPrefix) | |
80 { | |
81 std::string s = "Hello world"; | |
82 | |
83 std::string compressed; | |
84 GzipCompressor c; | |
85 c.SetPrefixWithUncompressedSize(true); | |
86 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
87 IBufferCompressor::Compress(compressed, c, s); | |
88 | |
89 std::string uncompressed; | |
90 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
91 ASSERT_EQ(s.size(), uncompressed.size()); | |
92 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
93 } | |
94 | |
95 | |
96 TEST(Gzip, EmptyWithPrefix) | |
97 { | |
98 std::string s; | |
99 | |
100 std::string compressed; | |
101 GzipCompressor c; | |
102 c.SetPrefixWithUncompressedSize(true); | |
103 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
104 IBufferCompressor::Compress(compressed, c, s); | |
105 | |
106 std::string uncompressed; | |
107 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 108 ASSERT_TRUE(uncompressed.empty()); |
1525 | 109 } |
110 | |
111 | |
112 TEST(Zlib, Basic) | |
113 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
114 std::string s = Toolbox::GenerateUuid(); |
1525 | 115 s = s + s + s + s; |
116 | |
4204 | 117 std::string compressed; |
1525 | 118 ZlibCompressor c; |
119 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
120 IBufferCompressor::Compress(compressed, c, s); | |
121 | |
122 std::string uncompressed; | |
123 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
124 ASSERT_EQ(s.size(), uncompressed.size()); | |
125 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
126 } | |
127 | |
128 | |
129 TEST(Zlib, Level) | |
130 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
131 std::string s = Toolbox::GenerateUuid(); |
1525 | 132 s = s + s + s + s; |
133 | |
134 std::string compressed, compressed2; | |
135 ZlibCompressor c; | |
136 c.SetCompressionLevel(9); | |
137 IBufferCompressor::Compress(compressed, c, s); | |
138 | |
139 c.SetCompressionLevel(0); | |
140 IBufferCompressor::Compress(compressed2, c, s); | |
141 | |
142 ASSERT_TRUE(compressed.size() < compressed2.size()); | |
143 } | |
144 | |
145 | |
146 TEST(Zlib, DISABLED_Corrupted) // Disabled because it may result in a crash | |
147 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
148 std::string s = Toolbox::GenerateUuid(); |
1525 | 149 s = s + s + s + s; |
150 | |
151 std::string compressed; | |
152 ZlibCompressor c; | |
153 IBufferCompressor::Compress(compressed, c, s); | |
154 | |
1972 | 155 ASSERT_FALSE(compressed.empty()); |
1525 | 156 compressed[compressed.size() - 1] = 'a'; |
157 std::string u; | |
158 | |
159 ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException); | |
160 } | |
161 | |
162 | |
163 TEST(Zlib, Empty) | |
164 { | |
165 std::string s = ""; | |
166 | |
167 std::string compressed, compressed2; | |
168 ZlibCompressor c; | |
169 IBufferCompressor::Compress(compressed, c, s); | |
170 ASSERT_EQ(compressed, compressed2); | |
171 | |
172 std::string uncompressed; | |
173 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 174 ASSERT_TRUE(uncompressed.empty()); |
1525 | 175 } |
176 | |
177 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
178 #if ORTHANC_SANDBOXED != 1 |
1525 | 179 static bool ReadAllStream(std::string& result, |
180 IHttpStreamAnswer& stream, | |
181 bool allowGzip = false, | |
182 bool allowDeflate = false) | |
183 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
184 stream.SetupHttpCompression(allowGzip, allowDeflate); |
1525 | 185 |
1545 | 186 result.resize(static_cast<size_t>(stream.GetContentLength())); |
1525 | 187 |
188 size_t pos = 0; | |
189 while (stream.ReadNextChunk()) | |
190 { | |
191 size_t s = stream.GetChunkSize(); | |
192 if (pos + s > result.size()) | |
193 { | |
194 return false; | |
195 } | |
196 | |
197 memcpy(&result[pos], stream.GetChunkContent(), s); | |
198 pos += s; | |
199 } | |
200 | |
201 return pos == result.size(); | |
202 } | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
203 #endif |
1525 | 204 |
205 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
206 #if ORTHANC_SANDBOXED != 1 |
1525 | 207 TEST(BufferHttpSender, Basic) |
208 { | |
209 const std::string s = "Hello world"; | |
210 std::string t; | |
211 | |
212 { | |
213 BufferHttpSender sender; | |
214 sender.SetChunkSize(1); | |
215 ASSERT_TRUE(ReadAllStream(t, sender)); | |
216 ASSERT_EQ(0u, t.size()); | |
217 } | |
218 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
219 for (int cs = 0; cs < 5; cs++) |
1525 | 220 { |
221 BufferHttpSender sender; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
222 sender.SetChunkSize(cs); |
1525 | 223 sender.GetBuffer() = s; |
224 ASSERT_TRUE(ReadAllStream(t, sender)); | |
225 ASSERT_EQ(s, t); | |
226 } | |
227 } | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
228 #endif |
1525 | 229 |
230 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
231 #if ORTHANC_SANDBOXED != 1 |
1525 | 232 TEST(FilesystemHttpSender, Basic) |
233 { | |
234 const std::string& path = "UnitTestsResults/stream"; | |
235 const std::string s = "Hello world"; | |
236 std::string t; | |
237 | |
238 { | |
2140 | 239 SystemToolbox::WriteFile(s, path); |
1525 | 240 FilesystemHttpSender sender(path); |
241 ASSERT_TRUE(ReadAllStream(t, sender)); | |
242 ASSERT_EQ(s, t); | |
243 } | |
244 | |
245 { | |
2140 | 246 SystemToolbox::WriteFile("", path); |
1525 | 247 FilesystemHttpSender sender(path); |
248 ASSERT_TRUE(ReadAllStream(t, sender)); | |
249 ASSERT_EQ(0u, t.size()); | |
250 } | |
251 } | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
252 #endif |
1525 | 253 |
254 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
255 #if ORTHANC_SANDBOXED != 1 |
1525 | 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 } |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
336 #endif |