2019-03-23 21:53:14 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2020-06-28 13:51:34 +00:00
|
|
|
#include "webfuse/credentials.h"
|
|
|
|
#include "webfuse/impl/credentials.h"
|
2019-03-23 21:53:14 +00:00
|
|
|
#include <jansson.h>
|
|
|
|
|
|
|
|
TEST(Credentials, Type)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_init(&creds, "test", nullptr);
|
2020-03-21 21:25:50 +00:00
|
|
|
ASSERT_STREQ("test", wf_credentials_type(&creds));
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Credentials, Get)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
2019-03-23 21:53:14 +00:00
|
|
|
json_t * data = json_object();
|
|
|
|
json_object_set_new(data, "username", json_string("bob"));
|
|
|
|
json_object_set_new(data, "password", json_string("<secret>"));
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_init(&creds, "username", data);
|
2020-03-21 21:25:50 +00:00
|
|
|
ASSERT_STREQ("username", wf_credentials_type(&creds));
|
|
|
|
ASSERT_STREQ("bob", wf_credentials_get(&creds, "username"));
|
|
|
|
ASSERT_STREQ("<secret>", wf_credentials_get(&creds, "password"));
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
json_decref(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Credentials, FailedToGetNonexistingValue)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
2019-03-23 21:53:14 +00:00
|
|
|
json_t * data = json_object();
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_init(&creds, "username", data);
|
2020-03-21 21:25:50 +00:00
|
|
|
ASSERT_STREQ("username", wf_credentials_type(&creds));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
json_decref(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Credentials, FailedToGetWithoutData)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_init(&creds, "username", nullptr);
|
2020-03-21 21:25:50 +00:00
|
|
|
ASSERT_STREQ("username", wf_credentials_type(&creds));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Credentials, FailedToGetWrongDataType)
|
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_credentials creds;
|
2019-03-23 21:53:14 +00:00
|
|
|
json_t * data = json_string("invalid_creds");
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_init(&creds, "username", data);
|
2020-03-21 21:25:50 +00:00
|
|
|
ASSERT_STREQ("username", wf_credentials_type(&creds));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_credentials_cleanup(&creds);
|
2019-03-23 21:53:14 +00:00
|
|
|
json_decref(data);
|
|
|
|
}
|
|
|
|
|
2020-03-21 21:25:50 +00:00
|
|
|
TEST(Credentials, FailedToGetWrongElementDataType)
|
|
|
|
{
|
|
|
|
struct wf_credentials creds;
|
|
|
|
json_t * data = json_object();
|
|
|
|
json_object_set_new(data, "username", json_integer(42));
|
|
|
|
|
|
|
|
wf_impl_credentials_init(&creds, "username", data);
|
|
|
|
ASSERT_STREQ("username", wf_credentials_type(&creds));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username"));
|
|
|
|
ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password"));
|
|
|
|
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
|
|
|
json_decref(data);
|
|
|
|
}
|
2020-06-10 20:42:26 +00:00
|
|
|
|
|
|
|
TEST(Credentials, SetType)
|
|
|
|
{
|
|
|
|
struct wf_credentials creds;
|
|
|
|
wf_impl_credentials_init_default(&creds);
|
|
|
|
|
|
|
|
wf_credentials_set_type(&creds, "username");
|
|
|
|
ASSERT_STREQ("username", wf_credentials_type(&creds));
|
|
|
|
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Credentials, Add)
|
|
|
|
{
|
|
|
|
struct wf_credentials creds;
|
|
|
|
wf_impl_credentials_init_default(&creds);
|
|
|
|
|
|
|
|
wf_credentials_add(&creds, "a.value", "a");
|
|
|
|
ASSERT_STREQ("a", wf_credentials_get(&creds, "a.value"));
|
|
|
|
|
|
|
|
wf_credentials_add(&creds, "b.value", "b");
|
|
|
|
ASSERT_STREQ("b", wf_credentials_get(&creds, "b.value"));
|
|
|
|
|
|
|
|
wf_credentials_add(&creds, "a.value", "A");
|
|
|
|
ASSERT_STREQ("A", wf_credentials_get(&creds, "a.value"));
|
|
|
|
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
|
|
|
}
|