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-2021 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 Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
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
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program. If not, see
19  * <http://www.gnu.org/licenses/>.
20  **/
21 
22 
23 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1
24 // Must be the first to be sure to use the Orthanc framework shared library
25 #  include <OrthancFramework.h>
26 #endif
27 
28 #include <gtest/gtest.h>
29 
30 #include "../Sources/FileStorage/FilesystemStorage.h"
31 #include "../Sources/FileStorage/StorageAccessor.h"
32 #include "../Sources/HttpServer/BufferHttpSender.h"
33 #include "../Sources/HttpServer/FilesystemHttpSender.h"
34 #include "../Sources/Logging.h"
35 #include "../Sources/OrthancException.h"
36 #include "../Sources/Toolbox.h"
37 
38 #include <ctype.h>
39 
40 
41 using namespace Orthanc;
42 
43 
StringToVector(std::vector<uint8_t> & v,const std::string & s)44 static void StringToVector(std::vector<uint8_t>& v,
45                            const std::string& s)
46 {
47   v.resize(s.size());
48   for (size_t i = 0; i < s.size(); i++)
49     v[i] = s[i];
50 }
51 
52 
TEST(FilesystemStorage,Basic)53 TEST(FilesystemStorage, Basic)
54 {
55   FilesystemStorage s("UnitTestsStorage");
56 
57   std::string data = Toolbox::GenerateUuid();
58   std::string uid = Toolbox::GenerateUuid();
59   s.Create(uid.c_str(), &data[0], data.size(), FileContentType_Unknown);
60   std::string d;
61   {
62     std::unique_ptr<IMemoryBuffer> buffer(s.Read(uid, FileContentType_Unknown));
63     buffer->MoveToString(d);
64   }
65   ASSERT_EQ(d.size(), data.size());
66   ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
67   ASSERT_EQ(s.GetSize(uid), data.size());
68 }
69 
TEST(FilesystemStorage,Basic2)70 TEST(FilesystemStorage, Basic2)
71 {
72   FilesystemStorage s("UnitTestsStorage");
73 
74   std::vector<uint8_t> data;
75   StringToVector(data, Toolbox::GenerateUuid());
76   std::string uid = Toolbox::GenerateUuid();
77   s.Create(uid.c_str(), &data[0], data.size(), FileContentType_Unknown);
78   std::string d;
79   {
80     std::unique_ptr<IMemoryBuffer> buffer(s.Read(uid, FileContentType_Unknown));
81     buffer->MoveToString(d);
82   }
83   ASSERT_EQ(d.size(), data.size());
84   ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
85   ASSERT_EQ(s.GetSize(uid), data.size());
86 }
87 
TEST(FilesystemStorage,EndToEnd)88 TEST(FilesystemStorage, EndToEnd)
89 {
90   FilesystemStorage s("UnitTestsStorage");
91   s.Clear();
92 
93   std::list<std::string> u;
94   for (unsigned int i = 0; i < 10; i++)
95   {
96     std::string t = Toolbox::GenerateUuid();
97     std::string uid = Toolbox::GenerateUuid();
98     s.Create(uid.c_str(), &t[0], t.size(), FileContentType_Unknown);
99     u.push_back(uid);
100   }
101 
102   std::set<std::string> ss;
103   s.ListAllFiles(ss);
104   ASSERT_EQ(10u, ss.size());
105 
106   unsigned int c = 0;
107   for (std::list<std::string>::const_iterator
108          i = u.begin(); i != u.end(); ++i, c++)
109   {
110     ASSERT_TRUE(ss.find(*i) != ss.end());
111     if (c < 5)
112       s.Remove(*i, FileContentType_Unknown);
113   }
114 
115   s.ListAllFiles(ss);
116   ASSERT_EQ(5u, ss.size());
117 
118   s.Clear();
119   s.ListAllFiles(ss);
120   ASSERT_EQ(0u, ss.size());
121 }
122 
123 
TEST(StorageAccessor,NoCompression)124 TEST(StorageAccessor, NoCompression)
125 {
126   FilesystemStorage s("UnitTestsStorage");
127   StorageAccessor accessor(s);
128 
129   std::string data = "Hello world";
130   FileInfo info = accessor.Write(data, FileContentType_Dicom, CompressionType_None, true);
131 
132   std::string r;
133   accessor.Read(r, info);
134 
135   ASSERT_EQ(data, r);
136   ASSERT_EQ(CompressionType_None, info.GetCompressionType());
137   ASSERT_EQ(11u, info.GetUncompressedSize());
138   ASSERT_EQ(11u, info.GetCompressedSize());
139   ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
140   ASSERT_EQ("3e25960a79dbc69b674cd4ec67a72c62", info.GetUncompressedMD5());
141   ASSERT_EQ(info.GetUncompressedMD5(), info.GetCompressedMD5());
142 }
143 
144 
TEST(StorageAccessor,Compression)145 TEST(StorageAccessor, Compression)
146 {
147   FilesystemStorage s("UnitTestsStorage");
148   StorageAccessor accessor(s);
149 
150   std::string data = "Hello world";
151   FileInfo info = accessor.Write(data, FileContentType_Dicom, CompressionType_ZlibWithSize, true);
152 
153   std::string r;
154   accessor.Read(r, info);
155 
156   ASSERT_EQ(data, r);
157   ASSERT_EQ(CompressionType_ZlibWithSize, info.GetCompressionType());
158   ASSERT_EQ(11u, info.GetUncompressedSize());
159   ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
160   ASSERT_EQ("3e25960a79dbc69b674cd4ec67a72c62", info.GetUncompressedMD5());
161   ASSERT_NE(info.GetUncompressedMD5(), info.GetCompressedMD5());
162 }
163 
164 
TEST(StorageAccessor,Mix)165 TEST(StorageAccessor, Mix)
166 {
167   FilesystemStorage s("UnitTestsStorage");
168   StorageAccessor accessor(s);
169 
170   std::string r;
171   std::string compressedData = "Hello";
172   std::string uncompressedData = "HelloWorld";
173 
174   FileInfo compressedInfo = accessor.Write(compressedData, FileContentType_Dicom, CompressionType_ZlibWithSize, false);
175   FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom, CompressionType_None, false);
176 
177   accessor.Read(r, compressedInfo);
178   ASSERT_EQ(compressedData, r);
179 
180   accessor.Read(r, uncompressedInfo);
181   ASSERT_EQ(uncompressedData, r);
182   ASSERT_NE(compressedData, r);
183 
184   /*
185   // This test is too slow on Windows
186   accessor.SetCompressionForNextOperations(CompressionType_ZlibWithSize);
187   ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid(), FileContentType_Unknown), OrthancException);
188   */
189 }
190