108 lines
3.6 KiB
C
108 lines
3.6 KiB
C
|
|
#include "test_framework.h"
|
||
|
|
#include "../src/types.h"
|
||
|
|
#include "../src/auth.h"
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
void test_auth_init_and_enabled(void) {
|
||
|
|
TEST_SUITE_BEGIN("Auth Init and Enabled");
|
||
|
|
|
||
|
|
auth_init(NULL, NULL);
|
||
|
|
TEST_ASSERT_EQ(0, auth_is_enabled(), "Auth disabled with NULL credentials");
|
||
|
|
|
||
|
|
auth_init("", "");
|
||
|
|
TEST_ASSERT_EQ(0, auth_is_enabled(), "Auth disabled with empty credentials");
|
||
|
|
|
||
|
|
auth_init("admin", "secret");
|
||
|
|
TEST_ASSERT_EQ(1, auth_is_enabled(), "Auth enabled with valid credentials");
|
||
|
|
|
||
|
|
TEST_SUITE_END();
|
||
|
|
}
|
||
|
|
|
||
|
|
void test_auth_check_credentials(void) {
|
||
|
|
TEST_SUITE_BEGIN("Auth Check Credentials");
|
||
|
|
|
||
|
|
auth_init("testuser", "testpass");
|
||
|
|
|
||
|
|
TEST_ASSERT_EQ(1, auth_check_credentials("testuser", "testpass"), "Correct credentials accepted");
|
||
|
|
TEST_ASSERT_EQ(0, auth_check_credentials("testuser", "wrongpass"), "Wrong password rejected");
|
||
|
|
TEST_ASSERT_EQ(0, auth_check_credentials("wronguser", "testpass"), "Wrong username rejected");
|
||
|
|
TEST_ASSERT_EQ(0, auth_check_credentials("wronguser", "wrongpass"), "Wrong both rejected");
|
||
|
|
TEST_ASSERT_EQ(0, auth_check_credentials(NULL, "testpass"), "NULL username rejected");
|
||
|
|
TEST_ASSERT_EQ(0, auth_check_credentials("testuser", NULL), "NULL password rejected");
|
||
|
|
|
||
|
|
TEST_SUITE_END();
|
||
|
|
}
|
||
|
|
|
||
|
|
void test_auth_check_basic_auth(void) {
|
||
|
|
TEST_SUITE_BEGIN("Auth Check Basic Auth Header");
|
||
|
|
|
||
|
|
auth_init("admin", "password123");
|
||
|
|
|
||
|
|
char error[256];
|
||
|
|
|
||
|
|
int result = auth_check_basic_auth("Basic YWRtaW46cGFzc3dvcmQxMjM=", error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(1, result, "Valid Basic auth header accepted");
|
||
|
|
|
||
|
|
result = auth_check_basic_auth("Basic aW52YWxpZDppbnZhbGlk", error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(0, result, "Invalid credentials rejected");
|
||
|
|
|
||
|
|
result = auth_check_basic_auth(NULL, error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(0, result, "NULL auth header rejected");
|
||
|
|
|
||
|
|
result = auth_check_basic_auth("Bearer token123", error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(0, result, "Bearer auth rejected");
|
||
|
|
|
||
|
|
result = auth_check_basic_auth("Basic !!invalid!!", error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(0, result, "Invalid base64 rejected");
|
||
|
|
|
||
|
|
result = auth_check_basic_auth("Basic bm9jb2xvbg==", error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(0, result, "Base64 without colon rejected");
|
||
|
|
|
||
|
|
TEST_SUITE_END();
|
||
|
|
}
|
||
|
|
|
||
|
|
void test_auth_route_basic_auth(void) {
|
||
|
|
TEST_SUITE_BEGIN("Auth Route Basic Auth");
|
||
|
|
|
||
|
|
route_config_t route;
|
||
|
|
memset(&route, 0, sizeof(route));
|
||
|
|
route.use_auth = 0;
|
||
|
|
|
||
|
|
char error[256];
|
||
|
|
int result = auth_check_route_basic_auth(&route, NULL, error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(1, result, "Route without auth passes");
|
||
|
|
|
||
|
|
route.use_auth = 1;
|
||
|
|
strcpy(route.username, "routeuser");
|
||
|
|
strcpy(route.password_hash, "5e884898da28047d9166d19d3de276ee6e8b0cb37d63e0c4c7ed6e1b4e5c7fd2");
|
||
|
|
|
||
|
|
result = auth_check_route_basic_auth(&route, NULL, error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(0, result, "Route with auth requires header");
|
||
|
|
|
||
|
|
result = auth_check_route_basic_auth(NULL, NULL, error, sizeof(error));
|
||
|
|
TEST_ASSERT_EQ(1, result, "NULL route passes");
|
||
|
|
|
||
|
|
TEST_SUITE_END();
|
||
|
|
}
|
||
|
|
|
||
|
|
void test_auth_disabled_passthrough(void) {
|
||
|
|
TEST_SUITE_BEGIN("Auth Disabled Passthrough");
|
||
|
|
|
||
|
|
auth_init(NULL, NULL);
|
||
|
|
|
||
|
|
TEST_ASSERT_EQ(1, auth_check_credentials("anyone", "anything"), "Disabled auth passes any credentials");
|
||
|
|
|
||
|
|
char error[256];
|
||
|
|
TEST_ASSERT_EQ(1, auth_check_basic_auth(NULL, error, sizeof(error)), "Disabled auth passes NULL header");
|
||
|
|
|
||
|
|
TEST_SUITE_END();
|
||
|
|
}
|
||
|
|
|
||
|
|
void run_auth_tests(void) {
|
||
|
|
test_auth_init_and_enabled();
|
||
|
|
test_auth_check_credentials();
|
||
|
|
test_auth_check_basic_auth();
|
||
|
|
test_auth_route_basic_auth();
|
||
|
|
test_auth_disabled_passthrough();
|
||
|
|
}
|