|
1 | 1 | package org.bouncycastle.mail.smime.examples.test; |
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
3 | 4 | import java.io.ByteArrayOutputStream; |
4 | 5 | import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStreamReader; |
5 | 8 | import java.io.PrintStream; |
| 9 | +import java.io.PrintWriter; |
| 10 | +import java.net.InetAddress; |
| 11 | +import java.net.ServerSocket; |
| 12 | +import java.net.Socket; |
6 | 13 | import java.security.KeyStore; |
7 | 14 | import java.util.Enumeration; |
8 | 15 |
|
@@ -91,10 +98,139 @@ public void testExamples() |
91 | 98 | } |
92 | 99 | } |
93 | 100 |
|
94 | | - SendSignedAndEncryptedMail.main(new String[]{"id.p12", "hello world", keyAlias, "smtp.gmail.com", "recipient@example.com"}); |
| 101 | + // SendSignedAndEncryptedMail ends with Transport.send(), so the test must not point it |
| 102 | + // at a real mail host: JavaMail has no connect timeout by default, and where outbound |
| 103 | + // port 25 is silently dropped rather than refused the connect blocked the whole |
| 104 | + // :misc:test run indefinitely (github #2407). Deliver to a loopback SMTP stub instead, |
| 105 | + // with timeouts as a backstop; the example reads the mail.smtp.* settings from the |
| 106 | + // system properties. |
| 107 | + SmtpStub smtp = new SmtpStub(); |
| 108 | + smtp.start(); |
| 109 | + try |
| 110 | + { |
| 111 | + System.setProperty("mail.smtp.port", Integer.toString(smtp.getPort())); |
| 112 | + System.setProperty("mail.smtp.connectiontimeout", "10000"); |
| 113 | + System.setProperty("mail.smtp.timeout", "10000"); |
| 114 | + System.setProperty("mail.smtp.writetimeout", "10000"); |
| 115 | + |
| 116 | + SendSignedAndEncryptedMail.main(new String[]{"id.p12", "hello world", keyAlias, "127.0.0.1", "recipient@example.com"}); |
| 117 | + } |
| 118 | + finally |
| 119 | + { |
| 120 | + smtp.close(); |
| 121 | + } |
| 122 | + assertTrue("SMTP stub did not receive the message: " + _currentErr, smtp.receivedMessage()); |
| 123 | + |
95 | 124 | ValidateSignedMail.main(null); |
96 | 125 | } |
97 | 126 |
|
| 127 | + /** |
| 128 | + * Minimal single-connection SMTP responder on a loopback port: greets, answers every command |
| 129 | + * with 250, accepts one DATA body, and records that a message was delivered. |
| 130 | + */ |
| 131 | + private static class SmtpStub |
| 132 | + extends Thread |
| 133 | + { |
| 134 | + private final ServerSocket serverSocket; |
| 135 | + private volatile boolean received; |
| 136 | + |
| 137 | + SmtpStub() |
| 138 | + throws IOException |
| 139 | + { |
| 140 | + serverSocket = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1")); |
| 141 | + serverSocket.setSoTimeout(30000); |
| 142 | + setDaemon(true); |
| 143 | + } |
| 144 | + |
| 145 | + int getPort() |
| 146 | + { |
| 147 | + return serverSocket.getLocalPort(); |
| 148 | + } |
| 149 | + |
| 150 | + boolean receivedMessage() |
| 151 | + { |
| 152 | + return received; |
| 153 | + } |
| 154 | + |
| 155 | + public void run() |
| 156 | + { |
| 157 | + try |
| 158 | + { |
| 159 | + Socket socket = serverSocket.accept(); |
| 160 | + try |
| 161 | + { |
| 162 | + socket.setSoTimeout(30000); |
| 163 | + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO-8859-1")); |
| 164 | + PrintWriter out = new PrintWriter(socket.getOutputStream(), true); |
| 165 | + |
| 166 | + out.print("220 localhost ESMTP stub\r\n"); |
| 167 | + out.flush(); |
| 168 | + |
| 169 | + boolean inData = false; |
| 170 | + String line; |
| 171 | + while ((line = in.readLine()) != null) |
| 172 | + { |
| 173 | + if (inData) |
| 174 | + { |
| 175 | + if (line.equals(".")) |
| 176 | + { |
| 177 | + inData = false; |
| 178 | + received = true; |
| 179 | + out.print("250 OK\r\n"); |
| 180 | + out.flush(); |
| 181 | + } |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + String cmd = line.toUpperCase(); |
| 186 | + if (cmd.startsWith("EHLO") || cmd.startsWith("HELO")) |
| 187 | + { |
| 188 | + out.print("250 localhost\r\n"); |
| 189 | + } |
| 190 | + else if (cmd.startsWith("DATA")) |
| 191 | + { |
| 192 | + inData = true; |
| 193 | + out.print("354 End data with <CR><LF>.<CR><LF>\r\n"); |
| 194 | + } |
| 195 | + else if (cmd.startsWith("QUIT")) |
| 196 | + { |
| 197 | + out.print("221 Bye\r\n"); |
| 198 | + out.flush(); |
| 199 | + break; |
| 200 | + } |
| 201 | + else |
| 202 | + { |
| 203 | + out.print("250 OK\r\n"); |
| 204 | + } |
| 205 | + out.flush(); |
| 206 | + } |
| 207 | + } |
| 208 | + finally |
| 209 | + { |
| 210 | + socket.close(); |
| 211 | + } |
| 212 | + } |
| 213 | + catch (IOException e) |
| 214 | + { |
| 215 | + // accept timed out or the client went away: receivedMessage() reports the result |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + void close() |
| 220 | + throws IOException |
| 221 | + { |
| 222 | + serverSocket.close(); |
| 223 | + try |
| 224 | + { |
| 225 | + join(30000); |
| 226 | + } |
| 227 | + catch (InterruptedException e) |
| 228 | + { |
| 229 | + Thread.currentThread().interrupt(); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
98 | 234 | public static void main(String[] args) |
99 | 235 | { |
100 | 236 | PrintTestResult.printResult(junit.textui.TestRunner.run(suite())); |
|
0 commit comments