chat_login_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  62. static void test_reply(void)
  63. {
  64. char red[256], msg[128];
  65. const char *ok = "{\"redirectTo\":\"https://my.example.com/openid/auth/UdA3\"}";
  66. const char *tfa = "{\"twoFactorRequired\":true,\"totpRequired\":true,\"passkeyOptions\":null}";
  67. const char *pk = "{\"twoFactorRequired\":true,\"totpRequired\":false,\"passkeyOptions\":{}}";
  68. const char *bad = "{\"status\":\"Unauthorized\",\"message\":\"Username and password does not match\"}";
  69. CHECK(chat_login_parse_reply(200, ok, strlen(ok), red, sizeof(red), msg,
  70. sizeof(msg)) == CHAT_REPLY_REDIRECT);
  71. CHECK(strcmp(red, "https://my.example.com/openid/auth/UdA3") == 0);
  72. CHECK(chat_login_parse_reply(200, tfa, strlen(tfa), red, sizeof(red), msg,
  73. sizeof(msg)) == CHAT_REPLY_TOTP);
  74. CHECK(chat_login_parse_reply(200, pk, strlen(pk), red, sizeof(red), msg,
  75. sizeof(msg)) == CHAT_REPLY_PASSKEY);
  76. CHECK(chat_login_parse_reply(401, bad, strlen(bad), red, sizeof(red), msg,
  77. sizeof(msg)) == CHAT_REPLY_DENIED);
  78. CHECK(strcmp(msg, "Username and password does not match") == 0);
  79. CHECK(chat_login_parse_reply(500, "oops", 4, red, sizeof(red), msg,
  80. sizeof(msg)) == CHAT_REPLY_ERROR);
  81. CHECK(chat_login_parse_reply(200, "{}", 2, red, sizeof(red), msg,
  82. sizeof(msg)) == CHAT_REPLY_ERROR);
  83. }
  84. static void test_config(void)
  85. {
  86. char server[192], cookie[2048], out[2600];
  87. const char *cfg = "# comment\r\nserver=https://chat.example.com\r\n"
  88. "junk=1\ncookie=connect.sid=s%3Aabc.def; theme=x \n";
  89. CHECK(chat_config_parse(cfg, strlen(cfg), server, sizeof(server), cookie,
  90. sizeof(cookie)) == 0);
  91. CHECK(strcmp(server, "https://chat.example.com") == 0);
  92. CHECK(strcmp(cookie, "connect.sid=s%3Aabc.def; theme=x") == 0);
  93. CHECK(chat_config_parse("", 0, server, sizeof(server), cookie,
  94. sizeof(cookie)) == -1);
  95. CHECK(chat_config_format(out, sizeof(out), "https://c.test",
  96. "connect.sid=1") > 0);
  97. CHECK(chat_config_parse(out, strlen(out), server, sizeof(server), cookie,
  98. sizeof(cookie)) == 0);
  99. CHECK(strcmp(server, "https://c.test") == 0);
  100. CHECK(strcmp(cookie, "connect.sid=1") == 0);
  101. CHECK(chat_config_format(out, 10, "https://c.test", "x=1") == -1);
  102. }
  103. static void test_normalize_and_body(void)
  104. {
  105. char s[192], body[256];
  106. CHECK(chat_server_normalize("chat.example.com", s, sizeof(s)) == 0);
  107. CHECK(strcmp(s, "https://chat.example.com") == 0);
  108. CHECK(chat_server_normalize(" http://10.0.2.2:8080/// ", s, sizeof(s)) == 0);
  109. CHECK(strcmp(s, "http://10.0.2.2:8080") == 0);
  110. CHECK(chat_server_normalize("https://", s, sizeof(s)) == -1);
  111. CHECK(chat_server_normalize("", s, sizeof(s)) == -1);
  112. CHECK(chat_server_normalize("a b", s, sizeof(s)) == -1);
  113. CHECK(chat_login_body(body, sizeof(body), "al\"ice", "p\\w", "") > 0);
  114. CHECK(strcmp(body, "{\"username\":\"al\\\"ice\",\"password\":\"p\\\\w\",\"totpToken\":\"\"}") == 0);
  115. CHECK(chat_login_body(body, 16, "alice", "secret", "123456") == -1);
  116. }
  117. int main(void)
  118. {
  119. test_page();
  120. test_reply();
  121. test_config();
  122. test_normalize_and_body();
  123. if (failures) {
  124. printf("chat_login_test: %d failure(s)\n", failures);
  125. return 1;
  126. }
  127. printf("chat_login_test: all tests passed\n");
  128. return 0;
  129. }