forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mysql.c
379 lines (314 loc) · 12 KB
/
test_mysql.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* 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_mysql.h"
#include "php_mysql_private.h"
#include "util_system.h"
static char* system_host_name;
#define DEFAULT_DATABASE_NAME "unknown"
#define DEFAULT_PORT "3306"
#define DEFAULT_SOCKET "mysql.sock"
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 = DEFAULT_DATABASE_NAME,
.host = "blue",
.port_path_or_id = "3333",
});
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(mysql_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, "mysql");
nr_php_mysql_save_datastore_instance(NULL, NULL TSRMLS_CC);
assert_datastore_instance_equals(
"null conn and null host_and_port", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_hashmap_delete(NRPRG(datastore_connections), key, nr_strlen(key));
nr_php_mysql_save_datastore_instance(NULL, "" TSRMLS_CC);
assert_datastore_instance_equals(
"null conn and empty host_and_port", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_free(key);
key = nr_php_datastore_make_key(conn, "mysql");
nr_php_mysql_save_datastore_instance(conn, NULL TSRMLS_CC);
assert_datastore_instance_equals(
"null host_and_port", expected_default,
nr_hashmap_get(NRPRG(datastore_connections), key, nr_strlen(key)));
nr_hashmap_delete(NRPRG(datastore_connections), key, nr_strlen(key));
nr_php_mysql_save_datastore_instance(conn, "" TSRMLS_CC);
assert_datastore_instance_equals(
"empty host_and_port", 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(mysql_last_conn));
/*
* Test: Normal operation
*/
nr_php_mysql_save_datastore_instance(conn, "blue:3333" 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_mysql_save_datastore_instance(conn, "blue:3333" 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(mysql_last_conn));
/*
* Test: Unknown non-null connection
*/
tlib_pass_if_null("unknown non-null connection info isn't found",
nr_php_mysql_retrieve_datastore_instance(conn TSRMLS_CC));
tlib_pass_if_null(
"an unknown non-null connection should not update the global",
NRPRG(mysql_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_mysql_retrieve_datastore_instance(NULL TSRMLS_CC));
key = nr_php_datastore_make_key(NULL, "mysql");
tlib_pass_if_str_equal("global properly set", key, NRPRG(mysql_last_conn));
/*
* Test: Normal operation
*/
nr_hashmap_update(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_mysql_create_datastore_instance(NULL));
assert_datastore_instance_equals(
"connection info is found", expected,
nr_php_mysql_retrieve_datastore_instance(NULL TSRMLS_CC));
nr_free(key);
key = nr_php_datastore_make_key(conn, "mysql");
nr_hashmap_set(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_mysql_create_datastore_instance(NULL));
assert_datastore_instance_equals(
"connection info is found", expected,
nr_php_mysql_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(mysql_last_conn));
/*
* Test: Unknown connection
*/
key = nr_php_datastore_make_key(NULL, "mysql");
nr_php_mysql_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(mysql_last_conn));
/*
* Test: null connection
*/
nr_hashmap_set(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_mysql_create_datastore_instance(NULL));
nr_php_mysql_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(mysql_last_conn));
/*
* Test: Normal operation
*/
nr_free(key);
key = nr_php_datastore_make_key(conn, "mysql");
nr_php_mysql_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(mysql_last_conn));
nr_hashmap_set(NRPRG(datastore_connections), key, nr_strlen(key),
nr_php_mysql_create_datastore_instance(NULL));
nr_php_mysql_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(mysql_last_conn));
nr_free(key);
nr_php_zval_free(&conn);
tlib_php_request_end();
}
static void test_default_port_host_and_socket() {
char* port = NULL;
char* host = NULL;
char* socket = NULL;
/*
* Test: Normal operation
*/
port = nr_php_mysql_default_port();
tlib_pass_if_str_equal("default port", DEFAULT_PORT, port);
host = nr_php_mysql_default_host();
tlib_pass_if_str_equal("default host", "localhost", host);
socket = nr_php_mysql_default_socket();
tlib_pass_if_str_equal("default socket", DEFAULT_SOCKET, socket);
nr_free(port);
nr_free(host);
nr_free(socket);
}
static void test_host_and_port_path_or_id_early_return(void) {
char* host = nr_strdup("no");
char* port_path_or_id = nr_strdup("nope");
/*
* Test: Non-NULL return value params don't blow up
*/
nr_php_mysql_get_host_and_port_path_or_id("", &host, &port_path_or_id);
nr_free(host);
nr_free(port_path_or_id);
}
static void test_host_and_port_path_or_id(
const char* host_and_port,
const char* expected_host,
const char* expected_port_path_or_id) {
char* host = NULL;
char* port_path_or_id = NULL;
nr_php_mysql_get_host_and_port_path_or_id(host_and_port, &host,
&port_path_or_id);
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);
nr_free(host);
nr_free(port_path_or_id);
}
static void test_get_host_and_port_path_or_id() {
/*
* Test: Bad input
*/
test_host_and_port_path_or_id_early_return();
test_host_and_port_path_or_id("", "localhost", DEFAULT_SOCKET);
test_host_and_port_path_or_id(NULL, "localhost", DEFAULT_SOCKET);
/*
* Test: Localhost
*/
test_host_and_port_path_or_id("localhost", "localhost", DEFAULT_SOCKET);
test_host_and_port_path_or_id("localhost:1234", "localhost", DEFAULT_SOCKET);
test_host_and_port_path_or_id("localhost:", "localhost", DEFAULT_SOCKET);
test_host_and_port_path_or_id(":", "localhost", DEFAULT_SOCKET);
test_host_and_port_path_or_id("localhost:/path/to/socket", "localhost",
"/path/to/socket");
test_host_and_port_path_or_id(":/path/to/socket", "localhost",
"/path/to/socket");
/*
* Test: Non-localhost
*/
test_host_and_port_path_or_id("blue:star", "blue", "star");
test_host_and_port_path_or_id("blue:/path/to/socket", "blue",
"/path/to/socket");
test_host_and_port_path_or_id("blue:", "blue", DEFAULT_PORT);
test_host_and_port_path_or_id("blue", "blue", DEFAULT_PORT);
test_host_and_port_path_or_id("12:41", "12", "41");
}
static void test_instance(const char* message,
const char* host_and_port,
const nr_datastore_instance_t* expected) {
nr_datastore_instance_t* actual;
actual = nr_php_mysql_create_datastore_instance(host_and_port);
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", "localhost",
&((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", "blue:1234",
&((nr_datastore_instance_t){
.database_name = DEFAULT_DATABASE_NAME,
.host = "blue",
.port_path_or_id = "1234",
}));
}
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("mysql.default_socket=" DEFAULT_SOCKET PTSRMLS_CC);
if (tlib_php_require_extension("mysql" TSRMLS_CC)) {
test_save_datastore_instance(TSRMLS_C);
test_retrieve_datastore_instance(TSRMLS_C);
test_remove_datastore_instance(TSRMLS_C);
test_default_port_host_and_socket();
test_get_host_and_port_path_or_id();
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};