]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/ping/TestHTTP.java
fix: Avoid NPE when handling the HTTP response in tests
[gigi.git] / tests / org / cacert / gigi / ping / TestHTTP.java
index 64c9cd650e3e618d7c51cd194f3b70158554e7b1..03dada67beb1d199fdd8828f74ead4a026d4fea3 100644 (file)
@@ -4,8 +4,13 @@ import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 import static org.junit.Assume.*;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.ServerSocket;
+import java.net.Socket;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.sql.SQLException;
@@ -17,11 +22,10 @@ import javax.naming.NamingException;
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.dbObjects.Domain;
 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
-import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
-import org.cacert.gigi.pages.account.domain.DomainOverview;
+import org.cacert.gigi.dbObjects.DomainPingType;
 import org.cacert.gigi.testUtils.IOUtils;
 import org.cacert.gigi.testUtils.PingTest;
-import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
+import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
 import org.cacert.gigi.util.RandomToken;
 import org.junit.Test;
 
@@ -58,8 +62,7 @@ public class TestHTTP extends PingTest {
         String test = getTestProps().getProperty("domain.http");
         assumeNotNull(test);
 
-        URL u = new URL("https://" + getServerName() + DomainOverview.PATH);
-        Matcher m = initailizeDomainForm(u);
+        Matcher m = initailizeDomainForm();
         updateService(m.group(1) + (httpVariant == 1 ? "a" : ""), m.group(2) + (httpVariant == 2 ? "a" : ""), "http");
 
         String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
@@ -69,35 +72,35 @@ public class TestHTTP extends PingTest {
                 "&ssl-type-2=direct&ssl-port-2=" + //
                 "&ssl-type-3=direct&ssl-port-3=" + //
                 "&adddomain&csrf=" + csrf;
-        URL u2 = sendDomainForm(u, content);
+        String p2 = sendDomainForm(content);
 
-        TestMail mail = getMailReciever().recieve();
+        TestMail mail = getMailReceiver().receive();
         if (emailVariant == 0) {
-            String link = mail.extractLink();
-            new URL(link).openConnection().getHeaderField("");
+            mail.verify();
         }
         waitForPings(2);
 
-        String newcontent = IOUtils.readURL(cookie(u2.openConnection(), cookie));
+        String newcontent = IOUtils.readURL(get(p2));
         Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
         assertTrue(newcontent, !successHTTP ^ pat.matcher(newcontent).find());
         pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
         assertTrue(newcontent, !successMail ^ pat.matcher(newcontent).find());
 
         if (successHTTP) { // give it a second try
-            int id = Integer.parseInt(u2.toString().replaceFirst("^.*/([0-9]+)$", "$1"));
+            int id = Integer.parseInt(p2.replaceFirst("^.*/([0-9]+)$", "$1"));
             Domain d = Domain.getById(id);
             DomainPingConfiguration dpc = null;
             for (DomainPingConfiguration conf : d.getConfiguredPings()) {
-                if (conf.getType() == PingType.HTTP) {
+                if (conf.getType() == DomainPingType.HTTP) {
                     dpc = conf;
                     break;
                 }
             }
             if (dpc == null) {
                 fail("Http config not found");
+                return;
             }
-            String res = executeBasicWebInteraction(cookie, u2.getPath(), "configId=" + dpc.getId());
+            String res = executeBasicWebInteraction(cookie, p2, "configId=" + dpc.getId());
             assertThat(res, containsString("only allowed after"));
         }
     }
@@ -106,7 +109,86 @@ public class TestHTTP extends PingTest {
         String httpDom = getTestProps().getProperty("domain.http");
         assumeNotNull(httpDom);
         URL u = new URL("http://" + httpDom + "/cacert-" + token + ".txt");
-        return IOUtils.readURL(new InputStreamReader(u.openStream())).trim();
+        return IOUtils.readURL(new InputStreamReader(u.openStream(), "UTF-8")).trim();
+
+    }
+
+    @Test
+    public void testHttpRedirect() throws IOException, SQLException, InterruptedException {
+        try (ServerSocket s = openServer()) {
+            testHttpRedirect(s, true);
+        }
+    }
+
+    @Test
+    public void testHttpNoRedirect() throws IOException, SQLException, InterruptedException {
+        try (ServerSocket s = openServer()) {
+            testHttpRedirect(s, false);
+        }
+    }
+
+    private ServerSocket openServer() {
+        String localHTTP = getTestProps().getProperty("domain.localHTTP");
+        assumeNotNull(localHTTP);
+        try {
+            return new ServerSocket(Integer.parseInt(localHTTP));
+        } catch (IOException e) {
+            throw new Error("Requires a free port " + localHTTP);
+        }
+    }
+
+    public void testHttpRedirect(ServerSocket s, boolean doRedirect) throws IOException, SQLException, InterruptedException {
+        String test = getTestProps().getProperty("domain.local");
+        assumeNotNull(test);
+
+        Matcher m = initailizeDomainForm();
+
+        String content = "newdomain=" + URLEncoder.encode(test, "UTF-8") + //
+                "&emailType=y&email=2&HTTPType=y" + //
+                "&ssl-type-0=direct&ssl-port-0=" + //
+                "&ssl-type-1=direct&ssl-port-1=" + //
+                "&ssl-type-2=direct&ssl-port-2=" + //
+                "&ssl-type-3=direct&ssl-port-3=" + //
+                "&adddomain&csrf=" + csrf;
+        String p2 = sendDomainForm(content);
+        try (Socket s0 = s.accept()) {
+            BufferedReader br = new BufferedReader(new InputStreamReader(s0.getInputStream(), "UTF-8"));
+            String fst = br.readLine();
+            assertEquals("GET /cacert-" + m.group(1) + ".txt HTTP/1.1", fst);
+            while ( !"".equals(br.readLine())) {
+            }
+            String res = m.group(2);
+            PrintWriter out = new PrintWriter(new OutputStreamWriter(s0.getOutputStream(), "UTF-8"));
+            if ( !doRedirect) {
+                out.println("HTTP/1.1 200 OK");
+                out.println("Content-length: " + res.length());
+                out.println();
+                out.print(res);
+            } else {
+                out.println("HTTP/1.1 302 Moved");
+                out.println("Location: /token");
+                out.println();
+            }
+            out.flush();
+        }
+        waitForPings(2);
+
+        TestMail mail = getMailReceiver().receive();
+        mail.verify();
+
+        String newcontent = IOUtils.readURL(get(p2));
+        Pattern pat = Pattern.compile("<td>http</td>\\s*<td>success</td>");
+        pat = Pattern.compile("<td>http</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>\\s*<td>([^<]*)</td>");
+        Matcher m0 = pat.matcher(newcontent);
+        assertTrue(newcontent, m0.find());
+        if (doRedirect) {
+            assertEquals("failed", m0.group(1));
+            assertThat(m0.group(3), containsString("code 302"));
+        } else {
+            assertEquals("success", m0.group(1));
+        }
+        pat = Pattern.compile("<td>email</td>\\s*<td>success</td>");
+        assertTrue(newcontent, pat.matcher(newcontent).find());
 
     }
 }