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