/
Patching_on_function_test.php
247 lines (229 loc) · 5.9 KB
/
Patching_on_function_test.php
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
<?php
/**
* @group patcher
*/
class Patching_on_function_test extends TestCase
{
public function test_index_patch_on_mt_rand()
{
MonkeyPatch::patchFunction('mt_rand', 100, 'Patching_on_function');
$output = $this->request('GET', 'patching_on_function');
$this->assertContains('100', $output);
}
public function test_index_patch_on_mt_rand_return_null()
{
MonkeyPatch::patchFunction('mt_rand', null, 'Patching_on_function');
$output = $this->request('GET', 'patching_on_function');
$this->assertEquals('', $output);
}
public function test_another_patch_on_mt_rand()
{
MonkeyPatch::patchFunction(
'mt_rand',
function($a, $b) {
return $a . $b;
},
'Patching_on_function'
);
$output = $this->request('GET', 'patching_on_function/another');
$this->assertContains('19', $output);
}
public function test_openssl_random_pseudo_bytes()
{
MonkeyPatch::patchFunction(
'openssl_random_pseudo_bytes',
'aaaa',
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/openssl_random_pseudo_bytes'
);
$this->assertEquals("61616161\n1\n", $output);
}
public function test_openssl_random_pseudo_bytes_without_2nd_arg()
{
MonkeyPatch::patchFunction(
'openssl_random_pseudo_bytes',
'aaaa',
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/openssl_random_pseudo_bytes_without_2nd_arg'
);
$this->assertEquals("61616161\n", $output);
}
public function test_openssl_random_pseudo_bytes_null()
{
MonkeyPatch::patchFunction(
'openssl_random_pseudo_bytes',
null,
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/openssl_random_pseudo_bytes'
);
$this->assertEquals("\n1\n", $output);
}
public function test_openssl_random_pseudo_bytes_callable()
{
MonkeyPatch::patchFunction(
'openssl_random_pseudo_bytes',
function ($int, &$crypto_strong) {
$crypto_strong = false;
return 'bbbb';
},
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/openssl_random_pseudo_bytes'
);
$this->assertEquals("62626262\n\n", $output);
}
public function test_function_exists_use_random_bytes()
{
MonkeyPatch::patchFunction(
'function_exists',
function ($function) {
if ($function === 'random_bytes')
{
return true;
}
elseif ($function === 'openssl_random_pseudo_bytes')
{
return false;
}
elseif ($function === 'mcrypt_create_iv')
{
return false;
}
else
{
return __GO_TO_ORIG__;
}
},
'Patching_on_function'
);
MonkeyPatch::verifyInvokedOnce('function_exists', ['random_bytes']);
MonkeyPatch::verifyInvokedOnce('function_exists', ['exit']);
MonkeyPatch::verifyInvokedMultipleTimes('function_exists', 2);
MonkeyPatch::verifyNeverInvoked('function_exists', ['openssl_random_pseudo_bytes']);
MonkeyPatch::verifyNeverInvoked('function_exists', ['mcrypt_create_iv']);
$output = $this->request(
'GET', 'patching_on_function/function_exists'
);
$this->assertContains("I use random_bytes().", $output);
$this->assertContains("Do you know? There is no exit() function in PHP.", $output);
}
public function test_function_exists_use_openssl_random_pseudo_bytes()
{
MonkeyPatch::patchFunction(
'function_exists',
function ($function) {
if ($function === 'random_bytes')
{
return false;
}
elseif ($function === 'openssl_random_pseudo_bytes')
{
return true;
}
elseif ($function === 'mcrypt_create_iv')
{
return false;
}
else
{
return __GO_TO_ORIG__;
}
},
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/function_exists'
);
$this->assertContains("I use openssl_random_pseudo_bytes().", $output);
$this->assertContains("Do you know? There is no exit() function in PHP.", $output);
}
public function test_function_exists_use_mcrypt_create_iv()
{
MonkeyPatch::patchFunction(
'function_exists',
function ($function) {
if ($function === 'random_bytes')
{
return false;
}
elseif ($function === 'openssl_random_pseudo_bytes')
{
return false;
}
elseif ($function === 'mcrypt_create_iv')
{
return true;
}
else
{
return __GO_TO_ORIG__;
}
},
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/function_exists'
);
$this->assertContains("I use mcrypt_create_iv().", $output);
$this->assertContains("Do you know? There is no exit() function in PHP.", $output);
}
public function test_scope_limitation_method()
{
MonkeyPatch::patchFunction(
'function_exists',
false,
'Patching_on_function::scope_limitation_method'
);
$output = $this->request(
'GET', 'patching_on_function/scope_limitation_method'
);
$this->assertEquals(
"I don't have microtime(). I have microtime(). I have microtime().",
$output
);
}
public function test_scope_limitation_class()
{
MonkeyPatch::patchFunction(
'function_exists',
false,
'Patching_on_function'
);
$output = $this->request(
'GET', 'patching_on_function/scope_limitation_class'
);
$this->assertEquals(
"I don't have microtime(). I don't have microtime(). I have microtime().",
$output
);
}
public function test_header()
{
MonkeyPatch::patchFunction(
'header', null, 'Patching_on_function::header'
);
$output = $this->request('GET', 'patching_on_function/header');
MonkeyPatch::verifyInvokedOnce(
'header', ['Location: http://www.example.com/']
);
$this->assertEquals('call header()', $output);
}
public function test_setcookie()
{
MonkeyPatch::patchFunction(
'setcookie', true, 'Patching_on_function::setcookie'
);
$output = $this->request('GET', 'patching_on_function/setcookie');
MonkeyPatch::verifyInvokedOnce(
'setcookie', ['TestCookie', 'something from somewhere']
);
$this->assertEquals('call setcookie()', $output);
}
}