comparison OrthancFramework/UnitTestsSources/ToolboxTests.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents UnitTestsSources/ToolboxTests.cpp@27628b0f6ada
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1
35 # include <OrthancFramework.h>
36 #endif
37
38 #include "PrecompiledHeadersUnitTests.h"
39 #include "gtest/gtest.h"
40 #include "../Core/Compatibility.h"
41 #include "../Core/IDynamicObject.h"
42 #include "../Core/OrthancException.h"
43 #include "../Core/Toolbox.h"
44
45 using namespace Orthanc;
46
47 TEST(Toolbox, Base64_allByteValues)
48 {
49 std::string toEncode;
50 std::string base64Result;
51 std::string decodedResult;
52
53 size_t size = 2*256;
54 toEncode.reserve(size);
55 for (size_t i = 0; i < size; i++)
56 toEncode.push_back(i % 256);
57
58 Toolbox::EncodeBase64(base64Result, toEncode);
59 Toolbox::DecodeBase64(decodedResult, base64Result);
60
61 ASSERT_EQ(toEncode, decodedResult);
62 }
63
64 TEST(Toolbox, Base64_multipleSizes)
65 {
66 std::string toEncode;
67 std::string base64Result;
68 std::string decodedResult;
69
70 for (size_t size = 0; size <= 5; size++)
71 {
72 printf("base64, testing size %zu\n", size);
73 toEncode.clear();
74 toEncode.reserve(size);
75 for (size_t i = 0; i < size; i++)
76 toEncode.push_back(i % 256);
77
78 Toolbox::EncodeBase64(base64Result, toEncode);
79 Toolbox::DecodeBase64(decodedResult, base64Result);
80
81 ASSERT_EQ(toEncode, decodedResult);
82 }
83 }
84
85 static std::string EncodeBase64Bis(const std::string& s)
86 {
87 std::string result;
88 Toolbox::EncodeBase64(result, s);
89 return result;
90 }
91
92
93 TEST(Toolbox, Base64)
94 {
95 ASSERT_EQ("", EncodeBase64Bis(""));
96 ASSERT_EQ("YQ==", EncodeBase64Bis("a"));
97
98 const std::string hello = "SGVsbG8gd29ybGQ=";
99 ASSERT_EQ(hello, EncodeBase64Bis("Hello world"));
100
101 std::string decoded;
102 Toolbox::DecodeBase64(decoded, hello);
103 ASSERT_EQ("Hello world", decoded);
104
105 // Invalid character
106 ASSERT_THROW(Toolbox::DecodeBase64(decoded, "?"), OrthancException);
107
108 // All the allowed characters
109 Toolbox::DecodeBase64(decoded, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
110 }
111
112
113 #if 0 // enable only when compiling in Release with a C++ 11 compiler
114 #include <chrono> // I had troubles to link with boost::chrono ...
115
116 TEST(Toolbox, Base64_largeString)
117 {
118 std::string toEncode;
119 std::string base64Result;
120 std::string decodedResult;
121
122 size_t size = 10 * 1024 * 1024;
123 toEncode.reserve(size);
124 for (size_t i = 0; i < size; i++)
125 toEncode.push_back(i % 256);
126
127 std::chrono::high_resolution_clock::time_point start;
128 std::chrono::high_resolution_clock::time_point afterEncoding;
129 std::chrono::high_resolution_clock::time_point afterDecoding;
130
131 start = std::chrono::high_resolution_clock::now();
132 Orthanc::Toolbox::EncodeBase64(base64Result, toEncode);
133 afterEncoding = std::chrono::high_resolution_clock::now();
134 Orthanc::Toolbox::DecodeBase64(decodedResult, base64Result);
135 afterDecoding = std::chrono::high_resolution_clock::now();
136
137 ASSERT_EQ(toEncode, decodedResult);
138
139 printf("encoding took %zu ms\n", (std::chrono::duration_cast<std::chrono::milliseconds>(afterEncoding - start)));
140 printf("decoding took %zu ms\n", (std::chrono::duration_cast<std::chrono::milliseconds>(afterDecoding - afterEncoding)));
141 }
142 #endif
143
144
145 TEST(Toolbox, LargeHexadecimalToDecimal)
146 {
147 // https://stackoverflow.com/a/16967286/881731
148 ASSERT_EQ(
149 "166089946137986168535368849184301740204613753693156360462575217560130904921953976324839782808018277000296027060873747803291797869684516494894741699267674246881622658654267131250470956587908385447044319923040838072975636163137212887824248575510341104029461758594855159174329892125993844566497176102668262139513",
150 Toolbox::LargeHexadecimalToDecimal("EC851A69B8ACD843164E10CFF70CF9E86DC2FEE3CF6F374B43C854E3342A2F1AC3E30C741CC41E679DF6D07CE6FA3A66083EC9B8C8BF3AF05D8BDBB0AA6Cb3ef8c5baa2a5e531ba9e28592f99e0fe4f95169a6c63f635d0197e325c5ec76219b907e4ebdcd401fb1986e4e3ca661ff73e7e2b8fd9988e753b7042b2bbca76679"));
151
152 ASSERT_EQ("0", Toolbox::LargeHexadecimalToDecimal(""));
153 ASSERT_EQ("0", Toolbox::LargeHexadecimalToDecimal("0"));
154 ASSERT_EQ("0", Toolbox::LargeHexadecimalToDecimal("0000"));
155 ASSERT_EQ("255", Toolbox::LargeHexadecimalToDecimal("00000ff"));
156
157 ASSERT_THROW(Toolbox::LargeHexadecimalToDecimal("g"), Orthanc::OrthancException);
158 }
159
160
161 TEST(Toolbox, GenerateDicomPrivateUniqueIdentifier)
162 {
163 std::string s = Toolbox::GenerateDicomPrivateUniqueIdentifier();
164 ASSERT_EQ("2.25.", s.substr(0, 5));
165 }
166
167
168 TEST(Toolbox, UniquePtr)
169 {
170 std::unique_ptr<int> i(new int(42));
171 ASSERT_EQ(42, *i);
172
173 std::unique_ptr<SingleValueObject<int> > j(new SingleValueObject<int>(42));
174 ASSERT_EQ(42, j->GetValue());
175 }