diff options
Diffstat (limited to 'src/interfaces/libpq/test')
-rw-r--r-- | src/interfaces/libpq/test/.gitignore | 2 | ||||
-rw-r--r-- | src/interfaces/libpq/test/Makefile | 24 | ||||
-rw-r--r-- | src/interfaces/libpq/test/libpq_testclient.c | 37 | ||||
-rw-r--r-- | src/interfaces/libpq/test/libpq_uri_regress.c | 84 |
4 files changed, 147 insertions, 0 deletions
diff --git a/src/interfaces/libpq/test/.gitignore b/src/interfaces/libpq/test/.gitignore new file mode 100644 index 0000000..6ba78ad --- /dev/null +++ b/src/interfaces/libpq/test/.gitignore @@ -0,0 +1,2 @@ +/libpq_testclient +/libpq_uri_regress diff --git a/src/interfaces/libpq/test/Makefile b/src/interfaces/libpq/test/Makefile new file mode 100644 index 0000000..75ac08f --- /dev/null +++ b/src/interfaces/libpq/test/Makefile @@ -0,0 +1,24 @@ +# src/interfaces/libpq/test/Makefile + +PGFILEDESC = "libpq test program" +PGAPPICON = win32 + +subdir = src/interfaces/libpq/test +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global + +ifeq ($(PORTNAME), win32) +LDFLAGS += -lws2_32 +endif + +override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) +LDFLAGS_INTERNAL += $(libpq_pgport) + +PROGS = libpq_testclient libpq_uri_regress + +all: $(PROGS) + +$(PROGS): $(WIN32RES) + +clean distclean maintainer-clean: + rm -f $(PROGS) *.o diff --git a/src/interfaces/libpq/test/libpq_testclient.c b/src/interfaces/libpq/test/libpq_testclient.c new file mode 100644 index 0000000..d945bac --- /dev/null +++ b/src/interfaces/libpq/test/libpq_testclient.c @@ -0,0 +1,37 @@ +/* + * libpq_testclient.c + * A test program for the libpq public API + * + * Copyright (c) 2022, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/interfaces/libpq/test/libpq_testclient.c + */ + +#include "postgres_fe.h" + +#include "libpq-fe.h" + +static void +print_ssl_library() +{ + const char *lib = PQsslAttribute(NULL, "library"); + + if (!lib) + fprintf(stderr, "SSL is not enabled\n"); + else + printf("%s\n", lib); +} + +int +main(int argc, char *argv[]) +{ + if ((argc > 1) && !strcmp(argv[1], "--ssl")) + { + print_ssl_library(); + return 0; + } + + printf("currently only --ssl is supported\n"); + return 1; +} diff --git a/src/interfaces/libpq/test/libpq_uri_regress.c b/src/interfaces/libpq/test/libpq_uri_regress.c new file mode 100644 index 0000000..6046900 --- /dev/null +++ b/src/interfaces/libpq/test/libpq_uri_regress.c @@ -0,0 +1,84 @@ +/* + * libpq_uri_regress.c + * A test program for libpq URI format + * + * This is a helper for libpq conninfo regression testing. It takes a single + * conninfo string as a parameter, parses it using PQconninfoParse, and then + * prints out the values from the parsed PQconninfoOption struct that differ + * from the defaults (obtained from PQconndefaults). + * + * Portions Copyright (c) 2012-2022, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/interfaces/libpq/test/libpq_uri_regress.c + */ + +#include "postgres_fe.h" + +#include "libpq-fe.h" + +int +main(int argc, char *argv[]) +{ + PQconninfoOption *opts; + PQconninfoOption *defs; + PQconninfoOption *opt; + PQconninfoOption *def; + char *errmsg = NULL; + bool local = true; + + if (argc != 2) + return 1; + + opts = PQconninfoParse(argv[1], &errmsg); + if (opts == NULL) + { + fprintf(stderr, "libpq_uri_regress: %s", errmsg); + return 1; + } + + defs = PQconndefaults(); + if (defs == NULL) + { + fprintf(stderr, "libpq_uri_regress: cannot fetch default options\n"); + return 1; + } + + /* + * Loop on the options, and print the value of each if not the default. + * + * XXX this coding assumes that PQconninfoOption structs always have the + * keywords in the same order. + */ + for (opt = opts, def = defs; opt->keyword; ++opt, ++def) + { + if (opt->val != NULL) + { + if (def->val == NULL || strcmp(opt->val, def->val) != 0) + printf("%s='%s' ", opt->keyword, opt->val); + + /* + * Try to detect if this is a Unix-domain socket or inet. This is + * a bit grotty but it's the same thing that libpq itself does. + * + * Note that we directly test for '/' instead of using + * is_absolute_path, as that would be considerably more messy. + * This would fail on Windows, but that platform doesn't have + * Unix-domain sockets anyway. + */ + if (*opt->val && + (strcmp(opt->keyword, "hostaddr") == 0 || + (strcmp(opt->keyword, "host") == 0 && *opt->val != '/'))) + { + local = false; + } + } + } + + if (local) + printf("(local)\n"); + else + printf("(inet)\n"); + + return 0; +} |