Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestPartResult
#include <memory> // for make_shared, allocator, shared_ptr
#include <string> // for string
#include "caosdb/authentication.h" // for PlainPasswordAuthenticator
#include "caosdb/connection.h" // for InsecureCaosDBConnectionConfig
#include "caosdb/exceptions.h" // for AuthenticationError, ConnectionError
#include "caosdb/info.h" // for VersionInfo
#include "caosdb/utils.h" // for get_env_var
#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ, EXPECT_THROW
#include "caosdb_test_utility.h"
namespace caosdb::connection {
using caosdb::authentication::PlainPasswordAuthenticator;
/**
* Return a fresh CaosDBConnection pointer. The caller has the ownership.
*/
inline auto create_test_connection() -> CaosDBConnection * {
auto port_str =
caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443");
auto port = std::stoi(port_str);
const auto host =
caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost");
const auto path =
caosdb::utils::get_env_var("CAOSDB_SERVER_CERT", std::string());
const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin");
const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb");
auto auth = std::make_shared<PlainPasswordAuthenticator>(user, password);
auto ssloptions = std::make_shared<PemFileCACertProvider>(path);
auto config =
std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions, auth);
return new CaosDBConnection(config);
};
/**
* Singleton which holds a single global CaosDBConnection
*/
class ConnectionProvider {
private:
std::shared_ptr<CaosDBConnection> connection;
ConnectionProvider() : connection(create_test_connection()){};
public:
static ConnectionProvider &GetInstance() {
static ConnectionProvider instance;
return instance;
};
inline auto GetConnection() -> std::shared_ptr<CaosDBConnection> & {
return this->connection;
}
ConnectionProvider(ConnectionProvider const &) = delete;
void operator=(ConnectionProvider const &) = delete;
};
/**
* Return a connection for testing purposes.
*/
inline auto get_test_connection() -> const std::shared_ptr<CaosDBConnection> & {
return ConnectionProvider::GetInstance().GetConnection();
};
} // namespace caosdb::connection