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 Affero 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 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "../Plugins/PostgreSQLIndex.h"
23
24 #include <Logging.h>
25 #include <Toolbox.h>
26 #include <gtest/gtest.h>
27
28 OrthancDatabases::PostgreSQLParameters globalParameters_;
29
30 #include "../../Framework/Plugins/IndexUnitTests.h"
31 #include "../../Framework/PostgreSQL/PostgreSQLDatabase.h"
32
33
34 #if ORTHANC_POSTGRESQL_STATIC == 1
35 # include <c.h> // PostgreSQL includes
36
TEST(PostgreSQL,Version)37 TEST(PostgreSQL, Version)
38 {
39 ASSERT_STREQ("13.1", PG_VERSION);
40 }
41 #endif
42
43
TEST(PostgreSQLParameters,Basic)44 TEST(PostgreSQLParameters, Basic)
45 {
46 OrthancDatabases::PostgreSQLParameters p;
47 p.SetDatabase("world");
48
49 ASSERT_EQ("postgresql://localhost:5432/world", p.GetConnectionUri());
50
51 p.ResetDatabase();
52 ASSERT_EQ("postgresql://localhost:5432/", p.GetConnectionUri());
53
54 p.SetDatabase("hello");
55 ASSERT_EQ("postgresql://localhost:5432/hello", p.GetConnectionUri());
56
57 p.SetHost("server");
58 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
59
60 p.SetPortNumber(1234);
61 ASSERT_EQ("postgresql://server:1234/hello", p.GetConnectionUri());
62
63 p.SetPortNumber(5432);
64 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
65
66 p.SetUsername("user");
67 p.SetPassword("pass");
68 ASSERT_EQ("postgresql://user:pass@server:5432/hello", p.GetConnectionUri());
69
70 p.SetPassword("");
71 ASSERT_EQ("postgresql://user@server:5432/hello", p.GetConnectionUri());
72
73 p.SetUsername("");
74 p.SetPassword("pass");
75 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
76
77 p.SetUsername("");
78 p.SetPassword("");
79 ASSERT_EQ("postgresql://server:5432/hello", p.GetConnectionUri());
80
81 p.SetConnectionUri("hello://world");
82 ASSERT_EQ("hello://world", p.GetConnectionUri());
83 }
84
85
TEST(PostgreSQLIndex,Lock)86 TEST(PostgreSQLIndex, Lock)
87 {
88 OrthancDatabases::PostgreSQLParameters noLock = globalParameters_;
89 noLock.SetLock(false);
90
91 OrthancDatabases::PostgreSQLParameters lock = globalParameters_;
92 lock.SetLock(true);
93
94 OrthancDatabases::PostgreSQLIndex db1(NULL, noLock);
95 db1.SetClearAll(true);
96
97 std::unique_ptr<OrthancDatabases::DatabaseManager> manager1(OrthancDatabases::IndexBackend::CreateSingleDatabaseManager(db1));
98
99 {
100 OrthancDatabases::PostgreSQLIndex db2(NULL, lock);
101 std::unique_ptr<OrthancDatabases::DatabaseManager> manager2(OrthancDatabases::IndexBackend::CreateSingleDatabaseManager(db2));
102
103 OrthancDatabases::PostgreSQLIndex db3(NULL, lock);
104 ASSERT_THROW(OrthancDatabases::IndexBackend::CreateSingleDatabaseManager(db3), Orthanc::OrthancException);
105 }
106
107 OrthancDatabases::PostgreSQLIndex db4(NULL, lock);
108 std::unique_ptr<OrthancDatabases::DatabaseManager> manager4(OrthancDatabases::IndexBackend::CreateSingleDatabaseManager(db4));
109 }
110
111
main(int argc,char ** argv)112 int main(int argc, char **argv)
113 {
114 if (argc < 6)
115 {
116 std::cerr << "Usage: " << argv[0] << " <host> <port> <username> <password> <database>"
117 << std::endl << std::endl
118 << "Example: " << argv[0] << " localhost 5432 postgres postgres orthanctest"
119 << std::endl << std::endl;
120 return -1;
121 }
122
123 globalParameters_.SetHost(argv[1]);
124 globalParameters_.SetPortNumber(boost::lexical_cast<uint16_t>(argv[2]));
125 globalParameters_.SetUsername(argv[3]);
126 globalParameters_.SetPassword(argv[4]);
127 globalParameters_.SetDatabase(argv[5]);
128
129 ::testing::InitGoogleTest(&argc, argv);
130 Orthanc::Toolbox::InitializeOpenSsl();
131 Orthanc::Logging::Initialize();
132 Orthanc::Logging::EnableInfoLevel(true);
133 Orthanc::Logging::EnableTraceLevel(true);
134
135 int result = RUN_ALL_TESTS();
136
137 Orthanc::Logging::Finalize();
138 Orthanc::Toolbox::FinalizeOpenSsl();
139
140 return result;
141 }
142