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