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
2019-03-24 04:00:06 +01:00

155 lines
4.5 KiB
C++

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "wsfs/adapter/impl/authenticators.h"
#include "wsfs/adapter/impl/credentials.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 authenticators authenticators;
struct authenticators clone;
authenticators_init(&authenticators);
ASSERT_EQ(nullptr, authenticators.first);
authenticators_clone(&authenticators, &clone);
ASSERT_EQ(nullptr, clone.first);
authenticators_cleanup(&authenticators);
authenticators_cleanup(&clone);
}
TEST(Authenticators, Clone)
{
struct authenticators authenticators;
struct authenticators clone;
authenticators_init(&authenticators);
authenticators_add(&authenticators, "username", &authenticate, nullptr);
ASSERT_NE(nullptr, authenticators.first);
authenticators_clone(&authenticators, &clone);
ASSERT_NE(nullptr, clone.first);
ASSERT_NE(nullptr, authenticators.first);
ASSERT_NE(authenticators.first, clone.first);
authenticators_cleanup(&authenticators);
authenticators_cleanup(&clone);
}
TEST(Authenticators, Move)
{
struct authenticators authenticators;
struct authenticators clone;
authenticators_init(&authenticators);
authenticators_add(&authenticators, "username", &authenticate, nullptr);
ASSERT_NE(nullptr, authenticators.first);
authenticators_move(&authenticators, &clone);
ASSERT_NE(nullptr, clone.first);
ASSERT_EQ(nullptr, authenticators.first);
ASSERT_NE(authenticators.first, clone.first);
authenticators_cleanup(&authenticators);
authenticators_cleanup(&clone);
}
TEST(Authenticators, AuthenticateWithoutAuthenticators)
{
struct credentials creds;
credentials_init(&creds, "username", nullptr);
struct authenticators authenticators;
authenticators_init(&authenticators);
bool result =authenticators_authenticate(&authenticators, &creds);
ASSERT_TRUE(result);
result = authenticators_authenticate(&authenticators, nullptr);
ASSERT_TRUE(result);
authenticators_cleanup(&authenticators);
credentials_cleanup(&creds);
}
TEST(Authenticators, FailToAuthenticateWithoutCredentials)
{
MockAuthenticator mock;
set_authenticator(&mock);
struct authenticators authenticators;
authenticators_init(&authenticators);
authenticators_add(&authenticators, "username", &authenticate, nullptr);
bool result = authenticators_authenticate(&authenticators, nullptr);
ASSERT_FALSE(result);
authenticators_cleanup(&authenticators);
}
TEST(Authenticators, AuthenticateWithMultipleCredentials)
{
struct credentials creds;
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 authenticators authenticators;
authenticators_init(&authenticators);
authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
bool result = authenticators_authenticate(&authenticators, &creds);
ASSERT_TRUE(result);
authenticators_cleanup(&authenticators);
credentials_cleanup(&creds);
}
TEST(Authenticators, FailedAuthenticateWithWrongType)
{
struct credentials creds;
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 authenticators authenticators;
authenticators_init(&authenticators);
authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
bool result = authenticators_authenticate(&authenticators, &creds);
ASSERT_FALSE(result);
authenticators_cleanup(&authenticators);
credentials_cleanup(&creds);
}