Mercurial > hg > orthanc
annotate UnitTestsSources/StreamTests.cpp @ 2142:5a8840920121
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 16:26:40 +0100 |
parents | aa4b8895cd23 |
children | fd5875662670 |
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 |
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/HttpServer/BufferHttpSender.h" | |
39 #include "../Core/HttpServer/FilesystemHttpSender.h" | |
40 #include "../Core/HttpServer/HttpStreamTranscoder.h" | |
41 #include "../Core/Compression/ZlibCompressor.h" | |
42 #include "../Core/Compression/GzipCompressor.h" | |
43 | |
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 { | |
115 std::string s = Toolbox::GenerateUuid(); | |
116 s = s + s + s + s; | |
117 | |
118 std::string compressed, compressed2; | |
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 { | |
132 std::string s = Toolbox::GenerateUuid(); | |
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 { | |
149 std::string s = Toolbox::GenerateUuid(); | |
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 | |
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 { | |
2140 | 235 SystemToolbox::WriteFile(s, path); |
1525 | 236 FilesystemHttpSender sender(path); |
237 ASSERT_TRUE(ReadAllStream(t, sender)); | |
238 ASSERT_EQ(s, t); | |
239 } | |
240 | |
241 { | |
2140 | 242 SystemToolbox::WriteFile("", path); |
1525 | 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 } |