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