/* Host tests for the pure parts of chat_login.c (make test-chat). */ #include #include #include "chat_login.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) /* Trimmed copy of the real Cloudron OIDC login page. */ static const char page[] = "\nLogin\n" " \n" "\n" "
\n"; static void test_page(void) { char url[1280], name[64]; const char *base = "https://my.example.com/openid/interaction/UdA3nm_PJ"; CHECK(chat_login_parse_page(page, sizeof(page) - 1, base, url, sizeof(url), name, sizeof(name)) == 0); CHECK(strcmp(url, "https://my.example.com/openid/interaction/UdA3nm_PJ/login") == 0); CHECK(strcmp(name, "chat") == 0); /* Escaped slashes, no config id: the fallback scan. */ { const char *p2 = ""; CHECK(chat_login_parse_page(p2, strlen(p2), "http://10.0.2.2:8080/a/b", url, sizeof(url), name, sizeof(name)) == 0); CHECK(strcmp(url, "http://10.0.2.2:8080/openid/x/login") == 0); } /* Relative and absolute submit URLs. */ { const char *p3 = ""; const char *p4 = ""; CHECK(chat_login_parse_page(p3, strlen(p3), "https://h.test/openid/interaction/?x=1", url, sizeof(url), name, sizeof(name)) == 0); CHECK(strcmp(url, "https://h.test/openid/interaction/uid/login") == 0); CHECK(chat_login_parse_page(p4, strlen(p4), "https://h.test/", url, sizeof(url), name, sizeof(name)) == 0); CHECK(strcmp(url, "https://id.test/l") == 0); } /* An SSO page without the config. */ { const char *p5 = "Login"; CHECK(chat_login_parse_page(p5, strlen(p5), "https://h.test/", url, sizeof(url), name, sizeof(name)) == -1); } /* Broken JSON in the config and no fallback string. */ { const char *p6 = ""; CHECK(chat_login_parse_page(p6, strlen(p6), "https://h.test/", url, sizeof(url), name, sizeof(name)) == -1); } /* The real Cloudron consent page: a bare auto-submit form with no * cloudron-config JSON (dashboard/oidc_interaction_confirm.html). */ { const char *p7 = "\n\n \n" " Authorize chat\n \n\n" "
\n" " \n" "
\n" "\n" "\n\n"; CHECK(chat_login_parse_page(p7, strlen(p7), "https://my.example.com/openid/interaction/UdA3nm_PJ", url, sizeof(url), name, sizeof(name)) == 0); CHECK(strcmp(url, "https://my.example.com/openid/interaction/UdA3nm_PJ/confirm") == 0); } /* Other forms must not be mistaken for an interaction form. */ { const char *p8 = "
"; CHECK(chat_login_parse_page(p8, strlen(p8), "https://h.test/", url, sizeof(url), name, sizeof(name)) == -1); } } static void test_reply(void) { char red[256], msg[128]; const char *ok = "{\"redirectTo\":\"https://my.example.com/openid/auth/UdA3\"}"; const char *tfa = "{\"twoFactorRequired\":true,\"totpRequired\":true,\"passkeyOptions\":null}"; const char *pk = "{\"twoFactorRequired\":true,\"totpRequired\":false,\"passkeyOptions\":{}}"; const char *bad = "{\"status\":\"Unauthorized\",\"message\":\"Username and password does not match\"}"; CHECK(chat_login_parse_reply(200, ok, strlen(ok), red, sizeof(red), msg, sizeof(msg)) == CHAT_REPLY_REDIRECT); CHECK(strcmp(red, "https://my.example.com/openid/auth/UdA3") == 0); CHECK(chat_login_parse_reply(200, tfa, strlen(tfa), red, sizeof(red), msg, sizeof(msg)) == CHAT_REPLY_TOTP); CHECK(chat_login_parse_reply(200, pk, strlen(pk), red, sizeof(red), msg, sizeof(msg)) == CHAT_REPLY_PASSKEY); CHECK(chat_login_parse_reply(401, bad, strlen(bad), red, sizeof(red), msg, sizeof(msg)) == CHAT_REPLY_DENIED); CHECK(strcmp(msg, "Username and password does not match") == 0); CHECK(chat_login_parse_reply(500, "oops", 4, red, sizeof(red), msg, sizeof(msg)) == CHAT_REPLY_ERROR); CHECK(chat_login_parse_reply(200, "{}", 2, red, sizeof(red), msg, sizeof(msg)) == CHAT_REPLY_ERROR); } static void test_config(void) { char server[192], cookie[2048], out[2600]; const char *cfg = "# comment\r\nserver=https://chat.example.com\r\n" "junk=1\ncookie=connect.sid=s%3Aabc.def; theme=x \n"; CHECK(chat_config_parse(cfg, strlen(cfg), server, sizeof(server), cookie, sizeof(cookie)) == 0); CHECK(strcmp(server, "https://chat.example.com") == 0); CHECK(strcmp(cookie, "connect.sid=s%3Aabc.def; theme=x") == 0); CHECK(chat_config_parse("", 0, server, sizeof(server), cookie, sizeof(cookie)) == -1); CHECK(chat_config_format(out, sizeof(out), "https://c.test", "connect.sid=1") > 0); CHECK(chat_config_parse(out, strlen(out), server, sizeof(server), cookie, sizeof(cookie)) == 0); CHECK(strcmp(server, "https://c.test") == 0); CHECK(strcmp(cookie, "connect.sid=1") == 0); CHECK(chat_config_format(out, 10, "https://c.test", "x=1") == -1); } static void test_normalize_and_body(void) { char s[192], body[256]; CHECK(chat_server_normalize("chat.example.com", s, sizeof(s)) == 0); CHECK(strcmp(s, "https://chat.example.com") == 0); CHECK(chat_server_normalize(" http://10.0.2.2:8080/// ", s, sizeof(s)) == 0); CHECK(strcmp(s, "http://10.0.2.2:8080") == 0); CHECK(chat_server_normalize("https://", s, sizeof(s)) == -1); CHECK(chat_server_normalize("", s, sizeof(s)) == -1); CHECK(chat_server_normalize("a b", s, sizeof(s)) == -1); CHECK(chat_login_body(body, sizeof(body), "al\"ice", "p\\w", "") > 0); CHECK(strcmp(body, "{\"username\":\"al\\\"ice\",\"password\":\"p\\\\w\",\"totpToken\":\"\"}") == 0); CHECK(chat_login_body(body, 16, "alice", "secret", "123456") == -1); } int main(void) { test_page(); test_reply(); test_config(); test_normalize_and_body(); if (failures) { printf("chat_login_test: %d failure(s)\n", failures); return 1; } printf("chat_login_test: all tests passed\n"); return 0; }