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