chat_login_test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Host tests for the pure parts of chat_login.c (make test-chat). */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "chat_login.h"
  5. static int failures;
  6. #define CHECK(cond) do { \
  7. if (!(cond)) { \
  8. printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  9. failures++; \
  10. } \
  11. } while (0)
  12. /* Trimmed copy of the real Cloudron OIDC login page. */
  13. static const char page[] =
  14. "<!DOCTYPE html>\n<html><head><title>Login</title>\n"
  15. " <script id=\"cloudron-config\" type=\"application/json\">\n"
  16. " {\"iconUrl\":\"/api/v1/apps/bc9e/icon\",\"name\":\"chat\",\"note\":\"\","
  17. "\"submitUrl\":\"/openid/interaction/UdA3nm_PJ/login\","
  18. "\"passkeyAuthOptionsUrl\":\"/openid/interaction/UdA3nm_PJ/passkey_auth_options\","
  19. "\"footer\":\"&copy; 2026 [Cloudron](https://cloudron.io)\",\"language\":null}\n"
  20. " </script>\n"
  21. "<script type=\"module\" crossorigin src=\"/assets/oidc_login.js\"></script>\n"
  22. "</head><body><div id=\"app\"></div></body></html>\n";
  23. static void test_page(void)
  24. {
  25. char url[1280], name[64];
  26. const char *base = "https://my.example.com/openid/interaction/UdA3nm_PJ";
  27. CHECK(chat_login_parse_page(page, sizeof(page) - 1, base, url,
  28. sizeof(url), name, sizeof(name)) == 0);
  29. CHECK(strcmp(url, "https://my.example.com/openid/interaction/UdA3nm_PJ/login") == 0);
  30. CHECK(strcmp(name, "chat") == 0);
  31. /* Escaped slashes, no config id: the fallback scan. */
  32. {
  33. const char *p2 = "<script>var c={\"submitUrl\":\"\\/openid\\/x\\/login\"};</script>";
  34. CHECK(chat_login_parse_page(p2, strlen(p2), "http://10.0.2.2:8080/a/b",
  35. url, sizeof(url), name, sizeof(name)) == 0);
  36. CHECK(strcmp(url, "http://10.0.2.2:8080/openid/x/login") == 0);
  37. }
  38. /* Relative and absolute submit URLs. */
  39. {
  40. const char *p3 = "<script id=\"cloudron-config\">{\"submitUrl\":\"uid/login\"}</script>";
  41. const char *p4 = "<script id=\"cloudron-config\">{\"submitUrl\":\"https://id.test/l\"}</script>";
  42. CHECK(chat_login_parse_page(p3, strlen(p3), "https://h.test/openid/interaction/?x=1",
  43. url, sizeof(url), name, sizeof(name)) == 0);
  44. CHECK(strcmp(url, "https://h.test/openid/interaction/uid/login") == 0);
  45. CHECK(chat_login_parse_page(p4, strlen(p4), "https://h.test/",
  46. url, sizeof(url), name, sizeof(name)) == 0);
  47. CHECK(strcmp(url, "https://id.test/l") == 0);
  48. }
  49. /* An SSO page without the config. */
  50. {
  51. const char *p5 = "<html><body><a href=\"https://sso\">Login</a></body></html>";
  52. CHECK(chat_login_parse_page(p5, strlen(p5), "https://h.test/",
  53. url, sizeof(url), name, sizeof(name)) == -1);
  54. }
  55. /* Broken JSON in the config and no fallback string. */
  56. {
  57. const char *p6 = "<script id=\"cloudron-config\">{\"submitUrl\": </script>";
  58. CHECK(chat_login_parse_page(p6, strlen(p6), "https://h.test/",
  59. url, sizeof(url), name, sizeof(name)) == -1);
  60. }
  61. /* The real Cloudron consent page: a bare auto-submit form with no
  62. * cloudron-config JSON (dashboard/oidc_interaction_confirm.html). */
  63. {
  64. const char *p7 =
  65. "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n"
  66. " <title>Authorize chat</title>\n </head>\n<body>\n"
  67. "<form id=\"submitForm\" method=\"post\" "
  68. "action=\"/openid/interaction/UdA3nm_PJ/confirm\">\n"
  69. " <!-- <button type=\"submit\"></button> -->\n"
  70. "</form>\n"
  71. "<script type=\"module\" "
  72. "src=\"/src/oidcinteractionconfirm.js\"></script>\n"
  73. "</body>\n</html>\n";
  74. CHECK(chat_login_parse_page(p7, strlen(p7),
  75. "https://my.example.com/openid/interaction/UdA3nm_PJ",
  76. url, sizeof(url), name, sizeof(name)) == 0);
  77. CHECK(strcmp(url, "https://my.example.com/openid/interaction/UdA3nm_PJ/confirm") == 0);
  78. }
  79. /* Other forms must not be mistaken for an interaction form. */
  80. {
  81. const char *p8 = "<form action=\"/search\"><input name=\"q\"></form>";
  82. CHECK(chat_login_parse_page(p8, strlen(p8), "https://h.test/",
  83. url, sizeof(url), name, sizeof(name)) == -1);
  84. }
  85. }
  86. static void test_reply(void)
  87. {
  88. char red[256], msg[128];
  89. const char *ok = "{\"redirectTo\":\"https://my.example.com/openid/auth/UdA3\"}";
  90. const char *tfa = "{\"twoFactorRequired\":true,\"totpRequired\":true,\"passkeyOptions\":null}";
  91. const char *pk = "{\"twoFactorRequired\":true,\"totpRequired\":false,\"passkeyOptions\":{}}";
  92. const char *bad = "{\"status\":\"Unauthorized\",\"message\":\"Username and password does not match\"}";
  93. CHECK(chat_login_parse_reply(200, ok, strlen(ok), red, sizeof(red), msg,
  94. sizeof(msg)) == CHAT_REPLY_REDIRECT);
  95. CHECK(strcmp(red, "https://my.example.com/openid/auth/UdA3") == 0);
  96. CHECK(chat_login_parse_reply(200, tfa, strlen(tfa), red, sizeof(red), msg,
  97. sizeof(msg)) == CHAT_REPLY_TOTP);
  98. CHECK(chat_login_parse_reply(200, pk, strlen(pk), red, sizeof(red), msg,
  99. sizeof(msg)) == CHAT_REPLY_PASSKEY);
  100. CHECK(chat_login_parse_reply(401, bad, strlen(bad), red, sizeof(red), msg,
  101. sizeof(msg)) == CHAT_REPLY_DENIED);
  102. CHECK(strcmp(msg, "Username and password does not match") == 0);
  103. CHECK(chat_login_parse_reply(500, "oops", 4, red, sizeof(red), msg,
  104. sizeof(msg)) == CHAT_REPLY_ERROR);
  105. CHECK(chat_login_parse_reply(200, "{}", 2, red, sizeof(red), msg,
  106. sizeof(msg)) == CHAT_REPLY_ERROR);
  107. }
  108. static void test_config(void)
  109. {
  110. char server[192], cookie[2048], out[2600];
  111. const char *cfg = "# comment\r\nserver=https://chat.example.com\r\n"
  112. "junk=1\ncookie=connect.sid=s%3Aabc.def; theme=x \n";
  113. CHECK(chat_config_parse(cfg, strlen(cfg), server, sizeof(server), cookie,
  114. sizeof(cookie)) == 0);
  115. CHECK(strcmp(server, "https://chat.example.com") == 0);
  116. CHECK(strcmp(cookie, "connect.sid=s%3Aabc.def; theme=x") == 0);
  117. CHECK(chat_config_parse("", 0, server, sizeof(server), cookie,
  118. sizeof(cookie)) == -1);
  119. CHECK(chat_config_format(out, sizeof(out), "https://c.test",
  120. "connect.sid=1") > 0);
  121. CHECK(chat_config_parse(out, strlen(out), server, sizeof(server), cookie,
  122. sizeof(cookie)) == 0);
  123. CHECK(strcmp(server, "https://c.test") == 0);
  124. CHECK(strcmp(cookie, "connect.sid=1") == 0);
  125. CHECK(chat_config_format(out, 10, "https://c.test", "x=1") == -1);
  126. }
  127. static void test_normalize_and_body(void)
  128. {
  129. char s[192], body[256];
  130. CHECK(chat_server_normalize("chat.example.com", s, sizeof(s)) == 0);
  131. CHECK(strcmp(s, "https://chat.example.com") == 0);
  132. CHECK(chat_server_normalize(" http://10.0.2.2:8080/// ", s, sizeof(s)) == 0);
  133. CHECK(strcmp(s, "http://10.0.2.2:8080") == 0);
  134. CHECK(chat_server_normalize("https://", s, sizeof(s)) == -1);
  135. CHECK(chat_server_normalize("", s, sizeof(s)) == -1);
  136. CHECK(chat_server_normalize("a b", s, sizeof(s)) == -1);
  137. CHECK(chat_login_body(body, sizeof(body), "al\"ice", "p\\w", "") > 0);
  138. CHECK(strcmp(body, "{\"username\":\"al\\\"ice\",\"password\":\"p\\\\w\",\"totpToken\":\"\"}") == 0);
  139. CHECK(chat_login_body(body, 16, "alice", "secret", "123456") == -1);
  140. }
  141. int main(void)
  142. {
  143. test_page();
  144. test_reply();
  145. test_config();
  146. test_normalize_and_body();
  147. if (failures) {
  148. printf("chat_login_test: %d failure(s)\n", failures);
  149. return 1;
  150. }
  151. printf("chat_login_test: all tests passed\n");
  152. return 0;
  153. }