| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /* Host tests for the pure parts of chat_login.c (make test-chat). */
- #include <stdio.h>
- #include <string.h>
- #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[] =
- "<!DOCTYPE html>\n<html><head><title>Login</title>\n"
- " <script id=\"cloudron-config\" type=\"application/json\">\n"
- " {\"iconUrl\":\"/api/v1/apps/bc9e/icon\",\"name\":\"chat\",\"note\":\"\","
- "\"submitUrl\":\"/openid/interaction/UdA3nm_PJ/login\","
- "\"passkeyAuthOptionsUrl\":\"/openid/interaction/UdA3nm_PJ/passkey_auth_options\","
- "\"footer\":\"© 2026 [Cloudron](https://cloudron.io)\",\"language\":null}\n"
- " </script>\n"
- "<script type=\"module\" crossorigin src=\"/assets/oidc_login.js\"></script>\n"
- "</head><body><div id=\"app\"></div></body></html>\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 = "<script>var c={\"submitUrl\":\"\\/openid\\/x\\/login\"};</script>";
- 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 = "<script id=\"cloudron-config\">{\"submitUrl\":\"uid/login\"}</script>";
- const char *p4 = "<script id=\"cloudron-config\">{\"submitUrl\":\"https://id.test/l\"}</script>";
- 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 = "<html><body><a href=\"https://sso\">Login</a></body></html>";
- 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 = "<script id=\"cloudron-config\">{\"submitUrl\": </script>";
- CHECK(chat_login_parse_page(p6, strlen(p6), "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;
- }
|