prole/pg-knoe-auth/test/unit/test_helpers.h
chrisfu 57886f9268 feat(pg-knoe-auth): import upstream PostgreSQL JWT auth extension; compile in knoe-db image
- Copy pg-knoe-auth/ wholesale from upstream/knoe-db/20260523 (Task 1 of
  docs/plans/junie/upstream-knoe-db-20260523-integration.md).
- Extension: PG18 OAUTHBEARER JWT validator using libcurl + OpenSSL RS256.
- knoe-db/Dockerfile: add libcurl4-openssl-dev to dev deps; COPY src/ and
  build with make USE_PGXS=1 install after tds_fdw.
- NOT enabled in the default database build (absent from 20_create_extensions.sh).
  To enable: CREATE EXTENSION pg_knoe_auth; (requires pg_hba.conf oauth_issuer).

Closes Task 1 of upstream-knoe-db-20260523-integration.md.
2026-05-23 21:52:06 -07:00

72 lines
2.6 KiB
C

/*
* test_helpers.h — minimal test harness for pg_knoe_auth unit tests.
*
* Provides palloc/pfree/pstrdup/pnstrdup stubs (no PG cluster needed),
* a simple PASS/FAIL assertion macro, and a test-runner entry point.
*
* See docs/plans/junie/pg_knoe_auth-rename-harden-modularize.md §2.5
*/
#ifndef TEST_HELPERS_H
#define TEST_HELPERS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
/* ── PG memory stubs ─────────────────────────────────────────────────────── */
static inline void *palloc(size_t n) { return malloc(n); }
static inline void *repalloc(void *p, size_t n) { return realloc(p, n); }
static inline void pfree(void *p) { free(p); }
static inline char *pstrdup(const char *s) { return strdup(s); }
static inline char *pnstrdup(const char *s, size_t n) {
char *r = malloc(n + 1);
memcpy(r, s, n);
r[n] = '\0';
return r;
}
/* ── PG logging stubs ────────────────────────────────────────────────────── */
#define WARNING 0
#define LOG 1
static inline void elog(int level, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "[%s] ", level == LOG ? "LOG" : "WARNING");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/* ── TopMemoryContext stub ───────────────────────────────────────────────── */
typedef void *MemoryContext;
static MemoryContext TopMemoryContext = NULL;
static inline MemoryContext MemoryContextSwitchTo(MemoryContext ctx) {
(void) ctx; return NULL;
}
static inline void *MemoryContextAlloc(MemoryContext ctx, size_t n) {
(void) ctx; return malloc(n);
}
/* ── Test runner ─────────────────────────────────────────────────────────── */
static int _tests_run = 0;
static int _tests_failed = 0;
#define ASSERT(cond, msg) do { \
_tests_run++; \
if (!(cond)) { \
fprintf(stderr, "FAIL [%s:%d] %s\n", __FILE__, __LINE__, msg); \
_tests_failed++; \
} else { \
fprintf(stdout, "PASS %s\n", msg); \
} \
} while (0)
#define TEST_SUMMARY() do { \
printf("\n%d/%d tests passed\n", _tests_run - _tests_failed, _tests_run); \
return _tests_failed ? 1 : 0; \
} while (0)
#endif /* TEST_HELPERS_H */