-
Notifications
You must be signed in to change notification settings - Fork 0
/
bintest.c
124 lines (103 loc) · 3.41 KB
/
bintest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <libpq-fe.h>
#include <netinet/in.h>
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
static void
clear_and_exit_nicely(PGconn *conn, PGresult *res)
{
PQclear(res);
exit_nicely(conn);
}
int main(int argc, char **argv)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
Oid types[] = { 0, 0, 0 }, *oid = NULL;
int lengths[] = { 2, 4, 8 }; // length in bytes
int formats[] = { 1, 1, 1 }; // all binary
uint32_t binary2 = htonl((uint16_t) 65535);
uint32_t binary4 = htonl((uint32_t) 65535);
// XXX htonll is very probably not portable. It's defined on macOS 10.14
uint64_t binary8 = htonll((uint64_t) 65535);
const char *values[] = { (const char*) &binary2,
(const char*) &binary4,
(const char*) &binary8
};
int err = 0;
if (argc < 2) {
fprintf(stderr, "Usage: bintest \"connection string\"\n");
return 1;
}
conninfo = argv[1];
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK) {
fprintf(stderr, "Connection failure: %s\n",
PQerrorMessage(conn));
exit_nicely(conn);
}
res = PQexec(conn, "SET search_path = crockford");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
clear_and_exit_nicely(conn, res);
}
PQclear(res);
res = PQexecParams(conn,
"select 'crockford.crockford2'::regtype::oid,"
" 'crockford.crockford4'::regtype::oid,"
" 'crockford.crockford8'::regtype::oid",
0, // no parameters
NULL, NULL, NULL, NULL, // no parameters, so no parameter meta
1); // ask for binary results
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Error executing query: %s\n",
PQerrorMessage(conn));
clear_and_exit_nicely(conn, res);
}
for (int i = 0; i < 3; i++) {
oid = (Oid*) PQgetvalue(res, 0, i);
types[i] = ntohl(*oid);
}
PQclear(res);
res = PQexecParams(conn, "SELECT $1, $2, $3",
3, types, values, lengths, formats, 1);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Error executing query: %s\n",
PQerrorMessage(conn));
clear_and_exit_nicely(conn, res);
}
for (int i = 0; i < 3; i++) {
if (PQftype(res, i) != types[i]) {
fprintf(stderr, "Column %d is not the expected type.\n", i + 1);
err++;
}
if (PQgetlength(res, 0, i) != lengths[i]) {
fprintf(stderr, "Column %d is not of the right length.\n", i + 1);
err++;
}
if (memcmp(PQgetvalue(res, 0, i), values[i], lengths[i]) != 0) {
fprintf(stderr, "Data in column %d is not equal to value sent.\n", i + 1);
char *val = PQgetvalue(res, 0, i);
for (int b = 0; b < lengths[i]; b++)
fprintf(stderr, "<0x%0x>\n", val[b]);
err++;
}
}
if (err > 0) {
fprintf(stderr, "Not OK. Exiting.\n");
clear_and_exit_nicely(conn, res);
}
fprintf(stdout, "All checks: OK\n");
PQclear(res);
PQfinish(conn);
return 0;
}