1 package org.cacert.gigi.email;
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.junit.Assert.*;
5 import static org.junit.Assume.*;
7 import java.io.BufferedReader;
8 import java.io.EOFException;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.io.OutputStreamWriter;
12 import java.io.PrintWriter;
13 import java.net.Socket;
14 import java.security.GeneralSecurityException;
15 import java.security.InvalidKeyException;
16 import java.security.KeyPair;
17 import java.security.NoSuchAlgorithmException;
18 import java.security.NoSuchProviderException;
19 import java.security.SignatureException;
20 import java.security.cert.CertificateException;
21 import java.util.Base64;
22 import java.util.Date;
23 import java.util.Properties;
24 import java.util.Random;
26 import javax.net.ssl.SSLSocketFactory;
28 import org.cacert.gigi.testUtils.ConfiguredTest;
29 import org.cacert.gigi.util.ServerConstants;
30 import org.junit.Test;
32 import sun.security.x509.AlgorithmId;
33 import sun.security.x509.CertificateAlgorithmId;
34 import sun.security.x509.CertificateSerialNumber;
35 import sun.security.x509.CertificateValidity;
36 import sun.security.x509.CertificateVersion;
37 import sun.security.x509.CertificateX509Key;
38 import sun.security.x509.X500Name;
39 import sun.security.x509.X509CertImpl;
40 import sun.security.x509.X509CertInfo;
42 public class TestSendmail extends ConfiguredTest {
44 private static final Random rng = new Random();
47 public void testSendmail() throws IOException, GeneralSecurityException {
50 String succmail = getTestProps().getProperty("email.address");
51 String pass = getTestProps().getProperty("email.password");
52 String imap = getTestProps().getProperty("email.imap");
53 String imapuser = getTestProps().getProperty("email.imap.user");
54 assumeNotNull(succmail, pass, imap, imapuser);
56 String subj = "subj-" + createUniqueName();
57 String msg = "msg-" + createUniqueName();
58 EmailProvider.getInstance().sendMail(succmail, subj, msg, "system@cacert.org", "Testtarget", "Testsender", null, false);
60 try (Socket s = SSLSocketFactory.getDefault().createSocket(imap, 993);//
61 PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"), true);//
62 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"))) {
63 pw.println("a001 login " + imapuser + " " + pass);
64 imapUntil(br, "a001");
65 pw.println("a002 select inbox");
66 String overview = imapUntil(br, "a002");
67 overview = overview.replaceFirst(".*\\* ([0-9]+) EXISTS.*", "$1");
68 int cont = Integer.parseInt(overview);
71 for (int i = 1; i <= cont; i++) {
72 pw.println("m003" + i + " fetch " + i + " body[header]");
73 String body = imapUntil(br, "m003" + i);
74 if (body.contains(subj)) {
79 assertNotEquals( -1, msgid);
80 pw.println("a003 fetch " + msgid + " body[]");
81 String body = imapUntil(br, "a003");
82 pw.println("delete store " + msgid + " +flags \\deleted");
83 imapUntil(br, "delete");
84 pw.println("exp expunge");
86 pw.println("log logout");
88 assertThat(body, containsString("From: " + ServerConstants.getSupportMailAddress()));
89 assertThat(body, containsString("To: gigi-testuser@dogcraft.de"));
90 assertThat(body, containsString("Subject: " + subj));
91 assertThat(body, containsString(Base64.getEncoder().encodeToString(msg.getBytes("UTF-8"))));
93 // TODO maybe verify signature
97 private String imapUntil(BufferedReader br, String target) throws IOException {
98 StringBuffer response = new StringBuffer();
100 while ( !line.startsWith(target)) {
101 line = br.readLine();
103 throw new EOFException();
105 response.append(line);
107 return response.toString();
110 private void initSelfsign() throws GeneralSecurityException, CertificateException, IOException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
111 assumeNotNull(getTestProps().getProperty("emailProvider.smtpHost"), getTestProps().getProperty("emailProvider.smtpPort"));
112 Properties prop = new Properties();
113 prop.setProperty("emailProvider", "org.cacert.gigi.email.SendMail");
114 prop.setProperty("emailProvider.smtpHost", getTestProps().getProperty("emailProvider.smtpHost"));
115 prop.setProperty("emailProvider.smtpPort", getTestProps().getProperty("emailProvider.smtpPort"));
116 KeyPair kp = generateKeypair();
117 X509CertInfo info = new X509CertInfo();
118 // Add all mandatory attributes
119 info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
120 info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(rng.nextInt() & 0x7fffffff));
121 AlgorithmId algID = AlgorithmId.get("SHA256WithRSA");
122 info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));
123 info.set(X509CertInfo.SUBJECT, new X500Name("EMAIL=system@cacert.org"));
124 info.set(X509CertInfo.KEY, new CertificateX509Key(kp.getPublic()));
125 info.set(X509CertInfo.VALIDITY, new CertificateValidity(new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 60 * 60 * 1000)));
126 info.set(X509CertInfo.ISSUER, new X500Name("CN=test-issue"));
127 X509CertImpl cert = new X509CertImpl(info);
128 cert.sign(kp.getPrivate(), "SHA256WithRSA");
129 EmailProvider.initSystem(prop, cert, kp.getPrivate());