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