comparison UnitTestsSources/UnitTestsMain.cpp @ 3987:e12c8ac6dc82

fix build
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 29 May 2020 18:42:18 +0200
parents 94f4a18a79cc
children 5d2348b39392
comparison
equal deleted inserted replaced
3986:df453020eaf5 3987:e12c8ac6dc82
34 #include "PrecompiledHeadersUnitTests.h" 34 #include "PrecompiledHeadersUnitTests.h"
35 #include "../Core/EnumerationDictionary.h" 35 #include "../Core/EnumerationDictionary.h"
36 36
37 #include "gtest/gtest.h" 37 #include "gtest/gtest.h"
38 38
39 #include <ctype.h> 39 #include "../Core/Logging.h"
40 #include "../Core/Toolbox.h"
41 #include "../Core/OrthancException.h"
40 42
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/TemporaryFile.h"
48 #include "../Core/Toolbox.h"
49 #include "../OrthancServer/OrthancInitialization.h" 43 #include "../OrthancServer/OrthancInitialization.h"
44 #include "../OrthancServer/ServerEnumerations.h"
50 45
51 46
52 using namespace Orthanc; 47 using namespace Orthanc;
53 48
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
574 #include "../OrthancServer/ServerEnumerations.h"
575 49
576 TEST(EnumerationDictionary, Simple) 50 TEST(EnumerationDictionary, Simple)
577 { 51 {
578 Toolbox::EnumerationDictionary<MetadataType> d; 52 Toolbox::EnumerationDictionary<MetadataType> d;
579 53
641 ASSERT_STREQ("GenericNoWildcardInDates", EnumerationToString(StringToModalityManufacturer("AgfaImpax"))); 115 ASSERT_STREQ("GenericNoWildcardInDates", EnumerationToString(StringToModalityManufacturer("AgfaImpax")));
642 } 116 }
643 117
644 118
645 119
646 TEST(Toolbox, WriteFile)
647 {
648 std::string path;
649
650 {
651 TemporaryFile tmp;
652 path = tmp.GetPath();
653
654 std::string s;
655 s.append("Hello");
656 s.push_back('\0');
657 s.append("World");
658 ASSERT_EQ(11u, s.size());
659
660 SystemToolbox::WriteFile(s, path.c_str());
661
662 std::string t;
663 SystemToolbox::ReadFile(t, path.c_str());
664
665 ASSERT_EQ(11u, t.size());
666 ASSERT_EQ(0, t[5]);
667 ASSERT_EQ(0, memcmp(s.c_str(), t.c_str(), s.size()));
668
669 std::string h;
670 ASSERT_EQ(true, SystemToolbox::ReadHeader(h, path.c_str(), 1));
671 ASSERT_EQ(1u, h.size());
672 ASSERT_EQ('H', h[0]);
673 ASSERT_TRUE(SystemToolbox::ReadHeader(h, path.c_str(), 0));
674 ASSERT_EQ(0u, h.size());
675 ASSERT_FALSE(SystemToolbox::ReadHeader(h, path.c_str(), 32));
676 ASSERT_EQ(11u, h.size());
677 ASSERT_EQ(0, memcmp(s.c_str(), h.c_str(), s.size()));
678 }
679
680 std::string u;
681 ASSERT_THROW(SystemToolbox::ReadFile(u, path.c_str()), OrthancException);
682 }
683
684
685 TEST(Toolbox, FileBuffer)
686 {
687 FileBuffer f;
688 f.Append("a", 1);
689 f.Append("", 0);
690 f.Append("bc", 2);
691
692 std::string s;
693 f.Read(s);
694 ASSERT_EQ("abc", s);
695
696 ASSERT_THROW(f.Append("d", 1), OrthancException); // File is closed
697 }
698
699
700 TEST(Toolbox, Wildcard)
701 {
702 ASSERT_EQ("abcd", Toolbox::WildcardToRegularExpression("abcd"));
703 ASSERT_EQ("ab.*cd", Toolbox::WildcardToRegularExpression("ab*cd"));
704 ASSERT_EQ("ab..cd", Toolbox::WildcardToRegularExpression("ab??cd"));
705 ASSERT_EQ("a.*b.c.*d", Toolbox::WildcardToRegularExpression("a*b?c*d"));
706 ASSERT_EQ("a\\{b\\]", Toolbox::WildcardToRegularExpression("a{b]"));
707 }
708
709
710 TEST(Toolbox, Tokenize)
711 {
712 std::vector<std::string> t;
713
714 Toolbox::TokenizeString(t, "", ',');
715 ASSERT_EQ(1u, t.size());
716 ASSERT_EQ("", t[0]);
717
718 Toolbox::TokenizeString(t, "abc", ',');
719 ASSERT_EQ(1u, t.size());
720 ASSERT_EQ("abc", t[0]);
721
722 Toolbox::TokenizeString(t, "ab,cd,ef,", ',');
723 ASSERT_EQ(4u, t.size());
724 ASSERT_EQ("ab", t[0]);
725 ASSERT_EQ("cd", t[1]);
726 ASSERT_EQ("ef", t[2]);
727 ASSERT_EQ("", t[3]);
728 }
729
730 TEST(Toolbox, Enumerations)
731 {
732 ASSERT_EQ(Encoding_Utf8, StringToEncoding(EnumerationToString(Encoding_Utf8)));
733 ASSERT_EQ(Encoding_Ascii, StringToEncoding(EnumerationToString(Encoding_Ascii)));
734 ASSERT_EQ(Encoding_Latin1, StringToEncoding(EnumerationToString(Encoding_Latin1)));
735 ASSERT_EQ(Encoding_Latin2, StringToEncoding(EnumerationToString(Encoding_Latin2)));
736 ASSERT_EQ(Encoding_Latin3, StringToEncoding(EnumerationToString(Encoding_Latin3)));
737 ASSERT_EQ(Encoding_Latin4, StringToEncoding(EnumerationToString(Encoding_Latin4)));
738 ASSERT_EQ(Encoding_Latin5, StringToEncoding(EnumerationToString(Encoding_Latin5)));
739 ASSERT_EQ(Encoding_Cyrillic, StringToEncoding(EnumerationToString(Encoding_Cyrillic)));
740 ASSERT_EQ(Encoding_Arabic, StringToEncoding(EnumerationToString(Encoding_Arabic)));
741 ASSERT_EQ(Encoding_Greek, StringToEncoding(EnumerationToString(Encoding_Greek)));
742 ASSERT_EQ(Encoding_Hebrew, StringToEncoding(EnumerationToString(Encoding_Hebrew)));
743 ASSERT_EQ(Encoding_Japanese, StringToEncoding(EnumerationToString(Encoding_Japanese)));
744 ASSERT_EQ(Encoding_Chinese, StringToEncoding(EnumerationToString(Encoding_Chinese)));
745 ASSERT_EQ(Encoding_Thai, StringToEncoding(EnumerationToString(Encoding_Thai)));
746 ASSERT_EQ(Encoding_Korean, StringToEncoding(EnumerationToString(Encoding_Korean)));
747 ASSERT_EQ(Encoding_JapaneseKanji, StringToEncoding(EnumerationToString(Encoding_JapaneseKanji)));
748 ASSERT_EQ(Encoding_SimplifiedChinese, StringToEncoding(EnumerationToString(Encoding_SimplifiedChinese)));
749
750 ASSERT_EQ(ResourceType_Patient, StringToResourceType(EnumerationToString(ResourceType_Patient)));
751 ASSERT_EQ(ResourceType_Study, StringToResourceType(EnumerationToString(ResourceType_Study)));
752 ASSERT_EQ(ResourceType_Series, StringToResourceType(EnumerationToString(ResourceType_Series)));
753 ASSERT_EQ(ResourceType_Instance, StringToResourceType(EnumerationToString(ResourceType_Instance)));
754
755 ASSERT_EQ(ImageFormat_Png, StringToImageFormat(EnumerationToString(ImageFormat_Png)));
756
757 ASSERT_EQ(PhotometricInterpretation_ARGB, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_ARGB)));
758 ASSERT_EQ(PhotometricInterpretation_CMYK, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_CMYK)));
759 ASSERT_EQ(PhotometricInterpretation_HSV, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_HSV)));
760 ASSERT_EQ(PhotometricInterpretation_Monochrome1, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Monochrome1)));
761 ASSERT_EQ(PhotometricInterpretation_Monochrome2, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Monochrome2)));
762 ASSERT_EQ(PhotometricInterpretation_Palette, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_Palette)));
763 ASSERT_EQ(PhotometricInterpretation_RGB, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_RGB)));
764 ASSERT_EQ(PhotometricInterpretation_YBRFull, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRFull)));
765 ASSERT_EQ(PhotometricInterpretation_YBRFull422, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRFull422)));
766 ASSERT_EQ(PhotometricInterpretation_YBRPartial420, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRPartial420)));
767 ASSERT_EQ(PhotometricInterpretation_YBRPartial422, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBRPartial422)));
768 ASSERT_EQ(PhotometricInterpretation_YBR_ICT, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBR_ICT)));
769 ASSERT_EQ(PhotometricInterpretation_YBR_RCT, StringToPhotometricInterpretation(EnumerationToString(PhotometricInterpretation_YBR_RCT)));
770
771 ASSERT_STREQ("Unknown", EnumerationToString(PhotometricInterpretation_Unknown));
772 ASSERT_THROW(StringToPhotometricInterpretation("Unknown"), OrthancException);
773
774 ASSERT_EQ(DicomVersion_2008, StringToDicomVersion(EnumerationToString(DicomVersion_2008)));
775 ASSERT_EQ(DicomVersion_2017c, StringToDicomVersion(EnumerationToString(DicomVersion_2017c)));
776
777 for (int i = static_cast<int>(ValueRepresentation_ApplicationEntity);
778 i < static_cast<int>(ValueRepresentation_NotSupported); i += 1)
779 {
780 ValueRepresentation vr = static_cast<ValueRepresentation>(i);
781 ASSERT_EQ(vr, StringToValueRepresentation(EnumerationToString(vr), true));
782 }
783
784 ASSERT_THROW(StringToValueRepresentation("nope", true), OrthancException);
785
786 ASSERT_EQ(JobState_Pending, StringToJobState(EnumerationToString(JobState_Pending)));
787 ASSERT_EQ(JobState_Running, StringToJobState(EnumerationToString(JobState_Running)));
788 ASSERT_EQ(JobState_Success, StringToJobState(EnumerationToString(JobState_Success)));
789 ASSERT_EQ(JobState_Failure, StringToJobState(EnumerationToString(JobState_Failure)));
790 ASSERT_EQ(JobState_Paused, StringToJobState(EnumerationToString(JobState_Paused)));
791 ASSERT_EQ(JobState_Retry, StringToJobState(EnumerationToString(JobState_Retry)));
792 ASSERT_THROW(StringToJobState("nope"), OrthancException);
793
794 ASSERT_EQ(MimeType_Binary, StringToMimeType(EnumerationToString(MimeType_Binary)));
795 ASSERT_EQ(MimeType_Css, StringToMimeType(EnumerationToString(MimeType_Css)));
796 ASSERT_EQ(MimeType_Dicom, StringToMimeType(EnumerationToString(MimeType_Dicom)));
797 ASSERT_EQ(MimeType_Gif, StringToMimeType(EnumerationToString(MimeType_Gif)));
798 ASSERT_EQ(MimeType_Gzip, StringToMimeType(EnumerationToString(MimeType_Gzip)));
799 ASSERT_EQ(MimeType_Html, StringToMimeType(EnumerationToString(MimeType_Html)));
800 ASSERT_EQ(MimeType_JavaScript, StringToMimeType(EnumerationToString(MimeType_JavaScript)));
801 ASSERT_EQ(MimeType_Jpeg, StringToMimeType(EnumerationToString(MimeType_Jpeg)));
802 ASSERT_EQ(MimeType_Jpeg2000, StringToMimeType(EnumerationToString(MimeType_Jpeg2000)));
803 ASSERT_EQ(MimeType_Json, StringToMimeType(EnumerationToString(MimeType_Json)));
804 ASSERT_EQ(MimeType_NaCl, StringToMimeType(EnumerationToString(MimeType_NaCl)));
805 ASSERT_EQ(MimeType_PNaCl, StringToMimeType(EnumerationToString(MimeType_PNaCl)));
806 ASSERT_EQ(MimeType_Pam, StringToMimeType(EnumerationToString(MimeType_Pam)));
807 ASSERT_EQ(MimeType_Pdf, StringToMimeType(EnumerationToString(MimeType_Pdf)));
808 ASSERT_EQ(MimeType_PlainText, StringToMimeType(EnumerationToString(MimeType_PlainText)));
809 ASSERT_EQ(MimeType_Png, StringToMimeType(EnumerationToString(MimeType_Png)));
810 ASSERT_EQ(MimeType_Svg, StringToMimeType(EnumerationToString(MimeType_Svg)));
811 ASSERT_EQ(MimeType_WebAssembly, StringToMimeType(EnumerationToString(MimeType_WebAssembly)));
812 ASSERT_EQ(MimeType_Xml, StringToMimeType("application/xml"));
813 ASSERT_EQ(MimeType_Xml, StringToMimeType("text/xml"));
814 ASSERT_EQ(MimeType_Xml, StringToMimeType(EnumerationToString(MimeType_Xml)));
815 ASSERT_EQ(MimeType_DicomWebJson, StringToMimeType(EnumerationToString(MimeType_DicomWebJson)));
816 ASSERT_EQ(MimeType_DicomWebXml, StringToMimeType(EnumerationToString(MimeType_DicomWebXml)));
817 ASSERT_THROW(StringToMimeType("nope"), OrthancException);
818
819 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Patient));
820 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Study));
821 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Series));
822 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Patient, ResourceType_Instance));
823
824 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Patient));
825 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Study));
826 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Series));
827 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Study, ResourceType_Instance));
828
829 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Patient));
830 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Study));
831 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Series));
832 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Series, ResourceType_Instance));
833
834 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Patient));
835 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Study));
836 ASSERT_FALSE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Series));
837 ASSERT_TRUE(IsResourceLevelAboveOrEqual(ResourceType_Instance, ResourceType_Instance));
838 }
839
840
841 #if defined(__linux__) || defined(__OpenBSD__)
842 #include <endian.h>
843 #elif defined(__FreeBSD__)
844 #include <machine/endian.h>
845 #endif
846
847
848 TEST(Toolbox, Endianness)
849 {
850 // Parts of this test come from Adam Conrad
851 // http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=728822#5
852
853
854 /**
855 * Windows and OS X are assumed to always little-endian.
856 **/
857
858 #if defined(_WIN32) || defined(__APPLE__)
859 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness());
860
861
862 /**
863 * FreeBSD.
864 **/
865
866 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
867 # if _BYTE_ORDER == _BIG_ENDIAN
868 ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness());
869 # else // _LITTLE_ENDIAN
870 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness());
871 # endif
872
873
874 /**
875 * Linux.
876 **/
877
878 #elif defined(__linux__) || defined(__FreeBSD_kernel__)
879
880 #if !defined(__BYTE_ORDER)
881 # error Support your platform here
882 #endif
883
884 # if __BYTE_ORDER == __BIG_ENDIAN
885 ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness());
886 # else // __LITTLE_ENDIAN
887 ASSERT_EQ(Endianness_Little, Toolbox::DetectEndianness());
888 # endif
889
890 #else
891 #error Support your platform here
892 #endif
893 }
894
895
896 #include "../Core/Endianness.h"
897
898 static void ASSERT_EQ16(uint16_t a, uint16_t b)
899 {
900 #ifdef __MINGW32__
901 // This cast solves a linking problem with MinGW
902 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
903 #else
904 ASSERT_EQ(a, b);
905 #endif
906 }
907
908 static void ASSERT_NE16(uint16_t a, uint16_t b)
909 {
910 #ifdef __MINGW32__
911 // This cast solves a linking problem with MinGW
912 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
913 #else
914 ASSERT_NE(a, b);
915 #endif
916 }
917
918 static void ASSERT_EQ32(uint32_t a, uint32_t b)
919 {
920 #ifdef __MINGW32__
921 // This cast solves a linking problem with MinGW
922 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
923 #else
924 ASSERT_EQ(a, b);
925 #endif
926 }
927
928 static void ASSERT_NE32(uint32_t a, uint32_t b)
929 {
930 #ifdef __MINGW32__
931 // This cast solves a linking problem with MinGW
932 ASSERT_NE(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
933 #else
934 ASSERT_NE(a, b);
935 #endif
936 }
937
938 static void ASSERT_EQ64(uint64_t a, uint64_t b)
939 {
940 #ifdef __MINGW32__
941 // This cast solves a linking problem with MinGW
942 ASSERT_EQ(static_cast<unsigned int>(a), static_cast<unsigned int>(b));
943 #else
944 ASSERT_EQ(a, b);
945 #endif
946 }
947
948 static void ASSERT_NE64(uint64_t a, uint64_t b)
949 {
950 #ifdef __MINGW32__
951 // This cast solves a linking problem with MinGW
952 ASSERT_NE(static_cast<unsigned long long>(a), static_cast<unsigned long long>(b));
953 #else
954 ASSERT_NE(a, b);
955 #endif
956 }
957
958
959
960 TEST(Toolbox, EndiannessConversions16)
961 {
962 Endianness e = Toolbox::DetectEndianness();
963
964 for (unsigned int i = 0; i < 65536; i += 17)
965 {
966 uint16_t v = static_cast<uint16_t>(i);
967 ASSERT_EQ16(v, be16toh(htobe16(v)));
968 ASSERT_EQ16(v, le16toh(htole16(v)));
969
970 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&v);
971 if (bytes[0] != bytes[1])
972 {
973 ASSERT_NE16(v, le16toh(htobe16(v)));
974 ASSERT_NE16(v, be16toh(htole16(v)));
975 }
976 else
977 {
978 ASSERT_EQ16(v, le16toh(htobe16(v)));
979 ASSERT_EQ16(v, be16toh(htole16(v)));
980 }
981
982 switch (e)
983 {
984 case Endianness_Little:
985 ASSERT_EQ16(v, htole16(v));
986 if (bytes[0] != bytes[1])
987 {
988 ASSERT_NE16(v, htobe16(v));
989 }
990 else
991 {
992 ASSERT_EQ16(v, htobe16(v));
993 }
994 break;
995
996 case Endianness_Big:
997 ASSERT_EQ16(v, htobe16(v));
998 if (bytes[0] != bytes[1])
999 {
1000 ASSERT_NE16(v, htole16(v));
1001 }
1002 else
1003 {
1004 ASSERT_EQ16(v, htole16(v));
1005 }
1006 break;
1007
1008 default:
1009 throw OrthancException(ErrorCode_ParameterOutOfRange);
1010 }
1011 }
1012 }
1013
1014
1015 TEST(Toolbox, EndiannessConversions32)
1016 {
1017 const uint32_t v = 0xff010203u;
1018 const uint32_t r = 0x030201ffu;
1019 ASSERT_EQ32(v, be32toh(htobe32(v)));
1020 ASSERT_EQ32(v, le32toh(htole32(v)));
1021 ASSERT_NE32(v, be32toh(htole32(v)));
1022 ASSERT_NE32(v, le32toh(htobe32(v)));
1023
1024 switch (Toolbox::DetectEndianness())
1025 {
1026 case Endianness_Little:
1027 ASSERT_EQ32(r, htobe32(v));
1028 ASSERT_EQ32(v, htole32(v));
1029 ASSERT_EQ32(r, be32toh(v));
1030 ASSERT_EQ32(v, le32toh(v));
1031 break;
1032
1033 case Endianness_Big:
1034 ASSERT_EQ32(v, htobe32(v));
1035 ASSERT_EQ32(r, htole32(v));
1036 ASSERT_EQ32(v, be32toh(v));
1037 ASSERT_EQ32(r, le32toh(v));
1038 break;
1039
1040 default:
1041 throw OrthancException(ErrorCode_ParameterOutOfRange);
1042 }
1043 }
1044
1045
1046 TEST(Toolbox, EndiannessConversions64)
1047 {
1048 const uint64_t v = 0xff01020304050607LL;
1049 const uint64_t r = 0x07060504030201ffLL;
1050 ASSERT_EQ64(v, be64toh(htobe64(v)));
1051 ASSERT_EQ64(v, le64toh(htole64(v)));
1052 ASSERT_NE64(v, be64toh(htole64(v)));
1053 ASSERT_NE64(v, le64toh(htobe64(v)));
1054
1055 switch (Toolbox::DetectEndianness())
1056 {
1057 case Endianness_Little:
1058 ASSERT_EQ64(r, htobe64(v));
1059 ASSERT_EQ64(v, htole64(v));
1060 ASSERT_EQ64(r, be64toh(v));
1061 ASSERT_EQ64(v, le64toh(v));
1062 break;
1063
1064 case Endianness_Big:
1065 ASSERT_EQ64(v, htobe64(v));
1066 ASSERT_EQ64(r, htole64(v));
1067 ASSERT_EQ64(v, be64toh(v));
1068 ASSERT_EQ64(r, le64toh(v));
1069 break;
1070
1071 default:
1072 throw OrthancException(ErrorCode_ParameterOutOfRange);
1073 }
1074 }
1075
1076
1077 TEST(Toolbox, Now)
1078 {
1079 LOG(WARNING) << "Local time: " << SystemToolbox::GetNowIsoString(false);
1080 LOG(WARNING) << "Universal time: " << SystemToolbox::GetNowIsoString(true);
1081
1082 std::string date, time;
1083 SystemToolbox::GetNowDicom(date, time, false);
1084 LOG(WARNING) << "Local DICOM time: [" << date << "] [" << time << "]";
1085
1086 SystemToolbox::GetNowDicom(date, time, true);
1087 LOG(WARNING) << "Universal DICOM time: [" << date << "] [" << time << "]";
1088 }
1089
1090
1091
1092 #if ORTHANC_ENABLE_PUGIXML == 1
1093 TEST(Toolbox, Xml)
1094 {
1095 Json::Value a;
1096 a["hello"] = "world";
1097 a["42"] = 43;
1098 a["b"] = Json::arrayValue;
1099 a["b"].append("test");
1100 a["b"].append("test2");
1101
1102 std::string s;
1103 Toolbox::JsonToXml(s, a);
1104
1105 std::cout << s;
1106 }
1107 #endif
1108
1109
1110 #if !defined(_WIN32)
1111 TEST(Toolbox, ExecuteSystemCommand)
1112 {
1113 std::vector<std::string> args(2);
1114 args[0] = "Hello";
1115 args[1] = "World";
1116
1117 SystemToolbox::ExecuteSystemCommand("echo", args);
1118 }
1119 #endif
1120
1121
1122 TEST(Toolbox, IsInteger)
1123 {
1124 ASSERT_TRUE(Toolbox::IsInteger("00236"));
1125 ASSERT_TRUE(Toolbox::IsInteger("-0042"));
1126 ASSERT_TRUE(Toolbox::IsInteger("0"));
1127 ASSERT_TRUE(Toolbox::IsInteger("-0"));
1128
1129 ASSERT_FALSE(Toolbox::IsInteger(""));
1130 ASSERT_FALSE(Toolbox::IsInteger("42a"));
1131 ASSERT_FALSE(Toolbox::IsInteger("42-"));
1132 }
1133
1134
1135 TEST(Toolbox, StartsWith)
1136 {
1137 ASSERT_TRUE(Toolbox::StartsWith("hello world", ""));
1138 ASSERT_TRUE(Toolbox::StartsWith("hello world", "hello"));
1139 ASSERT_TRUE(Toolbox::StartsWith("hello world", "h"));
1140 ASSERT_FALSE(Toolbox::StartsWith("hello world", "H"));
1141 ASSERT_FALSE(Toolbox::StartsWith("h", "hello"));
1142 ASSERT_TRUE(Toolbox::StartsWith("h", "h"));
1143 ASSERT_FALSE(Toolbox::StartsWith("", "h"));
1144 }
1145
1146
1147 TEST(Toolbox, UriEncode)
1148 {
1149 std::string s;
1150
1151 // Unreserved characters must not be modified
1152 std::string t = "aAzZ09.-~_";
1153 Toolbox::UriEncode(s, t);
1154 ASSERT_EQ(t, s);
1155
1156 Toolbox::UriEncode(s, "!#$&'()*+,/:;=?@[]"); ASSERT_EQ("%21%23%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D", s);
1157 Toolbox::UriEncode(s, "%"); ASSERT_EQ("%25", s);
1158
1159 // Encode characters from UTF-8. This is the test string from the
1160 // file "../Resources/EncodingTests.py"
1161 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");
1162 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);
1163 }
1164
1165
1166 TEST(Toolbox, AccessJson)
1167 {
1168 Json::Value v = Json::arrayValue;
1169 ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope"));
1170
1171 v = Json::objectValue;
1172 ASSERT_EQ("nope", Toolbox::GetJsonStringField(v, "hello", "nope"));
1173 ASSERT_EQ(-10, Toolbox::GetJsonIntegerField(v, "hello", -10));
1174 ASSERT_EQ(10u, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10));
1175 ASSERT_TRUE(Toolbox::GetJsonBooleanField(v, "hello", true));
1176
1177 v["hello"] = "world";
1178 ASSERT_EQ("world", Toolbox::GetJsonStringField(v, "hello", "nope"));
1179 ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException);
1180 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException);
1181 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException);
1182
1183 v["hello"] = -42;
1184 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException);
1185 ASSERT_EQ(-42, Toolbox::GetJsonIntegerField(v, "hello", -10));
1186 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException);
1187 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException);
1188
1189 v["hello"] = 42;
1190 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException);
1191 ASSERT_EQ(42, Toolbox::GetJsonIntegerField(v, "hello", -10));
1192 ASSERT_EQ(42u, Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10));
1193 ASSERT_THROW(Toolbox::GetJsonBooleanField(v, "hello", true), OrthancException);
1194
1195 v["hello"] = false;
1196 ASSERT_THROW(Toolbox::GetJsonStringField(v, "hello", "nope"), OrthancException);
1197 ASSERT_THROW(Toolbox::GetJsonIntegerField(v, "hello", -10), OrthancException);
1198 ASSERT_THROW(Toolbox::GetJsonUnsignedIntegerField(v, "hello", 10), OrthancException);
1199 ASSERT_FALSE(Toolbox::GetJsonBooleanField(v, "hello", true));
1200 }
1201
1202
1203 TEST(Toolbox, LinesIterator)
1204 {
1205 std::string s;
1206
1207 {
1208 std::string content;
1209 Toolbox::LinesIterator it(content);
1210 ASSERT_FALSE(it.GetLine(s));
1211 }
1212
1213 {
1214 std::string content = "\n\r";
1215 Toolbox::LinesIterator it(content);
1216 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1217 ASSERT_FALSE(it.GetLine(s));
1218 }
1219
1220 {
1221 std::string content = "\n Hello \n\nWorld\n\n";
1222 Toolbox::LinesIterator it(content);
1223 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1224 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1225 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1226 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1227 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1228 ASSERT_FALSE(it.GetLine(s)); it.Next();
1229 ASSERT_FALSE(it.GetLine(s));
1230 }
1231
1232 {
1233 std::string content = "\r Hello \r\rWorld\r\r";
1234 Toolbox::LinesIterator it(content);
1235 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1236 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1237 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1238 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1239 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1240 ASSERT_FALSE(it.GetLine(s)); it.Next();
1241 ASSERT_FALSE(it.GetLine(s));
1242 }
1243
1244 {
1245 std::string content = "\n\r Hello \n\r\n\rWorld\n\r\n\r";
1246 Toolbox::LinesIterator it(content);
1247 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1248 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1249 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1250 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1251 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1252 ASSERT_FALSE(it.GetLine(s)); it.Next();
1253 ASSERT_FALSE(it.GetLine(s));
1254 }
1255
1256 {
1257 std::string content = "\r\n Hello \r\n\r\nWorld\r\n\r\n";
1258 Toolbox::LinesIterator it(content);
1259 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1260 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ(" Hello ", s);
1261 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1262 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("World", s);
1263 ASSERT_TRUE(it.GetLine(s)); it.Next(); ASSERT_EQ("", s);
1264 ASSERT_FALSE(it.GetLine(s)); it.Next();
1265 ASSERT_FALSE(it.GetLine(s));
1266 }
1267 }
1268
1269
1270 TEST(Toolbox, SubstituteVariables)
1271 {
1272 std::map<std::string, std::string> env;
1273 env["NOPE"] = "nope";
1274 env["WORLD"] = "world";
1275
1276 ASSERT_EQ("Hello world\r\nWorld \r\nDone world\r\n",
1277 Toolbox::SubstituteVariables(
1278 "Hello ${WORLD}\r\nWorld ${HELLO}\r\nDone ${WORLD}\r\n",
1279 env));
1280
1281 ASSERT_EQ("world A a B world C 'c' D {\"a\":\"b\"} E ",
1282 Toolbox::SubstituteVariables(
1283 "${WORLD} A ${WORLD2:-a} B ${WORLD:-b} C ${WORLD2:-\"'c'\"} D ${WORLD2:-'{\"a\":\"b\"}'} E ${WORLD2:-}",
1284 env));
1285
1286 SystemToolbox::GetEnvironmentVariables(env);
1287 ASSERT_TRUE(env.find("NOPE") == env.end());
1288
1289 // The "PATH" environment variable should always be available on
1290 // machines running the unit tests
1291 ASSERT_TRUE(env.find("PATH") != env.end() /* Case used by UNIX */ ||
1292 env.find("Path") != env.end() /* Case used by Windows */);
1293
1294 env["PATH"] = "hello";
1295 ASSERT_EQ("AhelloB",
1296 Toolbox::SubstituteVariables("A${PATH}B", env));
1297 }
1298
1299
1300 TEST(MetricsRegistry, Basic)
1301 {
1302 {
1303 MetricsRegistry m;
1304 m.SetEnabled(false);
1305 m.SetValue("hello.world", 42.5f);
1306
1307 std::string s;
1308 m.ExportPrometheusText(s);
1309 ASSERT_TRUE(s.empty());
1310 }
1311
1312 {
1313 MetricsRegistry m;
1314 m.Register("hello.world", MetricsType_Default);
1315
1316 std::string s;
1317 m.ExportPrometheusText(s);
1318 ASSERT_TRUE(s.empty());
1319 }
1320
1321 {
1322 MetricsRegistry m;
1323 m.SetValue("hello.world", 42.5f);
1324 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.world"));
1325 ASSERT_THROW(m.GetMetricsType("nope"), OrthancException);
1326
1327 std::string s;
1328 m.ExportPrometheusText(s);
1329
1330 std::vector<std::string> t;
1331 Toolbox::TokenizeString(t, s, '\n');
1332 ASSERT_EQ(2u, t.size());
1333 ASSERT_EQ("hello.world 42.5 ", t[0].substr(0, 17));
1334 ASSERT_TRUE(t[1].empty());
1335 }
1336
1337 {
1338 MetricsRegistry m;
1339 m.Register("hello.max", MetricsType_MaxOver10Seconds);
1340 m.SetValue("hello.max", 10);
1341 m.SetValue("hello.max", 20);
1342 m.SetValue("hello.max", -10);
1343 m.SetValue("hello.max", 5);
1344
1345 m.Register("hello.min", MetricsType_MinOver10Seconds);
1346 m.SetValue("hello.min", 10);
1347 m.SetValue("hello.min", 20);
1348 m.SetValue("hello.min", -10);
1349 m.SetValue("hello.min", 5);
1350
1351 m.Register("hello.default", MetricsType_Default);
1352 m.SetValue("hello.default", 10);
1353 m.SetValue("hello.default", 20);
1354 m.SetValue("hello.default", -10);
1355 m.SetValue("hello.default", 5);
1356
1357 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("hello.max"));
1358 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("hello.min"));
1359 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("hello.default"));
1360
1361 std::string s;
1362 m.ExportPrometheusText(s);
1363
1364 std::vector<std::string> t;
1365 Toolbox::TokenizeString(t, s, '\n');
1366 ASSERT_EQ(4u, t.size());
1367 ASSERT_TRUE(t[3].empty());
1368
1369 std::map<std::string, std::string> u;
1370 for (size_t i = 0; i < t.size() - 1; i++)
1371 {
1372 std::vector<std::string> v;
1373 Toolbox::TokenizeString(v, t[i], ' ');
1374 u[v[0]] = v[1];
1375 }
1376
1377 ASSERT_EQ("20", u["hello.max"]);
1378 ASSERT_EQ("-10", u["hello.min"]);
1379 ASSERT_EQ("5", u["hello.default"]);
1380 }
1381
1382 {
1383 MetricsRegistry m;
1384
1385 m.SetValue("a", 10);
1386 m.SetValue("b", 10, MetricsType_MinOver10Seconds);
1387
1388 m.Register("c", MetricsType_MaxOver10Seconds);
1389 m.SetValue("c", 10, MetricsType_MinOver10Seconds);
1390
1391 m.Register("d", MetricsType_MaxOver10Seconds);
1392 m.Register("d", MetricsType_Default);
1393
1394 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("a"));
1395 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b"));
1396 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("c"));
1397 ASSERT_EQ(MetricsType_Default, m.GetMetricsType("d"));
1398 }
1399
1400 {
1401 MetricsRegistry m;
1402
1403 {
1404 MetricsRegistry::Timer t1(m, "a");
1405 MetricsRegistry::Timer t2(m, "b", MetricsType_MinOver10Seconds);
1406 }
1407
1408 ASSERT_EQ(MetricsType_MaxOver10Seconds, m.GetMetricsType("a"));
1409 ASSERT_EQ(MetricsType_MinOver10Seconds, m.GetMetricsType("b"));
1410 }
1411 }
1412
1413
1414 int main(int argc, char **argv) 120 int main(int argc, char **argv)
1415 { 121 {
1416 Logging::Initialize(); 122 Logging::Initialize();
1417 Toolbox::InitializeGlobalLocale(NULL); 123 Toolbox::InitializeGlobalLocale(NULL);
1418 Logging::EnableInfoLevel(true); 124 Logging::EnableInfoLevel(true);