2019-03-23 21:53:14 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
#include "webfuse/adapter/impl/authenticators.h"
|
|
|
|
#include "webfuse/adapter/impl/credentials.h"
|
2019-03-23 21:53:14 +00:00
|
|
|
#include "mock_authenticator.hpp"
|
|
|
|
|
|
|
|
using ::testing::_;
|
|
|
|
using ::testing::Return;
|
2019-03-26 22:04:53 +00:00
|
|
|
using ::webfuse_test::MockAuthenticator;
|
|
|
|
using ::webfuse_test::set_authenticator;
|
|
|
|
using ::webfuse_test::authenticate;
|
|
|
|
using ::webfuse_test::authenticate_1;
|
|
|
|
using ::webfuse_test::authenticate_2;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(Authenticators, CloneEmpty)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
struct wf_impl_authenticators clone;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_init(&authenticators);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_EQ(nullptr, authenticators.first);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_clone(&authenticators, &clone);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_EQ(nullptr, clone.first);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
|
|
|
wf_impl_authenticators_cleanup(&clone);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Authenticators, Clone)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
struct wf_impl_authenticators clone;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_init(&authenticators);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "username", &authenticate, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_NE(nullptr, authenticators.first);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_clone(&authenticators, &clone);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_NE(nullptr, clone.first);
|
|
|
|
ASSERT_NE(nullptr, authenticators.first);
|
|
|
|
ASSERT_NE(authenticators.first, clone.first);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
|
|
|
wf_impl_authenticators_cleanup(&clone);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Authenticators, Move)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
struct wf_impl_authenticators clone;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_init(&authenticators);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "username", &authenticate, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_NE(nullptr, authenticators.first);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_move(&authenticators, &clone);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_NE(nullptr, clone.first);
|
|
|
|
ASSERT_EQ(nullptr, authenticators.first);
|
|
|
|
ASSERT_NE(authenticators.first, clone.first);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
|
|
|
wf_impl_authenticators_cleanup(&clone);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Authenticators, AuthenticateWithoutAuthenticators)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
|
|
|
wf_impl_credentials_init(&creds, "username", nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
wf_impl_authenticators_init(&authenticators);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
bool result = wf_impl_authenticators_authenticate(&authenticators, &creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_TRUE(result);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
result = wf_impl_authenticators_authenticate(&authenticators, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_TRUE(result);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Authenticators, FailToAuthenticateWithoutCredentials)
|
|
|
|
{
|
|
|
|
MockAuthenticator mock;
|
|
|
|
set_authenticator(&mock);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
wf_impl_authenticators_init(&authenticators);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "username", &authenticate, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
bool result = wf_impl_authenticators_authenticate(&authenticators, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_FALSE(result);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Authenticators, AuthenticateWithMultipleCredentials)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
|
|
|
wf_impl_credentials_init(&creds, "username", nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
wf_impl_authenticators_init(&authenticators);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
bool result = wf_impl_authenticators_authenticate(&authenticators, &creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_TRUE(result);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Authenticators, FailedAuthenticateWithWrongType)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
|
|
|
wf_impl_credentials_init(&creds, "token", nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_authenticators authenticators;
|
|
|
|
wf_impl_authenticators_init(&authenticators);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "username", &authenticate_1, nullptr);
|
|
|
|
wf_impl_authenticators_add(&authenticators, "certificate", &authenticate_2, nullptr);
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
bool result = wf_impl_authenticators_authenticate(&authenticators, &creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
ASSERT_FALSE(result);
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_authenticators_cleanup(&authenticators);
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|