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