-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathServerTest.php
199 lines (156 loc) · 5.92 KB
/
ServerTest.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
<?php
namespace TheFox\Test;
use PHPUnit\Framework\TestCase;
use Zend\Mail\Message;
use TheFox\Smtp\Server;
use TheFox\Smtp\Client;
use TheFox\Smtp\Event;
use TheFox\Network\Socket;
class ServerTest extends TestCase
{
public function testClientNew()
{
$socket = new Socket();
$server = new Server();
$client = $server->newClient($socket);
$this->assertTrue($client instanceof Client);
}
public function testClientGetByHandle()
{
$socket = new Socket();
$socket->bind('127.0.0.1', 22143);
$socket->listen();
$handle1 = $socket->getHandle();
$server = new Server();
$client1 = $server->newClient($socket);
$client2 = $server->getClientByHandle($handle1);
$this->assertEquals($client1, $client2);
$res = fopen('data://text/plain,string', 'r');
$this->assertNull($server->getClientByHandle($res));
$server->shutdown();
}
public function testClientRemove()
{
$socket = new Socket();
$socket->bind('127.0.0.1', 22143);
$socket->listen();
$server = new Server();
$client = $server->newClient($socket);
$server->removeClient($client);
$this->assertTrue($client->getStatus('hasShutdown'));
$server->shutdown();
}
public function testEvent()
{
$server = new Server();
$testData = 21;
$phpunit = $this;
$fn = function ($event, $from, $rcpt, $mail) use ($phpunit, &$testData) {
$testData = 24;
$phpunit->assertEquals('[email protected]', $from);
$phpunit->assertEquals(
$rcpt
);
$current = [];
foreach ($mail->getTo() as $n => $address) {
$current[] = $address->toString();
}
$phpunit->assertEquals(['Joe User <[email protected]>', '<[email protected]>'], $current);
$phpunit->assertEquals('Here is the subject', $mail->getSubject());
return 42;
};
$event1 = new Event(Event::TRIGGER_NEW_MAIL, null, $fn);
$server->addEvent($event1);
$testObj = new TestObj();
$event2 = new Event(Event::TRIGGER_NEW_MAIL, $testObj, 'test1');
$server->addEvent($event2);
$mail = '';
$mail .= 'Date: Thu, 31 Jul 2014 22:18:51 +0200' . Client::MSG_SEPARATOR;
$mail .= 'To: Joe User <[email protected]>, [email protected]' . Client::MSG_SEPARATOR;
$mail .= 'From: Mailer <[email protected]>' . Client::MSG_SEPARATOR;
$mail .= 'Cc: [email protected]' . Client::MSG_SEPARATOR;
$mail .= 'Reply-To: Information <[email protected]>' . Client::MSG_SEPARATOR;
$mail .= 'Subject: Here is the subject' . Client::MSG_SEPARATOR;
$mail .= 'MIME-Version: 1.0' . Client::MSG_SEPARATOR;
$mail .= 'Content-Type: text/plain; charset=iso-8859-1' . Client::MSG_SEPARATOR;
$mail .= 'Content-Transfer-Encoding: 8bit' . Client::MSG_SEPARATOR;
$mail .= '' . Client::MSG_SEPARATOR;
$mail .= 'This is the message body.' . Client::MSG_SEPARATOR;
$mail .= 'END' . Client::MSG_SEPARATOR;
$zmail = Message::fromString($mail);
$server->newMail('[email protected]', $rcpt, $zmail);
$this->assertEquals(24, $testData);
$this->assertEquals(42, $event1->getReturnValue());
$this->assertEquals(43, $event2->getReturnValue());
}
public function rcptProvider()
{
return [
'valid' => ['[email protected]', true],
'invalid' => ['[email protected]', false],
];
}
/**
* @dataProvider rcptProvider
* @param string $mail
* @param bool $valid
*/
public function testEventNewRcpt($mail, $valid)
{
$server = new Server();
$phpunit = $this;
$event1 = new Event(Event::TRIGGER_NEW_RCPT, null, function ($event, $rcpt) use ($phpunit, $mail, $valid) {
$phpunit->assertEquals($mail, $rcpt);
return $valid;
});
$server->addEvent($event1);
$return = $server->newRcpt($mail);
$this->assertEquals($valid, $return);
}
public function testEventAuthWithFalse()
{
$server = new Server();
$username = 'testuser';
$password = 'super_secret_password';
$phpunit = $this;
$event1 = new Event(
Event::TRIGGER_AUTH_ATTEMPT,
null,
function ($event, $method, $credentials) {
return false;
}
);
$server->addEvent($event1);
$testObj = new TestObj();
$event2 = new Event(Event::TRIGGER_AUTH_ATTEMPT, $testObj, 'test2');
$server->addEvent($event2);
$method = 'LOGIN';
$credentials = ['user' => base64_encode($username), 'password' => base64_encode($password)];
$authenticated = $server->authenticateUser($method, $credentials);
$this->assertFalse($authenticated);
}
public function testEventAuthWithAllTrue()
{
$server = new Server();
$username = 'testuser';
$password = 'super_secret_password';
$phpunit = $this;
$event1 = new Event(
Event::TRIGGER_AUTH_ATTEMPT,
null,
function ($event, $method, $credentials) {
return true;
}
);
$server->addEvent($event1);
$testObj = new TestObj();
$event2 = new Event(Event::TRIGGER_AUTH_ATTEMPT, $testObj, 'test2');
$server->addEvent($event2);
$method = 'LOGIN';
$credentials = ['user' => base64_encode($username), 'password' => base64_encode($password)];
$authenticated = $server->authenticateUser($method, $credentials);
$this->assertTrue($authenticated);
}
}