comparison OrthancFramework/UnitTestsSources/FrameworkTests.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents UnitTestsSources/FrameworkTests.cpp@27628b0f6ada
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
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 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1
35 # include <OrthancFramework.h>
36 #endif
37
38 #include "PrecompiledHeadersUnitTests.h"
39 #include "../Core/EnumerationDictionary.h"
40
41 #include "gtest/gtest.h"
42
43 #include <ctype.h>
44
45 #include "../Core/DicomFormat/DicomTag.h"
46 #include "../Core/FileBuffer.h"
47 #include "../Core/HttpServer/HttpToolbox.h"
48 #include "../Core/Logging.h"
49 #include "../Core/MetricsRegistry.h"
50 #include "../Core/OrthancException.h"
51 #include "../Core/SystemToolbox.h"
52 #include "../Core/TemporaryFile.h"
53 #include "../Core/Toolbox.h"
54
55
56 using namespace Orthanc;
57
58
59 TEST(Uuid, Generation)
60 {
61 for (int i = 0; i < 10; i++)
62 {
63 std::string s = Toolbox::GenerateUuid();
64 ASSERT_TRUE(Toolbox::IsUuid(s));
65 }
66 }
67
68 TEST(Uuid, Test)
69 {
70 ASSERT_FALSE(Toolbox::IsUuid(""));
71 ASSERT_FALSE(Toolbox::IsUuid("012345678901234567890123456789012345"));
72 ASSERT_TRUE(Toolbox::IsUuid("550e8400-e29b-41d4-a716-446655440000"));
73 ASSERT_FALSE(Toolbox::IsUuid("550e8400-e29b-41d4-a716-44665544000_"));
74 ASSERT_FALSE(Toolbox::IsUuid("01234567890123456789012345678901234_"));
75 ASSERT_FALSE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-44665544000"));
76 ASSERT_TRUE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000"));
77 ASSERT_TRUE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000 ok"));
78 ASSERT_FALSE(Toolbox::StartsWithUuid("550e8400-e29b-41d4-a716-446655440000ok"));
79 }
80
81 TEST(Toolbox, IsSHA1)
82 {
83 ASSERT_FALSE(Toolbox::IsSHA1(""));
84 ASSERT_FALSE(Toolbox::IsSHA1("01234567890123456789012345678901234567890123"));
85 ASSERT_FALSE(Toolbox::IsSHA1("012345678901234567890123456789012345678901234"));
86 ASSERT_TRUE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b9"));
87
88 std::string sha = " b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b9 ";
89 ASSERT_TRUE(Toolbox::IsSHA1(sha));
90 sha[3] = '\0';
91 sha[53] = '\0';
92 ASSERT_TRUE(Toolbox::IsSHA1(sha));
93 sha[40] = '\0';
94 ASSERT_FALSE(Toolbox::IsSHA1(sha));
95 ASSERT_FALSE(Toolbox::IsSHA1(" "));
96
97 ASSERT_TRUE(Toolbox::IsSHA1("16738bc3-e47ed42a-43ce044c-a3414a45-cb069bd0"));
98
99 std::string s;
100 Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog");
101 ASSERT_TRUE(Toolbox::IsSHA1(s));
102 ASSERT_EQ("2fd4e1c6-7a2d28fc-ed849ee1-bb76e739-1b93eb12", s);
103
104 ASSERT_FALSE(Toolbox::IsSHA1("b5ed549f-956400ce-69a8c063-bf5b78be-2732a4b_"));
105 }
106
107
108 TEST(ParseGetArguments, Basic)
109 {
110 IHttpHandler::GetArguments b;
111 HttpToolbox::ParseGetArguments(b, "aaa=baaa&bb=a&aa=c");
112
113 IHttpHandler::Arguments a;
114 HttpToolbox::CompileGetArguments(a, b);
115
116 ASSERT_EQ(3u, a.size());
117 ASSERT_EQ(a["aaa"], "baaa");
118 ASSERT_EQ(a["bb"], "a");
119 ASSERT_EQ(a["aa"], "c");
120 }
121
122 TEST(ParseGetArguments, BasicEmpty)
123 {
124 IHttpHandler::GetArguments b;
125 HttpToolbox::ParseGetArguments(b, "aaa&bb=aa&aa");
126
127 IHttpHandler::Arguments a;
128 HttpToolbox::CompileGetArguments(a, b);
129
130 ASSERT_EQ(3u, a.size());
131 ASSERT_EQ(a["aaa"], "");
132 ASSERT_EQ(a["bb"], "aa");
133 ASSERT_EQ(a["aa"], "");
134 }
135
136 TEST(ParseGetArguments, Single)
137 {
138 IHttpHandler::GetArguments b;
139 HttpToolbox::ParseGetArguments(b, "aaa=baaa");
140
141 IHttpHandler::Arguments a;
142 HttpToolbox::CompileGetArguments(a, b);
143
144 ASSERT_EQ(1u, a.size());
145 ASSERT_EQ(a["aaa"], "baaa");
146 }
147
148 TEST(ParseGetArguments, SingleEmpty)
149 {
150 IHttpHandler::GetArguments b;
151 HttpToolbox::ParseGetArguments(b, "aaa");
152
153 IHttpHandler::Arguments a;
154 HttpToolbox::CompileGetArguments(a, b);
155
156 ASSERT_EQ(1u, a.size());
157 ASSERT_EQ(a["aaa"], "");
158 }
159
160 TEST(ParseGetQuery, Test1)
161 {
162 UriComponents uri;
163 IHttpHandler::GetArguments b;
164 HttpToolbox::ParseGetQuery(uri, b, "/instances/test/world?aaa=baaa&bb=a&aa=c");
165
166 IHttpHandler::Arguments a;
167 HttpToolbox::CompileGetArguments(a, b);
168
169 ASSERT_EQ(3u, uri.size());
170 ASSERT_EQ("instances", uri[0]);
171 ASSERT_EQ("test", uri[1]);
172 ASSERT_EQ("world", uri[2]);
173 ASSERT_EQ(3u, a.size());
174 ASSERT_EQ(a["aaa"], "baaa");
175 ASSERT_EQ(a["bb"], "a");
176 ASSERT_EQ(a["aa"], "c");
177 }
178
179 TEST(ParseGetQuery, Test2)
180 {
181 UriComponents uri;
182 IHttpHandler::GetArguments b;
183 HttpToolbox::ParseGetQuery(uri, b, "/instances/test/world");
184
185 IHttpHandler::Arguments a;
186 HttpToolbox::CompileGetArguments(a, b);
187
188 ASSERT_EQ(3u, uri.size());
189 ASSERT_EQ("instances", uri[0]);
190 ASSERT_EQ("test", uri[1]);
191 ASSERT_EQ("world", uri[2]);
192 ASSERT_EQ(0u, a.size());
193 }
194
195 TEST(Uri, SplitUriComponents)
196 {
197 UriComponents c, d;
198 Toolbox::SplitUriComponents(c, "/cou/hello/world");
199 ASSERT_EQ(3u, c.size());
200 ASSERT_EQ("cou", c[0]);
201 ASSERT_EQ("hello", c[1]);
202 ASSERT_EQ("world", c[2]);
203
204 Toolbox::SplitUriComponents(c, "/cou/hello/world/");
205 ASSERT_EQ(3u, c.size());
206 ASSERT_EQ("cou", c[0]);
207 ASSERT_EQ("hello", c[1]);
208 ASSERT_EQ("world", c[2]);
209
210 Toolbox::SplitUriComponents(c, "/cou/hello/world/a");
211 ASSERT_EQ(4u, c.size());
212 ASSERT_EQ("cou", c[0]);
213 ASSERT_EQ("hello", c[1]);
214 ASSERT_EQ("world", c[2]);
215 ASSERT_EQ("a", c[3]);
216
217 Toolbox::SplitUriComponents(c, "/");
218 ASSERT_EQ(0u, c.size());
219
220 Toolbox::SplitUriComponents(c, "/hello");
221 ASSERT_EQ(1u, c.size());
222 ASSERT_EQ("hello", c[0]);
223
224 Toolbox::SplitUriComponents(c, "/hello/");
225 ASSERT_EQ(1u, c.size());
226 ASSERT_EQ("hello", c[0]);
227
228 ASSERT_THROW(Toolbox::SplitUriComponents(c, ""), OrthancException);
229 ASSERT_THROW(Toolbox::SplitUriComponents(c, "a"), OrthancException);
230 ASSERT_THROW(Toolbox::SplitUriComponents(c, "/coucou//coucou"), OrthancException);
231
232 c.clear();
233 c.push_back("test");
234 ASSERT_EQ("/", Toolbox::FlattenUri(c, 10));
235 }
236
237
238 TEST(Uri, Truncate)
239 {
240 UriComponents c, d;
241 Toolbox::SplitUriComponents(c, "/cou/hello/world");
242
243 Toolbox::TruncateUri(d, c, 0);
244 ASSERT_EQ(3u, d.size());
245 ASSERT_EQ("cou", d[0]);
246 ASSERT_EQ("hello", d[1]);
247 ASSERT_EQ("world", d[2]);
248
249 Toolbox::TruncateUri(d, c, 1);
250 ASSERT_EQ(2u, d.size());
251 ASSERT_EQ("hello", d[0]);
252 ASSERT_EQ("world", d[1]);
253
254 Toolbox::TruncateUri(d, c, 2);
255 ASSERT_EQ(1u, d.size());
256 ASSERT_EQ("world", d[0]);
257
258 Toolbox::TruncateUri(d, c, 3);
259 ASSERT_EQ(0u, d.size());
260
261 Toolbox::TruncateUri(d, c, 4);
262 ASSERT_EQ(0u, d.size());
263
264 Toolbox::TruncateUri(d, c, 5);
265 ASSERT_EQ(0u, d.size());
266 }
267
268
269 TEST(Uri, Child)
270 {
271 UriComponents c1; Toolbox::SplitUriComponents(c1, "/hello/world");
272 UriComponents c2; Toolbox::SplitUriComponents(c2, "/hello/hello");
273 UriComponents c3; Toolbox::SplitUriComponents(c3, "/hello");
274 UriComponents c4; Toolbox::SplitUriComponents(c4, "/world");
275 UriComponents c5; Toolbox::SplitUriComponents(c5, "/");
276
277 ASSERT_TRUE(Toolbox::IsChildUri(c1, c1));
278 ASSERT_FALSE(Toolbox::IsChildUri(c1, c2));
279 ASSERT_FALSE(Toolbox::IsChildUri(c1, c3));
280 ASSERT_FALSE(Toolbox::IsChildUri(c1, c4));
281 ASSERT_FALSE(Toolbox::IsChildUri(c1, c5));
282
283 ASSERT_FALSE(Toolbox::IsChildUri(c2, c1));
284 ASSERT_TRUE(Toolbox::IsChildUri(c2, c2));
285 ASSERT_FALSE(Toolbox::IsChildUri(c2, c3));
286 ASSERT_FALSE(Toolbox::IsChildUri(c2, c4));
287 ASSERT_FALSE(Toolbox::IsChildUri(c2, c5));
288
289 ASSERT_TRUE(Toolbox::IsChildUri(c3, c1));
290 ASSERT_TRUE(Toolbox::IsChildUri(c3, c2));
291 ASSERT_TRUE(Toolbox::IsChildUri(c3, c3));
292 ASSERT_FALSE(Toolbox::IsChildUri(c3, c4));
293 ASSERT_FALSE(Toolbox::IsChildUri(c3, c5));
294
295 ASSERT_FALSE(Toolbox::IsChildUri(c4, c1));
296 ASSERT_FALSE(Toolbox::IsChildUri(c4, c2));
297 ASSERT_FALSE(Toolbox::IsChildUri(c4, c3));
298 ASSERT_TRUE(Toolbox::IsChildUri(c4, c4));
299 ASSERT_FALSE(Toolbox::IsChildUri(c4, c5));
300
301 ASSERT_TRUE(Toolbox::IsChildUri(c5, c1));
302 ASSERT_TRUE(Toolbox::IsChildUri(c5, c2));
303 ASSERT_TRUE(Toolbox::IsChildUri(c5, c3));
304 ASSERT_TRUE(Toolbox::IsChildUri(c5, c4));
305 ASSERT_TRUE(Toolbox::IsChildUri(c5, c5));
306 }
307
308 TEST(Uri, AutodetectMimeType)
309 {
310 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("../NOTES"));
311 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType(""));
312 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("/"));
313 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("a/a"));
314 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("..\\a\\"));
315 ASSERT_EQ(MimeType_Binary, SystemToolbox::AutodetectMimeType("..\\a\\a"));
316
317 ASSERT_EQ(MimeType_PlainText, SystemToolbox::AutodetectMimeType("../NOTES.txt"));
318 ASSERT_EQ(MimeType_PlainText, SystemToolbox::AutodetectMimeType("../coucou.xml/NOTES.txt"));
319 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("..\\coucou.\\NOTES.xml"));
320 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("../.xml"));
321 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("../.XmL"));
322
323 ASSERT_EQ(MimeType_JavaScript, SystemToolbox::AutodetectMimeType("NOTES.js"));
324 ASSERT_EQ(MimeType_Json, SystemToolbox::AutodetectMimeType("NOTES.json"));
325 ASSERT_EQ(MimeType_Pdf, SystemToolbox::AutodetectMimeType("NOTES.pdf"));
326 ASSERT_EQ(MimeType_Css, SystemToolbox::AutodetectMimeType("NOTES.css"));
327 ASSERT_EQ(MimeType_Html, SystemToolbox::AutodetectMimeType("NOTES.html"));
328 ASSERT_EQ(MimeType_PlainText, SystemToolbox::AutodetectMimeType("NOTES.txt"));
329 ASSERT_EQ(MimeType_Xml, SystemToolbox::AutodetectMimeType("NOTES.xml"));
330 ASSERT_EQ(MimeType_Gif, SystemToolbox::AutodetectMimeType("NOTES.gif"));
331 ASSERT_EQ(MimeType_Jpeg, SystemToolbox::AutodetectMimeType("NOTES.jpg"));
332 ASSERT_EQ(MimeType_Jpeg, SystemToolbox::AutodetectMimeType("NOTES.jpeg"));
333 ASSERT_EQ(MimeType_Png, SystemToolbox::AutodetectMimeType("NOTES.png"));
334 ASSERT_EQ(MimeType_NaCl, SystemToolbox::AutodetectMimeType("NOTES.nexe"));
335 ASSERT_EQ(MimeType_Json, SystemToolbox::AutodetectMimeType("NOTES.nmf"));
336 ASSERT_EQ(MimeType_PNaCl, SystemToolbox::AutodetectMimeType("NOTES.pexe"));
337 ASSERT_EQ(MimeType_Svg, SystemToolbox::AutodetectMimeType("NOTES.svg"));
338 ASSERT_EQ(MimeType_Woff, SystemToolbox::AutodetectMimeType("NOTES.woff"));
339 ASSERT_EQ(MimeType_Woff2, SystemToolbox::AutodetectMimeType("NOTES.woff2"));
340
341 // Test primitives from the "RegisterDefaultExtensions()" that was
342 // present in the sample "Serve Folders plugin" of Orthanc 1.4.2
343 ASSERT_STREQ("application/javascript", EnumerationToString(SystemToolbox::AutodetectMimeType(".js")));
344 ASSERT_STREQ("application/json", EnumerationToString(SystemToolbox::AutodetectMimeType(".json")));
345 ASSERT_STREQ("application/json", EnumerationToString(SystemToolbox::AutodetectMimeType(".nmf")));
346 ASSERT_STREQ("application/octet-stream", EnumerationToString(SystemToolbox::AutodetectMimeType("")));
347 ASSERT_STREQ("application/wasm", EnumerationToString(SystemToolbox::AutodetectMimeType(".wasm")));
348 ASSERT_STREQ("application/x-font-woff", EnumerationToString(SystemToolbox::AutodetectMimeType(".woff")));
349 ASSERT_STREQ("application/x-nacl", EnumerationToString(SystemToolbox::AutodetectMimeType(".nexe")));
350 ASSERT_STREQ("application/x-pnacl", EnumerationToString(SystemToolbox::AutodetectMimeType(".pexe")));
351 ASSERT_STREQ("application/xml", EnumerationToString(SystemToolbox::AutodetectMimeType(".xml")));
352 ASSERT_STREQ("font/woff2", EnumerationToString(SystemToolbox::AutodetectMimeType(".woff2")));
353 ASSERT_STREQ("image/gif", EnumerationToString(SystemToolbox::AutodetectMimeType(".gif")));
354 ASSERT_STREQ("image/jpeg", EnumerationToString(SystemToolbox::AutodetectMimeType(".jpeg")));
355 ASSERT_STREQ("image/jpeg", EnumerationToString(SystemToolbox::AutodetectMimeType(".jpg")));
356 ASSERT_STREQ("image/png", EnumerationToString(SystemToolbox::AutodetectMimeType(".png")));
357 ASSERT_STREQ("image/svg+xml", EnumerationToString(SystemToolbox::AutodetectMimeType(".svg")));
358 ASSERT_STREQ("text/css", EnumerationToString(SystemToolbox::AutodetectMimeType(".css")));
359 ASSERT_STREQ("text/html", EnumerationToString(SystemToolbox::AutodetectMimeType(".html")));
360 }
361
362 TEST(Toolbox, ComputeMD5)
363 {
364 std::string s;
365
366 // # echo -n "Hello" | md5sum
367
368 Toolbox::ComputeMD5(s, "Hello");
369 ASSERT_EQ("8b1a9953c4611296a827abf8c47804d7", s);
370 Toolbox::ComputeMD5(s, "");
371 ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s);
372 }
373
374 TEST(Toolbox, ComputeSHA1)
375 {
376 std::string s;
377
378 Toolbox::ComputeSHA1(s, "The quick brown fox jumps over the lazy dog");
379 ASSERT_EQ("2fd4e1c6-7a2d28fc-ed849ee1-bb76e739-1b93eb12", s);
380 Toolbox::ComputeSHA1(s, "");
381 ASSERT_EQ("da39a3ee-5e6b4b0d-3255bfef-95601890-afd80709", s);
382 }
383
384 TEST(Toolbox, PathToExecutable)
385 {
386 printf("[%s]\n", SystemToolbox::GetPathToExecutable().c_str());
387 printf("[%s]\n", SystemToolbox::GetDirectoryOfExecutable().c_str());
388 }
389
390 TEST(Toolbox, StripSpaces)
391 {
392 ASSERT_EQ("", Toolbox::StripSpaces(" \t \r \n "));
393 ASSERT_EQ("coucou", Toolbox::StripSpaces(" coucou \t \r \n "));
394 ASSERT_EQ("cou cou", Toolbox::StripSpaces(" cou cou \n "));
395 ASSERT_EQ("c", Toolbox::StripSpaces(" \n\t c\r \n "));
396 }
397
398 TEST(Toolbox, Case)
399 {
400 std::string s = "CoU";
401 std::string ss;
402
403 Toolbox::ToUpperCase(ss, s);
404 ASSERT_EQ("COU", ss);
405 Toolbox::ToLowerCase(ss, s);
406 ASSERT_EQ("cou", ss);
407
408 s = "CoU";
409 Toolbox::ToUpperCase(s);
410 ASSERT_EQ("COU", s);
411
412 s = "CoU";
413 Toolbox::ToLowerCase(s);
414 ASSERT_EQ("cou", s);
415 }
416
417
418 TEST(Logger, Basic)
419 {
420 LOG(INFO) << "I say hello";
421 }
422
423 TEST(Toolbox, ConvertFromLatin1)
424 {
425 // This is a Latin-1 test string
426 const unsigned char data[10] = { 0xe0, 0xe9, 0xea, 0xe7, 0x26, 0xc6, 0x61, 0x62, 0x63, 0x00 };
427
428 std::string s((char*) &data[0], 10);
429 ASSERT_EQ("&abc", Toolbox::ConvertToAscii(s));
430
431 // Open in Emacs, then save with UTF-8 encoding, then "hexdump -C"
432 std::string utf8 = Toolbox::ConvertToUtf8(s, Encoding_Latin1, false);
433 ASSERT_EQ(15u, utf8.size());
434 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[0]));
435 ASSERT_EQ(0xa0, static_cast<unsigned char>(utf8[1]));
436 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[2]));
437 ASSERT_EQ(0xa9, static_cast<unsigned char>(utf8[3]));
438 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[4]));
439 ASSERT_EQ(0xaa, static_cast<unsigned char>(utf8[5]));
440 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[6]));
441 ASSERT_EQ(0xa7, static_cast<unsigned char>(utf8[7]));
442 ASSERT_EQ(0x26, static_cast<unsigned char>(utf8[8]));
443 ASSERT_EQ(0xc3, static_cast<unsigned char>(utf8[9]));
444 ASSERT_EQ(0x86, static_cast<unsigned char>(utf8[10]));
445 ASSERT_EQ(0x61, static_cast<unsigned char>(utf8[11]));
446 ASSERT_EQ(0x62, static_cast<unsigned char>(utf8[12]));
447 ASSERT_EQ(0x63, static_cast<unsigned char>(utf8[13]));
448 ASSERT_EQ(0x00, static_cast<unsigned char>(utf8[14])); // Null-terminated string
449 }
450
451
452 TEST(Toolbox, FixUtf8)
453 {
454 // This is a Latin-1 test string: "crane" with a circumflex accent
455 const unsigned char latin1[] = { 0x63, 0x72, 0xe2, 0x6e, 0x65 };
456
457 std::string s((char*) &latin1[0], sizeof(latin1) / sizeof(char));
458
459 ASSERT_EQ(s, Toolbox::ConvertFromUtf8(Toolbox::ConvertToUtf8(s, Encoding_Latin1, false), Encoding_Latin1));
460 ASSERT_EQ("cre", Toolbox::ConvertToUtf8(s, Encoding_Utf8, false));
461 }
462
463
464 static int32_t GetUnicode(const uint8_t* data,
465 size_t size,
466 size_t expectedLength)
467 {
468 std::string s((char*) &data[0], size);
469 uint32_t unicode;
470 size_t length;
471 Toolbox::Utf8ToUnicodeCharacter(unicode, length, s, 0);
472 if (length != expectedLength)
473 {
474 return -1; // Error case
475 }
476 else
477 {
478 return unicode;
479 }
480 }
481
482
483 TEST(Toolbox, Utf8ToUnicode)
484 {
485 // https://en.wikipedia.org/wiki/UTF-8
486
487 ASSERT_EQ(1u, sizeof(char));
488 ASSERT_EQ(1u, sizeof(uint8_t));
489
490 {
491 const uint8_t data[] = { 0x24 };
492 ASSERT_EQ(0x24, GetUnicode(data, 1, 1));
493 ASSERT_THROW(GetUnicode(data, 0, 1), OrthancException);
494 }
495
496 {
497 const uint8_t data[] = { 0xc2, 0xa2 };
498 ASSERT_EQ(0xa2, GetUnicode(data, 2, 2));
499 ASSERT_THROW(GetUnicode(data, 1, 2), OrthancException);
500 }
501
502 {
503 const uint8_t data[] = { 0xe0, 0xa4, 0xb9 };
504 ASSERT_EQ(0x0939, GetUnicode(data, 3, 3));
505 ASSERT_THROW(GetUnicode(data, 2, 3), OrthancException);
506 }
507
508 {
509 const uint8_t data[] = { 0xe2, 0x82, 0xac };
510 ASSERT_EQ(0x20ac, GetUnicode(data, 3, 3));
511 ASSERT_THROW(GetUnicode(data, 2, 3), OrthancException);
512 }
513
514 {
515 const uint8_t data[] = { 0xf0, 0x90, 0x8d, 0x88 };
516 ASSERT_EQ(0x010348, GetUnicode(data, 4, 4));
517 ASSERT_THROW(GetUnicode(data, 3, 4), OrthancException);
518 }
519
520 {
521 const uint8_t data[] = { 0xe0 };
522 ASSERT_THROW(GetUnicode(data, 1, 1), OrthancException);
523 }
524 }
525
526
527 TEST(Toolbox, UrlDecode)
528 {
529 std::string s;
530
531 s = "Hello%20World";
532 Toolbox::UrlDecode(s);
533 ASSERT_EQ("Hello World", s);
534
535 s = "%21%23%24%26%27%28%29%2A%2B%2c%2f%3A%3b%3d%3f%40%5B%5D%90%ff";
536 Toolbox::UrlDecode(s);
537 std::string ss = "!#$&'()*+,/:;=?@[]";
538 ss.push_back((char) 144);
539 ss.push_back((char) 255);
540 ASSERT_EQ(ss, s);
541
542 s = "(2000%2C00A4)+Other";
543 Toolbox::UrlDecode(s);
544 ASSERT_EQ("(2000,00A4) Other", s);
545 }
546
547
548 TEST(Toolbox, IsAsciiString)
549 {
550 std::string s = "Hello 12 /";
551 ASSERT_EQ(10u, s.size());
552 ASSERT_TRUE(Toolbox::IsAsciiString(s));
553 ASSERT_TRUE(Toolbox::IsAsciiString(s.c_str(), 10));
554 ASSERT_FALSE(Toolbox::IsAsciiString(s.c_str(), 11)); // Taking the trailing hidden '\0'
555
556 s[2] = '\0';
557 ASSERT_EQ(10u, s.size());
558 ASSERT_FALSE(Toolbox::IsAsciiString(s));
559
560 ASSERT_TRUE(Toolbox::IsAsciiString("Hello\nworld"));
561 ASSERT_FALSE(Toolbox::IsAsciiString("Hello\rworld"));
562
563 ASSERT_EQ("Hello\nworld", Toolbox::ConvertToAscii("Hello\nworld"));
564 ASSERT_EQ("Helloworld", Toolbox::ConvertToAscii("Hello\r\tworld"));
565 }
566
567
568 #if defined(__linux__)
569 TEST(Toolbox, AbsoluteDirectory)
570 {
571 ASSERT_EQ("/tmp/hello", SystemToolbox::InterpretRelativePath("/tmp", "hello"));
572 ASSERT_EQ("/tmp", SystemToolbox::InterpretRelativePath("/tmp", "/tmp"));
573 }
574 #endif
575
576
577 TEST(Toolbox, WriteFile)
578 {
579 std::string path;
580
581 {
582 TemporaryFile tmp;
583 path = tmp.GetPath();
584
585 std::string s;
586 s.append("Hello");
587 s.push_back('\0');
588 s.append("World");
589 ASSERT_EQ(11u, s.size());
590
591 SystemToolbox::WriteFile(s, path.c_str());
592
593 std::string t;
594 SystemToolbox::ReadFile(t, path.c_str());
595
596 ASSERT_EQ(11u, t.size());
597 ASSERT_EQ(0, t[5]);
598 ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size()));
599
600 std::string h;
601 ASSERT_EQ(true, SystemToolbox::ReadHeader(h, path.c_str(), 1));
602 ASSERT_EQ(1u, h.size());
603 ASSERT_EQ('H', h[0]);
604 ASSERT_TRUE(SystemToolbox::ReadHeader(h, path.c_str(), 0));
605 ASSERT_EQ(0u, h.size());
606 ASSERT_FALSE(SystemToolbox::ReadHeader(h, path.c_str(), 32));
607 ASSERT_EQ(11u, h.size());
608 ASSERT_EQ(0, memcmp(s.c_str(), h.c_str(), s.size()));
609 }
610
611 std::string u;
612 ASSERT_THROW(SystemToolbox::ReadFile(u, path.c_str()), OrthancException);
613 }
614
615
616 TEST(Toolbox, FileBuffer)
617 {
618 FileBuffer f;
619 f.Append("a", 1);
620 f.Append("", 0);
621 f.Append("bc", 2);
622
623 std::string s;
624 f.Read(s);
625 ASSERT_EQ("abc", s);
626
627 ASSERT_THROW(f.Append("d", 1), OrthancException); // File is closed
628 }
629
630
631 TEST(Toolbox, Wildcard)
632 {
633 ASSERT_EQ("abcd", Toolbox::WildcardToRegularExpression("abcd"));
634 ASSERT_EQ("ab.*cd", Toolbox::WildcardToRegularExpression("ab*cd"));
635 ASSERT_EQ("ab..cd", Toolbox::WildcardToRegularExpression("ab??cd"));
636 ASSERT_EQ("a.*b.c.*d", Toolbox::WildcardToRegularExpression("a*b?c*d"));
637 ASSERT_EQ("a\\{b\\]", Toolbox::WildcardToRegularExpression("a{b]"));
638 }
639
640
641 TEST(Toolbox, Tokenize)
642 {
643 std::vector<std::string> t;
644
645 Toolbox::TokenizeString(t, "", ',');
646 ASSERT_EQ(1u, t.size());
647 ASSERT_EQ("", t[0]);
648
649 Toolbox::TokenizeString(t, "abc", ',');
650 ASSERT_EQ(1u, t.size());
651 ASSERT_EQ("abc", t[0]);
652
653 Toolbox::TokenizeString(t, "ab,cd,ef,", ',');
654 ASSERT_EQ(4u, t.size());
655 ASSERT_EQ("ab", t[0]);
656 ASSERT_EQ("cd", t[1]);
657 ASSERT_EQ("ef", t[2]);
658 ASSERT_EQ("", t[3]);
659 }
660
661 TEST(Toolbox, Enumerations)
662 {
663 ASSERT_EQ(Encoding_Utf8, StringToEncoding(EnumerationToString(Encoding_Utf8)));
664 ASSERT_EQ(Encoding_Ascii, StringToEncoding(EnumerationToString(Encoding_Ascii)));
665 ASSERT_EQ(Encoding_Latin1, StringToEncoding(EnumerationToString(Encoding_Latin1)));
666 ASSERT_EQ(Encoding_Latin2, StringToEncoding(EnumerationToString(Encoding_Latin2)));
667 ASSERT_EQ(Encoding_Latin3, StringToEncoding(EnumerationToString(Encoding_Latin3)));
668 ASSERT_EQ(Encoding_Latin4, StringToEncoding(EnumerationToString(Encoding_Latin4)));
669 ASSERT_EQ(Encoding_Latin5, StringToEncoding(EnumerationToString(Encoding_Latin5)));
670 ASSERT_EQ(Encoding_Cyrillic, StringToEncoding(EnumerationToString(Encoding_Cyrillic)));
671 ASSERT_EQ(Encoding_Arabic, StringToEncoding(EnumerationToString(Encoding_Arabic)));
672 ASSERT_EQ(Encoding_Greek, StringToEncoding(EnumerationToString(Encoding_Greek)));
673 ASSERT_EQ(Encoding_Hebrew, StringToEncoding(EnumerationToString(Encoding_Hebrew)));
674 ASSERT_EQ(Encoding_Japanese, StringToEncoding(EnumerationToString(Encoding_Japanese)));
675 ASSERT_EQ(Encoding_Chinese, StringToEncoding(EnumerationToString(Encoding_Chinese)));
676 ASSERT_EQ(Encoding_Thai, StringToEncoding(EnumerationToString(Encoding_Thai)));
677 ASSERT_EQ(Encoding_Korean, StringToEncoding(EnumerationToString(Encoding_Korean)));
678 ASSERT_EQ(Encoding_JapaneseKanji, StringToEncoding(EnumerationToString(Encoding_JapaneseKanji)));
679 ASSERT_EQ(Encoding_SimplifiedChinese, StringToEncoding(EnumerationToString(Encoding_SimplifiedChinese)));
680
681 ASSERT_EQ(ResourceType_Patient, StringToResourceType(EnumerationToString(ResourceType_Patient)));
682 ASSERT_EQ(ResourceType_Study, StringToResourceType(EnumerationToString(ResourceType_Study)));
683 ASSERT_EQ(ResourceType_Series, StringToResourceType(EnumerationToString(ResourceType_Series)));
684 ASSERT_EQ(ResourceType_Instance, StringToResourceType(EnumerationToString(ResourceType_Instance)));
685
686 ASSERT_EQ(ImageFormat_Png, StringToImageFormat(EnumerationToString(ImageFormat_Png)));
687
688 ASSERT_EQ(PhotometricInterpretation_ARGB, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_ARGB)));
689 ASSERT_EQ(PhotometricInterpretation_CMYK, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_CMYK)));
690 ASSERT_EQ(PhotometricInterpretation_HSV, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_HSV)));
691 ASSERT_EQ(PhotometricInterpretation_Monochrome1, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Monochrome1)));
692 ASSERT_EQ(PhotometricInterpretation_Monochrome2, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Monochrome2)));
693 ASSERT_EQ(PhotometricInterpretation_Palette, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Palette)));
694 ASSERT_EQ(PhotometricInterpretation_RGB, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_RGB)));
695 ASSERT_EQ(PhotometricInterpretation_YBRFull, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRFull)));
696 ASSERT_EQ(PhotometricInterpretation_YBRFull422, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRFull422)));
697 ASSERT_EQ(PhotometricInterpretation_YBRPartial420, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRPartial420)));
698 ASSERT_EQ(PhotometricInterpretation_YBRPartial422, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRPartial422)));
699 ASSERT_EQ(PhotometricInterpretation_YBR_ICT, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBR_ICT)));
700 ASSERT_EQ(PhotometricInterpretation_YBR_RCT, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBR_RCT)));
701
702 ASSERT_STREQ("Unknown", EnumerationToString(PhotometricInterpretation_Unknown));
703 ASSERT_THROW(StringToPhotometricInterpretation("Unknown"), OrthancException);
704
705 ASSERT_EQ(DicomVersion_2008, StringToDicomVersion(EnumerationToString(DicomVersion_2008)));
706 ASSERT_EQ(DicomVersion_2017c, StringToDicomVersion(EnumerationToString(DicomVersion_2017c)));
707
708 for (int i = static_cast<int>(ValueRepresentation_ApplicationEntity);
709 i < static_cast<int>(ValueRepresentation_NotSupported); i += 1)
710 {
711 ValueRepresentation vr = static_cast<ValueRepresentation>(i);
712 ASSERT_EQ(vr, StringToValueRepresentation(EnumerationToString(vr), true));
713 }
714
715 ASSERT_THROW(StringToValueRepresentation("nope", true), OrthancException);
716
717 ASSERT_EQ(JobState_Pending, StringToJobState(EnumerationToString(JobState_Pending)));
718 ASSERT_EQ(JobState_Running, StringToJobState(EnumerationToString(JobState_Running)));
719 ASSERT_EQ(JobState_Success, StringToJobState(EnumerationToString(JobState_Success)));
720 ASSERT_EQ(JobState_Failure, StringToJobState(EnumerationToString(JobState_Failure)));
721 ASSERT_EQ(JobState_Paused, StringToJobState(EnumerationToString(JobState_Paused)));
722 ASSERT_EQ(JobState_Retry, StringToJobState(EnumerationToString(JobState_Retry)));
723 ASSERT_THROW(StringToJobState("nope"), OrthancException);
724
725 ASSERT_EQ(MimeType_Binary, StringToMimeType(EnumerationToString(MimeType_Binary)));
726 ASSERT_EQ(MimeType_Css, StringToMimeType(EnumerationToString(MimeType_Css)));
727 ASSERT_EQ(MimeType_Dicom, StringToMimeType(EnumerationToString(MimeType_Dicom)));
728 ASSERT_EQ(MimeType_Gif, StringToMimeType(EnumerationToString(MimeType_Gif)));
729 ASSERT_EQ(MimeType_Gzip, StringToMimeType(EnumerationToString(MimeType_Gzip)));
730 ASSERT_EQ(MimeType_Html, StringToMimeType(EnumerationToString(MimeType_Html)));
731 ASSERT_EQ(MimeType_JavaScript, StringToMimeType(EnumerationToString(MimeType_JavaScript)));
732 ASSERT_EQ(MimeType_Jpeg, StringToMimeType(EnumerationToString(MimeType_Jpeg)));
733 ASSERT_EQ(MimeType_Jpeg2000, StringToMimeType(EnumerationToString(MimeType_Jpeg2000)));
734 ASSERT_EQ(MimeType_Json, StringToMimeType(EnumerationToString(MimeType_Json)));
735 ASSERT_EQ(MimeType_NaCl, StringToMimeType(EnumerationToString(MimeType_NaCl)));
736 ASSERT_EQ(MimeType_PNaCl, StringToMimeType(EnumerationToString(MimeType_PNaCl)));
737 ASSERT_EQ(MimeType_Pam, StringToMimeType(EnumerationToString(MimeType_Pam)));
738 ASSERT_EQ(MimeType_Pdf, StringToMimeType(EnumerationToString(MimeType_Pdf)));
739 ASSERT_EQ(MimeType_PlainText, StringToMimeType(EnumerationToString(MimeType_PlainText)));
740 ASSERT_EQ(MimeType_Png, StringToMimeType(EnumerationToString(MimeType_Png)));
741 ASSERT_EQ(MimeType_Svg, StringToMimeType(EnumerationToString(MimeType_Svg)));
742 ASSERT_EQ(MimeType_WebAssembly, StringToMimeType(EnumerationToString(MimeType_WebAssembly)));
743 ASSERT_EQ(MimeType_Xml, StringToMimeType("application/xml"));
744 ASSERT_EQ(MimeType_Xml, StringToMimeType("text/xml"));
745 ASSERT_EQ(MimeType_Xml, StringToMimeType(EnumerationToString(MimeType_Xml)));
746 ASSERT_EQ(MimeType_DicomWebJson, StringToMimeType(EnumerationToString(MimeType_DicomWebJson)));
747 ASSERT_EQ(MimeType_DicomWebXml, StringToMimeType(EnumerationToString(MimeType_DicomWebXml)));
748 ASSERT_THROW(StringToMimeType("nope"), OrthancException);
749
750 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Patient));
751 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Study));
752 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Series));
753 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Instance));
754
755 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Patient));
756 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Study));
757 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Series));
758 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Instance));
759
760 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Patient));
761 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Study));
762 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Series));
763 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Instance));
764
765 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Patient));
766 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Study));
767 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Series));
768 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Instance));
769 }
770
771
772 #if defined(__linux__) || defined(__OpenBSD__)
773 #include <endian.h>
774 #elif defined(__FreeBSD__)
775 #include <machine/endian.h>
776 #endif
777
778
779 TEST(Toolbox, Endianness)
780 {
781 // Parts of this test come from Adam Conrad
782 // http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=728822#5
783
784
785 /**
786 * Windows and OS X are assumed to always little-endian.
787 **/
788
789 #if defined(_WIN32) || defined(__APPLE__)
790 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness());
791
792
793 /**
794 * FreeBSD.
795 **/
796
797 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
798 # if _BYTE_ORDER == _BIG_ENDIAN
799 ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness());
800 # else // _LITTLE_ENDIAN
801 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness());
802 # endif
803
804
805 /**
806 * Linux.
807 **/
808
809 #elif defined(__linux__) || defined(__FreeBSD_kernel__)
810
811 #if !defined(__BYTE_ORDER)
812 # error Support your platform here
813 #endif
814
815 # if __BYTE_ORDER == __BIG_ENDIAN
816 ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness());
817 # else // __LITTLE_ENDIAN
818 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness());
819 # endif
820
821 #else
822 #error Support your platform here
823 #endif
824 }
825
826
827 #include "../Core/Endianness.h"
828
829 static void ASSERT_EQ16(uint16_t a, uint16_t b)
830 {
831 #ifdef __MINGW32__
832 // This cast solves a linking problem with MinGW
833 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
834 #else
835 ASSERT_EQ(a, b);
836 #endif
837 }
838
839 static void ASSERT_NE16(uint16_t a, uint16_t b)
840 {
841 #ifdef __MINGW32__
842 // This cast solves a linking problem with MinGW
843 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
844 #else
845 ASSERT_NE(a, b);
846 #endif
847 }
848
849 static void ASSERT_EQ32(uint32_t a, uint32_t b)
850 {
851 #ifdef __MINGW32__
852 // This cast solves a linking problem with MinGW
853 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
854 #else
855 ASSERT_EQ(a, b);
856 #endif
857 }
858
859 static void ASSERT_NE32(uint32_t a, uint32_t b)
860 {
861 #ifdef __MINGW32__
862 // This cast solves a linking problem with MinGW
863 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
864 #else
865 ASSERT_NE(a, b);
866 #endif
867 }
868
869 static void ASSERT_EQ64(uint64_t a, uint64_t b)
870 {
871 #ifdef __MINGW32__
872 // This cast solves a linking problem with MinGW
873 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
874 #else
875 ASSERT_EQ(a, b);
876 #endif
877 }
878
879 static void ASSERT_NE64(uint64_t a, uint64_t b)
880 {
881 #ifdef __MINGW32__
882 // This cast solves a linking problem with MinGW
883 ASSERT_NE(static_cast<unsigned long long>(a), static_cast<unsigned long long>(b));
884 #else
885 ASSERT_NE(a, b);
886 #endif
887 }
888
889
890
891 TEST(Toolbox, EndiannessConversions16)
892 {
893 Endianness e = Toolbox::DetectEndianness();
894
895 for (unsigned int i = 0; i < 65536; i += 17)
896 {
897 uint16_t v = static_cast<uint16_t>(i);
898 ASSERT_EQ16(v, be16toh(htobe16(v)));
899 ASSERT_EQ16(v, le16toh(htole16(v)));
900
901 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&v);
902 if (bytes[0] != bytes[1])
903 {
904 ASSERT_NE16(v, le16toh(htobe16(v)));
905 ASSERT_NE16(v, be16toh(htole16(v)));
906 }
907 else
908 {
909 ASSERT_EQ16(v, le16toh(htobe16(v)));
910 ASSERT_EQ16(v, be16toh(htole16(v)));
911 }
912
913 switch (e)
914 {
915 case Endianness_Little:
916 ASSERT_EQ16(v, htole16(v));
917 if (bytes[0] != bytes[1])
918 {
919 ASSERT_NE16(v, htobe16(v));
920 }
921 else
922 {
923 ASSERT_EQ16(v, htobe16(v));
924 }
925 break;
926
927 case Endianness_Big:
928 ASSERT_EQ16(v, htobe16(v));
929 if (bytes[0] != bytes[1])
930 {
931 ASSERT_NE16(v, htole16(v));
932 }
933 else
934 {
935 ASSERT_EQ16(v, htole16(v));
936 }
937 break;
938
939 default:
940 throw OrthancException(ErrorCode_ParameterOutOfRange);
941 }
942 }
943 }
944
945
946 TEST(Toolbox, EndiannessConversions32)
947 {
948 const uint32_t v = 0xff010203u;
949 const uint32_t r = 0x030201ffu;
950 ASSERT_EQ32(v, be32toh(htobe32(v)));
951 ASSERT_EQ32(v, le32toh(htole32(v)));
952 ASSERT_NE32(v, be32toh(htole32(v)));
953 ASSERT_NE32(v, le32toh(htobe32(v)));
954
955 switch (Toolbox::DetectEndianness())
956 {
957 case Endianness_Little:
958 ASSERT_EQ32(r, htobe32(v));
959 ASSERT_EQ32(v, htole32(v));
960 ASSERT_EQ32(r, be32toh(v));
961 ASSERT_EQ32(v, le32toh(v));
962 break;
963
964 case Endianness_Big:
965 ASSERT_EQ32(v, htobe32(v));
966 ASSERT_EQ32(r, htole32(v));
967 ASSERT_EQ32(v, be32toh(v));
968 ASSERT_EQ32(r, le32toh(v));
969 break;
970
971 default:
972 throw OrthancException(ErrorCode_ParameterOutOfRange);
973 }
974 }
975
976
977 TEST(Toolbox, EndiannessConversions64)
978 {
979 const uint64_t v = 0xff01020304050607LL;
980 const uint64_t r = 0x07060504030201ffLL;
981 ASSERT_EQ64(v, be64toh(htobe64(v)));
982 ASSERT_EQ64(v, le64toh(htole64(v)));
983 ASSERT_NE64(v, be64toh(htole64(v)));
984 ASSERT_NE64(v, le64toh(htobe64(v)));
985
986 switch (Toolbox::DetectEndianness())
987 {
988 case Endianness_Little:
989 ASSERT_EQ64(r, htobe64(v));
990 ASSERT_EQ64(v, htole64(v));
991 ASSERT_EQ64(r, be64toh(v));
992 ASSERT_EQ64(v, le64toh(v));
993 break;
994
995 case Endianness_Big:
996 ASSERT_EQ64(v, htobe64(v));
997 ASSERT_EQ64(r, htole64(v));
998 ASSERT_EQ64(v, be64toh(v));
999 ASSERT_EQ64(r, le64toh(v));
1000 break;
1001
1002 default:
1003 throw OrthancException(ErrorCode_ParameterOutOfRange);
1004 }
1005 }
1006
1007
1008 TEST(Toolbox, Now)
1009 {
1010 LOG(WARNING) << "Local time: " << SystemToolbox::GetNowIsoString(false);
1011 LOG(WARNING) << "Universal time: " << SystemToolbox::GetNowIsoString(true);
1012
1013 std::string date, time;
1014 SystemToolbox::GetNowDicom(date, time, false);
1015 LOG(WARNING) << "Local DICOM time: [" << date << "] [" << time << "]";
1016
1017 SystemToolbox::GetNowDicom(date, time, true);
1018 LOG(WARNING) << "Universal DICOM time: [" << date << "] [" << time << "]";
1019 }
1020
1021
1022
1023 #if ORTHANC_ENABLE_PUGIXML == 1
1024 TEST(Toolbox, Xml)
1025 {
1026 Json::Value a;
1027 a["hello"] = "world";
1028 a["42"] = 43;
1029 a["b"] = Json::arrayValue;
1030 a["b"].append("test");
1031 a["b"].append("test2");
1032
1033 std::string s;
1034 Toolbox::JsonToXml(s, a);
1035
1036 std::cout << s;
1037 }
1038 #endif
1039
1040
1041 #if !defined(_WIN32)
1042 TEST(Toolbox, ExecuteSystemCommand)
1043 {
1044 std::vector<std::string> args(2);
1045 args[0] = "Hello";
1046 args[1] = "World";
1047
1048 SystemToolbox::ExecuteSystemCommand("echo", args);
1049 }
1050 #endif
1051
1052
1053 TEST(Toolbox, IsInteger)
1054 {
1055 ASSERT_TRUE(Toolbox::IsInteger("00236"));
1056 ASSERT_TRUE(Toolbox::IsInteger("-0042"));
1057 ASSERT_TRUE(Toolbox::IsInteger("0"));
1058 ASSERT_TRUE(Toolbox::IsInteger("-0"));
1059
1060 ASSERT_FALSE(Toolbox::IsInteger(""));
1061 ASSERT_FALSE(Toolbox::IsInteger("42a"));
1062 ASSERT_FALSE(Toolbox::IsInteger("42-"));
1063 }
1064
1065
1066 TEST(Toolbox, StartsWith)
1067 {
1068 ASSERT_TRUE(Toolbox::StartsWith("hello world", ""));
1069 ASSERT_TRUE(Toolbox::StartsWith("hello world", "hello"));
1070 ASSERT_TRUE(Toolbox::StartsWith("hello world", "h"));
1071 ASSERT_FALSE(Toolbox::StartsWith("hello world", "H"));
1072 ASSERT_FALSE(Toolbox::StartsWith("h", "hello"));
1073 ASSERT_TRUE(Toolbox::StartsWith("h", "h"));
1074 ASSERT_FALSE(Toolbox::StartsWith("", "h"));
1075 }
1076
1077
1078 TEST(Toolbox, UriEncode)
1079 {
1080 std::string s;
1081
1082 // Unreserved characters must not be modified
1083 std::string t = "aAzZ09.-~_";
1084 Toolbox::UriEncode(s, t);
1085 ASSERT_EQ(t, s);
1086
1087 Toolbox::UriEncode(s, "!#$&'()*+,/:;=?@[]"); ASSERT_EQ("%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D", s);
1088 Toolbox::UriEncode(s, "%"); ASSERT_EQ("%25", s);
1089
1090 // Encode characters from UTF-8. This is the test string from the
1091 // file "../Resources/EncodingTests.py"
1092 Toolbox::UriEncode(s, "\x54\x65\x73\x74\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\xd0\x94\xce\x98\xc4\x9d\xd7\x93\xd8\xb5\xc4\xb7\xd1\x9b\xe0\xb9\x9b\xef\xbe\x88\xc4\xb0");
1093 ASSERT_EQ("Test%C3%A9%C3%A4%C3%B6%C3%B2%D0%94%CE%98%C4%9D%D7%93%D8%B5%C4%B7%D1%9B%E0%B9%9B%EF%BE%88%C4%B0", s);
1094 }
1095
1096
1097 TEST(Toolbox, AccessJson)
1098 {
1099 Json::Value v = Json::arrayValue;
1100 ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope"));
1101
1102 v = Json::objectValue;
1103 ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope"));
1104 ASSERT_EQ(-10, Toolbox::GetJsonIntegerField(v, "hello", -10));
1105 ASSERT_EQ(10u, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10));
1106 ASSERT_TRUE(Toolbox::GetJsonBooleanField(v, "hello", true));
1107
1108 v["hello"] = "world";
1109 ASSERT_EQ("world", Toolbox::GetJsonStringField(v, "hello", "nope"));
1110 ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException);
1111 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException);
1112 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException);
1113
1114 v["hello"] = -42;
1115 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException);
1116 ASSERT_EQ(-42, Toolbox::GetJsonIntegerField(v, "hello", -10));
1117 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException);
1118 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException);
1119
1120 v["hello"] = 42;
1121 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException);
1122 ASSERT_EQ(42, Toolbox::GetJsonIntegerField(v, "hello", -10));
1123 ASSERT_EQ(42u, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10));
1124 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException);
1125
1126 v["hello"] = false;
1127 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException);
1128 ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException);
1129 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException);
1130 ASSERT_FALSE(Toolbox::GetJsonBooleanField(v, "hello", true));
1131 }
1132
1133
1134 TEST(Toolbox, LinesIterator)
1135 {
1136 std::string s;
1137
1138 {
1139 std::string content;
1140 Toolbox::LinesIterator it(content);
1141 ASSERT_FALSE(it.GetLine(s));
1142 }
1143
1144 {
1145 std::string content = "\n\r";
1146 Toolbox::LinesIterator it(content);
1147 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1148 ASSERT_FALSE(it.GetLine(s));
1149 }
1150
1151 {
1152 std::string content = "\n Hello \n\nWorld\n\n";
1153 Toolbox::LinesIterator it(content);
1154 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1155 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1156 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1157 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1158 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1159 ASSERT_FALSE(it.GetLine(s)); it.Next();
1160 ASSERT_FALSE(it.GetLine(s));
1161 }
1162
1163 {
1164 std::string content = "\r Hello \r\rWorld\r\r";
1165 Toolbox::LinesIterator it(content);
1166 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1167 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1168 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1169 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1170 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1171 ASSERT_FALSE(it.GetLine(s)); it.Next();
1172 ASSERT_FALSE(it.GetLine(s));
1173 }
1174
1175 {
1176 std::string content = "\n\r Hello \n\r\n\rWorld\n\r\n\r";
1177 Toolbox::LinesIterator it(content);
1178 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1179 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1180 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1181 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1182 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1183 ASSERT_FALSE(it.GetLine(s)); it.Next();
1184 ASSERT_FALSE(it.GetLine(s));
1185 }
1186
1187 {
1188 std::string content = "\r\n Hello \r\n\r\nWorld\r\n\r\n";
1189 Toolbox::LinesIterator it(content);
1190 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1191 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1192 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1193 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1194 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1195 ASSERT_FALSE(it.GetLine(s)); it.Next();
1196 ASSERT_FALSE(it.GetLine(s));
1197 }
1198 }
1199
1200
1201 TEST(Toolbox, SubstituteVariables)
1202 {
1203 std::map<std::string, std::string> env;
1204 env["NOPE"] = "nope";
1205 env["WORLD"] = "world";
1206
1207 ASSERT_EQ("Hello world\r\nWorld \r\nDone world\r\n",
1208 Toolbox::SubstituteVariables(
1209 "Hello ${WORLD}\r\nWorld ${HELLO}\r\nDone ${WORLD}\r\n",
1210 env));
1211
1212 ASSERT_EQ("world A a B world C 'c' D {\"a\":\"b\"} E ",
1213 Toolbox::SubstituteVariables(
1214 "${WORLD} A ${WORLD2:-a} B ${WORLD:-b} C ${WORLD2:-\"'c'\"} D ${WORLD2:-'{\"a\":\"b\"}'} E ${WORLD2:-}",
1215 env));
1216
1217 SystemToolbox::GetEnvironmentVariables(env);
1218 ASSERT_TRUE(env.find("NOPE") == env.end());
1219
1220 // The "PATH" environment variable should always be available on
1221 // machines running the unit tests
1222 ASSERT_TRUE(env.find("PATH") != env.end() /* Case used by UNIX */ ||
1223 env.find("Path") != env.end() /* Case used by Windows */);
1224
1225 env["PATH"] = "hello";
1226 ASSERT_EQ("AhelloB",
1227 Toolbox::SubstituteVariables("A${PATH}B", env));
1228 }
1229
1230
1231 TEST(MetricsRegistry, Basic)
1232 {
1233 {
1234 MetricsRegistry m;
1235 m.SetEnabled(false);
1236 m.SetValue("hello.world", 42.5f);
1237
1238 std::string s;
1239 m.ExportPrometheusText(s);
1240 ASSERT_TRUE(s.empty());
1241 }
1242
1243 {
1244 MetricsRegistry m;
1245 m.Register("hello.world", MetricsType_Default);
1246
1247 std::string s;
1248 m.ExportPrometheusText(s);
1249 ASSERT_TRUE(s.empty());
1250 }
1251
1252 {
1253 MetricsRegistry m;
1254 m.SetValue("hello.world", 42.5f);
1255 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.world"));
1256 ASSERT_THROW(m.GetMetricsType("nope"), OrthancException);
1257
1258 std::string s;
1259 m.ExportPrometheusText(s);
1260
1261 std::vector<std::string> t;
1262 Toolbox::TokenizeString(t, s, '\n');
1263 ASSERT_EQ(2u, t.size());
1264 ASSERT_EQ("hello.world 42.5 ", t[0].substr(0, 17));
1265 ASSERT_TRUE(t[1].empty());
1266 }
1267
1268 {
1269 MetricsRegistry m;
1270 m.Register("hello.max", MetricsType_MaxOver10Seconds);
1271 m.SetValue("hello.max", 10);
1272 m.SetValue("hello.max", 20);
1273 m.SetValue("hello.max", -10);
1274 m.SetValue("hello.max", 5);
1275
1276 m.Register("hello.min", MetricsType_MinOver10Seconds);
1277 m.SetValue("hello.min", 10);
1278 m.SetValue("hello.min", 20);
1279 m.SetValue("hello.min", -10);
1280 m.SetValue("hello.min", 5);
1281
1282 m.Register("hello.default", MetricsType_Default);
1283 m.SetValue("hello.default", 10);
1284 m.SetValue("hello.default", 20);
1285 m.SetValue("hello.default", -10);
1286 m.SetValue("hello.default", 5);
1287
1288 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("hello.max"));
1289 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("hello.min"));
1290 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.default"));
1291
1292 std::string s;
1293 m.ExportPrometheusText(s);
1294
1295 std::vector<std::string> t;
1296 Toolbox::TokenizeString(t, s, '\n');
1297 ASSERT_EQ(4u, t.size());
1298 ASSERT_TRUE(t[3].empty());
1299
1300 std::map<std::string, std::string> u;
1301 for (size_t i = 0; i < t.size() - 1; i++)
1302 {
1303 std::vector<std::string> v;
1304 Toolbox::TokenizeString(v, t[i], ' ');
1305 u[v[0]] = v[1];
1306 }
1307
1308 ASSERT_EQ("20", u["hello.max"]);
1309 ASSERT_EQ("-10", u["hello.min"]);
1310 ASSERT_EQ("5", u["hello.default"]);
1311 }
1312
1313 {
1314 MetricsRegistry m;
1315
1316 m.SetValue("a", 10);
1317 m.SetValue("b", 10, MetricsType_MinOver10Seconds);
1318
1319 m.Register("c", MetricsType_MaxOver10Seconds);
1320 m.SetValue("c", 10, MetricsType_MinOver10Seconds);
1321
1322 m.Register("d", MetricsType_MaxOver10Seconds);
1323 m.Register("d", MetricsType_Default);
1324
1325 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("a"));
1326 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b"));
1327 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("c"));
1328 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("d"));
1329 }
1330
1331 {
1332 MetricsRegistry m;
1333
1334 {
1335 MetricsRegistry::Timer t1(m, "a");
1336 MetricsRegistry::Timer t2(m, "b", MetricsType_MinOver10Seconds);
1337 }
1338
1339 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("a"));
1340 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b"));
1341 }
1342 }