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