forked from andris9/simplesmtp
/
server.js
290 lines (274 loc) · 10.5 KB
/
server.js
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
var testCase = require('nodeunit').testCase,
runClientMockup = require("rai").runClientMockup,
simplesmtp = require("../index"),
netlib = require("net");
var PORT_NUMBER = 8397;
// monkey patch net and tls to support nodejs 0.4
if(!netlib.connect && netlib.createConnection){
netlib.connect = netlib.createConnection;
}
exports["General tests"] = {
setUp: function (callback) {
this.smtp = new simplesmtp.createServer({
SMTPBanner: "SCORPIO",
name: "MYRDO",
maxSize: 1234,
debug: true
});
this.smtp.listen(PORT_NUMBER, function(err){
if(err){
throw err;
}else{
callback();
}
});
},
tearDown: function (callback) {
this.smtp.end(callback);
},
"QUIT": function(test){
var cmds = ["QUIT"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("221",resp.toString("utf-8").trim().substr(0,3));
test.done();
});
},
"HELO": function(test){
var cmds = ["HELO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("250",resp.toString("utf-8").trim().substr(0,3));
test.done();
});
},
"HELO fails": function(test){
var cmds = ["HELO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"EHLO": function(test){
var cmds = ["EHLO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
resp = resp.toString("utf-8").trim();
var lines = resp.split("\r\n");
for(var i=0; i<lines.length-1; i++){
test.equal("250-", lines[i].substr(0,4));
}
test.equal("250 ", lines[i].substr(0,4));
test.done();
});
},
"EHLO fails": function(test){
var cmds = ["EHLO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"HELO after STARTTLS": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "HELO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("250",resp.toString("utf-8").trim().substr(0,3));
test.done();
});
},
"HELO fails after STARTTLS": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "HELO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"EHLO after STARTTLS": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "HELO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
resp = resp.toString("utf-8").trim();
var lines = resp.split("\r\n");
for(var i=0; i<lines.length-1; i++){
test.equal("250-", lines[i].substr(0,4));
}
test.equal("250 ", lines[i].substr(0,4));
test.done();
});
},
"EHLO fails after STARTTLS": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH fails if not required": function(test){
var cmds = ["EHLO FOO", "AUTH LOGIN"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH fails if not required TLS": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "AUTH LOGIN"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"Custom Greeting banner": function(test){
var client = netlib.connect(PORT_NUMBER, function(){
client.on("data", function(chunk){
test.equal("SCORPIO", (chunk || "").toString().trim().split(" ").pop());
client.end();
});
client.on('end', function() {
test.done();
});
});
},
"HELO name": function(test){
var cmds = ["HELO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("MYRDO",resp.toString("utf-8").trim().substr(4).split(" ").shift());
test.done();
});
},
"EHLO name": function(test){
var cmds = ["EHLO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("MYRDO",resp.toString("utf-8").trim().substr(4).split(" ").shift());
test.done();
});
},
"MAXSIZE": function(test){
var cmds = ["EHLO FOO"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.ok(resp.toString("utf-8").trim().match(/^250[\- ]SIZE 1234$/mi));
test.done();
});
}
};
exports["EHLO setting"] = {
setUp: function (callback) {
this.smtp = new simplesmtp.createServer({
disableEHLO: true
});
this.smtp.listen(PORT_NUMBER, function(err){
if(err){
throw err;
}else{
callback();
}
});
},
tearDown: function (callback) {
this.smtp.end(callback);
},
"Disable EHLO": function(test){
runClientMockup(PORT_NUMBER, "localhost", ["EHLO foo"], function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
runClientMockup(PORT_NUMBER, "localhost", ["HELO foo"], function(resp){
test.equal("2",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
});
}
};
exports["Require AUTH"] = {
setUp: function (callback) {
this.smtp = new simplesmtp.createServer({requireAuthentication: true});
this.smtp.listen(PORT_NUMBER, function(err){
if(err){
throw err;
}else{
callback();
}
});
this.smtp.on("authorizeUser", function(envelope, username, password, callback){
callback(null, username=="andris" && password=="test");
});
},
tearDown: function (callback) {
this.smtp.end(callback);
},
"Unknown AUTH": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH CRAM"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH fails before STARTTLS": function(test){
var cmds = ["EHLO FOO", "AUTH LOGIN"];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH LOGIN Invalid login": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH LOGIN",
new Buffer("inv").toString("base64"),
new Buffer("alid").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH LOGIN Invalid username": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH LOGIN",
new Buffer("inv").toString("base64"),
new Buffer("test").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH LOGIN Invalid password": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH LOGIN",
new Buffer("andris").toString("base64"),
new Buffer("alid").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH LOGIN Login success": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH LOGIN",
new Buffer("andris").toString("base64"),
new Buffer("test").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("2",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH PLAIN Invalid login": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH PLAIN"+
new Buffer("inv\u0000inv\u0000alid").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH PLAIN Invalid user": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH PLAIN"+
new Buffer("inv\u0000inv\u0000test").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH PLAIN Invalid password": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH PLAIN"+
new Buffer("andris\u0000andris\u0000alid").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
},
"AUTH PLAIN Login success": function(test){
var cmds = ["EHLO FOO", "STARTTLS", "EHLO FOO", "AUTH PLAIN"+
new Buffer("andris\u0000andris\u0000test").toString("base64")];
runClientMockup(PORT_NUMBER, "localhost", cmds, function(resp){
test.equal("5",resp.toString("utf-8").trim().substr(0,1));
test.done();
});
}
};