Mercurial > hg > orthanc
annotate UnitTestsSources/StreamTests.cpp @ 1804:bdef349f742c
integration worklists->default
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 20 Nov 2015 16:50:01 +0100 |
parents | 33d34bc4ac15 |
children | b1291df2f780 |
rev | line source |
---|---|
1525 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "PrecompiledHeadersUnitTests.h" | |
34 #include "gtest/gtest.h" | |
35 | |
36 #include "../Core/Toolbox.h" | |
37 #include "../Core/OrthancException.h" | |
38 #include "../Core/Uuid.h" | |
39 #include "../Core/HttpServer/BufferHttpSender.h" | |
40 #include "../Core/HttpServer/FilesystemHttpSender.h" | |
41 #include "../Core/HttpServer/HttpStreamTranscoder.h" | |
42 #include "../Core/Compression/ZlibCompressor.h" | |
43 #include "../Core/Compression/GzipCompressor.h" | |
44 | |
45 | |
46 using namespace Orthanc; | |
47 | |
48 | |
49 TEST(Gzip, Basic) | |
50 { | |
51 std::string s = "Hello world"; | |
52 | |
53 std::string compressed; | |
54 GzipCompressor c; | |
55 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
56 IBufferCompressor::Compress(compressed, c, s); | |
57 | |
58 std::string uncompressed; | |
59 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
60 ASSERT_EQ(s.size(), uncompressed.size()); | |
61 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
62 } | |
63 | |
64 | |
65 TEST(Gzip, Empty) | |
66 { | |
67 std::string s; | |
68 | |
69 std::string compressed; | |
70 GzipCompressor c; | |
71 ASSERT_FALSE(c.HasPrefixWithUncompressedSize()); | |
72 c.SetPrefixWithUncompressedSize(false); | |
73 IBufferCompressor::Compress(compressed, c, s); | |
74 | |
75 std::string uncompressed; | |
76 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
77 ASSERT_EQ(0, uncompressed.size()); | |
78 } | |
79 | |
80 | |
81 TEST(Gzip, BasicWithPrefix) | |
82 { | |
83 std::string s = "Hello world"; | |
84 | |
85 std::string compressed; | |
86 GzipCompressor c; | |
87 c.SetPrefixWithUncompressedSize(true); | |
88 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
89 IBufferCompressor::Compress(compressed, c, s); | |
90 | |
91 std::string uncompressed; | |
92 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
93 ASSERT_EQ(s.size(), uncompressed.size()); | |
94 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
95 } | |
96 | |
97 | |
98 TEST(Gzip, EmptyWithPrefix) | |
99 { | |
100 std::string s; | |
101 | |
102 std::string compressed; | |
103 GzipCompressor c; | |
104 c.SetPrefixWithUncompressedSize(true); | |
105 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
106 IBufferCompressor::Compress(compressed, c, s); | |
107 | |
108 std::string uncompressed; | |
109 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
110 ASSERT_EQ(0, uncompressed.size()); | |
111 } | |
112 | |
113 | |
114 TEST(Zlib, Basic) | |
115 { | |
116 std::string s = Toolbox::GenerateUuid(); | |
117 s = s + s + s + s; | |
118 | |
119 std::string compressed, compressed2; | |
120 ZlibCompressor c; | |
121 ASSERT_TRUE(c.HasPrefixWithUncompressedSize()); | |
122 IBufferCompressor::Compress(compressed, c, s); | |
123 | |
124 std::string uncompressed; | |
125 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
126 ASSERT_EQ(s.size(), uncompressed.size()); | |
127 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
128 } | |
129 | |
130 | |
131 TEST(Zlib, Level) | |
132 { | |
133 std::string s = Toolbox::GenerateUuid(); | |
134 s = s + s + s + s; | |
135 | |
136 std::string compressed, compressed2; | |
137 ZlibCompressor c; | |
138 c.SetCompressionLevel(9); | |
139 IBufferCompressor::Compress(compressed, c, s); | |
140 | |
141 c.SetCompressionLevel(0); | |
142 IBufferCompressor::Compress(compressed2, c, s); | |
143 | |
144 ASSERT_TRUE(compressed.size() < compressed2.size()); | |
145 } | |
146 | |
147 | |
148 TEST(Zlib, DISABLED_Corrupted) // Disabled because it may result in a crash | |
149 { | |
150 std::string s = Toolbox::GenerateUuid(); | |
151 s = s + s + s + s; | |
152 | |
153 std::string compressed; | |
154 ZlibCompressor c; | |
155 IBufferCompressor::Compress(compressed, c, s); | |
156 | |
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); | |
175 ASSERT_EQ(0u, uncompressed.size()); | |
176 } | |
177 | |
178 | |
179 static bool ReadAllStream(std::string& result, | |
180 IHttpStreamAnswer& stream, | |
181 bool allowGzip = false, | |
182 bool allowDeflate = false) | |
183 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
184 stream.SetupHttpCompression(allowGzip, allowDeflate); |
1525 | 185 |
1545 | 186 result.resize(static_cast<size_t>(stream.GetContentLength())); |
1525 | 187 |
188 size_t pos = 0; | |
189 while (stream.ReadNextChunk()) | |
190 { | |
191 size_t s = stream.GetChunkSize(); | |
192 if (pos + s > result.size()) | |
193 { | |
194 return false; | |
195 } | |
196 | |
197 memcpy(&result[pos], stream.GetChunkContent(), s); | |
198 pos += s; | |
199 } | |
200 | |
201 return pos == result.size(); | |
202 } | |
203 | |
204 | |
205 TEST(BufferHttpSender, Basic) | |
206 { | |
207 const std::string s = "Hello world"; | |
208 std::string t; | |
209 | |
210 { | |
211 BufferHttpSender sender; | |
212 sender.SetChunkSize(1); | |
213 ASSERT_TRUE(ReadAllStream(t, sender)); | |
214 ASSERT_EQ(0u, t.size()); | |
215 } | |
216 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
217 for (int cs = 0; cs < 5; cs++) |
1525 | 218 { |
219 BufferHttpSender sender; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
220 sender.SetChunkSize(cs); |
1525 | 221 sender.GetBuffer() = s; |
222 ASSERT_TRUE(ReadAllStream(t, sender)); | |
223 ASSERT_EQ(s, t); | |
224 } | |
225 } | |
226 | |
227 | |
228 TEST(FilesystemHttpSender, Basic) | |
229 { | |
230 const std::string& path = "UnitTestsResults/stream"; | |
231 const std::string s = "Hello world"; | |
232 std::string t; | |
233 | |
234 { | |
235 Toolbox::WriteFile(s, path); | |
236 FilesystemHttpSender sender(path); | |
237 ASSERT_TRUE(ReadAllStream(t, sender)); | |
238 ASSERT_EQ(s, t); | |
239 } | |
240 | |
241 { | |
242 Toolbox::WriteFile("", path); | |
243 FilesystemHttpSender sender(path); | |
244 ASSERT_TRUE(ReadAllStream(t, sender)); | |
245 ASSERT_EQ(0u, t.size()); | |
246 } | |
247 } | |
248 | |
249 | |
250 TEST(HttpStreamTranscoder, Basic) | |
251 { | |
252 ZlibCompressor compressor; | |
253 | |
254 const std::string s = "Hello world " + Toolbox::GenerateUuid(); | |
255 | |
256 std::string t; | |
257 IBufferCompressor::Compress(t, compressor, s); | |
258 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
259 for (int cs = 0; cs < 5; cs++) |
1525 | 260 { |
261 BufferHttpSender sender; | |
262 sender.SetChunkSize(cs); | |
263 sender.GetBuffer() = t; | |
264 std::string u; | |
265 ASSERT_TRUE(ReadAllStream(u, sender)); | |
266 | |
267 std::string v; | |
268 IBufferCompressor::Uncompress(v, compressor, u); | |
269 ASSERT_EQ(s, v); | |
270 } | |
271 | |
272 // 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
|
273 for (int cs = 0; cs < 5; cs++) |
1525 | 274 { |
275 BufferHttpSender sender; | |
276 sender.SetChunkSize(cs); | |
277 sender.GetBuffer() = t; | |
278 | |
279 HttpStreamTranscoder transcode(sender, CompressionType_None); | |
280 | |
281 std::string u; | |
282 ASSERT_TRUE(ReadAllStream(u, transcode)); | |
283 | |
284 ASSERT_EQ(t, u); | |
285 } | |
286 | |
287 // 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
|
288 for (int cs = 0; cs < 5; cs++) |
1525 | 289 { |
290 BufferHttpSender sender; | |
291 sender.SetChunkSize(cs); | |
292 sender.GetBuffer() = t; | |
293 | |
294 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
295 | |
296 std::string u; | |
297 ASSERT_TRUE(ReadAllStream(u, transcode, false, false)); | |
298 | |
299 ASSERT_EQ(s, u); | |
300 } | |
301 | |
302 // 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
|
303 for (int cs = 0; cs < 16; cs++) |
1525 | 304 { |
305 BufferHttpSender sender; | |
306 sender.SetChunkSize(cs); | |
307 sender.GetBuffer() = t; | |
308 | |
309 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
310 | |
311 std::string u; | |
312 ASSERT_TRUE(ReadAllStream(u, transcode, false, true)); | |
313 | |
314 ASSERT_EQ(t.size() - sizeof(uint64_t), u.size()); | |
315 ASSERT_EQ(t.substr(sizeof(uint64_t)), u); | |
316 } | |
1526
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 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
|
319 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
320 BufferHttpSender sender; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
321 sender.SetChunkSize(cs); |
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 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
324 std::string u; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
325 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
|
326 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
327 ASSERT_EQ(0u, u.size()); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
328 } |
1525 | 329 } |