diff --git a/.docker/Dockerfile b/.docker/Dockerfile index 47cacfaf867e18c7ecfa8629e0d31cc00d5edf9c..a0d975f82b721dd71cc1e4f7e724c47c20e28a55 100644 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -13,6 +13,8 @@ COPY . /caosdb-cppinttest WORKDIR /caosdb-cppinttest RUN rm -rf .git +COPY .docker/caosdb-client.json /caosdb-client.json + # build and run tests CMD mkdir build && \ cd build && \ diff --git a/.docker/caosdb-client.json b/.docker/caosdb-client.json new file mode 100644 index 0000000000000000000000000000000000000000..5da162e2561da384892fc62e02f2fdaddad2e6aa --- /dev/null +++ b/.docker/caosdb-client.json @@ -0,0 +1,14 @@ +{ + "connections": { + "default": { + "host": "caosdb-server", + "port": 8443, + "server_certificate_path": "/cert/caosdb.cert.pem", + "authentication": { + "type": "plain", + "username": "admin", + "password": "caosdb" + } + } + } +} diff --git a/.docker/tester.yml b/.docker/tester.yml index f801f421f2cfb179da86f69cd5bc58d312f48882..764fcd63475a8baa1b2e8aed533336f1326c161f 100644 --- a/.docker/tester.yml +++ b/.docker/tester.yml @@ -5,6 +5,7 @@ services: environment: CAOSDB_SERVER_HOST: caosdb-server CAOSDB_SERVER_CERT: /cert/caosdb.cert.pem + CAOSDB_CLIENT_CONFIGURATION: /caosdb-client.json networks: - docker_caosnet volumes: diff --git a/conanfile.txt b/conanfile.txt index 6e14d4783d406fd06def9dcd692db8622712f9d3..048f038956408610ddfa02e4d3b780b990f2d657 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,5 +1,5 @@ [requires] -caosdb/0.0.4 +caosdb/0.0.5 gtest/1.11.0 [generators] diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 81f1fbe0be5d70c05c6b395ab56f38c922dae004..49dde42cf0d3a578f9bd1f50cd99b1c88ccf3fcb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -24,6 +24,7 @@ set(test_cases test_connection test_transaction + test_ccaosdb ) ####################################################### diff --git a/test/test_ccaosdb.cpp b/test/test_ccaosdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b99e8e0c8869230648b5d85359e3ecbeebf07e6 --- /dev/null +++ b/test/test_ccaosdb.cpp @@ -0,0 +1,42 @@ +/* + * 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_info_version_info, caosdb_... +#include "gtest/gtest-message.h" // for Message +#include "gtest/gtest-test-part.h" // for TestPartResult +#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, SuiteApiResolver + +TEST(test_ccaosdb, connection_ssl_authentication_success) { + + caosdb_connection_connection connection; + caosdb_connection_connection_manager_get_default_connection(&connection); + + 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); +} diff --git a/test/test_connection.cpp b/test/test_connection.cpp index fddc26b97e23f3fbd1c658ee6a93899c93f1ebe5..08eec662fd93ac3e0b7b05579f0aa4c184017a38 100644 --- a/test/test_connection.cpp +++ b/test/test_connection.cpp @@ -23,13 +23,13 @@ #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 InsecureConnectionConfiguration #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 +38,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 = InsecureConnectionConfiguration(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 = InsecureConnectionConfiguration(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 = InsecureConnectionConfiguration(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 = TlsConnectionConfiguration(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 = TlsConnectionConfiguration(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 = ConnectionManager::GetDefaultConnection(); - 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()); diff --git a/test/test_connection.h b/test/test_connection.h deleted file mode 100644 index 9c60f4526761b726896e44e125973e853918faf8..0000000000000000000000000000000000000000 --- a/test/test_connection.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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, 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 - -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 diff --git a/test/test_transaction.cpp b/test/test_transaction.cpp index 15901dfdac7dbec8e0e1bc5952693ce994649fba..ec59df9e692416d727529f3c320583be99f6b8f8 100644 --- a/test/test_transaction.cpp +++ b/test/test_transaction.cpp @@ -25,12 +25,12 @@ #include "caosdb/entity.h" // for Entity, EntityID #include "caosdb/transaction.h" // for Transaction, UniqueResult, Entity #include "gtest/gtest_pred_impl.h" // for Test, TestInfo, EXPECT_EQ, TEST -#include "test_connection.h" // for get_test_connection namespace caosdb::transaction { TEST(test_transaction, DISABLED_retrieve_manufacturer_by_id) { - const auto &connection = caosdb::connection::get_test_connection(); + const auto &connection = + caosdb::connection::ConnectionManager::GetDefaultConnection(); const auto *id = "107"; const auto *role = "RecordType";