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