Skip to content
Snippets Groups Projects
Verified Commit 0853915f authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: test c interface

parent 8bea800f
No related branches found
No related tags found
1 merge request!1F extern c
Pipeline #10490 passed
[requires]
caosdb/0.0.4
caosdb/0.0.5
gtest/1.11.0
[generators]
......
......@@ -24,6 +24,7 @@
set(test_cases
test_connection
test_transaction
test_ccaosdb
)
#######################################################
......
/*
* 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 <memory> // for allocator, unique_ptr
#include "ccaosdb.h" // for caosdb_connection_create_connection
#include "gtest/gtest.h" // for Test, SuiteApiResolver, TestInfo, TEST
#include "test_connection.h" // for create_test_connection_config
TEST(test_ccaosdb, connection_ssl_authentication_success) {
caosdb_connection_connection_configuration config;
auto wrapped = caosdb::connection::create_test_connection_config();
config.wrapped_connection_configuration = wrapped.get();
caosdb_connection_connection connection;
caosdb_connection_create_connection(&connection, &config);
caosdb_info_version_info version_info;
caosdb_connection_get_version_info(&version_info, &connection);
auto major = caosdb_constants_COMPATIBLE_SERVER_VERSION_MAJOR();
auto minor = caosdb_constants_COMPATIBLE_SERVER_VERSION_MINOR();
const auto *const pre_release = caosdb_constants_COMPATIBLE_SERVER_VERSION_PRE_RELEASE();
EXPECT_EQ(major, version_info.major);
EXPECT_EQ(minor, version_info.minor);
EXPECT_STREQ(pre_release, version_info.pre_release);
}
......@@ -23,13 +23,14 @@
#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/connection.h" // for InsecureConnectionConfig
#include "caosdb/exceptions.h" // for AuthenticationError, ConnectionError
#include "caosdb/info.h" // for VersionInfo
#include "caosdb/utils.h" // for get_env_var
#include "caosdb/utility.h" // for get_env_var
#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ, EXPECT_THROW
#include "caosdb_test_utility.h"
#include "test_connection.h"
#include "caosdb/constants.h"
namespace caosdb::connection {
using caosdb::authentication::PlainPasswordAuthenticator;
......@@ -38,82 +39,81 @@ using caosdb::exceptions::ConnectionError;
TEST(test_connection, config_somehost_25323) {
auto port = 25323;
const auto *pHost = "somehost";
auto config = std::make_unique<InsecureCaosDBConnectionConfig>(pHost, port);
const auto *host = "somehost";
auto config = InsecureConnectionConfig(host, port);
EXPECT_EQ(pHost, config->getHost());
EXPECT_EQ(port, config->getPort());
EXPECT_EQ(host, config.GetHost());
EXPECT_EQ(port, config.GetPort());
}
TEST(test_connection, connect_somehost_42347_fails) {
auto port = 42347;
const auto *pHost = "somehost";
auto config = std::make_shared<InsecureCaosDBConnectionConfig>(pHost, port);
CaosDBConnection connection(config);
const auto *host = "somehost";
auto config = InsecureConnectionConfig(host, port);
Connection connection(config);
EXPECT_THROW(connection.GetVersionInfo(), ConnectionError);
}
TEST(test_connection, connection_insecure_authentication_error_anonymous) {
auto port_str =
caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTP", "8080");
const auto *port_str =
caosdb::utility::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTP", "8080");
auto port = std::stoi(port_str);
const auto host =
caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost");
const auto *const host =
caosdb::utility::get_env_var("CAOSDB_SERVER_HOST", "localhost");
auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port);
auto connection = CaosDBConnection(config);
auto config = InsecureConnectionConfig(host, port);
auto connection = Connection(config);
EXPECT_THROW(connection.GetVersionInfo(), AuthenticationError);
}
TEST(test_connection, connection_ssl_authentication_error_anonymous) {
auto port_str =
caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443");
const auto *port_str =
caosdb::utility::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 *const host =
caosdb::utility::get_env_var("CAOSDB_SERVER_HOST", "localhost");
const auto path =
caosdb::utils::get_env_var("CAOSDB_SERVER_CERT", std::string());
caosdb::utility::get_env_var("CAOSDB_SERVER_CERT", std::string());
auto ssloptions = std::make_shared<PemFileCACertProvider>(path);
auto config =
std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions);
auto connection = CaosDBConnection(config);
auto cert = PemFileCertificateProvider(path);
auto config = TlsConnectionConfig(host, port, cert);
auto connection = Connection(config);
EXPECT_THROW_MESSAGE(connection.GetVersionInfo(), AuthenticationError,
"Please login.");
}
TEST(test_connection, connection_ssl_authentication_error_wrong_credentials) {
auto port_str =
caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443");
const auto *port_str =
caosdb::utility::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 *const host =
caosdb::utility::get_env_var("CAOSDB_SERVER_HOST", "localhost");
const auto path =
caosdb::utils::get_env_var("CAOSDB_SERVER_CERT", std::string());
caosdb::utility::get_env_var("CAOSDB_SERVER_CERT", std::string());
const auto *const user = "not-a-user-23461237";
const auto *const password = "very-c-cred";
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);
auto connection = CaosDBConnection(config);
auto auth = PlainPasswordAuthenticator(user, password);
auto cert = PemFileCertificateProvider(path);
auto config = TlsConnectionConfig(host, port, cert, auth);
auto connection = Connection(config);
EXPECT_THROW_MESSAGE(connection.GetVersionInfo(), AuthenticationError,
"Authentication failed. Username or password wrong.");
}
TEST(test_connection, connection_ssl_authentication_success) {
const auto &pConnection = get_test_connection();
const auto& connection = get_test_connection();
auto major = 0;
auto minor = 5;
const auto *const pre_release = "GRPC004";
auto major = caosdb::COMPATIBLE_SERVER_VERSION_MAJOR;
auto minor = caosdb::COMPATIBLE_SERVER_VERSION_MINOR;
const auto pre_release =
std::string(caosdb::COMPATIBLE_SERVER_VERSION_PRE_RELEASE);
auto v_info = pConnection->GetVersionInfo();
auto v_info = connection->GetVersionInfo();
EXPECT_EQ(major, v_info->GetMajor());
EXPECT_EQ(minor, v_info->GetMinor());
EXPECT_EQ(pre_release, v_info->GetPreRelease());
......
......@@ -21,39 +21,44 @@
#include <memory> // for allocator, make_shared, shared_ptr
#include <string> // for stoi, string
#include "caosdb/authentication.h" // for PlainPasswordAuthenticator
#include "caosdb/connection.h" // for PemFileCACertProvider, SslCaosDBC...
#include "caosdb/utils.h" // for get_env_var
#include "caosdb/connection.h" // for PemFileCertificateProvider, TlsCaosDBC...
#include "caosdb/utility.h" // for get_env_var
namespace caosdb::connection {
using caosdb::authentication::PlainPasswordAuthenticator;
/**
* Return a fresh CaosDBConnection pointer. The caller has the ownership.
*/
inline auto create_test_connection() -> CaosDBConnection * {
inline auto create_test_connection_config()
-> std::unique_ptr<TlsConnectionConfig> {
auto port_str =
caosdb::utils::get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "8443");
caosdb::utility::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");
caosdb::utility::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");
caosdb::utility::get_env_var("CAOSDB_SERVER_CERT", std::string());
const auto user = caosdb::utility::get_env_var("CAOSDB_USER", "admin");
const auto password =
caosdb::utility::get_env_var("CAOSDB_PASSWORD", "caosdb");
auto auth = PlainPasswordAuthenticator(user, password);
auto cert = PemFileCertificateProvider(path);
return std::make_unique<TlsConnectionConfig>(host, port, cert, auth);
}
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);
/**
* Return a fresh Connection pointer. The caller has the ownership.
*/
inline auto create_test_connection() -> std::unique_ptr<Connection> {
auto config = create_test_connection_config();
return std::make_unique<Connection>(*(config.get()));
};
/**
* Singleton which holds a single global CaosDBConnection
* Singleton which holds a single global Connection
*/
class ConnectionProvider {
private:
std::shared_ptr<CaosDBConnection> connection;
std::shared_ptr<Connection> connection;
ConnectionProvider() : connection(create_test_connection()){};
public:
......@@ -61,7 +66,7 @@ public:
static ConnectionProvider instance;
return instance;
};
inline auto GetConnection() -> std::shared_ptr<CaosDBConnection> & {
inline auto GetConnection() -> std::shared_ptr<Connection> & {
return this->connection;
}
......@@ -72,7 +77,7 @@ public:
/**
* Return a connection for testing purposes.
*/
inline auto get_test_connection() -> const std::shared_ptr<CaosDBConnection> & {
inline auto get_test_connection() -> const std::shared_ptr<Connection> & {
return ConnectionProvider::GetInstance().GetConnection();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment