Mercurial
comparison connectors/tests/auth_http_adapter_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 "connectors/auth_http_adapter.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 static const char k_secret_hex[] = | |
| 12 "30313233343536373839616263646566" | |
| 13 "30313233343536373839616263646566"; | |
| 14 | |
| 15 static Seobeo_Request_Entry *request_with( | |
| 16 Dowa_Arena *arena, const char *cookie, const char *origin, | |
| 17 const char *csrf) | |
| 18 { | |
| 19 Seobeo_Request_Entry *request = NULL; | |
| 20 Dowa_HashMap_Push_Arena(request, "Host", "localhost:6981", arena); | |
| 21 if (cookie) | |
| 22 Dowa_HashMap_Push_Arena(request, "Cookie", (char *)cookie, arena); | |
| 23 if (origin) | |
| 24 Dowa_HashMap_Push_Arena(request, "Origin", (char *)origin, arena); | |
| 25 if (csrf) | |
| 26 Dowa_HashMap_Push_Arena( | |
| 27 request, "X-CSRF-Token", (char *)csrf, arena); | |
| 28 return request; | |
| 29 } | |
| 30 | |
| 31 int main(void) | |
| 32 { | |
| 33 char database[256]; | |
| 34 snprintf( | |
| 35 database, sizeof(database), "connector_auth_%ld_%lld.db", | |
| 36 (long)getpid(), (long long)time(NULL)); | |
| 37 unlink(database); | |
| 38 Auth_Store *store = Auth_Store_Create(database); | |
| 39 assert(store); | |
| 40 char password_hash[AUTH_CRYPTO_PASSWORD_HASH_ENCODED_SIZE]; | |
| 41 assert(Auth_Crypto_Password_Hash( | |
| 42 "test-password", password_hash, sizeof(password_hash)) == | |
| 43 AUTH_CRYPTO_OK); | |
| 44 char user_id[37]; | |
| 45 assert(Auth_Store_Create_User( | |
| 46 store, "connectoruser", password_hash, "member", FALSE, user_id) == | |
| 47 AUTH_STORE_OK); | |
| 48 char token[AUTH_CRYPTO_TOKEN_SIZE]; | |
| 49 char token_digest[AUTH_CRYPTO_TOKEN_DIGEST_SIZE]; | |
| 50 char csrf[AUTH_CRYPTO_TOKEN_SIZE]; | |
| 51 char csrf_digest[AUTH_CRYPTO_TOKEN_DIGEST_SIZE]; | |
| 52 assert(Auth_Crypto_Token_Generate(token, sizeof(token)) == AUTH_CRYPTO_OK); | |
| 53 assert(Auth_Crypto_Token_Digest( | |
| 54 token, token_digest, sizeof(token_digest)) == AUTH_CRYPTO_OK); | |
| 55 assert(Auth_HTTP_Derive_CSRF( | |
| 56 k_secret, sizeof(k_secret) - 1, token_digest, csrf, sizeof(csrf))); | |
| 57 assert(Auth_Crypto_Token_Digest( | |
| 58 csrf, csrf_digest, sizeof(csrf_digest)) == AUTH_CRYPTO_OK); | |
| 59 Auth_Session_Record session; | |
| 60 int64 now = (int64)time(NULL); | |
| 61 assert(Auth_Store_Create_Session( | |
| 62 store, user_id, token_digest, csrf_digest, 3600, 86400, now, | |
| 63 &session) == AUTH_STORE_OK); | |
| 64 Auth_Store_Destroy(store); | |
| 65 | |
| 66 Connector_Auth_HTTP_Context context; | |
| 67 assert(Connector_Auth_HTTP_Init( | |
| 68 &context, database, k_secret_hex, 3600)); | |
| 69 Connector_Auth_Adapter adapter = | |
| 70 Connector_Auth_HTTP_Create_Adapter(&context); | |
| 71 Dowa_Arena *arena = Dowa_Arena_Create(32 * 1024); | |
| 72 char cookie[AUTH_CRYPTO_TOKEN_SIZE + 32]; | |
| 73 snprintf(cookie, sizeof(cookie), "mjj_session=%s", token); | |
| 74 | |
| 75 Seobeo_Request_Entry *read_request = | |
| 76 request_with(arena, cookie, NULL, NULL); | |
| 77 const char *resolved = adapter.resolve_user( | |
| 78 read_request, FALSE, arena, adapter.context); | |
| 79 assert(resolved && !strcmp(resolved, user_id)); | |
| 80 Connector_Auth_Session connector_session; | |
| 81 assert(adapter.resolve_session( | |
| 82 read_request, &connector_session, adapter.context)); | |
| 83 assert(!strcmp(connector_session.user_id, user_id)); | |
| 84 assert(!strcmp(connector_session.username, "connectoruser")); | |
| 85 assert(!strcmp(connector_session.role, "member")); | |
| 86 assert(!strcmp(connector_session.csrf_token, csrf)); | |
| 87 assert(!adapter.resolve_user( | |
| 88 read_request, TRUE, arena, adapter.context)); | |
| 89 | |
| 90 Seobeo_Request_Entry *write_request = request_with( | |
| 91 arena, cookie, "http://localhost:6981", csrf); | |
| 92 resolved = adapter.resolve_user( | |
| 93 write_request, TRUE, arena, adapter.context); | |
| 94 assert(resolved && !strcmp(resolved, user_id)); | |
| 95 | |
| 96 Seobeo_Request_Entry *wrong_origin = request_with( | |
| 97 arena, cookie, "https://evil.example", csrf); | |
| 98 assert(!adapter.resolve_user( | |
| 99 wrong_origin, TRUE, arena, adapter.context)); | |
| 100 Seobeo_Request_Entry *wrong_cookie = request_with( | |
| 101 arena, "mjj_session=invalid", NULL, NULL); | |
| 102 assert(!adapter.resolve_user( | |
| 103 wrong_cookie, FALSE, arena, adapter.context)); | |
| 104 assert(!adapter.resolve_session( | |
| 105 wrong_cookie, &connector_session, adapter.context)); | |
| 106 | |
| 107 Dowa_Arena_Free(arena); | |
| 108 Connector_Auth_HTTP_Destroy(&context); | |
| 109 unlink(database); | |
| 110 puts("auth_http_adapter_test: ok"); | |
| 111 return 0; | |
| 112 } |