/* * 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" #include "test_connection.h" namespace caosdb::connection { using caosdb::authentication::PlainPasswordAuthenticator; using caosdb::exceptions::AuthenticationError; 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); EXPECT_EQ(pHost, 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); 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"); auto port = std::stoi(port_str); const auto host = caosdb::utils::get_env_var("CAOSDB_SERVER_HOST", "localhost"); auto config = std::make_shared<InsecureCaosDBConnectionConfig>(host, port); auto connection = CaosDBConnection(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"); 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()); auto ssloptions = std::make_shared<PemFileCACertProvider>(path); auto config = std::make_shared<SslCaosDBConnectionConfig>(host, port, ssloptions); auto connection = CaosDBConnection(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"); 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 *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); EXPECT_THROW_MESSAGE(connection.GetVersionInfo(), AuthenticationError, "Authentication failed. Username or password wrong."); } TEST(test_connection, connection_ssl_authentication_success) { auto pConnection = get_test_connection(); auto major = 0; auto minor = 5; const auto *const pre_release = "GRPC004"; auto v_info = pConnection->GetVersionInfo(); EXPECT_EQ(major, v_info->GetMajor()); EXPECT_EQ(minor, v_info->GetMinor()); EXPECT_EQ(pre_release, v_info->GetPreRelease()); } } // namespace caosdb::connection