dcrouter/test/suite/connection/test.tls-connection.ts

59 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-05-23 19:03:44 +00:00
import { tap, expect } from '@git.zone/tstest/tapbundle';
2025-05-23 19:49:25 +00:00
import { startTestServer, stopTestServer, type ITestServer } from '../server.loader.js';
import type { SmtpServer } from '../../../ts/mail/delivery/smtpserver/index.js';
2025-05-23 19:03:44 +00:00
import { connectToSmtp, performSmtpHandshake, closeSmtpConnection } from '../../helpers/test.utils.js';
2025-05-23 19:49:25 +00:00
let testServer: SmtpServer;
2025-05-23 19:03:44 +00:00
tap.test('setup - start SMTP server with TLS support', async () => {
2025-05-23 19:49:25 +00:00
testServer = await startTestServer();
await new Promise(resolve => setTimeout(resolve, 1000));
2025-05-23 19:03:44 +00:00
expect(testServer.port).toEqual(2525);
});
tap.test('CM-01: TLS Connection Test - server should advertise STARTTLS capability', async () => {
const startTime = Date.now();
try {
// Connect to SMTP server
const socket = await connectToSmtp(testServer.hostname, testServer.port);
expect(socket).toBeInstanceOf(Object);
// Perform handshake and get capabilities
const capabilities = await performSmtpHandshake(socket, 'test.example.com');
expect(capabilities).toBeArray();
// Check for STARTTLS support
const supportsStarttls = capabilities.some(cap => cap.toUpperCase().includes('STARTTLS'));
expect(supportsStarttls).toBeTrue();
// Close connection gracefully
await closeSmtpConnection(socket);
const duration = Date.now() - startTime;
console.log(`✅ TLS capability test completed in ${duration}ms`);
console.log(`📋 Server capabilities: ${capabilities.join(', ')}`);
} catch (error) {
const duration = Date.now() - startTime;
console.error(`❌ TLS connection test failed after ${duration}ms:`, error);
throw error;
}
});
tap.test('CM-01: TLS Connection Test - verify TLS certificate configuration', async () => {
// This test verifies that the server has TLS certificates configured
expect(testServer.config.tlsEnabled).toBeTrue();
// The server should have loaded certificates during startup
// In production, this would validate actual certificate properties
console.log('✅ TLS configuration verified');
});
tap.test('cleanup - stop SMTP server', async () => {
2025-05-23 19:49:25 +00:00
await stopTestServer();
2025-05-23 19:03:44 +00:00
console.log('✅ Test server stopped');
});
tap.start();