forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_environment.c
174 lines (151 loc) · 4.76 KB
/
test_environment.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "tlib_php.h"
#include "php_agent.h"
#include "php_environment.h"
tlib_parallel_info_t parallel_info
= {.suggested_nthreads = -1, .state_size = 0};
static void test_single_rocket_assignment(const char* key, const char* value) {
nrobj_t* result_env = NULL;
nrobj_t* expect_env = NULL;
char* s = NULL;
const char* r = NULL;
nr_status_t err;
char buf[BUFSIZ];
char* result_str;
char* expect_str;
snprintf(buf, sizeof(buf), "\n%s => %s\n", key, value);
result_env = nro_new_hash();
s = nr_strdup(buf);
nr_php_parse_rocket_assignment_list(s, nr_strlen(s), result_env);
r = nro_get_hash_string(result_env, key, &err);
tlib_pass_if_true("index OK", NR_SUCCESS == err, "success=%d", (int)err);
tlib_pass_if_true("pick", 0 == nr_strcmp(r, value), "r=%s but expected %s", r,
value);
nr_free(s);
expect_env = nro_new_hash();
nro_set_hash_string(expect_env, key, value);
expect_str = nro_dump(expect_env);
result_str = nro_dump(result_env);
tlib_pass_if_true("contents", 0 == nr_strcmp(expect_str, result_str),
"\nresult_str=%s\nexpect_str=%s", result_str, expect_str);
nr_free(expect_str);
nr_free(result_str);
nro_delete(expect_env);
nro_delete(result_env);
}
#define test_rocket_assignment_string_to_obj(S, E) \
test_rocket_assignment_string_to_obj_fn((S), (E), __FILE__, __LINE__)
static void test_rocket_assignment_string_to_obj_fn(const char* stimulus,
nrobj_t* expect_env,
const char* file,
int line) {
char* s = NULL;
nrobj_t* result_env = NULL;
char* result;
char* expect;
result_env = nro_new_hash();
s = nr_strdup(stimulus);
nr_php_parse_rocket_assignment_list(s, nr_strlen(s), result_env);
result = nro_dump(result_env);
expect = nro_dump(expect_env);
test_pass_if_true("object identical", 0 == nr_strcmp(expect, result),
"\nexpect=%d: %s\nresult=%d: %s", nr_strlen(expect), expect,
nr_strlen(result), result);
nr_free(result);
nr_free(expect);
nr_free(s);
nro_delete(result_env);
}
static void test_rocket_assignments(void) {
nrobj_t* expect_env = NULL;
test_single_rocket_assignment("x", "17");
test_single_rocket_assignment("xxxx", "17");
test_single_rocket_assignment("x xx", "17");
test_single_rocket_assignment(" x", "17");
test_single_rocket_assignment("x ", "17");
test_single_rocket_assignment("x", " 17");
test_single_rocket_assignment("x", "17 ");
test_single_rocket_assignment("=>", "17");
test_single_rocket_assignment("XXXX", "=>");
test_single_rocket_assignment("X XXX", "=>");
expect_env = nro_new_hash();
test_rocket_assignment_string_to_obj(0, expect_env);
test_rocket_assignment_string_to_obj("\n", expect_env);
test_rocket_assignment_string_to_obj("", expect_env);
test_rocket_assignment_string_to_obj("\n\n\n", expect_env);
nro_set_hash_string(expect_env, "foo", "17");
test_rocket_assignment_string_to_obj(
"\n"
"foo => 17"
"\n",
expect_env);
test_rocket_assignment_string_to_obj(
"\n"
"foo => 17"
"\n"
"\n",
expect_env);
test_rocket_assignment_string_to_obj(
"\n"
"foo => 17"
"\n"
"bar =>",
expect_env);
test_rocket_assignment_string_to_obj(
"\n"
"foo => 18"
"\n"
"foo => 17\n",
expect_env);
/*
* This tests some unintentional non-spec-conforming behavior.
* The char immediately after newline gets dropped,
* but the assignment still gets processed.
*/
test_rocket_assignment_string_to_obj(
"\n"
"foo =\n117"
"\n",
expect_env);
/*
* Test multiple assignments.
*/
nro_set_hash_string(expect_env, "bar", "18");
test_rocket_assignment_string_to_obj(
"\n"
"foo => 17"
"\n"
"bar => 18\n",
expect_env);
test_rocket_assignment_string_to_obj(
"\n"
"foo => 17"
"\n\n\n"
"bar => 18\n",
expect_env);
/*
* Test spaces in key/value strings both before and after the "=>".
*/
nro_delete(expect_env);
expect_env = nro_new_hash();
nro_set_hash_string(expect_env, "f o o", "1 7");
nro_set_hash_string(expect_env, "b ar", "18 19");
test_rocket_assignment_string_to_obj(
"\n"
"f o o => 1 7"
"\n \n\n"
"b ar => 18 19\n",
expect_env);
nro_delete(expect_env);
}
void test_main(void* p NRUNUSED) {
#if defined(ZTS) && !defined(PHP7)
void*** tsrm_ls = NULL;
#endif /* ZTS && !PHP7 */
tlib_php_engine_create("" PTSRMLS_CC);
test_rocket_assignments();
tlib_php_engine_destroy(TSRMLS_C);
}