X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=util-testing%2Forg%2Fcacert%2Fgigi%2Fpages%2FManager.java;h=3ac191ac030bf4d4fc886cb685237d08e4c5977f;hp=8f77afbd63ea8bbdb61732e51dc36a5ad72aec2d;hb=f9da28083e2c72b95cf23d8b4fa4a164e01167fb;hpb=a0cae0454cb03690ce318fa0fc8b4ca6d6cca4f1 diff --git a/util-testing/org/cacert/gigi/pages/Manager.java b/util-testing/org/cacert/gigi/pages/Manager.java index 8f77afbd..3ac191ac 100644 --- a/util-testing/org/cacert/gigi/pages/Manager.java +++ b/util-testing/org/cacert/gigi/pages/Manager.java @@ -17,17 +17,22 @@ import java.util.LinkedList; import java.util.Locale; import java.util.Map; import java.util.Properties; +import java.util.TreeSet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.cacert.gigi.Gigi; import org.cacert.gigi.GigiApiException; import org.cacert.gigi.crypto.SPKAC; -import org.cacert.gigi.database.DatabaseConnection; import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.dbObjects.Assurance.AssuranceType; import org.cacert.gigi.dbObjects.Certificate; import org.cacert.gigi.dbObjects.Certificate.CertificateStatus; +import org.cacert.gigi.dbObjects.CertificateOwner; import org.cacert.gigi.dbObjects.Digest; +import org.cacert.gigi.dbObjects.Domain; +import org.cacert.gigi.dbObjects.DomainPingType; import org.cacert.gigi.dbObjects.EmailAddress; import org.cacert.gigi.dbObjects.Group; import org.cacert.gigi.dbObjects.Name; @@ -37,6 +42,8 @@ import org.cacert.gigi.localisation.Language; import org.cacert.gigi.output.template.IterableDataset; import org.cacert.gigi.output.template.Template; import org.cacert.gigi.pages.account.certs.CertificateRequest; +import org.cacert.gigi.ping.DomainPinger; +import org.cacert.gigi.ping.PingerDaemon; import org.cacert.gigi.util.AuthorizationContext; import org.cacert.gigi.util.Notary; @@ -48,6 +55,8 @@ public class Manager extends Page { Field f; + private static HashMap dps; + private Manager() { super("Test Manager"); try { @@ -58,43 +67,68 @@ public class Manager extends Page { System.out.println("I don't have 'hash', we are working probably in layered mode. Test Manager may not work."); // throw new Error(e); } + + try { + Field gigiInstance = Gigi.class.getDeclaredField("instance"); + gigiInstance.setAccessible(true); + Gigi g = (Gigi) gigiInstance.get(null); + + Field gigiPinger = Gigi.class.getDeclaredField("pinger"); + gigiPinger.setAccessible(true); + PingerDaemon pd = (PingerDaemon) gigiPinger.get(g); + + Field f = PingerDaemon.class.getDeclaredField("pingers"); + f.setAccessible(true); + dps = (HashMap) f.get(pd); + HashMap pingers = new HashMap<>(); + for (DomainPingType dpt : DomainPingType.values()) { + pingers.put(dpt, new PingerFetcher(dpt)); + } + f.set(pd, pingers); + } catch (ReflectiveOperationException e) { + e.printStackTrace(); + } } - public User[] getAssurers() { - if (assurers != null) { - return assurers; + public User getAssurer(int i) { + if (assurers[i] != null) { + return assurers[i]; } - assurers = new User[10]; try { - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?"); - for (int i = 0; i < assurers.length; i++) { - String mail = "test-assurer" + i + "@example.com"; - User u = User.getByEmail(mail); - if (u == null) { - System.out.println("Creating assurer"); - createUser(mail); - u = User.getByEmail(mail); - passCATS(u); - ps.setInt(1, u.getId()); - ps.setInt(2, u.getId()); - ps.setInt(3, 100); - ps.setString(4, "Manager init code"); - ps.setString(5, "1990-01-01"); - ps.execute(); - } - assurers[i] = u; + User u = createAssurer(i); + assurers[i] = u; - } } catch (ReflectiveOperationException | GigiApiException e) { e.printStackTrace(); } - return assurers; + return assurers[i]; + } + + private User createAssurer(int i) throws GigiApiException, IllegalAccessException { + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) { + String mail = "test-assurer" + i + "@example.com"; + User u = User.getByEmail(mail); + if (u == null) { + System.out.println("Creating assurer"); + createUser(mail); + u = User.getByEmail(mail); + passCATS(u); + ps.setInt(1, u.getId()); + ps.setInt(2, u.getId()); + ps.setInt(3, 100); + ps.setString(4, "Manager init code"); + ps.setString(5, "1990-01-01"); + ps.execute(); + } + return u; + } } private void passCATS(User u) { - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO cats_passed SET user_id=?, variant_id=1"); - ps.setInt(1, u.getId()); - ps.execute(); + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=1")) { + ps.setInt(1, u.getId()); + ps.execute(); + } } private static Manager instance; @@ -103,6 +137,8 @@ public class Manager extends Page { HashMap> emails = new HashMap<>(); + private static TreeSet pingExempt = new TreeSet<>(); + public static Manager getInstance() { if (instance == null) { instance = new Manager(); @@ -131,6 +167,26 @@ public class Manager extends Page { } + public class PingerFetcher extends DomainPinger { + + private DomainPingType dpt; + + public PingerFetcher(DomainPingType dpt) { + this.dpt = dpt; + } + + @Override + public void ping(Domain domain, String configuration, CertificateOwner target, int confId) { + System.out.println("Test: " + domain); + if (pingExempt.contains(domain.getSuffix())) { + enterPingResult(confId, DomainPinger.PING_SUCCEDED, "Succeeded by TestManager pass-by", null); + } else { + dps.get(dpt).ping(domain, configuration, target, confId); + } + } + + } + public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) { try { @@ -165,7 +221,7 @@ public class Manager extends Page { ea.verify(hash); } - User[] assurers; + User[] assurers = new User[25]; @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { @@ -205,13 +261,24 @@ public class Manager extends Page { return; } try { - for (int i = 0; i < getAssurers().length; i++) { - Notary.assure(getAssurers()[i], byEmail, byEmail.getName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06"); + for (int i = 0; i < 10; i++) { + Notary.assure(getAssurer(i), byEmail, byEmail.getName(), byEmail.getDoB(), 10, "Testmanager Assure up code", "2014-11-06", AssuranceType.FACE_TO_FACE); } } catch (GigiApiException e) { throw new Error(e); } resp.getWriter().println("User has been assured."); + } else if (req.getParameter("letassure") != null) { + String mail = req.getParameter("letassureEmail"); + User byEmail = User.getByEmail(mail); + try { + for (int i = 0; i < 25; i++) { + User a = getAssurer(i); + Notary.assure(byEmail, a, a.getName(), a.getDoB(), 10, "Testmanager exp up code", "2014-11-06", AssuranceType.FACE_TO_FACE); + } + } catch (GigiApiException e) { + throw new Error(e); + } } else if (req.getParameter("addEmail") != null) { User u = User.getByEmail(req.getParameter("addEmailEmail")); try { @@ -263,6 +330,16 @@ public class Manager extends Page { resp.getWriter().println("interrupted"); } + } else if (req.getParameter("addExDom") != null) { + String dom = req.getParameter("exemtDom"); + pingExempt.add(dom); + resp.getWriter().println("Updated domains exempt from pings. Current set:
"); + resp.getWriter().println(pingExempt); + } else if (req.getParameter("delExDom") != null) { + String dom = req.getParameter("exemtDom"); + pingExempt.remove(dom); + resp.getWriter().println("Updated domains exempt from pings. Current set:
"); + resp.getWriter().println(pingExempt); } } @@ -301,7 +378,6 @@ public class Manager extends Page { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - getAssurers(); String pi = req.getPathInfo().substring(PATH.length()); if (pi.length() > 1 && pi.startsWith("/fetch-")) { String mail = pi.substring(pi.indexOf('-', 2) + 1);