forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_agent.c
593 lines (488 loc) · 19 KB
/
test_agent.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "tlib_php.h"
#include "php_agent.h"
#include "php_call.h"
#include "php_globals.h"
#include "php_hash.h"
tlib_parallel_info_t parallel_info
= {.suggested_nthreads = -1, .state_size = 0};
static void test_extension_loaded(void) {
/*
* Test : Invalid arguments.
*/
tlib_pass_if_int_equal("NULL name", 0, nr_php_extension_loaded(NULL));
/*
* Test : Normal operation.
*/
tlib_pass_if_int_equal("empty name", 0, nr_php_extension_loaded(""));
tlib_pass_if_int_equal("missing extension", 0,
nr_php_extension_loaded("foo"));
tlib_fail_if_int_equal("real extension", 0,
nr_php_extension_loaded("standard"));
}
static void test_find_function(TSRMLS_D) {
zend_function* func;
/*
* Test : Invalid arguments.
*/
tlib_pass_if_null("NULL name", nr_php_find_function(NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
func = nr_php_find_function("newrelic_get_request_metadata" TSRMLS_CC);
tlib_pass_if_zend_function_is("name", NULL, "newrelic_get_request_metadata",
func);
}
static void test_is_static_function(TSRMLS_D) {
zend_function* func;
zend_class_entry* clas;
tlib_php_request_start();
/*
* Test: Invalid/NULL argument
*/
tlib_pass_if_false("NULL zend_function",
nr_php_function_is_static_method(NULL),
"Expected null parameter to return false");
/*
* Test: When checking a PHP function like newrelic_get_request_metadata, we
* expect a non static response
*/
func = nr_php_find_function("newrelic_get_request_metadata" TSRMLS_CC);
tlib_pass_if_false("PHP Function from global namespace",
nr_php_function_is_static_method(func),
"Expected PHP function to return false");
tlib_php_request_eval(
"class newrelic_test_case_static_and_non_static_tests{public function "
"instance_method (){} static public function static_method "
"(){}}" TSRMLS_CC);
clas = nr_php_find_class(
"newrelic_test_case_static_and_non_static_tests" TSRMLS_CC);
/*
* Test: Static methods should return true
*/
func = nr_php_find_class_method(clas, "static_method");
tlib_pass_if_true(
"Static Method on newrelic_test_case_static_and_non_static_tests",
nr_php_function_is_static_method(func), "Expected true");
/*
* Test: Instance methods should return true
*/
func = nr_php_find_class_method(clas, "static_method");
tlib_pass_if_true(
"Instance Method on newrelic_test_case_static_and_non_static_tests",
nr_php_function_is_static_method(func), "Expected true");
tlib_php_request_end();
}
static void test_get_constant(TSRMLS_D) {
zval* constant;
/*
* Test : Invalid arguments.
*/
tlib_pass_if_null("NULL name", nr_php_get_constant(NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
constant = nr_php_get_constant("PHP_VERSION" TSRMLS_CC);
tlib_pass_if_not_null("PHP_VERSION", constant);
tlib_pass_if_uchar_equal("PHP_VERSION type", IS_STRING, Z_TYPE_P(constant));
nr_php_zval_free(&constant);
constant = nr_php_get_constant("NON_EXISTENT_CONSTANT" TSRMLS_CC);
tlib_pass_if_null("NON_EXISTENT_CONSTANT", constant);
}
static void test_function_debug_name(TSRMLS_D) {
zval* closure;
zend_function* func;
char* name;
tlib_php_request_start();
/*
* Test : Invalid arguments.
*/
tlib_pass_if_null("NULL function", nr_php_function_debug_name(NULL));
/*
* Test : Normal operation.
*/
func = nr_php_find_function("date" TSRMLS_CC);
name = nr_php_function_debug_name(func);
tlib_pass_if_str_equal("function", "date", name);
nr_free(name);
func = nr_php_find_class_method(
nr_php_find_class("reflectionfunction" TSRMLS_CC), "isdisabled");
name = nr_php_function_debug_name(func);
tlib_pass_if_str_equal("method", "ReflectionFunction::isDisabled", name);
nr_free(name);
closure = tlib_php_request_eval_expr("function () {}" TSRMLS_CC);
func = nr_php_zval_to_function(closure TSRMLS_CC);
name = nr_php_function_debug_name(func);
tlib_pass_if_str_equal("closure", "{closure} declared at -:1", name);
nr_php_zval_free(&closure);
nr_free(name);
tlib_php_request_end();
}
static void setup_inherited_classes(TSRMLS_D) {
/*
* PHP 7 will generate deprecation warnings for the code below because of the
* intentional use of class named constructors. We'll quiet those for now,
* and we can revisit this when PHP 8 removes support for class named
* constructors and we have to decide if this test still makes sense.
*/
tlib_php_request_eval(
"$er = error_reporting(E_ALL ^ E_DEPRECATED);" TSRMLS_CC);
tlib_php_request_eval(
"class A {"
"public $pub = 'A public';"
"protected $prot = 'A protected';"
"private $priv = 'A private';"
"function a() {}"
"}"
"class B extends A {"
"function b() {}"
"}"
"class C extends B {"
"function __call($name, $args) {}"
"}" TSRMLS_CC);
/*
* Reset the error reporting.
*/
tlib_php_request_eval("error_reporting($er);" TSRMLS_CC);
}
static void test_get_zval_object_property(TSRMLS_D) {
zval* obj;
zval* prop;
tlib_php_request_start();
obj = nr_php_zval_alloc();
/*
* Test : Invalid arguments.
*/
tlib_pass_if_null("NULL object",
nr_php_get_zval_object_property(NULL, "pub" TSRMLS_CC));
tlib_pass_if_null("invalid object",
nr_php_get_zval_object_property(obj, "pub" TSRMLS_CC));
nr_php_zval_free(&obj);
setup_inherited_classes(TSRMLS_C);
obj = tlib_php_request_eval_expr("new A" TSRMLS_CC);
tlib_pass_if_null("NULL name",
nr_php_get_zval_object_property(obj, NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_null("missing property",
nr_php_get_zval_object_property(obj, "foo" TSRMLS_CC));
prop = nr_php_get_zval_object_property(obj, "pub" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A public", Z_STRVAL_P(prop));
prop = nr_php_get_zval_object_property(obj, "prot" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A protected", Z_STRVAL_P(prop));
prop = nr_php_get_zval_object_property(obj, "priv" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A private", Z_STRVAL_P(prop));
/*
* Test : Can access inherited public and protected properties.
*/
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new C" TSRMLS_CC);
prop = nr_php_get_zval_object_property(obj, "pub" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A public", Z_STRVAL_P(prop));
prop = nr_php_get_zval_object_property(obj, "prot" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A protected", Z_STRVAL_P(prop));
/*
* Test : Cannot access inherited private properties.
*/
tlib_pass_if_null("inherited private",
nr_php_get_zval_object_property(obj, "priv" TSRMLS_CC));
nr_php_zval_free(&obj);
tlib_php_request_end();
}
static void test_get_zval_object_property_with_class(TSRMLS_D) {
zend_class_entry* a_ce;
zend_class_entry* c_ce;
zend_class_entry* stdclass_ce;
zval* obj;
zval* prop;
tlib_php_request_start();
obj = nr_php_zval_alloc();
setup_inherited_classes(TSRMLS_C);
a_ce = nr_php_find_class("a" TSRMLS_CC);
c_ce = nr_php_find_class("c" TSRMLS_CC);
stdclass_ce = nr_php_find_class("stdclass" TSRMLS_CC);
/*
* Test : Invalid arguments.
*/
tlib_pass_if_null("NULL object", nr_php_get_zval_object_property_with_class(
NULL, stdclass_ce, "pub" TSRMLS_CC));
tlib_pass_if_null("invalid object",
nr_php_get_zval_object_property_with_class(
obj, stdclass_ce, "pub" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new A" TSRMLS_CC);
tlib_pass_if_null(
"NULL class entry",
nr_php_get_zval_object_property_with_class(obj, NULL, "pub" TSRMLS_CC));
tlib_pass_if_null("NULL name", nr_php_get_zval_object_property_with_class(
obj, a_ce, NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_null(
"missing property",
nr_php_get_zval_object_property_with_class(obj, a_ce, "foo" TSRMLS_CC));
prop = nr_php_get_zval_object_property_with_class(obj, a_ce, "pub" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A public", Z_STRVAL_P(prop));
prop
= nr_php_get_zval_object_property_with_class(obj, a_ce, "prot" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A protected", Z_STRVAL_P(prop));
prop
= nr_php_get_zval_object_property_with_class(obj, a_ce, "priv" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A private", Z_STRVAL_P(prop));
/*
* Test : Can access inherited public and protected properties.
*/
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new C" TSRMLS_CC);
prop = nr_php_get_zval_object_property_with_class(obj, c_ce, "pub" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A public", Z_STRVAL_P(prop));
prop
= nr_php_get_zval_object_property_with_class(obj, c_ce, "prot" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A protected", Z_STRVAL_P(prop));
/*
* Test : Cannot access inherited private properties.
*/
tlib_pass_if_null(
"inherited private",
nr_php_get_zval_object_property_with_class(obj, c_ce, "priv" TSRMLS_CC));
/*
* Test : Can access inherited private properties with the right class entry.
*/
prop
= nr_php_get_zval_object_property_with_class(obj, a_ce, "priv" TSRMLS_CC);
tlib_pass_if_zval_type_is("property type", IS_STRING, prop);
tlib_pass_if_str_equal("property value", "A private", Z_STRVAL_P(prop));
nr_php_zval_free(&obj);
tlib_php_request_end();
}
static void test_class_entry_instanceof_class(TSRMLS_D) {
zend_class_entry* ce = NULL;
zval* obj;
tlib_php_request_start();
setup_inherited_classes(TSRMLS_C);
/*
* Test : Invalid arguments.
*/
tlib_pass_if_int_equal(
"NULL ce", 0, nr_php_class_entry_instanceof_class(NULL, "A" TSRMLS_CC));
obj = tlib_php_request_eval_expr("new B" TSRMLS_CC);
ce = Z_OBJCE_P(obj);
tlib_pass_if_int_equal(
"NULL name", 0, nr_php_class_entry_instanceof_class(ce, NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_int_equal(
"non-parent", 0, nr_php_class_entry_instanceof_class(ce, "C" TSRMLS_CC));
tlib_pass_if_int_equal(
"non-existent class", 0,
nr_php_class_entry_instanceof_class(ce, "D" TSRMLS_CC));
tlib_pass_if_int_equal(
"same class", 1, nr_php_class_entry_instanceof_class(ce, "B" TSRMLS_CC));
tlib_pass_if_int_equal(
"parent class", 1,
nr_php_class_entry_instanceof_class(ce, "A" TSRMLS_CC));
nr_php_zval_free(&obj);
tlib_php_request_end();
}
static void test_object_instanceof_class(TSRMLS_D) {
zval* obj;
tlib_php_request_start();
setup_inherited_classes(TSRMLS_C);
obj = nr_php_zval_alloc();
/*
* Test : Invalid arguments.
*/
tlib_pass_if_int_equal("NULL object", 0,
nr_php_object_instanceof_class(NULL, "A" TSRMLS_CC));
tlib_pass_if_int_equal("invalid object", 0,
nr_php_object_instanceof_class(obj, "A" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new B" TSRMLS_CC);
tlib_pass_if_int_equal("NULL name", 0,
nr_php_object_instanceof_class(obj, NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_int_equal("non-parent", 0,
nr_php_object_instanceof_class(obj, "C" TSRMLS_CC));
tlib_pass_if_int_equal("non-existent class", 0,
nr_php_object_instanceof_class(obj, "D" TSRMLS_CC));
tlib_pass_if_int_equal("same class", 1,
nr_php_object_instanceof_class(obj, "B" TSRMLS_CC));
tlib_pass_if_int_equal("parent class", 1,
nr_php_object_instanceof_class(obj, "A" TSRMLS_CC));
nr_php_zval_free(&obj);
tlib_php_request_end();
}
static void test_object_has_method(TSRMLS_D) {
zval* obj;
tlib_php_request_start();
setup_inherited_classes(TSRMLS_C);
obj = nr_php_zval_alloc();
/*
* Test : Invalid arguments.
*/
tlib_pass_if_int_equal("NULL object", 0,
nr_php_object_has_method(NULL, "a" TSRMLS_CC));
tlib_pass_if_int_equal("invalid object", 0,
nr_php_object_has_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new A" TSRMLS_CC);
tlib_pass_if_int_equal("NULL name", 0,
nr_php_object_has_method(obj, NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_int_equal("method does not exist on class", 0,
nr_php_object_has_method(obj, "foo" TSRMLS_CC));
tlib_fail_if_int_equal("method exists on class", 0,
nr_php_object_has_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new B" TSRMLS_CC);
tlib_pass_if_int_equal("method does not exist on class", 0,
nr_php_object_has_method(obj, "foo" TSRMLS_CC));
tlib_fail_if_int_equal("method exists on class", 0,
nr_php_object_has_method(obj, "b" TSRMLS_CC));
tlib_fail_if_int_equal("method inherited by class", 0,
nr_php_object_has_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new C" TSRMLS_CC);
tlib_fail_if_int_equal("class implements __call", 0,
nr_php_object_has_method(obj, "foo" TSRMLS_CC));
tlib_fail_if_int_equal("method exists on class", 0,
nr_php_object_has_method(obj, "b" TSRMLS_CC));
tlib_fail_if_int_equal("method inherited by class", 0,
nr_php_object_has_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
tlib_php_request_end();
}
static void test_object_has_concrete_method(TSRMLS_D) {
zval* obj;
tlib_php_request_start();
setup_inherited_classes(TSRMLS_C);
obj = nr_php_zval_alloc();
/*
* Test : Invalid arguments.
*/
tlib_pass_if_int_equal(
"NULL object", 0, nr_php_object_has_concrete_method(NULL, "a" TSRMLS_CC));
tlib_pass_if_int_equal("invalid object", 0,
nr_php_object_has_concrete_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new A" TSRMLS_CC);
tlib_pass_if_int_equal(
"NULL name", 0, nr_php_object_has_concrete_method(obj, NULL TSRMLS_CC));
/*
* Test : Normal operation.
*/
tlib_pass_if_int_equal(
"method does not exist on class", 0,
nr_php_object_has_concrete_method(obj, "foo" TSRMLS_CC));
tlib_fail_if_int_equal("method exists on class", 0,
nr_php_object_has_concrete_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new B" TSRMLS_CC);
tlib_pass_if_int_equal(
"method does not exist on class", 0,
nr_php_object_has_concrete_method(obj, "foo" TSRMLS_CC));
tlib_fail_if_int_equal("method exists on class", 0,
nr_php_object_has_concrete_method(obj, "b" TSRMLS_CC));
tlib_fail_if_int_equal("method inherited by class", 0,
nr_php_object_has_concrete_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
obj = tlib_php_request_eval_expr("new C" TSRMLS_CC);
tlib_pass_if_int_equal(
"class implements __call", 0,
nr_php_object_has_concrete_method(obj, "foo" TSRMLS_CC));
tlib_fail_if_int_equal("method exists on class", 0,
nr_php_object_has_concrete_method(obj, "b" TSRMLS_CC));
tlib_fail_if_int_equal("method inherited by class", 0,
nr_php_object_has_concrete_method(obj, "a" TSRMLS_CC));
nr_php_zval_free(&obj);
tlib_php_request_end();
}
static void test_parse_str(TSRMLS_D) {
zval* retval;
zval* value;
tlib_php_request_start();
/*
* Test : Invalid arguments.
*/
tlib_pass_if_null("NULL str", nr_php_parse_str(NULL, 0 TSRMLS_CC));
tlib_pass_if_null("invalid len",
nr_php_parse_str(NULL, INT_MAX + 1LL TSRMLS_CC));
/*
* Test : Normal operation.
*/
retval = nr_php_parse_str(NR_PSTR("") TSRMLS_CC);
tlib_pass_if_zval_type_is("empty string", IS_ARRAY, retval);
tlib_pass_if_size_t_equal("empty string", 0,
nr_php_zend_hash_num_elements(Z_ARRVAL_P(retval)));
nr_php_zval_free(&retval);
retval = nr_php_parse_str(NR_PSTR("a=b&c=d") TSRMLS_CC);
tlib_pass_if_zval_type_is("query string", IS_ARRAY, retval);
tlib_pass_if_size_t_equal("query string", 2,
nr_php_zend_hash_num_elements(Z_ARRVAL_P(retval)));
value = nr_php_zend_hash_find(Z_ARRVAL_P(retval), "a");
tlib_pass_if_zval_type_is("query string a", IS_STRING, value);
tlib_pass_if_str_equal("query string a", "b", Z_STRVAL_P(value));
value = nr_php_zend_hash_find(Z_ARRVAL_P(retval), "c");
tlib_pass_if_zval_type_is("query string c", IS_STRING, value);
tlib_pass_if_str_equal("query string c", "d", Z_STRVAL_P(value));
nr_php_zval_free(&retval);
tlib_php_request_end();
}
static void test_default_address() {
#ifdef NR_SYSTEM_LINUX
tlib_pass_if_str_equal("default daemon address", "@newrelic",
NR_PHP_PROCESS_GLOBALS(address_path));
#else
tlib_pass_if_str_equal("default daemon address", "/tmp/.newrelic.sock",
NR_PHP_PROCESS_GLOBALS(address_path));
#endif
}
void test_main(void* p NRUNUSED) {
#if defined(ZTS) && !defined(PHP7)
void*** tsrm_ls = NULL;
#endif /* ZTS && !PHP7 */
tlib_php_engine_create("" PTSRMLS_CC);
/*
* Read only tests that can operate within a single, empty request.
*/
tlib_php_request_start();
test_extension_loaded();
test_find_function(TSRMLS_C);
test_get_constant(TSRMLS_C);
test_default_address();
tlib_php_request_end();
/*
* Tests that require state and will handle their own request startup and
* shutdown.
*/
test_function_debug_name(TSRMLS_C);
test_get_zval_object_property(TSRMLS_C);
test_get_zval_object_property_with_class(TSRMLS_C);
test_class_entry_instanceof_class(TSRMLS_C);
test_object_instanceof_class(TSRMLS_C);
test_object_has_method(TSRMLS_C);
test_object_has_concrete_method(TSRMLS_C);
test_parse_str(TSRMLS_C);
test_is_static_function(TSRMLS_C);
tlib_php_engine_destroy(TSRMLS_C);
}