1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kodus\Mail\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Kodus\Mail\SMTP\SMTPDotStuffingFilter; |
6
|
|
|
use UnitTester; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is a pretty complex test for a fairly simple piece of code, but I had to be sure. |
10
|
|
|
* |
11
|
|
|
* The dot-stuffing filter has to maintain a two-byte trailing buffer from the previous |
12
|
|
|
* chunk, so as not to miss an "\r\n." sequence that started in the previous chunk. |
13
|
|
|
* |
14
|
|
|
* The test below attempts many different buffer-sizes, so that we're sure to hit the |
15
|
|
|
* edge-cases where an "\r\n." sequence spans two chunks. |
16
|
|
|
*/ |
17
|
|
|
class SMTPDotStuffingCest |
18
|
|
|
{ |
19
|
|
|
public function performDotStuffing(UnitTester $I) |
20
|
|
|
{ |
21
|
|
|
$data = str_repeat("aaaaa.aaaaa\r\n.", 100); |
22
|
|
|
|
23
|
|
|
$expected = $this->stuffDots($data); |
24
|
|
|
|
25
|
|
|
for ($chunk_size=1; $chunk_size<100; $chunk_size++) { |
26
|
|
|
$I->assertSame( |
27
|
|
|
$expected, |
28
|
|
|
$this->stuffDotsWithFilter($data, $chunk_size), |
29
|
|
|
"testing with chunk-size of {$chunk_size}" |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Correctly stuffs dots using a non-chunked str_replace() - guaranteed to work |
36
|
|
|
* |
37
|
|
|
* @param string $data |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
private function stuffDots($data) |
42
|
|
|
{ |
43
|
|
|
return str_replace("\r\n.", "\r\n..", $data); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Simulate a specified buffer-size and processes data in chunks. |
48
|
|
|
* |
49
|
|
|
* @param string $data |
50
|
|
|
* @param int $chunk_size |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
private function stuffDotsWithFilter($data, $chunk_size) |
55
|
|
|
{ |
56
|
|
|
$stream = fopen("php://temp", "rw+"); |
57
|
|
|
|
58
|
|
|
stream_set_write_buffer($stream, $chunk_size); |
59
|
|
|
|
60
|
|
|
$filter = stream_filter_append($stream, SMTPDotStuffingFilter::FILTER_NAME, STREAM_FILTER_WRITE); |
61
|
|
|
|
62
|
|
|
$chunks = str_split($data, $chunk_size); |
63
|
|
|
|
64
|
|
|
foreach ($chunks as $chunk) { |
65
|
|
|
fwrite($stream, $chunk); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
stream_filter_remove($filter); |
69
|
|
|
|
70
|
|
|
rewind($stream); |
71
|
|
|
|
72
|
|
|
$contents = stream_get_contents($stream); |
73
|
|
|
|
74
|
|
|
fclose($stream); |
75
|
|
|
|
76
|
|
|
return $contents; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|