- Ported CMD-06 RSET Command tests with 8 passing tests covering transaction resets and recipient clearing. - Ported SEC-06 IP Reputation tests with 7 passing tests validating infrastructure and legitimate traffic acceptance. - Ported ERR-01 Syntax Error tests with 10 passing tests for handling invalid commands and syntax errors. - Updated README files to reflect the new test statuses and coverage. - Added detailed test cases for handling invalid sequences in ERR-02 tests.
		
			
				
	
	
		
			304 lines
		
	
	
		
			8.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			304 lines
		
	
	
		
			8.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * ERR-02: Invalid Sequence Tests
 | |
|  * Tests SMTP server handling of commands in incorrect sequence
 | |
|  */
 | |
| 
 | |
| import { assert, assertMatch } from '@std/assert';
 | |
| import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.ts';
 | |
| import {
 | |
|   connectToSmtp,
 | |
|   waitForGreeting,
 | |
|   sendSmtpCommand,
 | |
|   readSmtpResponse,
 | |
|   closeSmtpConnection,
 | |
| } from '../../helpers/utils.ts';
 | |
| 
 | |
| const TEST_PORT = 25262;
 | |
| let testServer: ITestServer;
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-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: 'ERR-02: Invalid Sequence - rejects MAIL FROM before EHLO',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
| 
 | |
|       // Send MAIL FROM without EHLO
 | |
|       const encoder = new TextEncoder();
 | |
|       await conn.write(encoder.encode('MAIL FROM:<test@example.com>\r\n'));
 | |
| 
 | |
|       const response = await readSmtpResponse(conn);
 | |
| 
 | |
|       // Should return 503 (bad sequence of commands)
 | |
|       assertMatch(response, /^503/, 'Should reject MAIL FROM before EHLO with 503');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log('✓ MAIL FROM before EHLO rejected');
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - rejects RCPT TO before MAIL FROM',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
|       await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
 | |
| 
 | |
|       // Send RCPT TO without MAIL FROM
 | |
|       const encoder = new TextEncoder();
 | |
|       await conn.write(encoder.encode('RCPT TO:<test@example.com>\r\n'));
 | |
| 
 | |
|       const response = await readSmtpResponse(conn);
 | |
| 
 | |
|       // Should return 503 (bad sequence of commands)
 | |
|       assertMatch(response, /^503/, 'Should reject RCPT TO before MAIL FROM with 503');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log('✓ RCPT TO before MAIL FROM rejected');
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - rejects DATA before RCPT TO',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
|       await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
 | |
|       await sendSmtpCommand(conn, 'MAIL FROM:<test@example.com>', '250');
 | |
| 
 | |
|       // Send DATA without RCPT TO
 | |
|       const encoder = new TextEncoder();
 | |
|       await conn.write(encoder.encode('DATA\r\n'));
 | |
| 
 | |
|       const response = await readSmtpResponse(conn);
 | |
| 
 | |
|       // RFC 5321: Should return 503 (bad sequence of commands)
 | |
|       assertMatch(response, /^503/, 'Should reject DATA before RCPT TO with 503');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log('✓ DATA before RCPT TO rejected');
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - allows multiple EHLO commands',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
| 
 | |
|       // Send multiple EHLO commands
 | |
|       const response1 = await sendSmtpCommand(conn, 'EHLO test1.example.com', '250');
 | |
|       assert(response1.includes('250'), 'First EHLO should succeed');
 | |
| 
 | |
|       const response2 = await sendSmtpCommand(conn, 'EHLO test2.example.com', '250');
 | |
|       assert(response2.includes('250'), 'Second EHLO should succeed');
 | |
| 
 | |
|       const response3 = await sendSmtpCommand(conn, 'EHLO test3.example.com', '250');
 | |
|       assert(response3.includes('250'), 'Third EHLO should succeed');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log('✓ Multiple EHLO commands allowed');
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - rejects second MAIL FROM without RSET',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
|       await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
 | |
|       await sendSmtpCommand(conn, 'MAIL FROM:<sender1@example.com>', '250');
 | |
