219b8036
luigser
DEEP
|
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
|
<?php
/**
* Slim - a micro PHP 5 framework
*
* @author Josh Lockhart <info@slimframework.com>
* @copyright 2011 Josh Lockhart
* @link http://www.slimframework.com
* @license http://www.slimframework.com/license
* @version 2.6.1
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class SlimHttpUtilTest extends PHPUnit_Framework_TestCase
{
/**
* Test strip slashes when magic quotes disabled
*/
public function testStripSlashesWithoutMagicQuotes()
{
$data = "This should have \"quotes\" in it";
$stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, false);
$this->assertEquals($data, $stripped);
}
/**
* Test strip slashes from array when magic quotes disabled
*/
public function testStripSlashesFromArrayWithoutMagicQuotes()
{
$data = array("This should have \"quotes\" in it", "And this \"too\" has quotes");
$stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, false);
$this->assertEquals($data, $stripped);
}
/**
* Test strip slashes when magic quotes enabled
*/
public function testStripSlashesWithMagicQuotes()
{
$data = "This should have \"quotes\" in it";
$stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, true);
$this->assertEquals('This should have "quotes" in it', $stripped);
}
/**
* Test strip slashes from array when magic quotes enabled
*/
public function testStripSlashesFromArrayWithMagicQuotes()
{
$data = array("This should have \"quotes\" in it", "And this \"too\" has quotes");
$stripped = \Slim\Http\Util::stripSlashesIfMagicQuotes($data, true);
$this->assertEquals($data = array('This should have "quotes" in it', 'And this "too" has quotes'), $stripped);
}
/**
* Test encrypt and decrypt with valid data
*/
public function testEncryptAndDecryptWithValidData()
{
$data = 'foo';
$key = 'secret';
$iv = md5('initializationVector');
$encrypted = \Slim\Http\Util::encrypt($data, $key, $iv);
$decrypted = \Slim\Http\Util::decrypt($encrypted, $key, $iv);
$this->assertEquals($data, $decrypted);
$this->assertTrue($data !== $encrypted);
}
/**
* Test encrypt when data is empty string
*/
public function testEncryptWhenDataIsEmptyString()
{
$data = '';
$key = 'secret';
$iv = md5('initializationVector');
$encrypted = \Slim\Http\Util::encrypt($data, $key, $iv);
$this->assertEquals('', $encrypted);
}
/**
* Test decrypt when data is empty string
*/
public function testDecryptWhenDataIsEmptyString()
{
$data = '';
$key = 'secret';
$iv = md5('initializationVector');
$decrypted = \Slim\Http\Util::decrypt($data, $key, $iv);
$this->assertEquals('', $decrypted);
}
/**
* Test encrypt when IV and key sizes are too long
*/
public function testEncryptAndDecryptWhenKeyAndIvAreTooLong()
{
$data = 'foo';
$key = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
$iv = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
$encrypted = \Slim\Http\Util::encrypt($data, $key, $iv);
$decrypted = \Slim\Http\Util::decrypt($encrypted, $key, $iv);
$this->assertEquals($data, $decrypted);
$this->assertTrue($data !== $encrypted);
}
public function testEncodeAndDecodeSecureCookieWithValidData()
{
//Prepare cookie value
$value = 'foo';
$expires = time() + 86400;
$secret = 'password';
$algorithm = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
$encodedValue = \Slim\Http\Util::encodeSecureCookie($value, $expires, $secret, $algorithm, $mode);
$decodedValue = \Slim\Http\Util::decodeSecureCookie($encodedValue, $secret, $algorithm, $mode);
//Test secure cookie value
$parts = explode('|', $encodedValue);
$this->assertEquals(3, count($parts));
$this->assertEquals($expires, $parts[0]);
$this->assertEquals($value, $decodedValue);
}
/**
* Test encode/decode secure cookie with old expiration
*
* In this test, the expiration date is purposefully set to a time before now.
* When decoding the encoded cookie value, FALSE is returned since the cookie
* will have expired before it is decoded.
*/
public function testEncodeAndDecodeSecureCookieWithOldExpiration()
{
$value = 'foo';
$expires = time() - 100;
$secret = 'password';
$algorithm = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
$encodedValue = \Slim\Http\Util::encodeSecureCookie($value, $expires, $secret, $algorithm, $mode);
$decodedValue = \Slim\Http\Util::decodeSecureCookie($encodedValue, $secret, $algorithm, $mode);
$this->assertFalse($decodedValue);
}
/**
* Test encode/decode secure cookie with tampered data
*
* In this test, the encoded data is purposefully changed to simulate someone
* tampering with the client-side cookie data. When decoding the encoded cookie value,
* FALSE is returned since the verification key will not match.
*/
public function testEncodeAndDecodeSecureCookieWithTamperedData()
{
$value = 'foo';
$expires = time() + 86400;
$secret = 'password';
$algorithm = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
$encodedValue = \Slim\Http\Util::encodeSecureCookie($value, $expires, $secret, $algorithm, $mode);
$encodedValueParts = explode('|', $encodedValue);
$encodedValueParts[1] = $encodedValueParts[1] . 'changed';
$encodedValue = implode('|', $encodedValueParts);
$decodedValue = \Slim\Http\Util::decodeSecureCookie($encodedValue, $secret, $algorithm, $mode);
$this->assertFalse($decodedValue);
}
public function testSetCookieHeaderWithNameAndValue()
{
$name = 'foo';
$value = 'bar';
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, $value);
$this->assertEquals('foo=bar', $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueWhenCookieAlreadySet()
{
$name = 'foo';
$value = 'bar';
$header = array('Set-Cookie' => 'one=two');
\Slim\Http\Util::setCookieHeader($header, $name, $value);
$this->assertEquals("one=two\nfoo=bar", $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomain()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain
));
$this->assertEquals('foo=bar; domain=foo.com', $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomainAndPath()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$path = '/foo';
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain,
'path' => $path
));
$this->assertEquals('foo=bar; domain=foo.com; path=/foo', $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAsString()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$path = '/foo';
$expires = '2 days';
$expiresFormat = gmdate('D, d-M-Y H:i:s e', strtotime($expires));
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain,
'path' => '/foo',
'expires' => $expires
));
$this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat, $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAsInteger()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$path = '/foo';
$expires = strtotime('2 days');
$expiresFormat = gmdate('D, d-M-Y H:i:s e', $expires);
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain,
'path' => '/foo',
'expires' => $expires
));
$this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat, $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAsZero()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$path = '/foo';
$expires = 0;
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain,
'path' => '/foo',
'expires' => $expires
));
$this->assertEquals('foo=bar; domain=foo.com; path=/foo', $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAndSecure()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$path = '/foo';
$expires = strtotime('2 days');
$expiresFormat = gmdate('D, d-M-Y H:i:s e', $expires);
$secure = true;
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain,
'path' => '/foo',
'expires' => $expires,
'secure' => $secure
));
$this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat . '; secure', $header['Set-Cookie']);
}
public function testSetCookieHeaderWithNameAndValueAndDomainAndPathAndExpiresAndSecureAndHttpOnly()
{
$name = 'foo';
$value = 'bar';
$domain = 'foo.com';
$path = '/foo';
$expires = strtotime('2 days');
$expiresFormat = gmdate('D, d-M-Y H:i:s e', $expires);
$secure = true;
$httpOnly = true;
$header = array();
\Slim\Http\Util::setCookieHeader($header, $name, array(
'value' => $value,
'domain' => $domain,
'path' => '/foo',
'expires' => $expires,
'secure' => $secure,
'httponly' => $httpOnly
));
$this->assertEquals('foo=bar; domain=foo.com; path=/foo; expires=' . $expiresFormat . '; secure; HttpOnly', $header['Set-Cookie']);
}
/**
* Test serializeCookies and decrypt with string expires
*
* In this test a cookie with a string typed value for 'expires' is set,
* which should be parsed by `strtotime` to a timestamp when it's added to
* the headers; this timestamp should then be correctly parsed, and the
* value correctly decrypted, by `decodeSecureCookie`.
*/
public function testSerializeCookiesAndDecryptWithStringExpires()
{
$value = 'bar';
$headers = new \Slim\Http\Headers();
$settings = array(
'cookies.encrypt' => true,
'cookies.secret_key' => 'secret',
'cookies.cipher' => MCRYPT_RIJNDAEL_256,
'cookies.cipher_mode' => MCRYPT_MODE_CBC
);
$cookies = new \Slim\Http\Cookies();
$cookies->set('foo', array(
'value' => $value,
'expires' => '1 hour'
));
\Slim\Http\Util::serializeCookies($headers, $cookies, $settings);
$encrypted = $headers->get('Set-Cookie');
$encrypted = strstr($encrypted, ';', true);
$encrypted = urldecode(substr(strstr($encrypted, '='), 1));
$decrypted = \Slim\Http\Util::decodeSecureCookie(
$encrypted,
$settings['cookies.secret_key'],
$settings['cookies.cipher'],
$settings['cookies.cipher_mode']
);
$this->assertEquals($value, $decrypted);
$this->assertTrue($value !== $encrypted);
}
public function testDeleteCookieHeaderWithSurvivingCookie()
{
$header = array('Set-Cookie' => "foo=bar\none=two");
\Slim\Http\Util::deleteCookieHeader($header, 'foo');
$this->assertEquals(1, preg_match("@^one=two\nfoo=; expires=@", $header['Set-Cookie']));
}
public function testDeleteCookieHeaderWithoutSurvivingCookie()
{
$header = array('Set-Cookie' => "foo=bar");
\Slim\Http\Util::deleteCookieHeader($header, 'foo');
$this->assertEquals(1, preg_match("@foo=; expires=@", $header['Set-Cookie']));
}
public function testDeleteCookieHeaderWithMatchingDomain()
{
$header = array('Set-Cookie' => "foo=bar; domain=foo.com");
\Slim\Http\Util::deleteCookieHeader($header, 'foo', array(
'domain' => 'foo.com'
));
$this->assertEquals(1, preg_match("@foo=; domain=foo.com; expires=@", $header['Set-Cookie']));
}
public function testDeleteCookieHeaderWithoutMatchingDomain()
{
$header = array('Set-Cookie' => "foo=bar; domain=foo.com");
\Slim\Http\Util::deleteCookieHeader($header, 'foo', array(
'domain' => 'bar.com'
));
$this->assertEquals(1, preg_match("@foo=bar; domain=foo\.com\nfoo=; domain=bar\.com@", $header['Set-Cookie']));
}
/**
* Test parses Cookie: HTTP header
*/
public function testParsesCookieHeader()
{
$header = 'foo=bar; one=two; colors=blue';
$result = \Slim\Http\Util::parseCookieHeader($header);
$this->assertEquals(3, count($result));
$this->assertEquals('bar', $result['foo']);
$this->assertEquals('two', $result['one']);
$this->assertEquals('blue', $result['colors']);
}
/**
* Test parses Cookie: HTTP header with `=` in the cookie value
*/
public function testParsesCookieHeaderWithEqualSignInValue()
{
$header = 'foo=bar; one=two=; colors=blue';
$result = \Slim\Http\Util::parseCookieHeader($header);
$this->assertEquals(3, count($result));
$this->assertEquals('bar', $result['foo']);
$this->assertEquals('two=', $result['one']);
$this->assertEquals('blue', $result['colors']);
}
public function testParsesCookieHeaderWithCommaSeparator()
{
$header = 'foo=bar, one=two, colors=blue';
$result = \Slim\Http\Util::parseCookieHeader($header);
$this->assertEquals(3, count($result));
$this->assertEquals('bar', $result['foo']);
$this->assertEquals('two', $result['one']);
$this->assertEquals('blue', $result['colors']);
}
public function testPrefersLeftmostCookieWhenManyCookiesWithSameName()
{
$header = 'foo=bar; foo=beer';
$result = \Slim\Http\Util::parseCookieHeader($header);
$this->assertEquals('bar', $result['foo']);
}
}
|