1 package club.wpia.gigi.testUtils;
3 import static org.junit.Assert.*;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.ObjectInputStream;
10 import java.io.ObjectOutputStream;
11 import java.security.GeneralSecurityException;
12 import java.security.KeyPair;
13 import java.security.KeyPairGenerator;
14 import java.security.Signature;
15 import java.sql.SQLException;
16 import java.text.SimpleDateFormat;
17 import java.util.Calendar;
18 import java.util.Date;
19 import java.util.Properties;
20 import java.util.Random;
21 import java.util.TimeZone;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
28 import club.wpia.gigi.GigiApiException;
29 import club.wpia.gigi.database.DatabaseConnection;
30 import club.wpia.gigi.database.GigiPreparedStatement;
31 import club.wpia.gigi.database.DatabaseConnection.Link;
32 import club.wpia.gigi.database.SQLFileManager.ImportType;
33 import club.wpia.gigi.dbObjects.CertificateProfile;
34 import club.wpia.gigi.dbObjects.Domain;
35 import club.wpia.gigi.dbObjects.DomainPingType;
36 import club.wpia.gigi.dbObjects.User;
37 import club.wpia.gigi.dbObjects.CATS.CATSType;
38 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
39 import club.wpia.gigi.util.DatabaseManager;
40 import club.wpia.gigi.util.DomainAssessment;
41 import club.wpia.gigi.util.Notary;
42 import club.wpia.gigi.util.PEM;
43 import club.wpia.gigi.util.PasswordHash;
44 import club.wpia.gigi.util.ServerConstants;
45 import club.wpia.gigi.util.TimeConditions;
46 import sun.security.pkcs10.PKCS10;
47 import sun.security.pkcs10.PKCS10Attributes;
48 import sun.security.x509.X500Name;
51 * Base class for a Testsuite that makes use of the config variables that define
54 public abstract class ConfiguredTest {
56 static Properties testProps = new Properties();
58 public static Properties getTestProps() {
62 private static boolean envInited = false;
65 * Some password that fulfills the password criteria.
67 public static final String TEST_PASSWORD = "xvXV12°§";
69 public static final String DIFFICULT_CHARS = "ÜÖÄß𐀀";
72 public static void initEnvironmentHook() throws IOException {
76 private static Link l;
78 public static Properties initEnvironment() throws IOException {
79 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
81 return generateProps();
84 try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
85 testProps.load(inStream);
87 Properties props = generateProps();
88 ServerConstants.init(props);
89 TimeConditions.init(props);
90 DomainAssessment.init(props);
91 PasswordHash.init(props);
93 if ( !DatabaseConnection.isInited()) {
94 DatabaseConnection.init(testProps);
96 l = DatabaseConnection.newLink(false);
97 } catch (InterruptedException e) {
106 public static void closeDBLink() {
113 private static Properties generateProps() throws Error {
114 Properties mainProps = new Properties();
115 mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
116 mainProps.setProperty("name.www", testProps.getProperty("name.www"));
117 mainProps.setProperty("name.static", testProps.getProperty("name.static"));
118 mainProps.setProperty("name.api", testProps.getProperty("name.api"));
120 mainProps.setProperty("appName", "SomeCA");
122 mainProps.setProperty("https.port", testProps.getProperty("serverPort.https"));
123 mainProps.setProperty("http.port", testProps.getProperty("serverPort.http"));
125 File out = new File("financial.dat");
126 if ( !out.exists()) {
127 try (FileOutputStream fos = new FileOutputStream(out)) {
128 fos.write("google.com\ntwitter.com\n".getBytes("UTF-8"));
129 } catch (IOException e) {
133 mainProps.setProperty("highFinancialValue", out.getAbsolutePath());
134 mainProps.setProperty("scrypt.params", "1;1;1");
138 public static KeyPair generateKeypair() throws GeneralSecurityException {
139 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
140 kpg.initialize(4096);
141 KeyPair keyPair = null;
142 File f = new File("testKeypair");
144 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
145 keyPair = (KeyPair) ois.readObject();
146 } catch (ClassNotFoundException e) {
148 } catch (IOException e) {
152 keyPair = kpg.generateKeyPair();
153 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
154 oos.writeObject(keyPair);
156 } catch (IOException e) {
163 public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
164 return generatePEMCSR(kp, dn, new PKCS10Attributes());
167 public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
168 return generatePEMCSR(kp, dn, atts, "SHA512WithRSA");
171 public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
172 PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
173 Signature s = Signature.getInstance(signature);
174 s.initSign(kp.getPrivate());
175 p10.encodeAndSign(new X500Name(dn), s);
176 return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
179 static int count = 0;
181 public static String createRandomIDString() {
182 final char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
183 final int idStringLength = 16;
188 StringBuilder sb = new StringBuilder(idStringLength);
189 for (int i = 0; i < idStringLength; i++) {
190 sb.append(chars[sr.nextInt(chars.length)]);
193 return sb.toString();
196 public static synchronized String createUniqueName() {
197 return "test" + createRandomIDString() + "a" + (count++) + "u";
200 public static CertificateProfile getClientProfile() {
201 return CertificateProfile.getByName("client");
204 public static int countRegex(String text, String pattern) {
205 Pattern p = Pattern.compile(pattern);
206 Matcher m = p.matcher(text);
214 public static void makeAgent(int uid) {
215 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=?, language='en_EN', version='1'")) {
217 ps1.setInt(2, CATSType.AGENT_CHALLENGE.getId());
221 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'")) {
223 ps2.setInt(2, User.getById(uid).getPreferredName().getId());
228 public MailReceiver getMailReceiver() {
229 throw new Error("Feature requires Business or ManagedTest.");
232 public void verify(Domain d) {
234 d.addPing(DomainPingType.EMAIL, "admin");
235 TestMail testMail = getMailReceiver().receive();
237 assertTrue(d.isVerified());
238 } catch (GigiApiException e) {
240 } catch (IOException e) {
245 public static void purgeOnlyDB() throws SQLException, IOException {
246 System.out.println("... resetting Database");
247 long ms = System.currentTimeMillis();
249 DatabaseManager.run(new String[] {
250 testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
251 }, ImportType.TRUNCATE);
252 } catch (ClassNotFoundException e) {
255 System.out.println("Database reset complete in " + (System.currentTimeMillis() - ms) + " ms.");
258 public static String validVerificationDateString() {
259 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
260 Calendar c = Calendar.getInstance();
261 c.setTimeInMillis(System.currentTimeMillis());
262 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
263 return sdf.format(new Date(c.getTimeInMillis()));