forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pgsql.c
302 lines (251 loc) · 9.6 KB
/
test_pgsql.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "tlib_datastore.h"
#include "tlib_main.h"
#include "tlib_php.h"
#include "nr_datastore_instance.h"
#include "php_agent.h"
#include "php_datastore.h"
#include "php_pgsql.h"
#include "php_pgsql_private.h"
#include "util_system.h"
static char* system_host_name;
#define DEFAULT_DATABASE_NAME "unknown"
#define DEFAULT_PORT "5432"
#define DEFAULT_SOCKET "/tmp"
static void test_save_datastore_instance(TSRMLS_D) {
char* key = NULL;
zval* conn;
nr_datastore_instance_t* expected_default = &((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = system_host_name,
.port_path_or_id = DEFAULT_SOCKET,
});
nr_datastore_instance_t* expected = &((nr_datastore_instance_t){
.database_name = "kirk",
.host = "spock",
.port_path_or_id = "2345",
});
tlib_php_request_start();
conn = tlib_php_zval_create_default(IS_RESOURCE TSRMLS_CC);
/*
* Test: Global initialized
*/
tlib_pass_if_null("global is null at request start", NRPRG(pgsql_last_conn));
/*
* Test: Bad input saves the default instance information
*
* Note that without deleting the previous instance, the hashmap is not
* updated with new information.
*/
key = nr_php_datastore_make_key(NULL, "pgsql");
nr_php_pgsql_save_datastore_instance(NULL, NULL TSRMLS_CC);
assert_datastore_instance_equals(
"null conn and null conn_info", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_hashmap_delete(NRPRG(datastore_connections), key, nr_strlen(key));
nr_php_pgsql_save_datastore_instance(NULL, "" TSRMLS_CC);
assert_datastore_instance_equals(
"null conn and empty conn_info", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_free(key);
key = nr_php_datastore_make_key(conn, "pgsql");
nr_php_pgsql_save_datastore_instance(conn, NULL TSRMLS_CC);
assert_datastore_instance_equals(
"null conn_info", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_hashmap_delete(NRPRG(datastore_connections), key, nr_strlen(key));
nr_php_pgsql_save_datastore_instance(conn, "" TSRMLS_CC);
assert_datastore_instance_equals(
"empty conn_info", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
/*
* Test: Global updated
*
* Saving an instance should properly update the global with that connection's
* key.
*/
tlib_pass_if_str_equal("global properly set", key, NRPRG(pgsql_last_conn));
/*
* Test: Normal operation
*/
nr_php_pgsql_save_datastore_instance(
conn, "host=spock port=2345 dbname=kirk" TSRMLS_CC);
assert_datastore_instance_equals(
"same conn won't save new instance", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_hashmap_delete(NRPRG(datastore_connections), key, nr_strlen(key));
nr_php_pgsql_save_datastore_instance(
conn, "host=spock port=2345 dbname=kirk" TSRMLS_CC);
assert_datastore_instance_equals(
"new conn saves new instance", expected,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_php_zval_free(&conn);
nr_free(key);
tlib_php_request_end();
}
static void test_retrieve_datastore_instance(TSRMLS_D) {
char* key = NULL;
zval* conn;
nr_datastore_instance_t* expected = &((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = system_host_name,
.port_path_or_id = DEFAULT_SOCKET,
});
tlib_php_request_start();
conn = tlib_php_zval_create_default(IS_RESOURCE TSRMLS_CC);
/*
* Test: Global initialized
*/
tlib_pass_if_null("global is null at request start", NRPRG(pgsql_last_conn));
/*
* Test: Unknown non-null connection
*/
tlib_pass_if_null("unknown non-null connection info isn't found",
nr_php_pgsql_retrieve_datastore_instance(conn TSRMLS_CC));
tlib_pass_if_null(
"an unknown non-null connection should not update the global",
NRPRG(pgsql_last_conn));
/*
* Test: Unknown null connection
*
* Retrieving information for an unknown null connection will create and save
* a new default instance, updating the global.
*/
assert_datastore_instance_equals(
"unknown null connection saves a default instance", expected,
nr_php_pgsql_retrieve_datastore_instance(NULL TSRMLS_CC));
key = nr_php_datastore_make_key(NULL, "pgsql");
tlib_pass_if_str_equal("global properly set", key, NRPRG(pgsql_last_conn));
/*
* Test: Normal operation
*/
nr_hashmap_update(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_pgsql_create_datastore_instance(NULL));
assert_datastore_instance_equals(
"connection info is found", expected,
nr_php_pgsql_retrieve_datastore_instance(NULL TSRMLS_CC));
nr_free(key);
key = nr_php_datastore_make_key(conn, "pgsql");
nr_hashmap_set(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_pgsql_create_datastore_instance(NULL));
assert_datastore_instance_equals(
"connection info is found", expected,
nr_php_pgsql_retrieve_datastore_instance(conn TSRMLS_CC));
nr_php_zval_free(&conn);
nr_free(key);
tlib_php_request_end();
}
static void test_remove_datastore_instance(TSRMLS_D) {
zval* conn;
char* key = NULL;
tlib_php_request_start();
conn = tlib_php_zval_create_default(IS_RESOURCE TSRMLS_CC);
/*
* Test: Global initialized
*/
tlib_pass_if_null("global is null at request start", NRPRG(pgsql_last_conn));
/*
* Test: Unknown connection
*/
key = nr_php_datastore_make_key(NULL, "pgsql");
nr_php_pgsql_remove_datastore_instance(NULL TSRMLS_CC);
tlib_pass_if_int_equal("removing unknown connection has no effect", 0,
nr_php_datastore_has_conn(key TSRMLS_CC));
tlib_pass_if_null("global still null", NRPRG(pgsql_last_conn));
/*
* Test: null connection
*/
nr_hashmap_set(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_pgsql_create_datastore_instance(NULL));
nr_php_pgsql_remove_datastore_instance(NULL TSRMLS_CC);
tlib_pass_if_int_equal("removing known null connection works", 0,
nr_php_datastore_has_conn(key TSRMLS_CC));
tlib_pass_if_null("global has been reset", NRPRG(pgsql_last_conn));
/*
* Test: Normal operation
*/
nr_free(key);
key = nr_php_datastore_make_key(conn, "pgsql");
nr_php_pgsql_remove_datastore_instance(conn TSRMLS_CC);
tlib_pass_if_int_equal("removing unknown non-null connection has no effect",
0, nr_php_datastore_has_conn(key TSRMLS_CC));
tlib_pass_if_null("global still null", NRPRG(pgsql_last_conn));
nr_hashmap_set(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_pgsql_create_datastore_instance(NULL));
nr_php_pgsql_remove_datastore_instance(conn TSRMLS_CC);
tlib_pass_if_int_equal("removing known non-null connection works", 0,
nr_php_datastore_has_conn(key TSRMLS_CC));
tlib_pass_if_null("global properly unset", NRPRG(pgsql_last_conn));
nr_free(key);
nr_php_zval_free(&conn);
tlib_php_request_end();
}
static void test_instance(const char* message,
const char* conn_info,
const nr_datastore_instance_t* expected) {
nr_datastore_instance_t* actual;
actual = nr_php_pgsql_create_datastore_instance(conn_info);
assert_datastore_instance_equals(message, expected, actual);
nr_datastore_instance_destroy(&actual);
}
static void test_create_datastore_instance() {
/*
* Test: Bad input
*/
test_instance("null", NULL,
&((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = system_host_name,
.port_path_or_id = DEFAULT_SOCKET,
}));
test_instance("empty", "",
&((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = system_host_name,
.port_path_or_id = DEFAULT_SOCKET,
}));
/*
* Test: localhost
*/
test_instance("localhost port", "host=localhost",
&((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = system_host_name,
.port_path_or_id = DEFAULT_PORT,
}));
test_instance("localhost socket", "host=/tmp",
&((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = system_host_name,
.port_path_or_id = DEFAULT_SOCKET,
}));
/*
* Test: Non-localhost
*/
test_instance("non-localhost", "host=spock port=2345 dbname=kirk",
&((nr_datastore_instance_t){
.database_name = "kirk",
.host = "spock",
.port_path_or_id = "2345",
}));
}
void test_main(void* p NRUNUSED) {
#if defined(ZTS) && !defined(PHP7)
void*** tsrm_ls = NULL;
#endif /* ZTS && !PHP7 */
system_host_name = nr_system_get_hostname();
tlib_php_engine_create("" PTSRMLS_CC);
if (nr_php_extension_loaded("pgsql")) {
test_save_datastore_instance(TSRMLS_C);
test_retrieve_datastore_instance(TSRMLS_C);
test_remove_datastore_instance(TSRMLS_C);
test_create_datastore_instance();
}
tlib_php_engine_destroy(TSRMLS_C);
nr_free(system_host_name);
}
tlib_parallel_info_t parallel_info
= {.suggested_nthreads = -1, .state_size = 0};