1 package club.wpia.gigi.ping;
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.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.net.ServerSocket;
13 import java.net.Socket;
15 import java.net.URLEncoder;
16 import java.sql.SQLException;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import javax.naming.NamingException;
22 import org.junit.Test;
24 import club.wpia.gigi.GigiApiException;
25 import club.wpia.gigi.dbObjects.Domain;
26 import club.wpia.gigi.dbObjects.DomainPingConfiguration;
27 import club.wpia.gigi.dbObjects.DomainPingType;
28 import club.wpia.gigi.testUtils.IOUtils;
29 import club.wpia.gigi.testUtils.PingTest;
30 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
31 import club.wpia.gigi.util.RandomToken;
32 import club.wpia.gigi.util.SystemKeywords;
34 public class TestHTTP extends PingTest {
37 public void httpSanity() throws IOException, NamingException {
39 String token = RandomToken.generateToken(16);
40 String value = RandomToken.generateToken(16);
42 TestDNS.updateService(token, value, "http");
43 assertEquals(value, readHTTP(token));
48 public void httpAndMailSuccess() throws Exception {
49 testEmailAndHTTP(0, 0, true, true);
53 public void httpFailKeyAndMailSuccess() throws Exception {
54 testEmailAndHTTP(1, 0, false, true);
58 public void httpFailValAndMailFail() throws Exception {
59 testEmailAndHTTP(2, 1, false, false);
62 public void testEmailAndHTTP(int httpVariant, int emailVariant, boolean successHTTP, boolean successMail) throws IOException, InterruptedException, SQLException, GigiApiException {
64 String test = getTestProps().getProperty("domain.http");
67 Matcher m = initailizeDomainForm();
68 updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
70 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
71 "&emailType=y&email=2&HTTPType=y" + //
72 "&ssl-type-0=direct&ssl-port-0=" + //
73 "&ssl-type-1=direct&ssl-port-1=" + //
74 "&ssl-type-2=direct&ssl-port-2=" + //
75 "&ssl-type-3=direct&ssl-port-3=" + //
76 "&adddomain&csrf=" + csrf;
77 String p2 = sendDomainForm(content);
79 TestMail mail = getMailReceiver().receive();
80 if (emailVariant == 0) {
85 String newcontent = IOUtils.readURL(get(p2));
86 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
87 assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
88 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
89 assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
91 if (successHTTP) { // give it a second try
92 int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1"));
93 Domain d = Domain.getById(id);
94 DomainPingConfiguration dpc = null;
95 for (DomainPingConfiguration conf : d.getConfiguredPings()) {
96 if (conf.getType() == DomainPingType.HTTP) {
102 fail("Http config not found");
105 String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
106 assertThat(res, containsString("only allowed after"));
110 private String readHTTP(String token) throws IOException {
111 String httpDom = getTestProps().getProperty("domain.http");
112 assumeNotNull(httpDom);
113 URL u = new URL("http://" + httpDom + "/" + SystemKeywords.HTTP_CHALLENGE_PREFIX + token + ".txt");
114 return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
119 public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
120 try (ServerSocket s = openServer()) {
121 testHttpRedirect(s, true);
126 public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
127 try (ServerSocket s = openServer()) {
128 testHttpRedirect(s, false);
132 private ServerSocket openServer() {
133 String localHTTP = getTestProps().getProperty("domain.localHTTP");
134 assumeNotNull(localHTTP);
136 return new ServerSocket(Integer.parseInt(localHTTP));
137 } catch (IOException e) {
138 throw new Error("Requires a free port " + localHTTP);
142 public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
143 String test = getTestProps().getProperty("domain.local");
146 Matcher m = initailizeDomainForm();
148 String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
149 "&emailType=y&email=2&HTTPType=y" + //
150 "&ssl-type-0=direct&ssl-port-0=" + //
151 "&ssl-type-1=direct&ssl-port-1=" + //
152 "&ssl-type-2=direct&ssl-port-2=" + //
153 "&ssl-type-3=direct&ssl-port-3=" + //
154 "&adddomain&csrf=" + csrf;
155 String p2 = sendDomainForm(content);
156 try (Socket s0 = s.accept()) {
157 BufferedReader br = new BufferedReader(new InputStreamReader(s0.getInputStream(), "UTF-8"));
158 String fst = br.readLine();
159 assertEquals("GET /" + SystemKeywords.HTTP_CHALLENGE_PREFIX + m.group(1) + ".txt HTTP/1.1", fst);
160 while ( !"".equals(br.readLine())) {
162 String res = m.group(2);
163 PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
165 out.println("HTTP/1.1 200 OK");
166 out.println("Content-length: " + res.length());
170 out.println("HTTP/1.1 302 Moved");
171 out.println("Location: /token");
178 TestMail mail = getMailReceiver().receive();
181 String newcontent = IOUtils.readURL(get(p2));
182 Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
183 pat = Pattern.compile("<td>http</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>");
184 Matcher m0 = pat.matcher(newcontent);
185 assertTrue(newcontent, m0.find());
187 assertEquals("failed", m0.group(1));
188 assertThat(m0.group(3), containsString("code 302"));
190 assertEquals("success", m0.group(1));
192 pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
193 assertTrue(newcontent, pat.matcher(newcontent).find());