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.IOException;
9 import java.io.InputStreamReader;
10 import java.io.PrintWriter;
11 import java.net.Socket;
12 import java.security.GeneralSecurityException;
13 import java.security.InvalidKeyException;
14 import java.security.KeyPair;
15 import java.security.NoSuchAlgorithmException;
16 import java.security.NoSuchProviderException;
17 import java.security.SignatureException;
18 import java.security.cert.CertificateException;
19 import java.util.Base64;
20 import java.util.Date;
21 import java.util.Properties;
23 import javax.net.ssl.SSLSocketFactory;
25 import org.cacert.gigi.testUtils.ConfiguredTest;
26 import org.junit.Test;
28 import sun.security.x509.AlgorithmId;
29 import sun.security.x509.CertificateAlgorithmId;
30 import sun.security.x509.CertificateSerialNumber;
31 import sun.security.x509.CertificateValidity;
32 import sun.security.x509.CertificateVersion;
33 import sun.security.x509.CertificateX509Key;
34 import sun.security.x509.X500Name;
35 import sun.security.x509.X509CertImpl;
36 import sun.security.x509.X509CertInfo;
38 public class TestSendmail extends ConfiguredTest {
41 public void testSendmail() throws IOException, GeneralSecurityException {
44 String succmail = getTestProps().getProperty("email.address");
45 String pass = getTestProps().getProperty("email.password");
46 String imap = getTestProps().getProperty("email.imap");
47 String imapuser = getTestProps().getProperty("email.imap.user");
48 assumeNotNull(succmail, pass, imap, imapuser);
50 String subj = "subj-" + createUniqueName();
51 String msg = "msg-" + createUniqueName();
52 EmailProvider.getInstance().sendmail(succmail, subj, msg, "system@cacert.org", "system@cacert.org", "Testtarget", "Testsender", null, false);
54 Socket s = SSLSocketFactory.getDefault().createSocket(imap, 993);
55 PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
56 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
57 pw.println("a001 login " + imapuser + " " + pass);
58 imapUntil(br, "a001");
59 pw.println("a002 select inbox");
60 String overview = imapUntil(br, "a002");
61 overview = overview.replaceFirst(".*\\* ([0-9]+) EXISTS.*", "$1");
62 int cont = Integer.parseInt(overview);
65 for (int i = 1; i <= cont; i++) {
66 pw.println("m003" + i + " fetch " + i + " body[header]");
67 String body = imapUntil(br, "m003" + i);
68 if (body.contains(subj)) {
73 assertNotEquals( -1, msgid);
74 pw.println("a003 fetch " + msgid + " body[]");
75 String body = imapUntil(br, "a003");
76 pw.println("delete store " + msgid + " +flags \\deleted");
77 imapUntil(br, "delete");
78 pw.println("exp expunge");
80 pw.println("log logout");
82 assertThat(body, containsString("From: support@cacert.local"));
83 assertThat(body, containsString("To: gigi-testuser@dogcraft.de"));
84 assertThat(body, containsString("Subject: " + subj));
85 assertThat(body, containsString(Base64.getEncoder().encodeToString(msg.getBytes("UTF-8"))));
87 // TODO maybe verify signature
91 private String imapUntil(BufferedReader br, String target) throws IOException {
92 StringBuffer response = new StringBuffer();
94 while ( !line.startsWith(target)) {
96 response.append(line);
98 return response.toString();
101 private void initSelfsign() throws GeneralSecurityException, CertificateException, IOException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
102 Properties prop = new Properties();
103 prop.setProperty("emailProvider", "org.cacert.gigi.email.Sendmail");
104 KeyPair kp = generateKeypair();
105 X509CertInfo info = new X509CertInfo();
106 // Add all mandatory attributes
107 info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
108 info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(new java.util.Random().nextInt() & 0x7fffffff));
109 AlgorithmId algID = AlgorithmId.get("SHA256WithRSA");
110 info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algID));
111 info.set(X509CertInfo.SUBJECT, new X500Name("EMAIL=system@cacert.org"));
112 info.set(X509CertInfo.KEY, new CertificateX509Key(kp.getPublic()));
113 info.set(X509CertInfo.VALIDITY, new CertificateValidity(new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 60 * 60 * 1000)));
114 info.set(X509CertInfo.ISSUER, new X500Name("CN=test-issue"));
115 X509CertImpl cert = new X509CertImpl(info);
116 cert.sign(kp.getPrivate(), "SHA256WithRSA");
117 EmailProvider.initSystem(prop, cert, kp.getPrivate());