comparison UnitTestsSources/StreamTests.cpp @ 1525:f9b0169eb6bb

testing
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2015 17:50:38 +0200
parents
children 096a8af528c9
comparison
equal deleted inserted replaced
1524:4a0c2eedceb6 1525:f9b0169eb6bb
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 {
184 result.resize(stream.GetContentLength());
185
186 stream.SetupHttpCompression(allowGzip, allowDeflate);
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(0);
213 sender.GetBuffer() = s;
214 ASSERT_TRUE(ReadAllStream(t, sender));
215 ASSERT_EQ(s, t);
216 }
217
218 {
219 BufferHttpSender sender;
220 sender.SetChunkSize(1);
221 sender.GetBuffer() = s;
222 ASSERT_TRUE(ReadAllStream(t, sender));
223 ASSERT_EQ(s, t);
224 }
225
226 {
227 BufferHttpSender sender;
228 sender.SetChunkSize(1);
229 ASSERT_TRUE(ReadAllStream(t, sender));
230 ASSERT_EQ(0u, t.size());
231 }
232
233 {
234 BufferHttpSender sender;
235 sender.SetChunkSize(3);
236 sender.GetBuffer() = s;
237 ASSERT_TRUE(ReadAllStream(t, sender));
238 ASSERT_EQ(s, t);
239 }
240
241 {
242 BufferHttpSender sender;
243 sender.SetChunkSize(300);
244 sender.GetBuffer() = s;
245 ASSERT_TRUE(ReadAllStream(t, sender));
246 ASSERT_EQ(s, t);
247 }
248 }
249
250
251 TEST(FilesystemHttpSender, Basic)
252 {
253 const std::string& path = "UnitTestsResults/stream";
254 const std::string s = "Hello world";
255 std::string t;
256
257 {
258 Toolbox::WriteFile(s, path);
259 FilesystemHttpSender sender(path);
260 ASSERT_TRUE(ReadAllStream(t, sender));
261 ASSERT_EQ(s, t);
262 }
263
264 {
265 Toolbox::WriteFile("", path);
266 FilesystemHttpSender sender(path);
267 ASSERT_TRUE(ReadAllStream(t, sender));
268 ASSERT_EQ(0u, t.size());
269 }
270 }
271
272
273 TEST(HttpStreamTranscoder, Basic)
274 {
275 ZlibCompressor compressor;
276
277 const std::string s = "Hello world " + Toolbox::GenerateUuid();
278
279 std::string t;
280 IBufferCompressor::Compress(t, compressor, s);
281
282 for (int cs = 0; cs < 3; cs++)
283 {
284 BufferHttpSender sender;
285 sender.SetChunkSize(cs);
286 sender.GetBuffer() = t;
287 std::string u;
288 ASSERT_TRUE(ReadAllStream(u, sender));
289
290 std::string v;
291 IBufferCompressor::Uncompress(v, compressor, u);
292 ASSERT_EQ(s, v);
293 }
294
295 // Pass-through test, no decompression occurs
296 for (int cs = 0; cs < 3; cs++)
297 {
298 BufferHttpSender sender;
299 sender.SetChunkSize(cs);
300 sender.GetBuffer() = t;
301
302 HttpStreamTranscoder transcode(sender, CompressionType_None);
303
304 std::string u;
305 ASSERT_TRUE(ReadAllStream(u, transcode));
306
307 ASSERT_EQ(t, u);
308 }
309
310 // Pass-through test, decompression occurs
311 for (int cs = 0; cs < 3; cs++)
312 {
313 BufferHttpSender sender;
314 sender.SetChunkSize(cs);
315 sender.GetBuffer() = t;
316
317 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize);
318
319 std::string u;
320 ASSERT_TRUE(ReadAllStream(u, transcode, false, false));
321
322 ASSERT_EQ(s, u);
323 }
324
325 // Pass-through test with zlib, no decompression occurs but deflate is sent
326 for (int cs = 0; cs < 3; cs++)
327 {
328 BufferHttpSender sender;
329 sender.SetChunkSize(cs);
330 sender.GetBuffer() = t;
331
332 HttpStreamTranscoder transcode(sender, CompressionType_ZlibWithSize);
333
334 std::string u;
335 ASSERT_TRUE(ReadAllStream(u, transcode, false, true));
336
337 ASSERT_EQ(t.size() - sizeof(uint64_t), u.size());
338 ASSERT_EQ(t.substr(sizeof(uint64_t)), u);
339 }
340 }