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

@@ -7,7 +7,7 @@ let dcRouter: DcRouter;
tap.test('should use traditional port forwarding when useSocketHandler is false', async () => {
dcRouter = new DcRouter({
emailConfig: {
ports: [25, 587, 465],
ports: [2525, 2587, 2465],
hostname: 'mail.test.local',
domains: ['test.local'],
routes: [],
@@ -43,7 +43,7 @@ tap.test('should use traditional port forwarding when useSocketHandler is false'
tap.test('should use socket-handler mode when useSocketHandler is true', async () => {
dcRouter = new DcRouter({
emailConfig: {
ports: [25, 587, 465],
ports: [2525, 2587, 2465],
hostname: 'mail.test.local',
domains: ['test.local'],
routes: [],
@@ -78,106 +78,106 @@ tap.test('should use socket-handler mode when useSocketHandler is true', async (
tap.test('should generate correct email routes for each port', async () => {
const emailConfig = {
ports: [25, 587, 465],
ports: [2525, 2587, 2465],
hostname: 'mail.test.local',
domains: ['test.local'],
routes: [],
useSocketHandler: true
};
dcRouter = new DcRouter({ emailConfig });
// Access the private method to generate routes
const emailRoutes = (dcRouter as any).generateEmailRoutes(emailConfig);
expect(emailRoutes.length).toEqual(3);
// Check SMTP route (port 25)
const smtpRoute = emailRoutes.find((r: any) => r.name === 'smtp-route');
expect(smtpRoute).toBeDefined();
expect(smtpRoute.match.ports).toContain(25);
expect(smtpRoute.action.type).toEqual('socket-handler');
// Check Submission route (port 587)
const submissionRoute = emailRoutes.find((r: any) => r.name === 'submission-route');
expect(submissionRoute).toBeDefined();
expect(submissionRoute.match.ports).toContain(587);
expect(submissionRoute.action.type).toEqual('socket-handler');
// Check SMTPS route (port 465)
const smtpsRoute = emailRoutes.find((r: any) => r.name === 'smtps-route');
expect(smtpsRoute).toBeDefined();
expect(smtpsRoute.match.ports).toContain(465);
expect(smtpsRoute.action.type).toEqual('socket-handler');
// Check route for port 2525 (non-standard ports use generic naming)
const port2525Route = emailRoutes.find((r: any) => r.name === 'email-port-2525-route');
expect(port2525Route).toBeDefined();
expect(port2525Route.match.ports).toContain(2525);
expect(port2525Route.action.type).toEqual('socket-handler');
// Check route for port 2587
const port2587Route = emailRoutes.find((r: any) => r.name === 'email-port-2587-route');
expect(port2587Route).toBeDefined();
expect(port2587Route.match.ports).toContain(2587);
expect(port2587Route.action.type).toEqual('socket-handler');
// Check route for port 2465
const port2465Route = emailRoutes.find((r: any) => r.name === 'email-port-2465-route');
expect(port2465Route).toBeDefined();
expect(port2465Route.match.ports).toContain(2465);
expect(port2465Route.action.type).toEqual('socket-handler');
});
tap.test('email socket handler should handle different ports correctly', async () => {
dcRouter = new DcRouter({
emailConfig: {
ports: [25, 587, 465],
ports: [2525, 2587, 2465],
hostname: 'mail.test.local',
domains: ['test.local'],
routes: [],
useSocketHandler: true
}
});
await dcRouter.start();
// Test port 25 handler (plain SMTP)
const port25Handler = (dcRouter as any).createMailSocketHandler(25);
expect(port25Handler).toBeDefined();
expect(typeof port25Handler).toEqual('function');
// Test port 465 handler (SMTPS - should wrap in TLS)
const port465Handler = (dcRouter as any).createMailSocketHandler(465);
expect(port465Handler).toBeDefined();
expect(typeof port465Handler).toEqual('function');
// Test port 2525 handler (plain SMTP)
const port2525Handler = (dcRouter as any).createMailSocketHandler(2525);
expect(port2525Handler).toBeDefined();
expect(typeof port2525Handler).toEqual('function');
// Test port 2465 handler (SMTPS - should wrap in TLS)
const port2465Handler = (dcRouter as any).createMailSocketHandler(2465);
expect(port2465Handler).toBeDefined();
expect(typeof port2465Handler).toEqual('function');
await dcRouter.stop();
});
tap.test('email server handleSocket method should work', async () => {
dcRouter = new DcRouter({
emailConfig: {
ports: [25],
ports: [2525],
hostname: 'mail.test.local',
domains: ['test.local'],
routes: [],
useSocketHandler: true
}
});
await dcRouter.start();
const emailServer = (dcRouter as any).emailServer;
expect(emailServer).toBeDefined();
expect(emailServer.handleSocket).toBeDefined();
expect(typeof emailServer.handleSocket).toEqual('function');
// Create a mock socket
const mockSocket = new plugins.net.Socket();
let socketDestroyed = false;
mockSocket.destroy = () => {
socketDestroyed = true;
};
// Test handleSocket
try {
await emailServer.handleSocket(mockSocket, 25);
await emailServer.handleSocket(mockSocket, 2525);
// It will fail because we don't have a real socket, but it should handle it gracefully
} catch (error) {
// Expected to error with mock socket
}
await dcRouter.stop();
});
tap.test('should not create SMTP servers when useSocketHandler is true', async () => {
dcRouter = new DcRouter({
emailConfig: {
ports: [25, 587, 465],
ports: [2525, 2587, 2465],
hostname: 'mail.test.local',
domains: ['test.local'],
routes: [],
@@ -199,6 +199,8 @@ tap.test('should not create SMTP servers when useSocketHandler is true', async (
});
tap.test('TLS handling should differ between ports', async () => {
// Use standard ports 25 and 465 to test TLS behavior
// This test doesn't start the server, just checks route generation
const emailConfig = {
ports: [25, 465],
hostname: 'mail.test.local',
@@ -206,15 +208,15 @@ tap.test('TLS handling should differ between ports', async () => {
routes: [],
useSocketHandler: false // Use traditional mode to check TLS config
};
dcRouter = new DcRouter({ emailConfig });
const emailRoutes = (dcRouter as any).generateEmailRoutes(emailConfig);
// Port 25 should use passthrough
const smtpRoute = emailRoutes.find((r: any) => r.match.ports[0] === 25);
expect(smtpRoute.action.tls.mode).toEqual('passthrough');
// Port 465 should use terminate
const smtpsRoute = emailRoutes.find((r: any) => r.match.ports[0] === 465);
expect(smtpsRoute.action.tls.mode).toEqual('terminate');