Mercurial > hg > orthanc
annotate UnitTestsSources/StreamTests.cpp @ 2016:0ae26237569a
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 13 Jun 2016 15:26:20 +0200 |
parents | 38fb3f524d81 |
children | aa4b8895cd23 |
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/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); | |
1972 | 77 ASSERT_TRUE(uncompressed.empty()); |
1525 | 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); | |
1972 | 110 ASSERT_TRUE(uncompressed.empty()); |
1525 | 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 | |
1972 | 157 ASSERT_FALSE(compressed.empty()); |
1525 | 158 compressed[compressed.size() - 1] = 'a'; |
159 std::string u; | |
160 | |
161 ASSERT_THROW(IBufferCompressor::Uncompress(u, c, compressed), OrthancException); | |
162 } | |
163 | |
164 | |
165 TEST(Zlib, Empty) | |
166 { | |
167 std::string s = ""; | |
168 | |
169 std::string compressed, compressed2; | |
170 ZlibCompressor c; | |
171 IBufferCompressor::Compress(compressed, c, s); | |
172 ASSERT_EQ(compressed, compressed2); | |
173 | |
174 std::string uncompressed; | |
175 IBufferCompressor::Uncompress(uncompressed, c, compressed); | |
1972 | 176 ASSERT_TRUE(uncompressed.empty()); |
1525 | 177 } |
178 | |
179 | |
180 static bool ReadAllStream(std::string& result, | |
181 IHttpStreamAnswer& stream, | |
182 bool allowGzip = false, | |
183 bool allowDeflate = false) | |
184 { | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
185 stream.SetupHttpCompression(allowGzip, allowDeflate); |
1525 | 186 |
1545 | 187 result.resize(static_cast<size_t>(stream.GetContentLength())); |
1525 | 188 |
189 size_t pos = 0; | |
190 while (stream.ReadNextChunk()) | |
191 { | |
192 size_t s = stream.GetChunkSize(); | |
193 if (pos + s > result.size()) | |
194 { | |
195 return false; | |
196 } | |
197 | |
198 memcpy(&result[pos], stream.GetChunkContent(), s); | |
199 pos += s; | |
200 } | |
201 | |
202 return pos == result.size(); | |
203 } | |
204 | |
205 | |
206 TEST(BufferHttpSender, Basic) | |
207 { | |
208 const std::string s = "Hello world"; | |
209 std::string t; | |
210 | |
211 { | |
212 BufferHttpSender sender; | |
213 sender.SetChunkSize(1); | |
214 ASSERT_TRUE(ReadAllStream(t, sender)); | |
215 ASSERT_EQ(0u, t.size()); | |
216 } | |
217 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
218 for (int cs = 0; cs < 5; cs++) |
1525 | 219 { |
220 BufferHttpSender sender; | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
221 sender.SetChunkSize(cs); |
1525 | 222 sender.GetBuffer() = s; |
223 ASSERT_TRUE(ReadAllStream(t, sender)); | |
224 ASSERT_EQ(s, t); | |
225 } | |
226 } | |
227 | |
228 | |
229 TEST(FilesystemHttpSender, Basic) | |
230 { | |
231 const std::string& path = "UnitTestsResults/stream"; | |
232 const std::string s = "Hello world"; | |
233 std::string t; | |
234 | |
235 { | |
236 Toolbox::WriteFile(s, path); | |
237 FilesystemHttpSender sender(path); | |
238 ASSERT_TRUE(ReadAllStream(t, sender)); | |
239 ASSERT_EQ(s, t); | |
240 } | |
241 | |
242 { | |
243 Toolbox::WriteFile("", path); | |
244 FilesystemHttpSender sender(path); | |
245 ASSERT_TRUE(ReadAllStream(t, sender)); | |
246 ASSERT_EQ(0u, t.size()); | |
247 } | |
248 } | |
249 | |
250 | |
251 TEST(HttpStreamTranscoder, Basic) | |
252 { | |
253 ZlibCompressor compressor; | |
254 | |
255 const std::string s = "Hello world " + Toolbox::GenerateUuid(); | |
256 | |
257 std::string t; | |
258 IBufferCompressor::Compress(t, compressor, s); | |
259 | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
260 for (int cs = 0; cs < 5; cs++) |
1525 | 261 { |
262 BufferHttpSender sender; | |
263 sender.SetChunkSize(cs); | |
264 sender.GetBuffer() = t; | |
265 std::string u; | |
266 ASSERT_TRUE(ReadAllStream(u, sender)); | |
267 | |
268 std::string v; | |
269 IBufferCompressor::Uncompress(v, compressor, u); | |
270 ASSERT_EQ(s, v); | |
271 } | |
272 | |
273 // 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
|
274 for (int cs = 0; cs < 5; cs++) |
1525 | 275 { |
276 BufferHttpSender sender; | |
277 sender.SetChunkSize(cs); | |
278 sender.GetBuffer() = t; | |
279 | |
280 HttpStreamTranscoder transcode(sender, CompressionType_None); | |
281 | |
282 std::string u; | |
283 ASSERT_TRUE(ReadAllStream(u, transcode)); | |
284 | |
285 ASSERT_EQ(t, u); | |
286 } | |
287 | |
288 // 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
|
289 for (int cs = 0; cs < 5; cs++) |
1525 | 290 { |
291 BufferHttpSender sender; | |
292 sender.SetChunkSize(cs); | |
293 sender.GetBuffer() = t; | |
294 | |
295 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
296 | |
297 std::string u; | |
298 ASSERT_TRUE(ReadAllStream(u, transcode, false, false)); | |
299 | |
300 ASSERT_EQ(s, u); | |
301 } | |
302 | |
303 // 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
|
304 for (int cs = 0; cs < 16; cs++) |
1525 | 305 { |
306 BufferHttpSender sender; | |
307 sender.SetChunkSize(cs); | |
308 sender.GetBuffer() = t; | |
309 | |
310 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); | |
311 | |
312 std::string u; | |
313 ASSERT_TRUE(ReadAllStream(u, transcode, false, true)); | |
314 | |
315 ASSERT_EQ(t.size() - sizeof(uint64_t), u.size()); | |
316 ASSERT_EQ(t.substr(sizeof(uint64_t)), u); | |
317 } | |
1526
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
318 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
319 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
|
320 { |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
321 BufferHttpSender sender; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
322 sender.SetChunkSize(cs); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
323 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
324 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
325 std::string u; |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
326 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
|
327 |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
328 ASSERT_EQ(0u, u.size()); |
096a8af528c9
fix streams, initialization/finalization of libcurl and openssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1525
diff
changeset
|
329 } |
1525 | 330 } |