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