-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_postgres.c
200 lines (167 loc) · 5.94 KB
/
test_postgres.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <stddef.h>
#include <stdlib.h>
#include "nr_postgres.h"
#include "nr_postgres_private.h"
#include "util_memory.h"
#include "util_strings.h"
#include "tlib_main.h"
#define DEFAULT_DATABASE_NAME "unknown"
#define DEFAULT_PORT "5432"
#define DEFAULT_SOCKET "/tmp"
static void test_default_port_host_and_socket(void) {
char* host = NULL;
char* port = NULL;
char* database_name = NULL;
unsetenv("PGHOST");
unsetenv("PGHOSTADDR");
unsetenv("PGPORT");
unsetenv("PGUSER");
unsetenv("PGDATABASE");
/*
* Test: Missing environment variables
*/
host = nr_postgres_default_host();
tlib_pass_if_str_equal("default host", "localhost", host);
nr_free(host);
port = nr_postgres_default_port();
tlib_pass_if_str_equal("default port", DEFAULT_PORT, port);
nr_free(port);
database_name = nr_postgres_default_database_name();
tlib_pass_if_str_equal("default database_name", "", database_name);
nr_free(database_name);
/*
* Test: Environment variables: host
*/
putenv("PGHOST=spock");
host = nr_postgres_default_host();
tlib_pass_if_str_equal("host", "spock", host);
nr_free(host);
putenv("PGHOSTADDR=kirk");
host = nr_postgres_default_host();
tlib_pass_if_str_equal("hostaddr has precedence over host", "kirk", host);
nr_free(host);
unsetenv("PGHOST");
unsetenv("PGHOSTADDR");
/*
* Test: Environment variables: port
*/
putenv("PGPORT=2468");
port = nr_postgres_default_port();
tlib_pass_if_str_equal("port", "2468", port);
nr_free(port);
unsetenv("PGPORT");
/*
* Test: Environment variables: database_name
*/
putenv("PGUSER=uhura");
database_name = nr_postgres_default_database_name();
tlib_pass_if_str_equal("user is default database name", "uhura",
database_name);
nr_free(database_name);
putenv("PGDATABASE=scotty");
database_name = nr_postgres_default_database_name();
tlib_pass_if_str_equal("dbname has precedence over user", "scotty",
database_name);
nr_free(database_name);
unsetenv("PGUSER");
unsetenv("PGDATABASE");
}
static void test_conn_info_early_return(void) {
char* host = nr_strdup("no");
char* port_path_or_id = nr_strdup("nope");
char* database_name = nr_strdup("negatory");
/*
* Test: Non-NULL return value params don't blow up
*/
nr_postgres_parse_conn_info("", &host, &port_path_or_id, &database_name);
nr_free(host);
nr_free(port_path_or_id);
nr_free(database_name);
}
static void test_conn_info(const char* conn_info,
const char* expected_host,
const char* expected_port_path_or_id,
const char* expected_database_name) {
char* host = NULL;
char* port_path_or_id = NULL;
char* database_name = NULL;
nr_postgres_parse_conn_info(conn_info, &host, &port_path_or_id,
&database_name);
tlib_pass_if_str_equal("correct host", expected_host, host);
tlib_pass_if_str_equal("correct port_path_or_id", expected_port_path_or_id,
port_path_or_id);
tlib_pass_if_str_equal("correct database_name", expected_database_name,
database_name);
nr_free(host);
nr_free(port_path_or_id);
nr_free(database_name);
}
static void test_parse_conn_info(void) {
/*
* Test: Bad conn_info
*/
test_conn_info_early_return();
test_conn_info(NULL, "localhost", DEFAULT_SOCKET, "");
/*
* Test: Nonsensical information
*/
test_conn_info("host=/tmp port=4444", "localhost", DEFAULT_SOCKET, "");
/*
* Test: Missing information
*/
test_conn_info("", "localhost", DEFAULT_SOCKET, "");
test_conn_info(";", "localhost", DEFAULT_SOCKET, "");
test_conn_info(";;", "localhost", DEFAULT_SOCKET, "");
test_conn_info("host=", "localhost", DEFAULT_SOCKET, "");
test_conn_info("host", "localhost", DEFAULT_SOCKET, "");
test_conn_info("hostaddr=", "localhost", DEFAULT_SOCKET, "");
test_conn_info("port=", "localhost", DEFAULT_PORT, "");
test_conn_info("=5432", "localhost", DEFAULT_SOCKET, "");
test_conn_info("dbname=", "localhost", DEFAULT_SOCKET, "");
test_conn_info("user=", "localhost", DEFAULT_SOCKET, "");
test_conn_info("charset=UTF-8", "localhost", DEFAULT_SOCKET, "");
/*
* Test: Spaces, the final frontier
*/
test_conn_info("host = localhost port = 5432 user = scotty", "localhost",
DEFAULT_PORT, "scotty");
test_conn_info("host = localhost", "localhost", DEFAULT_PORT, "");
test_conn_info("host= localhost", "localhost", DEFAULT_PORT, "");
test_conn_info(" port", "localhost", DEFAULT_SOCKET, "");
test_conn_info("port = ", "localhost", DEFAULT_PORT, "");
/*
* Test: Localhost
*/
test_conn_info("host=localhost", "localhost", DEFAULT_PORT, "");
test_conn_info("hostaddr=localhost", "localhost", DEFAULT_PORT, "");
test_conn_info("host=localhost port=1234", "localhost", "1234", "");
test_conn_info("host=localhost port=/var/run/", "localhost", "/var/run/", "");
test_conn_info("host=/tmp", "localhost", DEFAULT_SOCKET, "");
test_conn_info("host=/tmp port=ignored", "localhost", DEFAULT_SOCKET, "");
/*
* Test: Precedence
*/
test_conn_info("host=localhost hostaddr=127.0.0.1", "127.0.0.1", DEFAULT_PORT,
"");
test_conn_info("user=uhura dbname=mccoy", "localhost", DEFAULT_SOCKET,
"mccoy");
test_conn_info("host=localhost user=chekov", "localhost", DEFAULT_PORT,
"chekov");
/*
* Test: Non-localhost
*/
test_conn_info("hostaddr=12.34.56.78", "12.34.56.78", DEFAULT_PORT, "");
test_conn_info("host=spock port=5432 user=kirk password=enterprise", "spock",
DEFAULT_PORT, "kirk");
}
tlib_parallel_info_t parallel_info
= {.suggested_nthreads = -1, .state_size = 0};
void test_main(void* p NRUNUSED) {
test_default_port_host_and_socket();
test_parse_conn_info();
}