0
# Mock Network Servers
1
2
Mock SMTP and Syslog servers for testing network appenders, supporting TCP, UDP, and TLS protocols with message capture and verification capabilities.
3
4
## Capabilities
5
6
### SMTP Mock Server
7
8
Dummy SMTP server for testing SMTP appender functionality with email capture and verification.
9
10
```java { .api }
11
/**
12
* Dummy SMTP server for testing email appenders
13
* Implements Runnable for background operation
14
*/
15
public class SimpleSmtpServer implements Runnable {
16
17
/**
18
* Default SMTP port number
19
*/
20
public static final int DEFAULT_SMTP_PORT = 25;
21
22
/**
23
* Starts SMTP server on default port (25)
24
* @return Running SimpleSmtpServer instance
25
*/
26
public static SimpleSmtpServer start();
27
28
/**
29
* Starts SMTP server on specified port
30
* @param port Port number to bind to
31
* @return Running SimpleSmtpServer instance
32
*/
33
public static SimpleSmtpServer start(int port);
34
35
/**
36
* Checks if server is stopped
37
* @return true if server is stopped, false if running
38
*/
39
public synchronized boolean isStopped();
40
41
/**
42
* Stops the SMTP server
43
*/
44
public synchronized void stop();
45
46
/**
47
* Gets iterator over received email messages
48
* @return Iterator of SmtpMessage objects
49
*/
50
public synchronized Iterator<SmtpMessage> getReceivedEmail();
51
52
/**
53
* Gets count of received email messages
54
* @return Number of received emails
55
*/
56
public synchronized int getReceivedEmailSize();
57
58
/**
59
* Gets the port the server is bound to
60
* @return Port number
61
*/
62
public int getPort();
63
}
64
```
65
66
### SMTP Message Components
67
68
Supporting classes for SMTP message handling and verification.
69
70
```java { .api }
71
/**
72
* SMTP message representation for testing
73
*/
74
public class SmtpMessage {
75
76
/**
77
* Gets the message headers
78
* @return Map of header names to values
79
*/
80
public Map<String, String> getHeaders();
81
82
/**
83
* Gets the message body
84
* @return Message body as string
85
*/
86
public String getBody();
87
88
/**
89
* Gets the sender address
90
* @return Sender email address
91
*/
92
public String getFrom();
93
94
/**
95
* Gets the recipient addresses
96
* @return List of recipient email addresses
97
*/
98
public List<String> getTo();
99
100
/**
101
* Gets the message subject
102
* @return Email subject line
103
*/
104
public String getSubject();
105
}
106
107
/**
108
* SMTP request representation
109
*/
110
public class SmtpRequest {
111
112
/**
113
* Gets the SMTP command
114
* @return SMTP command string
115
*/
116
public String getCommand();
117
118
/**
119
* Gets the command parameters
120
* @return Command parameters
121
*/
122
public String getParameters();
123
}
124
125
/**
126
* SMTP response representation
127
*/
128
public class SmtpResponse {
129
130
/**
131
* Gets the response code
132
* @return SMTP response code
133
*/
134
public int getCode();
135
136
/**
137
* Gets the response message
138
* @return Response message text
139
*/
140
public String getMessage();
141
}
142
143
/**
144
* SMTP state management for testing
145
*/
146
public class SmtpState {
147
148
/**
149
* Gets the current SMTP session state
150
* @return Current state name
151
*/
152
public String getCurrentState();
153
154
/**
155
* Transitions to new state
156
* @param newState The new state to transition to
157
*/
158
public void setState(String newState);
159
}
160
161
/**
162
* SMTP action type definitions
163
*/
164
public class SmtpActionType {
165
166
public static final String CONNECT = "CONNECT";
167
public static final String HELO = "HELO";
168
public static final String MAIL_FROM = "MAIL_FROM";
169
public static final String RCPT_TO = "RCPT_TO";
170
public static final String DATA = "DATA";
171
public static final String QUIT = "QUIT";
172
}
173
```
174
175
**SMTP Usage Examples:**
176
177
```java
178
import org.apache.logging.log4j.core.test.smtp.SimpleSmtpServer;
179
import org.apache.logging.log4j.core.test.smtp.SmtpMessage;
180
181
public class SmtpAppenderTest {
182
183
@Test
184
public void testSmtpAppender() throws Exception {
185
// Start mock SMTP server
186
SimpleSmtpServer smtpServer = SimpleSmtpServer.start(2525);
187
188
try {
189
// Configure SMTP appender to use localhost:2525
190
Logger logger = LogManager.getLogger();
191
logger.error("Test error message");
192
193
// Wait for message processing
194
Thread.sleep(1000);
195
196
// Verify email was sent
197
assertEquals(1, smtpServer.getReceivedEmailSize());
198
199
Iterator<SmtpMessage> messages = smtpServer.getReceivedEmail();
200
SmtpMessage message = messages.next();
201
202
assertEquals("Log4j Error", message.getSubject());
203
assertTrue(message.getBody().contains("Test error message"));
204
205
} finally {
206
smtpServer.stop();
207
}
208
}
209
}
210
```
211
212
### Syslog Mock Servers
213
214
Mock syslog servers supporting TCP, UDP, and TLS protocols for testing syslog appenders.
215
216
```java { .api }
217
/**
218
* Base class for mock syslog servers
219
* Abstract class extending Thread for background operation
220
*/
221
public abstract class MockSyslogServer extends Thread {
222
223
/**
224
* Gets the local port the server is bound to
225
* @return Port number
226
*/
227
public abstract int getLocalPort();
228
229
/**
230
* Shuts down the syslog server
231
*/
232
public void shutdown();
233
234
/**
235
* Gets count of received syslog messages
236
* @return Number of received messages
237
*/
238
public int getNumberOfReceivedMessages();
239
240
/**
241
* Gets list of received syslog messages
242
* @return List of message strings
243
*/
244
public List<String> getMessageList();
245
246
/**
247
* Clears all received messages
248
*/
249
public void clear();
250
}
251
252
/**
253
* Factory for creating mock syslog servers
254
*/
255
public class MockSyslogServerFactory {
256
257
/**
258
* Creates TCP syslog server
259
* @param port Port to bind to (0 for random available port)
260
* @return MockTcpSyslogServer instance
261
*/
262
public static MockTcpSyslogServer createTcpSyslogServer(int port);
263
264
/**
265
* Creates UDP syslog server
266
* @param port Port to bind to (0 for random available port)
267
* @return MockUdpSyslogServer instance
268
*/
269
public static MockUdpSyslogServer createUdpSyslogServer(int port);
270
271
/**
272
* Creates TLS syslog server
273
* @param port Port to bind to (0 for random available port)
274
* @return MockTlsSyslogServer instance
275
*/
276
public static MockTlsSyslogServer createTlsSyslogServer(int port);
277
}
278
279
/**
280
* TCP syslog server mock
281
*/
282
public class MockTcpSyslogServer extends MockSyslogServer {
283
284
/**
285
* Creates TCP syslog server on specified port
286
* @param port Port to bind to
287
*/
288
public MockTcpSyslogServer(int port);
289
}
290
291
/**
292
* UDP syslog server mock
293
*/
294
public class MockUdpSyslogServer extends MockSyslogServer {
295
296
/**
297
* Creates UDP syslog server on specified port
298
* @param port Port to bind to
299
*/
300
public MockUdpSyslogServer(int port);
301
}
302
303
/**
304
* TLS syslog server mock
305
*/
306
public class MockTlsSyslogServer extends MockSyslogServer {
307
308
/**
309
* Creates TLS syslog server on specified port
310
* @param port Port to bind to
311
*/
312
public MockTlsSyslogServer(int port);
313
}
314
```
315
316
### TLS Syslog Testing Utilities
317
318
Specialized utilities for testing TLS syslog functionality.
319
320
```java { .api }
321
/**
322
* TLS syslog input stream reader base class
323
*/
324
public abstract class TlsSyslogInputStreamReaderBase {
325
326
/**
327
* Reads syslog messages from TLS input stream
328
* @param inputStream The TLS input stream
329
* @return List of parsed syslog messages
330
*/
331
public abstract List<String> readMessages(InputStream inputStream);
332
}
333
334
/**
335
* Standard TLS syslog input stream reader
336
*/
337
public class TlsSyslogInputStreamReader extends TlsSyslogInputStreamReaderBase {
338
339
/**
340
* Reads RFC 5424 compliant TLS syslog messages
341
* @param inputStream The TLS input stream
342
* @return List of parsed syslog messages
343
*/
344
public List<String> readMessages(InputStream inputStream);
345
}
346
347
/**
348
* Legacy BSD TLS syslog input stream reader
349
*/
350
public class LegacyBsdTlsSyslogInputStreamReader extends TlsSyslogInputStreamReaderBase {
351
352
/**
353
* Reads legacy BSD format TLS syslog messages
354
* @param inputStream The TLS input stream
355
* @return List of parsed syslog messages
356
*/
357
public List<String> readMessages(InputStream inputStream);
358
}
359
360
/**
361
* TLS syslog message format definitions
362
*/
363
public enum TlsSyslogMessageFormat {
364
365
/** RFC 5424 compliant format */
366
RFC_5424,
367
368
/** Legacy BSD format */
369
LEGACY_BSD
370
}
371
372
/**
373
* TLS syslog testing utilities
374
*/
375
public class TlsSyslogTestUtil {
376
377
/**
378
* Creates SSL context for testing TLS syslog
379
* @return SSL context configured for testing
380
*/
381
public static SSLContext createTestSSLContext();
382
383
/**
384
* Creates test certificate for TLS syslog testing
385
* @return X509Certificate for testing
386
*/
387
public static X509Certificate createTestCertificate();
388
}
389
```
390
391
**Syslog Usage Examples:**
392
393
```java
394
import org.apache.logging.log4j.core.test.net.mock.MockSyslogServerFactory;
395
import org.apache.logging.log4j.core.test.net.mock.MockUdpSyslogServer;
396
397
public class SyslogAppenderTest {
398
399
@Test
400
public void testUdpSyslogAppender() throws Exception {
401
// Start mock UDP syslog server
402
MockUdpSyslogServer syslogServer = MockSyslogServerFactory.createUdpSyslogServer(0);
403
syslogServer.start();
404
405
int port = syslogServer.getLocalPort();
406
407
try {
408
// Configure syslog appender to use localhost:port
409
Logger logger = LogManager.getLogger();
410
logger.info("Test syslog message");
411
412
// Wait for message processing
413
Thread.sleep(500);
414
415
// Verify message was received
416
assertEquals(1, syslogServer.getNumberOfReceivedMessages());
417
418
List<String> messages = syslogServer.getMessageList();
419
assertTrue(messages.get(0).contains("Test syslog message"));
420
421
} finally {
422
syslogServer.shutdown();
423
}
424
}
425
426
@Test
427
public void testTcpSyslogAppender() throws Exception {
428
MockTcpSyslogServer tcpServer = MockSyslogServerFactory.createTcpSyslogServer(0);
429
tcpServer.start();
430
431
// Similar testing pattern for TCP
432
}
433
434
@Test
435
public void testTlsSyslogAppender() throws Exception {
436
MockTlsSyslogServer tlsServer = MockSyslogServerFactory.createTlsSyslogServer(0);
437
tlsServer.start();
438
439
// TLS-specific testing with certificates
440
}
441
}
442
```
443
444
## Usage Patterns
445
446
### SMTP Testing Pattern
447
448
```java
449
// 1. Start mock server
450
SimpleSmtpServer server = SimpleSmtpServer.start(2525);
451
452
// 2. Configure appender to use mock server
453
// 3. Perform logging that should trigger email
454
455
// 4. Verify messages
456
assertEquals(expectedCount, server.getReceivedEmailSize());
457
SmtpMessage message = server.getReceivedEmail().next();
458
assertEquals(expectedSubject, message.getSubject());
459
460
// 5. Clean up
461
server.stop();
462
```
463
464
### Syslog Testing Pattern
465
466
```java
467
// 1. Create and start mock server
468
MockUdpSyslogServer server = MockSyslogServerFactory.createUdpSyslogServer(0);
469
server.start();
470
471
// 2. Get assigned port
472
int port = server.getLocalPort();
473
474
// 3. Configure syslog appender
475
// 4. Perform logging
476
477
// 5. Verify messages
478
List<String> messages = server.getMessageList();
479
assertEquals(expectedCount, messages.size());
480
481
// 6. Clean up
482
server.shutdown();
483
```