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