Mercurial > hg > orthanc
annotate OrthancFramework/UnitTestsSources/StreamTests.cpp @ 4062:0953b3dc3261 framework
pretty-printing
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 11 Jun 2020 14:38:31 +0200 |
parents | 05b8fd21089c |
children | e00f3d089991 |
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 |
4062 | 38 #include <gtest/gtest.h> |
1525 | 39 |
4045 | 40 #include "../Sources/SystemToolbox.h" |
41 #include "../Sources/Toolbox.h" | |
42 #include "../Sources/OrthancException.h" | |
43 #include "../Sources/HttpServer/BufferHttpSender.h" | |
44 #include "../Sources/HttpServer/FilesystemHttpSender.h" | |
45 #include "../Sources/HttpServer/HttpStreamTranscoder.h" | |
46 #include "../Sources/Compression/ZlibCompressor.h" | |
47 #include "../Sources/Compression/GzipCompressor.h" | |
1525 | 48 |
49 | |
50 using namespace Orthanc; | |
51 | |
52 | |
53 TEST(Gzip, Basic) | |
54 { | |
55 std::string s = "Hello world"; | |
56 | |
57 std::string compressed; | |
58 GzipCompressor c; | |
59 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
60 IBufferCompressor::Compress(compressed, c, s); | |
61 | |
62 std::string uncompressed; | |
63 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
64 ASSERT_EQ(s.size(), uncompressed.size()); | |
65 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
66 } | |
67 | |
68 | |
69 TEST(Gzip, Empty) | |
70 { | |
71 std::string s; | |
72 | |
73 std::string compressed; | |
74 GzipCompressor c; | |
75 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
76 c.SetPrefixWithUncompressedSize(false); | |
77 IBufferCompressor::Compress(compressed, c, s); | |
78 | |
79 std::string uncompressed; | |
80 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 81 ASSERT_TRUE(uncompressed.empty()); |
1525 | 82 } |
83 | |
84 | |
85 TEST(Gzip, BasicWithPrefix) | |
86 { | |
87 std::string s = "Hello world"; | |
88 | |
89 std::string compressed; | |
90 GzipCompressor c; | |
91 c.SetPrefixWithUncompressedSize(true); | |
92 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
93 IBufferCompressor::Compress(compressed, c, s); | |
94 | |
95 std::string uncompressed; | |
96 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
97 ASSERT_EQ(s.size(), uncompressed.size()); | |
98 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
99 } | |
100 | |
101 | |
102 TEST(Gzip, EmptyWithPrefix) | |
103 { | |
104 std::string s; | |
105 | |
106 std::string compressed; | |
107 GzipCompressor c; | |
108 c.SetPrefixWithUncompressedSize(true); | |
109 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
110 IBufferCompressor::Compress(compressed, c, s); | |
111 | |
112 std::string uncompressed; | |
113 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 114 ASSERT_TRUE(uncompressed.empty()); |
1525 | 115 } |
116 | |
117 | |
118 TEST(Zlib, Basic) | |
119 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
120 std::string s = Toolbox::GenerateUuid(); |
1525 | 121 s = s + s + s + s; |
122 | |
123 std::string compressed, compressed2; | |
124 ZlibCompressor c; | |
125 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
126 IBufferCompressor::Compress(compressed, c, s); | |
127 | |
128 std::string uncompressed; | |
129 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
130 ASSERT_EQ(s.size(), uncompressed.size()); | |
131 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
132 } | |
133 | |
134 | |
135 TEST(Zlib, Level) | |
136 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
137 std::string s = Toolbox::GenerateUuid(); |
1525 | 138 s = s + s + s + s; |
139 | |
140 std::string compressed, compressed2; | |
141 ZlibCompressor c; | |
142 c.SetCompressionLevel(9); | |
143 IBufferCompressor::Compress(compressed, c, s); | |
144 | |
145 c.SetCompressionLevel(0); | |
146 IBufferCompressor::Compress(compressed2, c, s); | |
147 | |
148 ASSERT_TRUE(compressed.size() < compressed2.size()); | |
149 } | |
150 | |
151 | |
152 TEST(Zlib, DISABLED_Corrupted) // Disabled because it may result in a crash | |
153 { | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
154 std::string s = Toolbox::GenerateUuid(); |
1525 | 155 s = s + s + s + s; |
156 | |
157 std::string compressed; | |
158 ZlibCompressor c; | |
159 IBufferCompressor::Compress(compressed, c, s); | |
160 | |
1972 | 161 ASSERT_FALSE(compressed.empty()); |
1525 | 162 compressed[compressed.size() - 1] = 'a'; |
163 std::string u; | |
164 | |
165 ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException); | |
166 } | |
167 | |
168 | |
169 TEST(Zlib, Empty) | |
170 { | |
171 std::string s = ""; | |
172 | |
173 std::string compressed, compressed2; | |
174 ZlibCompressor c; | |
175 IBufferCompressor::Compress(compressed, c, s); | |
176 ASSERT_EQ(compressed, compressed2); | |
177 | |
178 std::string uncompressed; | |
179 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 180 ASSERT_TRUE(uncompressed.empty()); |
1525 | 181 } |
182 | |
183 | |
184 static bool ReadAllStream(std::string& result, | |
185 IHttpStreamAnswer& stream, | |
186 bool allowGzip = false, | |
187 bool allowDeflate = false) | |
188 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
189 stream.SetupHttpCompression(allowGzip, allowDeflate); |
1525 | 190 |
1545 | 191 result.resize(static_cast<size_t>(stream.GetContentLength())); |
1525 | 192 |
193 size_t pos = 0; | |
194 while (stream.ReadNextChunk()) | |
195 { | |
196 size_t s = stream.GetChunkSize(); | |
197 if (pos + s > result.size()) | |
198 { | |
199 return false; | |
200 } | |
201 | |
202 memcpy(&result[pos], stream.GetChunkContent(), s); | |
203 pos += s; | |
204 } | |
205 | |
206 return pos == result.size(); | |
207 } | |
208 | |
209 | |
210 TEST(BufferHttpSender, Basic) | |
211 { | |
212 const std::string s = "Hello world"; | |
213 std::string t; | |
214 | |
215 { | |
216 BufferHttpSender sender; | |
217 sender.SetChunkSize(1); | |
218 ASSERT_TRUE(ReadAllStream(t, sender)); | |
219 ASSERT_EQ(0u, t.size()); | |
220 } | |
221 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
222 for (int cs = 0; cs < 5; cs++) |
1525 | 223 { |
224 BufferHttpSender sender; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
225 sender.SetChunkSize(cs); |
1525 | 226 sender.GetBuffer() = s; |
227 ASSERT_TRUE(ReadAllStream(t, sender)); | |
228 ASSERT_EQ(s, t); | |
229 } | |
230 } | |
231 | |
232 | |
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 } | |
253 | |
254 | |
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 } |