|
| 1 | +// © Copyright 2026 Query Farm LLC - https://query.farm |
| 2 | +// Child process for the native Windows OAuth store tests. |
| 3 | + |
| 4 | +#include "vgi_oauth.hpp" |
| 5 | +#include "vgi_oauth_store.hpp" |
| 6 | + |
| 7 | +#ifndef WIN32_LEAN_AND_MEAN |
| 8 | +#define WIN32_LEAN_AND_MEAN |
| 9 | +#endif |
| 10 | +#include <windows.h> |
| 11 | + |
| 12 | +#include <chrono> |
| 13 | +#include <iostream> |
| 14 | +#include <stdexcept> |
| 15 | +#include <string> |
| 16 | +#include <thread> |
| 17 | + |
| 18 | +using namespace duckdb::vgi; |
| 19 | + |
| 20 | +static void Check(bool condition, const char *message) { |
| 21 | + if (!condition) { |
| 22 | + throw std::runtime_error(message); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +static OAuthRefreshContext TestBinding() { |
| 27 | + OAuthRefreshContext ctx; |
| 28 | + ctx.token_endpoint = "https://issuer.example.invalid/token"; |
| 29 | + ctx.issuer = "https://issuer.example.invalid"; |
| 30 | + ctx.resource = "https://resource.example.invalid/vgi"; |
| 31 | + ctx.client_id = "test-client"; |
| 32 | + ctx.scope = "openid offline_access"; |
| 33 | + return ctx; |
| 34 | +} |
| 35 | + |
| 36 | +int main(int argc, char **argv) { |
| 37 | + try { |
| 38 | + Check(argc >= 4, "Usage: oauth_store_probe operation key mode [value]"); |
| 39 | + const std::string operation = argv[1], key = argv[2], mode = argv[3]; |
| 40 | + const auto binding = TestBinding(); |
| 41 | + auto lease = AcquireOAuthCredentialLease(key, mode); |
| 42 | + // All persistent test cases have an available backend. In particular, |
| 43 | + // auto must not silently fall back after encountering an abandoned mutex. |
| 44 | + Check(bool(lease) == (mode == "persistent" || mode == "auto"), "Unexpected credential lease availability"); |
| 45 | + if (operation == "store") { |
| 46 | + Check(argc == 5, "Missing synthetic token"); |
| 47 | + StoreOAuthRefreshToken(key, binding, mode, argv[4]); |
| 48 | + } else if (operation == "read") { |
| 49 | + Check(argc == 5, "Missing expected token"); |
| 50 | + std::string token; |
| 51 | + Check(LoadOAuthRefreshToken(key, binding, mode, token), "Credential missing"); |
| 52 | + Check(token == argv[4], "Unexpected stored token"); |
| 53 | + } else if (operation == "absent") { |
| 54 | + std::string token; |
| 55 | + Check(!LoadOAuthRefreshToken(key, binding, mode, token), "Credential unexpectedly present"); |
| 56 | + } else if (operation == "delete") { |
| 57 | + DeleteOAuthRefreshToken(key, mode); |
| 58 | + } else if (operation == "increment") { |
| 59 | + std::string token; |
| 60 | + Check(LoadOAuthRefreshToken(key, binding, mode, token), "Counter missing"); |
| 61 | + std::this_thread::sleep_for(std::chrono::milliseconds(75)); |
| 62 | + StoreOAuthRefreshToken(key, binding, mode, std::to_string(std::stoi(token) + 1)); |
| 63 | + } else if (operation == "hold") { |
| 64 | + std::cout << "LOCKED" << std::endl; |
| 65 | + Sleep(INFINITE); |
| 66 | + } else { |
| 67 | + throw std::runtime_error("Unknown operation"); |
| 68 | + } |
| 69 | + std::cout << "OK " << operation << '\n'; |
| 70 | + return 0; |
| 71 | + } catch (const std::exception &e) { |
| 72 | + std::cerr << e.what() << '\n'; |
| 73 | + return 1; |
| 74 | + } |
| 75 | +} |
0 commit comments