comparison auth/test/auth_http_test.c @ 279:b3b547563ec7

Add Google connector service and agent wiki Implement the C/Seobeo Google Drive and Gmail connector with encrypted OAuth storage, Zenbu authentication, browser testing, AI tool discovery, chunked HTTP decoding, and Bazel coverage. Consolidate repository guidance into progressive wiki documentation and enforce arena-first allocation for new first-party C code. Co-authored-by: Copilot <[email protected]> Copilot-Session: 84c338fd-0939-4bb3-b7f3-1062eb213e5d
author MrJuneJune <me@mrjunejune.com>
date Mon, 17 Aug 2026 22:22:36 -0700
parents
children
comparison
equal deleted inserted replaced
278:8d560f50ed4c 279:b3b547563ec7
1 #include "auth/auth_http.h"
2
3 #include <assert.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <time.h>
7 #include <unistd.h>
8
9 static const uint8 k_secret[] =
10 "0123456789abcdef0123456789abcdef";
11
12 static Seobeo_Request_Entry *make_request(
13 Dowa_Arena *p_arena,
14 const char *host,
15 const char *origin,
16 const char *cookie,
17 const char *csrf)
18 {
19 Seobeo_Request_Entry *request = NULL;
20 if (host)
21 Dowa_HashMap_Push_Arena(request, "Host", (char *)host, p_arena);
22 if (origin)
23 Dowa_HashMap_Push_Arena(request, "Origin", (char *)origin, p_arena);
24 if (cookie)
25 Dowa_HashMap_Push_Arena(request, "Cookie", (char *)cookie, p_arena);
26 if (csrf)
27 Dowa_HashMap_Push_Arena(
28 request, "X-CSRF-Token", (char *)csrf, p_arena);
29 return request;
30 }
31
32 int main(void)
33 {
34 Dowa_Arena *p_arena = Dowa_Arena_Create(16 * 1024);
35 assert(p_arena);
36
37 char cookie_value[16];
38 assert(Auth_HTTP_Parse_Cookie(
39 "other=x; mjj_session=token; final=y",
40 AUTH_HTTP_SESSION_COOKIE_NAME, cookie_value, sizeof(cookie_value)));
41 assert(strcmp(cookie_value, "token") == 0);
42 assert(!Auth_HTTP_Parse_Cookie(
43 "not_mjj_session=token", AUTH_HTTP_SESSION_COOKIE_NAME,
44 cookie_value, sizeof(cookie_value)));
45
46 Seobeo_Request_Entry *same_origin = make_request(
47 p_arena, "localhost:6969", "http://localhost:6969", NULL, NULL);
48 Seobeo_Request_Entry *wrong_origin = make_request(
49 p_arena, "localhost:6969", "https://example.com", NULL, NULL);
50 assert(Auth_HTTP_Same_Origin(same_origin));
51 assert(!Auth_HTTP_Same_Origin(wrong_origin));
52
53 char csrf[AUTH_CRYPTO_TOKEN_SIZE] = {0};
54 assert(Auth_HTTP_Derive_CSRF(
55 k_secret, sizeof(k_secret) - 1, "binding", csrf, sizeof(csrf)));
56 Seobeo_Request_Entry *csrf_request = make_request(
57 p_arena, "localhost", "https://localhost", NULL, csrf);
58 assert(Auth_HTTP_Verify_CSRF(
59 csrf_request, k_secret, sizeof(k_secret) - 1, "binding"));
60 assert(!Auth_HTTP_Verify_CSRF_Token(
61 k_secret, sizeof(k_secret) - 1, "binding", "wrong-token"));
62 assert(!Auth_HTTP_Verify_CSRF(
63 wrong_origin, k_secret, sizeof(k_secret) - 1, "binding"));
64
65 char database_path[256];
66 snprintf(database_path, sizeof(database_path),
67 "auth_http_test_%ld_%ld.db", (long)getpid(), (long)time(NULL));
68 unlink(database_path);
69 Auth_Store *p_store = Auth_Store_Create(database_path);
70 assert(p_store);
71
72 char password_hash[AUTH_CRYPTO_PASSWORD_HASH_ENCODED_SIZE];
73 assert(Auth_Crypto_Password_Hash(
74 "test-password", password_hash, sizeof(password_hash)) ==
75 AUTH_CRYPTO_OK);
76 char user_id[37];
77 assert(Auth_Store_Create_User(
78 p_store, "httpuser", password_hash, "member", FALSE, user_id) ==
79 AUTH_STORE_OK);
80
81 char token[AUTH_CRYPTO_TOKEN_SIZE];
82 char token_digest[AUTH_CRYPTO_TOKEN_DIGEST_SIZE];
83 char stored_csrf_digest[AUTH_CRYPTO_TOKEN_DIGEST_SIZE];
84 assert(Auth_Crypto_Token_Generate(token, sizeof(token)) == AUTH_CRYPTO_OK);
85 assert(Auth_Crypto_Token_Digest(
86 token, token_digest, sizeof(token_digest)) == AUTH_CRYPTO_OK);
87 assert(Auth_Crypto_Token_Digest(
88 csrf, stored_csrf_digest, sizeof(stored_csrf_digest)) == AUTH_CRYPTO_OK);
89 int64 now = (int64)time(NULL);
90 Auth_Session_Record session;
91 assert(Auth_Store_Create_Session(
92 p_store, user_id, token_digest, stored_csrf_digest,
93 3600, 86400, now, &session) == AUTH_STORE_OK);
94
95 char cookie_header[AUTH_CRYPTO_TOKEN_SIZE + 32];
96 snprintf(cookie_header, sizeof(cookie_header), "%s=%s",
97 AUTH_HTTP_SESSION_COOKIE_NAME, token);
98 Seobeo_Request_Entry *user_request = make_request(
99 p_arena, "localhost", NULL, cookie_header, NULL);
100 Auth_HTTP_Authenticated_User user;
101 assert(Auth_HTTP_Resolve_Authenticated_User(
102 user_request, p_store, k_secret, sizeof(k_secret) - 1,
103 now + 1, 3600, &user) == AUTH_HTTP_RESOLVE_OK);
104 assert(strcmp(user.user.id, user_id) == 0);
105 assert(strcmp(user.token_digest, token_digest) == 0);
106 assert(user.csrf_token[0] != '\0');
107 assert(Auth_Store_Revoke_Session(p_store, token_digest) == AUTH_STORE_OK);
108 assert(Auth_HTTP_Resolve_Authenticated_User(
109 user_request, p_store, k_secret, sizeof(k_secret) - 1,
110 now + 2, 3600, &user) == AUTH_HTTP_RESOLVE_NOT_FOUND);
111
112 Seobeo_Request_Entry *anonymous_request = make_request(
113 p_arena, "localhost", NULL, NULL, NULL);
114 assert(Auth_HTTP_Resolve_Authenticated_User(
115 anonymous_request, p_store, k_secret, sizeof(k_secret) - 1,
116 now + 1, 3600, &user) == AUTH_HTTP_RESOLVE_NOT_FOUND);
117
118 Auth_Store_Destroy(p_store);
119 unlink(database_path);
120 Dowa_Arena_Free(p_arena);
121 puts("auth_http_test: PASS");
122 return 0;
123 }