feat: Add comprehensive SMTP test suite for Deno

- Implemented SMTP client utilities in `test/helpers/smtp.client.ts` for creating test clients, sending emails, and testing connections.
- Developed SMTP protocol test utilities in `test/helpers/utils.ts` for managing TCP connections, sending commands, and handling responses.
- Created a detailed README in `test/readme.md` outlining the test framework, infrastructure, organization, and running instructions.
- Ported CMD-01: EHLO Command tests in `test/suite/smtpserver_commands/test.cmd-01.ehlo-command.test.ts` with multiple scenarios including valid and invalid hostnames.
- Ported CMD-02: MAIL FROM Command tests in `test/suite/smtpserver_commands/test.cmd-02.mail-from.test.ts` covering valid address acceptance, invalid address rejection, SIZE parameter support, and command sequence enforcement.
This commit is contained in:
2025-10-25 15:05:11 +00:00
parent d7f37afc30
commit 1698df3a53
12 changed files with 1668 additions and 3 deletions

View File

@@ -0,0 +1,154 @@
/**
* CMD-01: EHLO Command Tests
* Tests SMTP EHLO command and server capabilities advertisement
*/
import { assert, assertEquals, assertMatch } from '@std/assert';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.ts';
import {
connectToSmtp,
waitForGreeting,
sendSmtpCommand,
closeSmtpConnection,
} from '../../helpers/utils.ts';
const TEST_PORT = 25251;
let testServer: ITestServer;
Deno.test({
name: 'CMD-01: Setup - Start SMTP server',
async fn() {
testServer = await startTestServer({ port: TEST_PORT });
assert(testServer, 'Test server should be created');
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-01: EHLO Command - server responds with proper capabilities',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
// Wait for greeting
const greeting = await waitForGreeting(conn);
assert(greeting.includes('220'), 'Should receive 220 greeting');
// Send EHLO
const ehloResponse = await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
// Parse capabilities
const lines = ehloResponse
.split('\r\n')
.filter((line) => line.startsWith('250'))
.filter((line) => line.length > 0);
const capabilities = lines.map((line) => line.substring(4).trim());
console.log('📋 Server capabilities:', capabilities);
// Verify essential capabilities
assert(
capabilities.some((cap) => cap.includes('SIZE')),
'Should advertise SIZE capability'
);
assert(
capabilities.some((cap) => cap.includes('8BITMIME')),
'Should advertise 8BITMIME capability'
);
// The last line should be "250 " (without hyphen)
const lastLine = lines[lines.length - 1];
assert(lastLine.startsWith('250 '), 'Last line should start with "250 " (space, not hyphen)');
} finally {
await closeSmtpConnection(conn);
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-01: EHLO with invalid hostname - server handles gracefully',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
await waitForGreeting(conn);
const invalidHostnames = [
'', // Empty hostname
' ', // Whitespace only
'invalid..hostname', // Double dots
'.invalid', // Leading dot
'invalid.', // Trailing dot
'very-long-hostname-that-exceeds-reasonable-limits-' + 'x'.repeat(200),
];
for (const hostname of invalidHostnames) {
console.log(`Testing invalid hostname: "${hostname}"`);
try {
const response = await sendSmtpCommand(conn, `EHLO ${hostname}`);
// Server should either accept with warning or reject with 5xx
assertMatch(response, /^(250|5\d\d)/, 'Server should respond with 250 or 5xx');
// Reset session for next test
if (response.startsWith('250')) {
await sendSmtpCommand(conn, 'RSET', '250');
}
} catch (error) {
// Some invalid hostnames might cause connection issues, which is acceptable
console.log(` Hostname "${hostname}" caused error (acceptable):`, error.message);
}
}
// Send QUIT
await sendSmtpCommand(conn, 'QUIT', '221');
} finally {
try {
conn.close();
} catch {
// Ignore close errors
}
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-01: EHLO command pipelining - multiple EHLO commands',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
await waitForGreeting(conn);
// First EHLO
const ehlo1Response = await sendSmtpCommand(conn, 'EHLO first.example.com', '250');
assert(ehlo1Response.startsWith('250'), 'First EHLO should succeed');
// Second EHLO (should reset session)
const ehlo2Response = await sendSmtpCommand(conn, 'EHLO second.example.com', '250');
assert(ehlo2Response.startsWith('250'), 'Second EHLO should succeed');
// Verify session was reset by trying MAIL FROM
const mailResponse = await sendSmtpCommand(conn, 'MAIL FROM:<test@example.com>', '250');
assert(mailResponse.startsWith('250'), 'MAIL FROM should work after second EHLO');
} finally {
await closeSmtpConnection(conn);
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-01: Cleanup - Stop SMTP server',
async fn() {
await stopTestServer(testServer);
},
sanitizeResources: false,
sanitizeOps: false,
});

View File

@@ -0,0 +1,169 @@
/**
* CMD-02: MAIL FROM Command Tests
* Tests SMTP MAIL FROM command validation and handling
*/
import { assert, assertMatch } from '@std/assert';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.ts';
import {
connectToSmtp,
waitForGreeting,
sendSmtpCommand,
closeSmtpConnection,
} from '../../helpers/utils.ts';
const TEST_PORT = 25252;
let testServer: ITestServer;
Deno.test({
name: 'CMD-02: Setup - Start SMTP server',
async fn() {
testServer = await startTestServer({ port: TEST_PORT });
assert(testServer, 'Test server should be created');
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-02: MAIL FROM - accepts valid sender addresses',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
await waitForGreeting(conn);
await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
const validAddresses = [
'sender@example.com',
'test.user+tag@example.com',
'user@[192.168.1.1]', // IP literal
'user@subdomain.example.com',
'user@very-long-domain-name-that-is-still-valid.example.com',
'test_user@example.com', // underscore in local part
];
for (const address of validAddresses) {
console.log(`✓ Testing valid address: ${address}`);
const response = await sendSmtpCommand(conn, `MAIL FROM:<${address}>`, '250');
assert(response.startsWith('250'), `Should accept valid address: ${address}`);
// Reset for next test
await sendSmtpCommand(conn, 'RSET', '250');
}
} finally {
await closeSmtpConnection(conn);
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-02: MAIL FROM - rejects invalid sender addresses',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
await waitForGreeting(conn);
await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
const invalidAddresses = [
'notanemail', // No @ symbol
'@example.com', // Missing local part
'user@', // Missing domain
'user@.com', // Invalid domain
'user@domain..com', // Double dot
'user space@example.com', // Space in address
];
for (const address of invalidAddresses) {
console.log(`✗ Testing invalid address: ${address}`);
try {
const response = await sendSmtpCommand(conn, `MAIL FROM:<${address}>`);
// Should get 5xx error
assertMatch(response, /^5\d\d/, `Should reject invalid address with 5xx: ${address}`);
} catch (error) {
// Connection might be dropped for really bad input, which is acceptable
console.log(` Address "${address}" caused error (acceptable):`, error.message);
}
// Try to reset (may fail if connection dropped)
try {
await sendSmtpCommand(conn, 'RSET', '250');
} catch {
// Reset after connection closed, reconnect for next test
conn.close();
return; // Exit test early if connection was dropped
}
}
} finally {
try {
await closeSmtpConnection(conn);
} catch {
// Ignore errors if connection already closed
}
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-02: MAIL FROM - supports SIZE parameter',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
await waitForGreeting(conn);
const caps = await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
// Verify SIZE is advertised
assert(caps.includes('SIZE'), 'Server should advertise SIZE capability');
// Try MAIL FROM with SIZE parameter
const response = await sendSmtpCommand(
conn,
'MAIL FROM:<sender@example.com> SIZE=5000',
'250'
);
assert(response.startsWith('250'), 'Should accept SIZE parameter');
} finally {
await closeSmtpConnection(conn);
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-02: MAIL FROM - enforces correct sequence',
async fn() {
const conn = await connectToSmtp('localhost', TEST_PORT);
try {
await waitForGreeting(conn);
// Try MAIL FROM before EHLO - should fail
const response = await sendSmtpCommand(conn, 'MAIL FROM:<sender@example.com>');
assertMatch(response, /^5\d\d/, 'Should reject MAIL FROM before EHLO/HELO');
} finally {
try {
conn.close();
} catch {
// Ignore close errors
}
}
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'CMD-02: Cleanup - Stop SMTP server',
async fn() {
await stopTestServer(testServer);
},
sanitizeResources: false,
sanitizeOps: false,
});