1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00
falk-werner_webfuse/test/test_authenticators.cc

155 lines
4.7 KiB
C++
Raw Normal View History

Feature/authentication (#14) * makes wsfs_server_config opaque * feature: try to create mount point, if not present * fixes server start failure due to existing mountpoint * added basic authentication infrastructure * makes wsfs_server_config opaque * feature: try to create mount point, if not present * fixes server start failure due to existing mountpoint * added basic authentication infrastructure * added unit tests for credentials * added unit tests for authenticators * propagates authenticators to server protocol * enabled username authentication in daemon example * adds example to compute password hash * adds infrastructure to execute commands * added userdb to encapsulate authentication stuff * adds session and session_manager * fixes warning about unused param * moves some logic from server_protocol to session * makes wsfs_server_config opaque * feature: try to create mount point, if not present * fixes server start failure due to existing mountpoint * added basic authentication infrastructure * makes wsfs_server_config opaque * added unit tests for credentials * added unit tests for authenticators * propagates authenticators to server protocol * enabled username authentication in daemon example * adds example to compute password hash * adds infrastructure to execute commands * added userdb to encapsulate authentication stuff * adds session and session_manager * fixes warning about unused param * moves some logic from server_protocol to session * updates libcrypto to version 1.1.0
2019-03-23 21:53:14 +00:00
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "wsfs/adapter/authenticators.h"
#include "wsfs/adapter/credentials_intern.h"
#include "mock_authenticator.hpp"
using ::testing::_;
using ::testing::Return;
using ::wsfs_test::MockAuthenticator;
using ::wsfs_test::set_authenticator;
using ::wsfs_test::authenticate;
using ::wsfs_test::authenticate_1;
using ::wsfs_test::authenticate_2;
TEST(Authenticators, CloneEmpty)
{
struct wsfs_authenticators authenticators;
struct wsfs_authenticators clone;
wsfs_authenticators_init(&authenticators);
ASSERT_EQ(nullptr, authenticators.first);
wsfs_authenticators_clone(&authenticators, &clone);
ASSERT_EQ(nullptr, clone.first);
wsfs_authenticators_cleanup(&authenticators);
wsfs_authenticators_cleanup(&clone);
}
TEST(Authenticators, Clone)
{
struct wsfs_authenticators authenticators;
struct wsfs_authenticators clone;
wsfs_authenticators_init(&authenticators);
wsfs_authenticators_add(&authenticators, "username", &authenticate, nullptr);
ASSERT_NE(nullptr, authenticators.first);
wsfs_authenticators_clone(&authenticators, &clone);
ASSERT_NE(nullptr, clone.first);
ASSERT_NE(nullptr, authenticators.first);
ASSERT_NE(authenticators.first, clone.first);
wsfs_authenticators_cleanup(&authenticators);
wsfs_authenticators_cleanup(&clone);
}
TEST(Authenticators, Move)
{
struct wsfs_authenticators authenticators;
struct wsfs_authenticators clone;
wsfs_authenticators_init(&authenticators);
wsfs_authenticators_add(&authenticators, "username", &authenticate, nullptr);
ASSERT_NE(nullptr, authenticators.first);
wsfs_authenticators_move(&authenticators, &clone);
ASSERT_NE(nullptr, clone.first);
ASSERT_EQ(nullptr, authenticators.first);
ASSERT_NE(authenticators.first, clone.first);
wsfs_authenticators_cleanup(&authenticators);
wsfs_authenticators_cleanup(&clone);
}
TEST(Authenticators, AuthenticateWithoutAuthenticators)
{
struct wsfs_credentials creds;
wsfs_credentials_init(&creds, "username", nullptr);
struct wsfs_authenticators authenticators;
wsfs_authenticators_init(&authenticators);
bool result = wsfs_authenticators_authenticate(&authenticators, &creds);
ASSERT_TRUE(result);
result = wsfs_authenticators_authenticate(&authenticators, nullptr);
ASSERT_TRUE(result);
wsfs_authenticators_cleanup(&authenticators);
wsfs_credentials_cleanup(&creds);
}
TEST(Authenticators, FailToAuthenticateWithoutCredentials)
{
MockAuthenticator mock;
set_authenticator(&mock);
struct wsfs_authenticators authenticators;
wsfs_authenticators_init(&authenticators);
wsfs_authenticators_add(&authenticators, "username", &authenticate, nullptr);
bool result = wsfs_authenticators_authenticate(&authenticators, nullptr);
ASSERT_FALSE(result);
wsfs_authenticators_cleanup(&authenticators);
}
TEST(Authenticators, AuthenticateWithMultipleCredentials)
{
struct wsfs_credentials creds;
wsfs_credentials_init(&creds, "username", nullptr);
MockAuthenticator username_mock;
set_authenticator(1, &username_mock);
EXPECT_CALL(username_mock, authenticate(&creds, nullptr))
.Times(1)
.WillRepeatedly(Return(true));
MockAuthenticator certificate_mock;
set_authenticator(2, &certificate_mock);
EXPECT_CALL(certificate_mock, authenticate(_, _))
.Times(0);
struct wsfs_authenticators authenticators;
wsfs_authenticators_init(&authenticators);
wsfs_authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
wsfs_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
bool result = wsfs_authenticators_authenticate(&authenticators, &creds);
ASSERT_TRUE(result);
wsfs_authenticators_cleanup(&authenticators);
wsfs_credentials_cleanup(&creds);
}
TEST(Authenticators, FailedAuthenticateWithWrongType)
{
struct wsfs_credentials creds;
wsfs_credentials_init(&creds, "token", nullptr);
MockAuthenticator username_mock;
set_authenticator(1, &username_mock);
EXPECT_CALL(username_mock, authenticate(&creds, nullptr))
.Times(0);
MockAuthenticator certificate_mock;
set_authenticator(2, &certificate_mock);
EXPECT_CALL(certificate_mock, authenticate(_, _))
.Times(0);
struct wsfs_authenticators authenticators;
wsfs_authenticators_init(&authenticators);
wsfs_authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
wsfs_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
bool result = wsfs_authenticators_authenticate(&authenticators, &creds);
ASSERT_FALSE(result);
wsfs_authenticators_cleanup(&authenticators);
wsfs_credentials_cleanup(&creds);
}