| 
 | |
|       // Send second MAIL FROM without RSET
 | |
|       const encoder = new TextEncoder();
 | |
|       await conn.write(encoder.encode('MAIL FROM:<sender2@example.com>\r\n'));
 | |
| 
 | |
|       const response = await readSmtpResponse(conn);
 | |
| 
 | |
|       // Should return 503 (bad sequence) or 250 (some implementations allow overwrite)
 | |
|       assertMatch(response, /^(503|250)/, 'Should handle second MAIL FROM');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log(`✓ Second MAIL FROM handled: ${response.substring(0, 3)}`);
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - rejects DATA without MAIL FROM',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
|       await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
 | |
| 
 | |
|       // Send DATA without MAIL FROM
 | |
|       const encoder = new TextEncoder();
 | |
|       await conn.write(encoder.encode('DATA\r\n'));
 | |
| 
 | |
|       const response = await readSmtpResponse(conn);
 | |
| 
 | |
|       // Should return 503 (bad sequence of commands)
 | |
|       assertMatch(response, /^503/, 'Should reject DATA without MAIL FROM with 503');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log('✓ DATA without MAIL FROM rejected');
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - handles commands after QUIT',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
|       await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
| 
 | |
|       // Try to send command after QUIT
 | |
|       const encoder = new TextEncoder();
 | |
|       let writeSucceeded = false;
 | |
| 
 | |
|       try {
 | |
|         await conn.write(encoder.encode('EHLO test.example.com\r\n'));
 | |
|         writeSucceeded = true;
 | |
| 
 | |
|         // If write succeeded, wait to see if we get a response (we shouldn't)
 | |
|         await new Promise((resolve) => setTimeout(resolve, 500));
 | |
|       } catch {
 | |
|         // Write failed - connection already closed (expected)
 | |
|       }
 | |
| 
 | |
|       // Either write failed or no response received after QUIT (both acceptable)
 | |
|       assert(true, 'Commands after QUIT handled correctly');
 | |
|       console.log(`✓ Commands after QUIT handled (write ${writeSucceeded ? 'succeeded but ignored' : 'failed'})`);
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Already closed
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Invalid Sequence - recovers from syntax error in sequence',
 | |
|   async fn() {
 | |
|     const conn = await connectToSmtp('localhost', TEST_PORT);
 | |
| 
 | |
|     try {
 | |
|       await waitForGreeting(conn);
 | |
|       await sendSmtpCommand(conn, 'EHLO test.example.com', '250');
 | |
|       await sendSmtpCommand(conn, 'MAIL FROM:<sender@example.com>', '250');
 | |
| 
 | |
|       // Send RCPT TO with wrong syntax (missing brackets)
 | |
|       const encoder = new TextEncoder();
 | |
|       await conn.write(encoder.encode('RCPT TO:recipient@example.com\r\n'));
 | |
| 
 | |
|       const badResponse = await readSmtpResponse(conn);
 | |
|       assertMatch(badResponse, /^501/, 'Should reject RCPT TO without brackets with 501');
 | |
| 
 | |
|       // Now send valid RCPT TO (session should still be valid)
 | |
|       const goodResponse = await sendSmtpCommand(conn, 'RCPT TO:<recipient@example.com>', '250');
 | |
|       assert(goodResponse.includes('250'), 'Should accept valid RCPT TO after syntax error');
 | |
| 
 | |
|       await sendSmtpCommand(conn, 'QUIT', '221');
 | |
|       console.log('✓ Session recovered from syntax error');
 | |
|     } finally {
 | |
|       try {
 | |
|         conn.close();
 | |
|       } catch {
 | |
|         // Ignore
 | |
|       }
 | |
|     }
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 | |
| 
 | |
| Deno.test({
 | |
|   name: 'ERR-02: Cleanup - Stop SMTP server',
 | |
|   async fn() {
 | |
|     await stopTestServer(testServer);
 | |
|   },
 | |
|   sanitizeResources: false,
 | |
|   sanitizeOps: false,
 | |
| });
 |