uhttp_cookie_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Host tests for uhttp_cookie.c (make test-chat). */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "uhttp_cookie.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. static struct uhttp_jar jar;
  13. static const char *hdr(const char *host, const char *path, int secure)
  14. {
  15. static char out[2048];
  16. uhttp_jar_header(&jar, host, path, secure, out, sizeof(out));
  17. return out;
  18. }
  19. static void test_basic(void)
  20. {
  21. uhttp_jar_init(&jar);
  22. CHECK(uhttp_jar_set(&jar, "chat.example.com", "/auth/login",
  23. "connect.sid=s%3Aabc.def; Path=/; Expires=Mon, 23 Nov 2026 18:03:29 GMT; HttpOnly; Secure; SameSite=Lax") == 1);
  24. CHECK(strcmp(hdr("chat.example.com", "/api/me", 1),
  25. "connect.sid=s%3Aabc.def") == 0);
  26. /* Secure cookie never goes over http. */
  27. CHECK(strcmp(hdr("chat.example.com", "/api/me", 0), "") == 0);
  28. /* Host-only: another host gets nothing, even a subdomain. */
  29. CHECK(strcmp(hdr("my.chat.example.com", "/", 1), "") == 0);
  30. CHECK(strcmp(hdr("CHAT.example.com", "/", 1),
  31. "connect.sid=s%3Aabc.def") == 0);
  32. /* Same value again: no change. */
  33. CHECK(uhttp_jar_set(&jar, "chat.example.com", "/",
  34. "connect.sid=s%3Aabc.def; Path=/; Secure") == 0);
  35. /* Rolled value replaces it. */
  36. CHECK(uhttp_jar_set(&jar, "chat.example.com", "/",
  37. "connect.sid=s%3Anew; Path=/; Secure") == 1);
  38. CHECK(strcmp(hdr("chat.example.com", "/", 1),
  39. "connect.sid=s%3Anew") == 0);
  40. CHECK(uhttp_jar_count(&jar, "chat.example.com") == 1);
  41. }
  42. static void test_paths(void)
  43. {
  44. const char *uid = "/openid/interaction/UdA3nm";
  45. uhttp_jar_init(&jar);
  46. CHECK(uhttp_jar_set(&jar, "id.example.com", uid,
  47. "_interaction=abc; path=/openid/interaction/UdA3nm; httponly; samesite=lax; secure") == 1);
  48. CHECK(uhttp_jar_set(&jar, "id.example.com", uid,
  49. "_interaction_resume=r1; path=/openid/auth/UdA3nm; secure") == 1);
  50. CHECK(strcmp(hdr("id.example.com", "/openid/interaction/UdA3nm/login", 1),
  51. "_interaction=abc") == 0);
  52. CHECK(strcmp(hdr("id.example.com", "/openid/interaction/UdA3nm", 1),
  53. "_interaction=abc") == 0);
  54. CHECK(strcmp(hdr("id.example.com", "/openid/interaction/UdA3nmX", 1),
  55. "") == 0);
  56. CHECK(strcmp(hdr("id.example.com", "/openid/auth/UdA3nm?x=1", 1),
  57. "_interaction_resume=r1") == 0);
  58. /* Default path is the request directory. */
  59. CHECK(uhttp_jar_set(&jar, "a.test", "/dir/page?q=1", "k=v") == 1);
  60. CHECK(strcmp(hdr("a.test", "/dir/other", 0), "k=v") == 0);
  61. CHECK(strcmp(hdr("a.test", "/dir", 0), "k=v") == 0);
  62. CHECK(strcmp(hdr("a.test", "/", 0), "") == 0);
  63. CHECK(uhttp_jar_set(&jar, "a.test", "/page", "root=1") == 1);
  64. CHECK(strcmp(hdr("a.test", "/x", 0), "root=1") == 0);
  65. /* Longer path first. */
  66. CHECK(strcmp(hdr("a.test", "/dir/x", 0), "k=v; root=1") == 0);
  67. }
  68. static void test_delete_and_errors(void)
  69. {
  70. uhttp_jar_init(&jar);
  71. CHECK(uhttp_jar_set(&jar, "h", "/", "a=1; Path=/") == 1);
  72. CHECK(uhttp_jar_set(&jar, "h", "/", "b = 2 ; Path=/") == 1);
  73. CHECK(strcmp(hdr("h", "/", 0), "a=1; b=2") == 0);
  74. CHECK(uhttp_jar_set(&jar, "h", "/", "a=; Path=/; Max-Age=0") == 1);
  75. CHECK(strcmp(hdr("h", "/", 0), "b=2") == 0);
  76. CHECK(uhttp_jar_set(&jar, "h", "/",
  77. "b=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT") == 1);
  78. CHECK(strcmp(hdr("h", "/", 0), "") == 0);
  79. /* Deleting a cookie that does not exist changes nothing. */
  80. CHECK(uhttp_jar_set(&jar, "h", "/", "zz=; Max-Age=0") == 0);
  81. CHECK(uhttp_jar_set(&jar, "h", "/", "novalue") == -1);
  82. CHECK(uhttp_jar_set(&jar, "h", "/", "=v") == -1);
  83. CHECK(uhttp_jar_set(&jar, "h", "/", "") == -1);
  84. CHECK(uhttp_jar_set(&jar, "h", "/", "c=\"quoted\"; Max-Age=3600") == 1);
  85. CHECK(strcmp(hdr("h", "/", 0), "c=\"quoted\"") == 0);
  86. }
  87. static void test_import_and_limits(void)
  88. {
  89. char big[1000];
  90. char out[32];
  91. int i;
  92. uhttp_jar_init(&jar);
  93. CHECK(uhttp_jar_import(&jar, "chat.example.com", 1,
  94. "connect.sid=s%3Axyz; theme=dark ; bad;=x") == 2);
  95. CHECK(strcmp(hdr("chat.example.com", "/api/channels", 1),
  96. "connect.sid=s%3Axyz; theme=dark") == 0);
  97. CHECK(strcmp(hdr("chat.example.com", "/api/channels", 0), "") == 0);
  98. /* An oversized value is refused instead of truncated. */
  99. memset(big, 'v', sizeof(big));
  100. memcpy(big, "x=", 2);
  101. big[sizeof(big) - 1] = '\0';
  102. CHECK(uhttp_jar_set(&jar, "h", "/", big) == -1);
  103. /* Truncation happens at whole cookies. */
  104. uhttp_jar_init(&jar);
  105. uhttp_jar_set(&jar, "h", "/", "aaaa=1111");
  106. uhttp_jar_set(&jar, "h", "/", "bbbb=2222");
  107. uhttp_jar_set(&jar, "h", "/", "cccc=3333");
  108. CHECK(uhttp_jar_header(&jar, "h", "/", 0, out, 24) == 20);
  109. CHECK(strcmp(out, "aaaa=1111; bbbb=2222") == 0);
  110. /* A full jar evicts the oldest cookie. */
  111. uhttp_jar_init(&jar);
  112. for (i = 0; i < UHTTP_COOKIE_MAX + 2; i++) {
  113. char c[32];
  114. snprintf(c, sizeof(c), "n%d=v%d", i, i);
  115. CHECK(uhttp_jar_set(&jar, "h", "/", c) == 1);
  116. }
  117. CHECK(uhttp_jar_count(&jar, "h") == UHTTP_COOKIE_MAX);
  118. CHECK(strstr(hdr("h", "/", 0), "n0=") == NULL);
  119. CHECK(strstr(hdr("h", "/", 0), "n1=") == NULL);
  120. CHECK(strstr(hdr("h", "/", 0), "n2=v2") != NULL);
  121. /* Updating a cookie makes it recently used: it survives eviction. */
  122. uhttp_jar_init(&jar);
  123. CHECK(uhttp_jar_set(&jar, "h", "/", "session=a") == 1);
  124. for (i = 0; i < UHTTP_COOKIE_MAX - 1; i++) {
  125. char c[32];
  126. snprintf(c, sizeof(c), "x%d=v", i);
  127. CHECK(uhttp_jar_set(&jar, "h", "/", c) == 1);
  128. }
  129. CHECK(uhttp_jar_set(&jar, "h", "/", "session=b") == 1);
  130. CHECK(uhttp_jar_set(&jar, "h", "/", "late=1") == 1);
  131. CHECK(strstr(hdr("h", "/", 0), "session=b") != NULL);
  132. CHECK(strstr(hdr("h", "/", 0), "x0=") == NULL);
  133. uhttp_jar_clear(&jar);
  134. CHECK(uhttp_jar_count(&jar, "h") == 0);
  135. }
  136. int main(void)
  137. {
  138. test_basic();
  139. test_paths();
  140. test_delete_and_errors();
  141. test_import_and_limits();
  142. if (failures) {
  143. printf("uhttp_cookie_test: %d failure(s)\n", failures);
  144. return 1;
  145. }
  146. printf("uhttp_cookie_test: all tests passed\n");
  147. return 0;
  148. }