Mercurial > hg > orthanc
annotate UnitTests/main.cpp @ 98:44c510d1c827 Orthanc-0.2.1
close
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 29 Apr 2013 12:54:45 +0200 |
parents | 8517e2c44283 |
children | 27dc762e3dc8 |
rev | line source |
---|---|
0 | 1 #include "gtest/gtest.h" |
2 | |
3 #include <ctype.h> | |
4 | |
5 #include "../Core/Compression/ZlibCompressor.h" | |
6 #include "../Core/DicomFormat/DicomTag.h" | |
7 #include "../Core/FileStorage.h" | |
63 | 8 #include "../OrthancCppClient/HttpClient.h" |
0 | 9 #include "../Core/HttpServer/HttpHandler.h" |
63 | 10 #include "../Core/OrthancException.h" |
0 | 11 #include "../Core/Toolbox.h" |
12 #include "../Core/Uuid.h" | |
63 | 13 #include "../OrthancServer/FromDcmtkBridge.h" |
14 #include "../OrthancServer/OrthancInitialization.h" | |
0 | 15 |
63 | 16 using namespace Orthanc; |
0 | 17 |
18 | |
19 TEST(Uuid, Generation) | |
20 { | |
21 for (int i = 0; i < 10; i++) | |
22 { | |
23 std::string s = Toolbox::GenerateUuid(); | |
24 ASSERT_TRUE(Toolbox::IsUuid(s)); | |
25 } | |
26 } | |
27 | |
28 TEST(Uuid, Test) | |
29 { | |
30 ASSERT_FALSE(Toolbox::IsUuid("")); | |
31 ASSERT_FALSE(Toolbox::IsUuid("012345678901234567890123456789012345")); | |
32 ASSERT_TRUE(Toolbox::IsUuid("550e8400-e29b-41d4-a716-446655440000")); | |
33 } | |
34 | |
35 TEST(Zlib, Basic) | |
36 { | |
37 std::string s = Toolbox::GenerateUuid(); | |
38 s = s + s + s + s; | |
39 | |
40 std::string compressed; | |
41 ZlibCompressor c; | |
42 c.Compress(compressed, s); | |
43 | |
44 std::string uncompressed; | |
45 c.Uncompress(uncompressed, compressed); | |
46 | |
47 ASSERT_EQ(s.size(), uncompressed.size()); | |
48 ASSERT_EQ(0, memcmp(&s[0], &uncompressed[0], s.size())); | |
49 } | |
50 | |
51 TEST(Zlib, Empty) | |
52 { | |
53 std::string s = ""; | |
54 | |
55 std::string compressed; | |
56 ZlibCompressor c; | |
57 c.Compress(compressed, s); | |
58 | |
59 std::string uncompressed; | |
60 c.Uncompress(uncompressed, compressed); | |
61 | |
62 ASSERT_EQ(0u, uncompressed.size()); | |
63 } | |
64 | |
65 TEST(ParseGetQuery, Basic) | |
66 { | |
67 HttpHandler::Arguments a; | |
68 HttpHandler::ParseGetQuery(a, "aaa=baaa&bb=a&aa=c"); | |
69 ASSERT_EQ(3u, a.size()); | |
70 ASSERT_EQ(a["aaa"], "baaa"); | |
71 ASSERT_EQ(a["bb"], "a"); | |
72 ASSERT_EQ(a["aa"], "c"); | |
73 } | |
74 | |
75 TEST(ParseGetQuery, BasicEmpty) | |
76 { | |
77 HttpHandler::Arguments a; | |
78 HttpHandler::ParseGetQuery(a, "aaa&bb=aa&aa"); | |
79 ASSERT_EQ(3u, a.size()); | |
80 ASSERT_EQ(a["aaa"], ""); | |
81 ASSERT_EQ(a["bb"], "aa"); | |
82 ASSERT_EQ(a["aa"], ""); | |
83 } | |
84 | |
85 TEST(ParseGetQuery, Single) | |
86 { | |
87 HttpHandler::Arguments a; | |
88 HttpHandler::ParseGetQuery(a, "aaa=baaa"); | |
89 ASSERT_EQ(1u, a.size()); | |
90 ASSERT_EQ(a["aaa"], "baaa"); | |
91 } | |
92 | |
93 TEST(ParseGetQuery, SingleEmpty) | |
94 { | |
95 HttpHandler::Arguments a; | |
96 HttpHandler::ParseGetQuery(a, "aaa"); | |
97 ASSERT_EQ(1u, a.size()); | |
98 ASSERT_EQ(a["aaa"], ""); | |
99 } | |
100 | |
101 TEST(FileStorage, Basic) | |
102 { | |
103 FileStorage s("FileStorageUnitTests"); | |
104 | |
105 std::string data = Toolbox::GenerateUuid(); | |
106 std::string uid = s.Create(data); | |
107 std::string d; | |
108 s.ReadFile(d, uid); | |
109 ASSERT_EQ(d.size(), data.size()); | |
110 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size())); | |
111 } | |
112 | |
113 TEST(FileStorage, EndToEnd) | |
114 { | |
115 FileStorage s("FileStorageUnitTests"); | |
116 s.Clear(); | |
117 | |
118 std::list<std::string> u; | |
119 for (unsigned int i = 0; i < 10; i++) | |
120 { | |
121 u.push_back(s.Create(Toolbox::GenerateUuid())); | |
122 } | |
123 | |
124 std::set<std::string> ss; | |
125 s.ListAllFiles(ss); | |
126 ASSERT_EQ(10u, ss.size()); | |
127 | |
128 unsigned int c = 0; | |
129 for (std::list<std::string>::iterator | |
130 i = u.begin(); i != u.end(); i++, c++) | |
131 { | |
132 ASSERT_TRUE(ss.find(*i) != ss.end()); | |
133 if (c < 5) | |
134 s.Remove(*i); | |
135 } | |
136 | |
137 s.ListAllFiles(ss); | |
138 ASSERT_EQ(5u, ss.size()); | |
139 | |
140 s.Clear(); | |
141 s.ListAllFiles(ss); | |
142 ASSERT_EQ(0u, ss.size()); | |
143 } | |
144 | |
145 | |
146 TEST(DicomFormat, Tag) | |
147 { | |
148 ASSERT_EQ("PatientName", FromDcmtkBridge::GetName(DicomTag(0x0010, 0x0010))); | |
149 | |
150 DicomTag t = FromDcmtkBridge::FindTag("SeriesDescription"); | |
151 ASSERT_EQ(0x0008, t.GetGroup()); | |
152 ASSERT_EQ(0x103E, t.GetElement()); | |
153 } | |
154 | |
155 | |
156 TEST(Uri, SplitUriComponents) | |
157 { | |
158 UriComponents c; | |
159 Toolbox::SplitUriComponents(c, "/cou/hello/world"); | |
160 ASSERT_EQ(3u, c.size()); | |
161 ASSERT_EQ("cou", c[0]); | |
162 ASSERT_EQ("hello", c[1]); | |
163 ASSERT_EQ("world", c[2]); | |
164 | |
165 Toolbox::SplitUriComponents(c, "/cou/hello/world/"); | |
166 ASSERT_EQ(3u, c.size()); | |
167 ASSERT_EQ("cou", c[0]); | |
168 ASSERT_EQ("hello", c[1]); | |
169 ASSERT_EQ("world", c[2]); | |
170 | |
171 Toolbox::SplitUriComponents(c, "/cou/hello/world/a"); | |
172 ASSERT_EQ(4u, c.size()); | |
173 ASSERT_EQ("cou", c[0]); | |
174 ASSERT_EQ("hello", c[1]); | |
175 ASSERT_EQ("world", c[2]); | |
176 ASSERT_EQ("a", c[3]); | |
177 | |
178 Toolbox::SplitUriComponents(c, "/"); | |
179 ASSERT_EQ(0u, c.size()); | |
180 | |
181 Toolbox::SplitUriComponents(c, "/hello"); | |
182 ASSERT_EQ(1u, c.size()); | |
183 ASSERT_EQ("hello", c[0]); | |
184 | |
185 Toolbox::SplitUriComponents(c, "/hello/"); | |
186 ASSERT_EQ(1u, c.size()); | |
187 ASSERT_EQ("hello", c[0]); | |
188 | |
63 | 189 ASSERT_THROW(Toolbox::SplitUriComponents(c, ""), OrthancException); |
190 ASSERT_THROW(Toolbox::SplitUriComponents(c, "a"), OrthancException); | |
0 | 191 } |
192 | |
193 | |
194 TEST(Uri, Child) | |
195 { | |
196 UriComponents c1; Toolbox::SplitUriComponents(c1, "/hello/world"); | |
197 UriComponents c2; Toolbox::SplitUriComponents(c2, "/hello/hello"); | |
198 UriComponents c3; Toolbox::SplitUriComponents(c3, "/hello"); | |
199 UriComponents c4; Toolbox::SplitUriComponents(c4, "/world"); | |
200 UriComponents c5; Toolbox::SplitUriComponents(c5, "/"); | |
201 | |
202 ASSERT_TRUE(Toolbox::IsChildUri(c1, c1)); | |
203 ASSERT_FALSE(Toolbox::IsChildUri(c1, c2)); | |
204 ASSERT_FALSE(Toolbox::IsChildUri(c1, c3)); | |
205 ASSERT_FALSE(Toolbox::IsChildUri(c1, c4)); | |
206 ASSERT_FALSE(Toolbox::IsChildUri(c1, c5)); | |
207 | |
208 ASSERT_FALSE(Toolbox::IsChildUri(c2, c1)); | |
209 ASSERT_TRUE(Toolbox::IsChildUri(c2, c2)); | |
210 ASSERT_FALSE(Toolbox::IsChildUri(c2, c3)); | |
211 ASSERT_FALSE(Toolbox::IsChildUri(c2, c4)); | |
212 ASSERT_FALSE(Toolbox::IsChildUri(c2, c5)); | |
213 | |
214 ASSERT_TRUE(Toolbox::IsChildUri(c3, c1)); | |
215 ASSERT_TRUE(Toolbox::IsChildUri(c3, c2)); | |
216 ASSERT_TRUE(Toolbox::IsChildUri(c3, c3)); | |
217 ASSERT_FALSE(Toolbox::IsChildUri(c3, c4)); | |
218 ASSERT_FALSE(Toolbox::IsChildUri(c3, c5)); | |
219 | |
220 ASSERT_FALSE(Toolbox::IsChildUri(c4, c1)); | |
221 ASSERT_FALSE(Toolbox::IsChildUri(c4, c2)); | |
222 ASSERT_FALSE(Toolbox::IsChildUri(c4, c3)); | |
223 ASSERT_TRUE(Toolbox::IsChildUri(c4, c4)); | |
224 ASSERT_FALSE(Toolbox::IsChildUri(c4, c5)); | |
225 | |
226 ASSERT_TRUE(Toolbox::IsChildUri(c5, c1)); | |
227 ASSERT_TRUE(Toolbox::IsChildUri(c5, c2)); | |
228 ASSERT_TRUE(Toolbox::IsChildUri(c5, c3)); | |
229 ASSERT_TRUE(Toolbox::IsChildUri(c5, c4)); | |
230 ASSERT_TRUE(Toolbox::IsChildUri(c5, c5)); | |
231 } | |
232 | |
233 TEST(Uri, AutodetectMimeType) | |
234 { | |
235 ASSERT_EQ("", Toolbox::AutodetectMimeType("../NOTES")); | |
236 ASSERT_EQ("", Toolbox::AutodetectMimeType("")); | |
237 ASSERT_EQ("", Toolbox::AutodetectMimeType("/")); | |
238 ASSERT_EQ("", Toolbox::AutodetectMimeType("a/a")); | |
239 | |
240 ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("../NOTES.txt")); | |
241 ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("../coucou.xml/NOTES.txt")); | |
242 ASSERT_EQ("text/xml", Toolbox::AutodetectMimeType("../.xml")); | |
243 | |
244 ASSERT_EQ("application/javascript", Toolbox::AutodetectMimeType("NOTES.js")); | |
245 ASSERT_EQ("application/json", Toolbox::AutodetectMimeType("NOTES.json")); | |
246 ASSERT_EQ("application/pdf", Toolbox::AutodetectMimeType("NOTES.pdf")); | |
247 ASSERT_EQ("text/css", Toolbox::AutodetectMimeType("NOTES.css")); | |
248 ASSERT_EQ("text/html", Toolbox::AutodetectMimeType("NOTES.html")); | |
249 ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("NOTES.txt")); | |
250 ASSERT_EQ("text/xml", Toolbox::AutodetectMimeType("NOTES.xml")); | |
251 ASSERT_EQ("image/gif", Toolbox::AutodetectMimeType("NOTES.gif")); | |
252 ASSERT_EQ("image/jpeg", Toolbox::AutodetectMimeType("NOTES.jpg")); | |
253 ASSERT_EQ("image/jpeg", Toolbox::AutodetectMimeType("NOTES.jpeg")); | |
254 ASSERT_EQ("image/png", Toolbox::AutodetectMimeType("NOTES.png")); | |
255 } | |
256 | |
22 | 257 TEST(Toolbox, ComputeMD5) |
258 { | |
259 std::string s; | |
260 | |
261 // # echo -n "Hello" | md5sum | |
262 | |
263 Toolbox::ComputeMD5(s, "Hello"); | |
264 ASSERT_EQ("8b1a9953c4611296a827abf8c47804d7", s); | |
265 Toolbox::ComputeMD5(s, ""); | |
266 ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s); | |
267 } | |
268 | |
24 | 269 TEST(Toolbox, Base64) |
270 { | |
271 ASSERT_EQ("", Toolbox::EncodeBase64("")); | |
272 ASSERT_EQ("YQ==", Toolbox::EncodeBase64("a")); | |
273 ASSERT_EQ("SGVsbG8gd29ybGQ=", Toolbox::EncodeBase64("Hello world")); | |
274 } | |
275 | |
87
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
276 TEST(Toolbox, PathToExecutable) |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
277 { |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
278 printf("[%s]\n", Toolbox::GetPathToExecutable().c_str()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
279 printf("[%s]\n", Toolbox::GetDirectoryOfExecutable().c_str()); |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
280 } |
8517e2c44283
path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
281 |
0 | 282 int main(int argc, char **argv) |
283 { | |
63 | 284 OrthancInitialize(); |
0 | 285 ::testing::InitGoogleTest(&argc, argv); |
286 int result = RUN_ALL_TESTS(); | |
63 | 287 OrthancFinalize(); |
0 | 288 return result; |
289 } |