Mercurial > hg > orthanc
annotate OrthancFramework/UnitTestsSources/StreamTests.cpp @ 4709:a9a75281cae9 openssl-3.x
fix build on MSVC 2008 32bit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Jun 2021 10:26:27 +0200 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
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 |
1525 | 6 * |
7 * 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
|
8 * 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
|
9 * 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
|
10 * the License, or (at your option) any later version. |
1525 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * 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
|
15 * Lesser General Public License for more details. |
1525 | 16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
1525 | 20 **/ |
21 | |
22 | |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
23 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1 |
4063
e00f3d089991
shared library of orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4062
diff
changeset
|
24 // 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
|
25 # include <OrthancFramework.h> |
3992
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
26 #endif |
f9863630ec7f
working on the shared library for Orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
27 |
4062 | 28 #include <gtest/gtest.h> |
1525 | 29 |
4045 | 30 #include "../Sources/Toolbox.h" |
31 #include "../Sources/OrthancException.h" | |
32 #include "../Sources/HttpServer/BufferHttpSender.h" | |
33 #include "../Sources/HttpServer/HttpStreamTranscoder.h" | |
34 #include "../Sources/Compression/ZlibCompressor.h" | |
35 #include "../Sources/Compression/GzipCompressor.h" | |
1525 | 36 |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
37 #if ORTHANC_SANDBOXED != 1 |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
38 # include "../Sources/HttpServer/FilesystemHttpSender.h" |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
39 # include "../Sources/SystemToolbox.h" |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
40 #endif |
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
41 |
1525 | 42 |
43 using namespace Orthanc; | |
44 | |
45 | |
46 TEST(Gzip, Basic) | |
47 { | |
48 std::string s = "Hello world"; | |
49 | |
50 std::string compressed; | |
51 GzipCompressor c; | |
52 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
53 IBufferCompressor::Compress(compressed, c, s); | |
54 | |
55 std::string uncompressed; | |
56 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
57 ASSERT_EQ(s.size(), uncompressed.size()); | |
58 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
59 } | |
60 | |
61 | |
62 TEST(Gzip, Empty) | |
63 { | |
64 std::string s; | |
65 | |
66 std::string compressed; | |
67 GzipCompressor c; | |
68 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
69 c.SetPrefixWithUncompressedSize(false); | |
70 IBufferCompressor::Compress(compressed, c, s); | |
71 | |
72 std::string uncompressed; | |
73 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 74 ASSERT_TRUE(uncompressed.empty()); |
1525 | 75 } |
76 | |
77 | |
78 TEST(Gzip, BasicWithPrefix) | |
79 { | |
80 std::string s = "Hello world"; | |
81 | |
82 std::string compressed; | |
83 GzipCompressor c; | |
84 c.SetPrefixWithUncompressedSize(true); | |
85 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
86 IBufferCompressor::Compress(compressed, c, s); | |
87 | |
88 std::string uncompressed; | |
89 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
90 ASSERT_EQ(s.size(), uncompressed.size()); | |
91 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
92 } | |
93 | |
94 | |
95 TEST(Gzip, EmptyWithPrefix) | |
96 { | |
97 std::string s; | |
98 | |
99 std::string compressed; | |
100 GzipCompressor c; | |
101 c.SetPrefixWithUncompressedSize(true); | |
102 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
103 IBufferCompressor::Compress(compressed, c, s); | |
104 | |
105 std::string uncompressed; | |
106 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 107 ASSERT_TRUE(uncompressed.empty()); |
1525 | 108 } |
109 | |
110 | |
111 TEST(Zlib, Basic) | |
112 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
113 std::string s = Toolbox::GenerateUuid(); |
1525 | 114 s = s + s + s + s; |
115 | |
4204 | 116 std::string compressed; |
1525 | 117 ZlibCompressor c; |
118 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
119 IBufferCompressor::Compress(compressed, c, s); | |
120 | |
121 std::string uncompressed; | |
122 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
123 ASSERT_EQ(s.size(), uncompressed.size()); | |
124 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
125 } | |
126 | |
127 | |
128 TEST(Zlib, Level) | |
129 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
130 std::string s = Toolbox::GenerateUuid(); |
1525 | 131 s = s + s + s + s; |
132 | |
133 std::string compressed, compressed2; | |
134 ZlibCompressor c; | |
135 c.SetCompressionLevel(9); | |
136 IBufferCompressor::Compress(compressed, c, s); | |
137 | |
138 c.SetCompressionLevel(0); | |
139 IBufferCompressor::Compress(compressed2, c, s); | |
140 | |
141 ASSERT_TRUE(compressed.size() < compressed2.size()); | |
142 } | |
143 | |
144 | |
145 TEST(Zlib, DISABLED_Corrupted) // Disabled because it may result in a crash | |
146 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
147 std::string s = Toolbox::GenerateUuid(); |
1525 | 148 s = s + s + s + s; |
149 | |
150 std::string compressed; | |
151 ZlibCompressor c; | |
152 IBufferCompressor::Compress(compressed, c, s); | |
153 | |
1972 | 154 ASSERT_FALSE(compressed.empty()); |
1525 | 155 compressed[compressed.size() - 1] = 'a'; |
156 std::string u; | |
157 | |
158 ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException); | |
159 } | |
160 | |
161 | |
162 TEST(Zlib, Empty) | |
163 { | |
164 std::string s = ""; | |
165 | |
166 std::string compressed, compressed2; | |
167 ZlibCompressor c; | |
168 IBufferCompressor::Compress(compressed, c, s); | |
169 ASSERT_EQ(compressed, compressed2); | |
170 | |
171 std::string uncompressed; | |
172 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 173 ASSERT_TRUE(uncompressed.empty()); |
1525 | 174 } |
175 | |
176 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
177 #if ORTHANC_SANDBOXED != 1 |
1525 | 178 static bool ReadAllStream(std::string& result, |
179 IHttpStreamAnswer& stream, | |
180 bool allowGzip = false, | |
181 bool allowDeflate = false) | |
182 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
183 stream.SetupHttpCompression(allowGzip, allowDeflate); |
1525 | 184 |
1545 | 185 result.resize(static_cast<size_t>(stream.GetContentLength())); |
1525 | 186 |
187 size_t pos = 0; | |
188 while (stream.ReadNextChunk()) | |
189 { | |
190 size_t s = stream.GetChunkSize(); | |
191 if (pos + s > result.size()) | |
192 { | |
193 return false; | |
194 } | |
195 | |
196 memcpy(&result[pos], stream.GetChunkContent(), s); | |
197 pos += s; | |
198 } | |
199 | |
200 return pos == result.size(); | |
201 } | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
202 #endif |
1525 | 203 |
204 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
205 #if ORTHANC_SANDBOXED != 1 |
1525 | 206 TEST(BufferHttpSender, Basic) |
207 { | |
208 const std::string s = "Hello world"; | |
209 std::string t; | |
210 | |
211 { | |
212 BufferHttpSender sender; | |
213 sender.SetChunkSize(1); | |
214 ASSERT_TRUE(ReadAllStream(t, sender)); | |
215 ASSERT_EQ(0u, t.size()); | |
216 } | |
217 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
218 for (int cs = 0; cs < 5; cs++) |
1525 | 219 { |
220 BufferHttpSender sender; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
221 sender.SetChunkSize(cs); |
1525 | 222 sender.GetBuffer() = s; |
223 ASSERT_TRUE(ReadAllStream(t, sender)); | |
224 ASSERT_EQ(s, t); | |
225 } | |
226 } | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
227 #endif |
1525 | 228 |
229 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
230 #if ORTHANC_SANDBOXED != 1 |
1525 | 231 TEST(FilesystemHttpSender, Basic) |
232 { | |
233 const std::string& path = "UnitTestsResults/stream"; | |
234 const std::string s = "Hello world"; | |
235 std::string t; | |
236 | |
237 { | |
2140 | 238 SystemToolbox::WriteFile(s, path); |
1525 | 239 FilesystemHttpSender sender(path); |
240 ASSERT_TRUE(ReadAllStream(t, sender)); | |
241 ASSERT_EQ(s, t); | |
242 } | |
243 | |
244 { | |
2140 | 245 SystemToolbox::WriteFile("", path); |
1525 | 246 FilesystemHttpSender sender(path); |
247 ASSERT_TRUE(ReadAllStream(t, sender)); | |
248 ASSERT_EQ(0u, t.size()); | |
249 } | |
250 } | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
251 #endif |
1525 | 252 |
253 | |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
254 #if ORTHANC_SANDBOXED != 1 |
1525 | 255 TEST(HttpStreamTranscoder, Basic) |
256 { | |
257 ZlibCompressor compressor; | |
258 | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
259 const std::string s = "Hello world " + Toolbox::GenerateUuid(); |
1525 | 260 |
261 std::string t; | |
262 IBufferCompressor::Compress(t, compressor, s); | |
263 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
264 for (int cs = 0; cs < 5; cs++) |
1525 | 265 { |
266 BufferHttpSender sender; | |
267 sender.SetChunkSize(cs); | |
268 sender.GetBuffer() = t; | |
269 std::string u; | |
270 ASSERT_TRUE(ReadAllStream(u, sender)); | |
271 | |
272 std::string v; | |
273 IBufferCompressor::Uncompress(v, compressor, u); | |
274 ASSERT_EQ(s, v); | |
275 } | |
276 | |
277 // 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
|
278 for (int cs = 0; cs < 5; cs++) |
1525 | 279 { |
280 BufferHttpSender sender; | |
281 sender.SetChunkSize(cs); | |
282 sender.GetBuffer() = t; | |
283 | |
284 HttpStreamTranscoder transcode(sender, CompressionType_None); | |
285 | |
286 std::string u; | |
287 ASSERT_TRUE(ReadAllStream(u, transcode)); | |
288 | |
289 ASSERT_EQ(t, u); | |
290 } | |
291 | |
292 // 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
|
293 for (int cs = 0; cs < 5; cs++) |
1525 | 294 { |
295 BufferHttpSender sender; | |
296 sender.SetChunkSize(cs); | |
297 sender.GetBuffer() = t; | |
298 | |
299 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
300 | |
301 std::string u; | |
302 ASSERT_TRUE(ReadAllStream(u, transcode, false, false)); | |
303 | |
304 ASSERT_EQ(s, u); | |
305 } | |
306 | |
307 // 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
|
308 for (int cs = 0; cs < 16; cs++) |
1525 | 309 { |
310 BufferHttpSender sender; | |
311 sender.SetChunkSize(cs); | |
312 sender.GetBuffer() = t; | |
313 | |
314 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
315 | |
316 std::string u; | |
317 ASSERT_TRUE(ReadAllStream(u, transcode, false, true)); | |
318 | |
319 ASSERT_EQ(t.size() - sizeof(uint64_t), u.size()); | |
320 ASSERT_EQ(t.substr(sizeof(uint64_t)), u); | |
321 } | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
322 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
323 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
|
324 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
325 BufferHttpSender sender; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
326 sender.SetChunkSize(cs); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
327 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
328 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
329 std::string u; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
330 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
|
331 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
332 ASSERT_EQ(0u, u.size()); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
333 } |
1525 | 334 } |
4325
b96aedfa8cc1
unit tests now running in WebAssembly
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4204
diff
changeset
|
335 #endif |