comparison UnitTestsSources/FrameworkTests.cpp @ 3987:e12c8ac6dc82

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