]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/email/TestSendmail.java
upd: rename package name and all references to it
[gigi.git] / tests / club / wpia / gigi / email / TestSendmail.java
1 package club.wpia.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.junit.Test;
29
30 import club.wpia.gigi.testUtils.ConfiguredTest;
31 import club.wpia.gigi.util.ServerConstants;
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;
41
42 public class TestSendmail extends ConfiguredTest {
43
44     private static final Random rng = new Random();
45
46     @Test
47     public void testSendmail() throws IOException, GeneralSecurityException {
48         initSelfsign();
49
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);
55
56         String subj = "subj-" + createUniqueName();
57         String msg = "msg-" + createUniqueName();
58         EmailProvider.getInstance().sendMail(succmail, subj, msg, "system@wpia.club", "Testtarget", "Testsender", null, false);
59
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);
69
70             int msgid = -1;
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)) {
75                     msgid = i;
76                     break;
77                 }
78             }
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");
85             imapUntil(br, "exp");
86             pw.println("log logout");
87             imapUntil(br, "log");
88             assertThat(body, containsString("From: " + ServerConstants.getSupportMailAddress()));
89             assertThat(body, containsString("To: " + succmail));
90             assertThat(body, containsString("Subject: " + subj));
91             assertThat(body, containsString(Base64.getEncoder().encodeToString(msg.getBytes("UTF-8"))));
92
93             // TODO maybe verify signature
94         }
95     }
96
97     private String imapUntil(BufferedReader br, String target) throws IOException {
98         StringBuffer response = new StringBuffer();
99         String line = "";
100         while ( !line.startsWith(target)) {
101             line = br.readLine();
102             if (line == null) {
103                 throw new EOFException();
104             }
105             response.append(line);
106         }
107         return response.toString();
108     }
109
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", "club.wpia.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@wpia.club"));
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());
130     }
131 }