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