Mercurial
comparison connectors/tests/google_provider_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/connector.h" | |
| 2 | |
| 3 #include <assert.h> | |
| 4 #include <stdio.h> | |
| 5 #include <string.h> | |
| 6 | |
| 7 typedef struct { | |
| 8 int32 calls; | |
| 9 char method[16]; | |
| 10 char url[2048]; | |
| 11 char body[4096]; | |
| 12 char token_body[4096]; | |
| 13 } Fake_Google; | |
| 14 | |
| 15 static Connector_Status fake_transport( | |
| 16 const Connector_HTTP_Request *request, const char *access_token, | |
| 17 Connector_HTTP_Response *response, Dowa_Arena *arena, void *context) | |
| 18 { | |
| 19 Fake_Google *fake = context; | |
| 20 ++fake->calls; | |
| 21 strcpy(fake->method, request->method); | |
| 22 strcpy(fake->url, request->url); | |
| 23 if (request->body && request->body_length < sizeof(fake->body)) { | |
| 24 memcpy(fake->body, request->body, request->body_length); | |
| 25 fake->body[request->body_length] = '\0'; | |
| 26 } else | |
| 27 fake->body[0] = '\0'; | |
| 28 const char *body; | |
| 29 if (strstr(request->url, "userinfo")) { | |
| 30 assert(access_token && !strcmp(access_token, "new-access")); | |
| 31 body = "{\"sub\":\"subject-7\",\"email\":\"[email protected]\"}"; | |
| 32 } else if (strstr(request->url, "token")) { | |
| 33 strcpy(fake->token_body, fake->body); | |
| 34 body = strstr(fake->body, "grant_type=refresh_token") | |
| 35 ? "{\"access_token\":\"refreshed\",\"expires_in\":3600}" | |
| 36 : "{\"access_token\":\"new-access\",\"refresh_token\":\"refresh\"," | |
| 37 "\"expires_in\":3600,\"scope\":\"drive gmail\"}"; | |
| 38 } else if (strstr(request->url, "revoke")) | |
| 39 body = "{}"; | |
| 40 else | |
| 41 body = "{\"ok\":true}"; | |
| 42 response->status_code = 200; | |
| 43 response->body_length = strlen(body); | |
| 44 response->body = Dowa_Arena_Allocate(arena, response->body_length + 1); | |
| 45 strcpy(response->body, body); | |
| 46 return CONNECTOR_OK; | |
| 47 } | |
| 48 | |
| 49 static Connector_Google_Config config(Fake_Google *fake) | |
| 50 { | |
| 51 Connector_Google_Config config = { | |
| 52 .client_id = "client id", | |
| 53 .client_secret = "secret&value", | |
| 54 .redirect_uri = "https://example.test/callback", | |
| 55 .oauth_authorize_url = "https://fake/authorize", | |
| 56 .oauth_token_url = "https://fake/token", | |
| 57 .oauth_revoke_url = "https://fake/revoke", | |
| 58 .identity_url = "https://fake/userinfo", | |
| 59 .drive_api_url = "https://fake", | |
| 60 .drive_upload_url = "https://fake", | |
| 61 .gmail_api_url = "https://fake", | |
| 62 .transport = fake_transport, | |
| 63 .transport_context = fake | |
| 64 }; | |
| 65 return config; | |
| 66 } | |
| 67 | |
| 68 int main(void) | |
| 69 { | |
| 70 Dowa_Arena *arena = Dowa_Arena_Create(256 * 1024); | |
| 71 Fake_Google fake = {0}; | |
| 72 Connector_Google_Config google = config(&fake); | |
| 73 Connector_OAuth_Start start; | |
| 74 assert(Connector_OAuth_PKCE_Start(&start)); | |
| 75 char *authorization = Connector_Google_Authorization_URL( | |
| 76 &google, &start, arena); | |
| 77 assert(authorization && strstr(authorization, "code_challenge_method=S256")); | |
| 78 assert(strstr(authorization, "client_id=client%20id")); | |
| 79 | |
| 80 Connector_Account account; | |
| 81 Connector_Provider_Error error; | |
| 82 assert(Connector_Google_Exchange_Code( | |
| 83 &google, "code+value", start.code_verifier, &account, &error, arena) == | |
| 84 CONNECTOR_OK); | |
| 85 assert(!strcmp(account.account_id, "google:subject-7")); | |
| 86 assert(!strcmp(account.email, "[email protected]")); | |
| 87 assert(strstr(fake.token_body, "code=code%2Bvalue")); | |
| 88 assert(strstr(fake.token_body, "client_secret=secret%26value")); | |
| 89 | |
| 90 assert(Connector_Google_Refresh(&google, &account, arena) == CONNECTOR_OK); | |
| 91 assert(!strcmp(account.access_token, "refreshed")); | |
| 92 assert(strstr(fake.token_body, "grant_type=refresh_token")); | |
| 93 assert(Connector_Google_Revoke(&google, account.refresh_token, arena) == | |
| 94 CONNECTOR_OK); | |
| 95 | |
| 96 Connector_Provider_Request request = { | |
| 97 .method = "GET", | |
| 98 .path = "/drive/v3/files", | |
| 99 .query = "pageSize=10" | |
| 100 }; | |
| 101 Connector_Provider_Response response; | |
| 102 assert(Connector_Google_Execute( | |
| 103 &google, CONNECTOR_OP_DRIVE_LIST, &request, account.access_token, | |
| 104 &response, arena) == CONNECTOR_OK); | |
| 105 assert(!strcmp(fake.method, "GET")); | |
| 106 assert(!strcmp(fake.url, "https://fake/drive/v3/files?pageSize=10")); | |
| 107 | |
| 108 request.method = "POST"; | |
| 109 request.path = "/gmail/v1/users/me/messages/send"; | |
| 110 request.query = NULL; | |
| 111 request.body = "{\"raw\":\"abc\"}"; | |
| 112 request.body_length = strlen(request.body); | |
| 113 assert(Connector_Google_Execute( | |
| 114 &google, CONNECTOR_OP_GMAIL_SEND, &request, account.access_token, | |
| 115 &response, arena) == CONNECTOR_OK); | |
| 116 assert(!strcmp(fake.method, "POST")); | |
| 117 assert(strstr(fake.url, "/gmail/v1/users/me/messages/send")); | |
| 118 | |
| 119 assert(Connector_Operation_Is_Allowed(CONNECTOR_OP_DRIVE_UPDATE)); | |
| 120 assert(Connector_Operation_Is_Mutation(CONNECTOR_OP_GMAIL_SEND)); | |
| 121 assert(!Connector_Operation_Is_Mutation(CONNECTOR_OP_GMAIL_HISTORY)); | |
| 122 Dowa_Arena_Free(arena); | |
| 123 printf("google_provider_test: ok\n"); | |
| 124 return 0; | |
| 125 } |