/* Host tests for uhttp_cookie.c (make test-chat). */ #include #include #include "uhttp_cookie.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static struct uhttp_jar jar; static const char *hdr(const char *host, const char *path, int secure) { static char out[2048]; uhttp_jar_header(&jar, host, path, secure, out, sizeof(out)); return out; } static void test_basic(void) { uhttp_jar_init(&jar); CHECK(uhttp_jar_set(&jar, "chat.example.com", "/auth/login", "connect.sid=s%3Aabc.def; Path=/; Expires=Mon, 23 Nov 2026 18:03:29 GMT; HttpOnly; Secure; SameSite=Lax") == 1); CHECK(strcmp(hdr("chat.example.com", "/api/me", 1), "connect.sid=s%3Aabc.def") == 0); /* Secure cookie never goes over http. */ CHECK(strcmp(hdr("chat.example.com", "/api/me", 0), "") == 0); /* Host-only: another host gets nothing, even a subdomain. */ CHECK(strcmp(hdr("my.chat.example.com", "/", 1), "") == 0); CHECK(strcmp(hdr("CHAT.example.com", "/", 1), "connect.sid=s%3Aabc.def") == 0); /* Same value again: no change. */ CHECK(uhttp_jar_set(&jar, "chat.example.com", "/", "connect.sid=s%3Aabc.def; Path=/; Secure") == 0); /* Rolled value replaces it. */ CHECK(uhttp_jar_set(&jar, "chat.example.com", "/", "connect.sid=s%3Anew; Path=/; Secure") == 1); CHECK(strcmp(hdr("chat.example.com", "/", 1), "connect.sid=s%3Anew") == 0); CHECK(uhttp_jar_count(&jar, "chat.example.com") == 1); } static void test_paths(void) { const char *uid = "/openid/interaction/UdA3nm"; uhttp_jar_init(&jar); CHECK(uhttp_jar_set(&jar, "id.example.com", uid, "_interaction=abc; path=/openid/interaction/UdA3nm; httponly; samesite=lax; secure") == 1); CHECK(uhttp_jar_set(&jar, "id.example.com", uid, "_interaction_resume=r1; path=/openid/auth/UdA3nm; secure") == 1); CHECK(strcmp(hdr("id.example.com", "/openid/interaction/UdA3nm/login", 1), "_interaction=abc") == 0); CHECK(strcmp(hdr("id.example.com", "/openid/interaction/UdA3nm", 1), "_interaction=abc") == 0); CHECK(strcmp(hdr("id.example.com", "/openid/interaction/UdA3nmX", 1), "") == 0); CHECK(strcmp(hdr("id.example.com", "/openid/auth/UdA3nm?x=1", 1), "_interaction_resume=r1") == 0); /* Default path is the request directory. */ CHECK(uhttp_jar_set(&jar, "a.test", "/dir/page?q=1", "k=v") == 1); CHECK(strcmp(hdr("a.test", "/dir/other", 0), "k=v") == 0); CHECK(strcmp(hdr("a.test", "/dir", 0), "k=v") == 0); CHECK(strcmp(hdr("a.test", "/", 0), "") == 0); CHECK(uhttp_jar_set(&jar, "a.test", "/page", "root=1") == 1); CHECK(strcmp(hdr("a.test", "/x", 0), "root=1") == 0); /* Longer path first. */ CHECK(strcmp(hdr("a.test", "/dir/x", 0), "k=v; root=1") == 0); } static void test_delete_and_errors(void) { uhttp_jar_init(&jar); CHECK(uhttp_jar_set(&jar, "h", "/", "a=1; Path=/") == 1); CHECK(uhttp_jar_set(&jar, "h", "/", "b = 2 ; Path=/") == 1); CHECK(strcmp(hdr("h", "/", 0), "a=1; b=2") == 0); CHECK(uhttp_jar_set(&jar, "h", "/", "a=; Path=/; Max-Age=0") == 1); CHECK(strcmp(hdr("h", "/", 0), "b=2") == 0); CHECK(uhttp_jar_set(&jar, "h", "/", "b=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT") == 1); CHECK(strcmp(hdr("h", "/", 0), "") == 0); /* Deleting a cookie that does not exist changes nothing. */ CHECK(uhttp_jar_set(&jar, "h", "/", "zz=; Max-Age=0") == 0); CHECK(uhttp_jar_set(&jar, "h", "/", "novalue") == -1); CHECK(uhttp_jar_set(&jar, "h", "/", "=v") == -1); CHECK(uhttp_jar_set(&jar, "h", "/", "") == -1); CHECK(uhttp_jar_set(&jar, "h", "/", "c=\"quoted\"; Max-Age=3600") == 1); CHECK(strcmp(hdr("h", "/", 0), "c=\"quoted\"") == 0); } static void test_import_and_limits(void) { char big[1000]; char out[32]; int i; uhttp_jar_init(&jar); CHECK(uhttp_jar_import(&jar, "chat.example.com", 1, "connect.sid=s%3Axyz; theme=dark ; bad;=x") == 2); CHECK(strcmp(hdr("chat.example.com", "/api/channels", 1), "connect.sid=s%3Axyz; theme=dark") == 0); CHECK(strcmp(hdr("chat.example.com", "/api/channels", 0), "") == 0); /* An oversized value is refused instead of truncated. */ memset(big, 'v', sizeof(big)); memcpy(big, "x=", 2); big[sizeof(big) - 1] = '\0'; CHECK(uhttp_jar_set(&jar, "h", "/", big) == -1); /* Truncation happens at whole cookies. */ uhttp_jar_init(&jar); uhttp_jar_set(&jar, "h", "/", "aaaa=1111"); uhttp_jar_set(&jar, "h", "/", "bbbb=2222"); uhttp_jar_set(&jar, "h", "/", "cccc=3333"); CHECK(uhttp_jar_header(&jar, "h", "/", 0, out, 24) == 20); CHECK(strcmp(out, "aaaa=1111; bbbb=2222") == 0); /* A full jar evicts the oldest cookie. */ uhttp_jar_init(&jar); for (i = 0; i < UHTTP_COOKIE_MAX + 2; i++) { char c[32]; snprintf(c, sizeof(c), "n%d=v%d", i, i); CHECK(uhttp_jar_set(&jar, "h", "/", c) == 1); } CHECK(uhttp_jar_count(&jar, "h") == UHTTP_COOKIE_MAX); CHECK(strstr(hdr("h", "/", 0), "n0=") == NULL); CHECK(strstr(hdr("h", "/", 0), "n1=") == NULL); CHECK(strstr(hdr("h", "/", 0), "n2=v2") != NULL); /* Updating a cookie makes it recently used: it survives eviction. */ uhttp_jar_init(&jar); CHECK(uhttp_jar_set(&jar, "h", "/", "session=a") == 1); for (i = 0; i < UHTTP_COOKIE_MAX - 1; i++) { char c[32]; snprintf(c, sizeof(c), "x%d=v", i); CHECK(uhttp_jar_set(&jar, "h", "/", c) == 1); } CHECK(uhttp_jar_set(&jar, "h", "/", "session=b") == 1); CHECK(uhttp_jar_set(&jar, "h", "/", "late=1") == 1); CHECK(strstr(hdr("h", "/", 0), "session=b") != NULL); CHECK(strstr(hdr("h", "/", 0), "x0=") == NULL); uhttp_jar_clear(&jar); CHECK(uhttp_jar_count(&jar, "h") == 0); } int main(void) { test_basic(); test_paths(); test_delete_and_errors(); test_import_and_limits(); if (failures) { printf("uhttp_cookie_test: %d failure(s)\n", failures); return 1; } printf("uhttp_cookie_test: all tests passed\n"); return 0; }