fix(tests): update tests and test helpers to current email/DNS APIs, use non-privileged ports, and improve robustness and resilience

This commit is contained in:
2026-02-01 18:10:30 +00:00
parent 2206abd04b
commit b90650c660
23 changed files with 2006 additions and 1756 deletions

View File

@@ -22,57 +22,74 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
let chunkingMode = false;
let totalChunks = 0;
let totalBytes = 0;
let state = 'ready';
let buffer = '';
socket.on('data', (data) => {
const text = data.toString();
if (chunkingMode) {
// In chunking mode, all data is message content
totalBytes += data.length;
console.log(` [Server] Received chunk: ${data.length} bytes`);
return;
}
const command = text.trim();
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-chunking.example.com\r\n');
socket.write('250-CHUNKING\r\n');
socket.write('250-8BITMIME\r\n');
socket.write('250-BINARYMIME\r\n');
socket.write('250 OK\r\n');
} else if (command.startsWith('MAIL FROM:')) {
if (command.includes('BODY=BINARYMIME')) {
console.log(' [Server] Binary MIME body declared');
buffer += data.toString();
let lines = buffer.split('\r\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (state === 'data') {
if (line === '.') {
socket.write('250 OK\r\n');
state = 'ready';
}
continue;
}
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('BDAT ')) {
// BDAT command format: BDAT <size> [LAST]
const parts = command.split(' ');
const chunkSize = parseInt(parts[1]);
const isLast = parts.includes('LAST');
totalChunks++;
console.log(` [Server] BDAT chunk ${totalChunks}: ${chunkSize} bytes${isLast ? ' (LAST)' : ''}`);
if (isLast) {
socket.write(`250 OK: Message accepted, ${totalChunks} chunks, ${totalBytes} total bytes\r\n`);
chunkingMode = false;
totalChunks = 0;
totalBytes = 0;
} else {
socket.write('250 OK: Chunk accepted\r\n');
chunkingMode = true;
const command = line.trim();
if (!command) continue;
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-chunking.example.com\r\n');
socket.write('250-CHUNKING\r\n');
socket.write('250-8BITMIME\r\n');
socket.write('250-BINARYMIME\r\n');
socket.write('250 OK\r\n');
} else if (command.startsWith('MAIL FROM:')) {
if (command.includes('BODY=BINARYMIME')) {
console.log(' [Server] Binary MIME body declared');
}
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('BDAT ')) {
// BDAT command format: BDAT <size> [LAST]
const parts = command.split(' ');
const chunkSize = parseInt(parts[1]);
const isLast = parts.includes('LAST');
totalChunks++;
console.log(` [Server] BDAT chunk ${totalChunks}: ${chunkSize} bytes${isLast ? ' (LAST)' : ''}`);
if (isLast) {
socket.write(`250 OK: Message accepted, ${totalChunks} chunks, ${totalBytes} total bytes\r\n`);
chunkingMode = false;
totalChunks = 0;
totalBytes = 0;
} else {
socket.write('250 OK: Chunk accepted\r\n');
chunkingMode = true;
}
} else if (command === 'DATA') {
// Accept DATA as fallback if client doesn't support BDAT
socket.write('354 Start mail input\r\n');
state = 'data';
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
} else if (command === 'DATA') {
// DATA not allowed when CHUNKING is available
socket.write('503 5.5.1 Use BDAT instead of DATA\r\n');
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
});
}
@@ -104,7 +121,7 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
const result = await smtpClient.sendMail(email);
console.log(' CHUNKING extension handled (if supported by client)');
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
expect(result.success).toBeTruthy();
await testServer.server.close();
})();
@@ -119,42 +136,60 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
console.log(' [Server] Client connected');
socket.write('220 deliverby.example.com ESMTP\r\n');
let state = 'ready';
let buffer = '';
socket.on('data', (data) => {
const command = data.toString().trim();
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-deliverby.example.com\r\n');
socket.write('250-DELIVERBY 86400\r\n'); // 24 hours max
socket.write('250 OK\r\n');
} else if (command.startsWith('MAIL FROM:')) {
// Check for DELIVERBY parameter
const deliverByMatch = command.match(/DELIVERBY=(\d+)([RN]?)/i);
if (deliverByMatch) {
const seconds = parseInt(deliverByMatch[1]);
const mode = deliverByMatch[2] || 'R'; // R=return, N=notify
console.log(` [Server] DELIVERBY: ${seconds} seconds, mode: ${mode}`);
if (seconds > 86400) {
socket.write('501 5.5.4 DELIVERBY time exceeds maximum\r\n');
} else if (seconds < 0) {
socket.write('501 5.5.4 Invalid DELIVERBY time\r\n');
} else {
socket.write('250 OK: Delivery deadline accepted\r\n');
buffer += data.toString();
let lines = buffer.split('\r\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (state === 'data') {
if (line === '.') {
socket.write('250 OK: Message queued with delivery deadline\r\n');
state = 'ready';
}
} else {
socket.write('250 OK\r\n');
continue;
}
const command = line.trim();
if (!command) continue;
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-deliverby.example.com\r\n');
socket.write('250-DELIVERBY 86400\r\n'); // 24 hours max
socket.write('250 OK\r\n');
} else if (command.startsWith('MAIL FROM:')) {
// Check for DELIVERBY parameter
const deliverByMatch = command.match(/DELIVERBY=(\d+)([RN]?)/i);
if (deliverByMatch) {
const seconds = parseInt(deliverByMatch[1]);
const mode = deliverByMatch[2] || 'R'; // R=return, N=notify
console.log(` [Server] DELIVERBY: ${seconds} seconds, mode: ${mode}`);
if (seconds > 86400) {
socket.write('501 5.5.4 DELIVERBY time exceeds maximum\r\n');
} else if (seconds < 0) {
socket.write('501 5.5.4 Invalid DELIVERBY time\r\n');
} else {
socket.write('250 OK: Delivery deadline accepted\r\n');
}
} else {
socket.write('250 OK\r\n');
}
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
state = 'data';
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
} else if (command === '.') {
socket.write('250 OK: Message queued with delivery deadline\r\n');
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
});
}
@@ -193,38 +228,56 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
console.log(' [Server] Client connected');
socket.write('220 etrn.example.com ESMTP\r\n');
let state = 'ready';
let buffer = '';
socket.on('data', (data) => {
const command = data.toString().trim();
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-etrn.example.com\r\n');
socket.write('250-ETRN\r\n');
socket.write('250 OK\r\n');
} else if (command.startsWith('ETRN ')) {
const domain = command.substring(5);
console.log(` [Server] ETRN request for domain: ${domain}`);
if (domain === '@example.com') {
socket.write('250 OK: Queue processing started for example.com\r\n');
} else if (domain === '#urgent') {
socket.write('250 OK: Urgent queue processing started\r\n');
} else if (domain.includes('unknown')) {
socket.write('458 Unable to queue messages for node\r\n');
} else {
socket.write('250 OK: Queue processing started\r\n');
buffer += data.toString();
let lines = buffer.split('\r\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (state === 'data') {
if (line === '.') {
socket.write('250 OK\r\n');
state = 'ready';
}
continue;
}
const command = line.trim();
if (!command) continue;
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-etrn.example.com\r\n');
socket.write('250-ETRN\r\n');
socket.write('250 OK\r\n');
} else if (command.startsWith('ETRN ')) {
const domain = command.substring(5);
console.log(` [Server] ETRN request for domain: ${domain}`);
if (domain === '@example.com') {
socket.write('250 OK: Queue processing started for example.com\r\n');
} else if (domain === '#urgent') {
socket.write('250 OK: Urgent queue processing started\r\n');
} else if (domain.includes('unknown')) {
socket.write('458 Unable to queue messages for node\r\n');
} else {
socket.write('250 OK: Queue processing started\r\n');
}
} else if (command.startsWith('MAIL FROM:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
state = 'data';
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
} else if (command.startsWith('MAIL FROM:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
} else if (command === '.') {
socket.write('250 OK\r\n');
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
});
}
@@ -294,59 +347,77 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
['support-team', ['support@example.com', 'admin@example.com']]
]);
let state = 'ready';
let buffer = '';
socket.on('data', (data) => {
const command = data.toString().trim();
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-verify.example.com\r\n');
socket.write('250-VRFY\r\n');
socket.write('250-EXPN\r\n');
socket.write('250 OK\r\n');
} else if (command.startsWith('VRFY ')) {
const query = command.substring(5);
console.log(` [Server] VRFY query: ${query}`);
// Look up user
const user = users.get(query.toLowerCase());
if (user) {
socket.write(`250 ${user.fullName} <${user.email}>\r\n`);
} else {
// Check if it's an email address
const emailMatch = Array.from(users.values()).find(u =>
u.email.toLowerCase() === query.toLowerCase()
);
if (emailMatch) {
socket.write(`250 ${emailMatch.fullName} <${emailMatch.email}>\r\n`);
} else {
socket.write('550 5.1.1 User unknown\r\n');
buffer += data.toString();
let lines = buffer.split('\r\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (state === 'data') {
if (line === '.') {
socket.write('250 OK\r\n');
state = 'ready';
}
continue;
}
} else if (command.startsWith('EXPN ')) {
const listName = command.substring(5);
console.log(` [Server] EXPN query: ${listName}`);
const list = mailingLists.get(listName.toLowerCase());
if (list) {
socket.write(`250-Mailing list ${listName}:\r\n`);
list.forEach((email, index) => {
const prefix = index < list.length - 1 ? '250-' : '250 ';
socket.write(`${prefix}${email}\r\n`);
});
} else {
socket.write('550 5.1.1 Mailing list not found\r\n');
const command = line.trim();
if (!command) continue;
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-verify.example.com\r\n');
socket.write('250-VRFY\r\n');
socket.write('250-EXPN\r\n');
socket.write('250 OK\r\n');
} else if (command.startsWith('VRFY ')) {
const query = command.substring(5);
console.log(` [Server] VRFY query: ${query}`);
// Look up user
const user = users.get(query.toLowerCase());
if (user) {
socket.write(`250 ${user.fullName} <${user.email}>\r\n`);
} else {
// Check if it's an email address
const emailMatch = Array.from(users.values()).find(u =>
u.email.toLowerCase() === query.toLowerCase()
);
if (emailMatch) {
socket.write(`250 ${emailMatch.fullName} <${emailMatch.email}>\r\n`);
} else {
socket.write('550 5.1.1 User unknown\r\n');
}
}
} else if (command.startsWith('EXPN ')) {
const listName = command.substring(5);
console.log(` [Server] EXPN query: ${listName}`);
const list = mailingLists.get(listName.toLowerCase());
if (list) {
socket.write(`250-Mailing list ${listName}:\r\n`);
list.forEach((email, index) => {
const prefix = index < list.length - 1 ? '250-' : '250 ';
socket.write(`${prefix}${email}\r\n`);
});
} else {
socket.write('550 5.1.1 Mailing list not found\r\n');
}
} else if (command.startsWith('MAIL FROM:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
state = 'data';
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
} else if (command.startsWith('MAIL FROM:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
} else if (command === '.') {
socket.write('250 OK\r\n');
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
});
}
@@ -431,43 +502,61 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
]]
]);
let state = 'ready';
let buffer = '';
socket.on('data', (data) => {
const command = data.toString().trim();
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-help.example.com\r\n');
socket.write('250-HELP\r\n');
socket.write('250 OK\r\n');
} else if (command === 'HELP' || command === 'HELP HELP') {
socket.write('214-This server provides HELP for the following topics:\r\n');
socket.write('214-COMMANDS - List of available commands\r\n');
socket.write('214-EXTENSIONS - List of supported extensions\r\n');
socket.write('214-SYNTAX - Command syntax rules\r\n');
socket.write('214 Use HELP <topic> for specific information\r\n');
} else if (command.startsWith('HELP ')) {
const topic = command.substring(5).toLowerCase();
const helpText = helpTopics.get(topic);
if (helpText) {
helpText.forEach((line, index) => {
const prefix = index < helpText.length - 1 ? '214-' : '214 ';
socket.write(`${prefix}${line}\r\n`);
});
} else {
socket.write('504 5.3.0 HELP topic not available\r\n');
buffer += data.toString();
let lines = buffer.split('\r\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (state === 'data') {
if (line === '.') {
socket.write('250 OK\r\n');
state = 'ready';
}
continue;
}
const command = line.trim();
if (!command) continue;
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-help.example.com\r\n');
socket.write('250-HELP\r\n');
socket.write('250 OK\r\n');
} else if (command === 'HELP' || command === 'HELP HELP') {
socket.write('214-This server provides HELP for the following topics:\r\n');
socket.write('214-COMMANDS - List of available commands\r\n');
socket.write('214-EXTENSIONS - List of supported extensions\r\n');
socket.write('214-SYNTAX - Command syntax rules\r\n');
socket.write('214 Use HELP <topic> for specific information\r\n');
} else if (command.startsWith('HELP ')) {
const topic = command.substring(5).toLowerCase();
const helpText = helpTopics.get(topic);
if (helpText) {
helpText.forEach((line, index) => {
const prefix = index < helpText.length - 1 ? '214-' : '214 ';
socket.write(`${prefix}${line}\r\n`);
});
} else {
socket.write('504 5.3.0 HELP topic not available\r\n');
}
} else if (command.startsWith('MAIL FROM:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
state = 'data';
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
} else if (command.startsWith('MAIL FROM:')) {
socket.write('250 OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
socket.write('250 OK\r\n');
} else if (command === 'DATA') {
socket.write('354 Start mail input\r\n');
} else if (command === '.') {
socket.write('250 OK\r\n');
} else if (command === 'QUIT') {
socket.write('221 Bye\r\n');
socket.end();
}
});
}
@@ -526,99 +615,114 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
socket.write('220 combined.example.com ESMTP\r\n');
let activeExtensions: string[] = [];
let state = 'ready';
let buffer = '';
socket.on('data', (data) => {
const command = data.toString().trim();
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-combined.example.com\r\n');
// Announce multiple extensions
const extensions = [
'SIZE 52428800',
'8BITMIME',
'SMTPUTF8',
'ENHANCEDSTATUSCODES',
'PIPELINING',
'DSN',
'DELIVERBY 86400',
'CHUNKING',
'BINARYMIME',
'HELP'
];
extensions.forEach(ext => {
socket.write(`250-${ext}\r\n`);
activeExtensions.push(ext.split(' ')[0]);
});
socket.write('250 OK\r\n');
console.log(` [Server] Active extensions: ${activeExtensions.join(', ')}`);
} else if (command.startsWith('MAIL FROM:')) {
// Check for multiple extension parameters
const params = [];
if (command.includes('SIZE=')) {
const sizeMatch = command.match(/SIZE=(\d+)/);
if (sizeMatch) params.push(`SIZE=${sizeMatch[1]}`);
}
if (command.includes('BODY=')) {
const bodyMatch = command.match(/BODY=(\w+)/);
if (bodyMatch) params.push(`BODY=${bodyMatch[1]}`);
}
if (command.includes('SMTPUTF8')) {
params.push('SMTPUTF8');
}
if (command.includes('DELIVERBY=')) {
const deliverByMatch = command.match(/DELIVERBY=(\d+)/);
if (deliverByMatch) params.push(`DELIVERBY=${deliverByMatch[1]}`);
}
if (params.length > 0) {
console.log(` [Server] Extension parameters: ${params.join(', ')}`);
}
socket.write('250 2.1.0 Sender OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
// Check for DSN parameters
if (command.includes('NOTIFY=')) {
const notifyMatch = command.match(/NOTIFY=([^,\s]+)/);
if (notifyMatch) {
console.log(` [Server] DSN NOTIFY: ${notifyMatch[1]}`);
}
}
socket.write('250 2.1.5 Recipient OK\r\n');
} else if (command === 'DATA') {
if (activeExtensions.includes('CHUNKING')) {
socket.write('503 5.5.1 Use BDAT when CHUNKING is available\r\n');
} else {
socket.write('354 Start mail input\r\n');
}
} else if (command.startsWith('BDAT ')) {
if (activeExtensions.includes('CHUNKING')) {
const parts = command.split(' ');
const size = parts[1];
const isLast = parts.includes('LAST');
console.log(` [Server] BDAT chunk: ${size} bytes${isLast ? ' (LAST)' : ''}`);
if (isLast) {
buffer += data.toString();
let lines = buffer.split('\r\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (state === 'data') {
if (line === '.') {
socket.write('250 2.0.0 Message accepted\r\n');
} else {
socket.write('250 2.0.0 Chunk accepted\r\n');
state = 'ready';
}
} else {
socket.write('500 5.5.1 CHUNKING not available\r\n');
continue;
}
const command = line.trim();
if (!command) continue;
console.log(` [Server] Received: ${command}`);
if (command.startsWith('EHLO')) {
socket.write('250-combined.example.com\r\n');
// Announce multiple extensions
const extensions = [
'SIZE 52428800',
'8BITMIME',
'SMTPUTF8',
'ENHANCEDSTATUSCODES',
'PIPELINING',
'DSN',
'DELIVERBY 86400',
'CHUNKING',
'BINARYMIME',
'HELP'
];
extensions.forEach(ext => {
socket.write(`250-${ext}\r\n`);
activeExtensions.push(ext.split(' ')[0]);
});
socket.write('250 OK\r\n');
console.log(` [Server] Active extensions: ${activeExtensions.join(', ')}`);
} else if (command.startsWith('MAIL FROM:')) {
// Check for multiple extension parameters
const params = [];
if (command.includes('SIZE=')) {
const sizeMatch = command.match(/SIZE=(\d+)/);
if (sizeMatch) params.push(`SIZE=${sizeMatch[1]}`);
}
if (command.includes('BODY=')) {
const bodyMatch = command.match(/BODY=(\w+)/);
if (bodyMatch) params.push(`BODY=${bodyMatch[1]}`);
}
if (command.includes('SMTPUTF8')) {
params.push('SMTPUTF8');
}
if (command.includes('DELIVERBY=')) {
const deliverByMatch = command.match(/DELIVERBY=(\d+)/);
if (deliverByMatch) params.push(`DELIVERBY=${deliverByMatch[1]}`);
}
if (params.length > 0) {
console.log(` [Server] Extension parameters: ${params.join(', ')}`);
}
socket.write('250 2.1.0 Sender OK\r\n');
} else if (command.startsWith('RCPT TO:')) {
// Check for DSN parameters
if (command.includes('NOTIFY=')) {
const notifyMatch = command.match(/NOTIFY=([^,\s]+)/);
if (notifyMatch) {
console.log(` [Server] DSN NOTIFY: ${notifyMatch[1]}`);
}
}
socket.write('250 2.1.5 Recipient OK\r\n');
} else if (command === 'DATA') {
// Accept DATA as fallback even when CHUNKING is advertised
// Most clients don't support BDAT
socket.write('354 Start mail input\r\n');
state = 'data';
} else if (command.startsWith('BDAT ')) {
if (activeExtensions.includes('CHUNKING')) {
const parts = command.split(' ');
const size = parts[1];
const isLast = parts.includes('LAST');
console.log(` [Server] BDAT chunk: ${size} bytes${isLast ? ' (LAST)' : ''}`);
if (isLast) {
socket.write('250 2.0.0 Message accepted\r\n');
} else {
socket.write('250 2.0.0 Chunk accepted\r\n');
}
} else {
socket.write('500 5.5.1 CHUNKING not available\r\n');
}
} else if (command === 'QUIT') {
socket.write('221 2.0.0 Bye\r\n');
socket.end();
}
} else if (command === '.') {
socket.write('250 2.0.0 Message accepted\r\n');
} else if (command === 'QUIT') {
socket.write('221 2.0.0 Bye\r\n');
socket.end();
}
});
}
@@ -645,7 +749,7 @@ tap.test('CRFC-08: should handle SMTP extensions correctly (Various RFCs)', asyn
const result = await smtpClient.sendMail(email);
console.log(' Multiple extension combination handled');
expect(result).toBeDefined();
expect(result.messageId).toBeDefined();
expect(result.success).toBeTruthy();
await testServer.server.close();
})();