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.math.BigInteger;
12 import java.security.GeneralSecurityException;
13 import java.security.KeyFactory;
14 import java.security.KeyPair;
15 import java.security.KeyPairGenerator;
16 import java.security.PrivateKey;
17 import java.security.PublicKey;
18 import java.security.SecureRandom;
19 import java.security.Signature;
20 import java.security.spec.RSAPrivateKeySpec;
21 import java.security.spec.RSAPublicKeySpec;
22 import java.sql.SQLException;
23 import java.text.SimpleDateFormat;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.Properties;
27 import java.util.Random;
28 import java.util.TimeZone;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
35 import club.wpia.gigi.GigiApiException;
36 import club.wpia.gigi.database.DatabaseConnection;
37 import club.wpia.gigi.database.DatabaseConnection.Link;
38 import club.wpia.gigi.database.GigiPreparedStatement;
39 import club.wpia.gigi.database.SQLFileManager.ImportType;
40 import club.wpia.gigi.dbObjects.CATS.CATSType;
41 import club.wpia.gigi.dbObjects.CertificateProfile;
42 import club.wpia.gigi.dbObjects.Domain;
43 import club.wpia.gigi.dbObjects.DomainPingType;
44 import club.wpia.gigi.dbObjects.User;
45 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
46 import club.wpia.gigi.util.DatabaseManager;
47 import club.wpia.gigi.util.DomainAssessment;
48 import club.wpia.gigi.util.Notary;
49 import club.wpia.gigi.util.PEM;
50 import club.wpia.gigi.util.PasswordHash;
51 import club.wpia.gigi.util.ServerConstants;
52 import club.wpia.gigi.util.TimeConditions;
53 import sun.security.pkcs10.PKCS10;
54 import sun.security.pkcs10.PKCS10Attributes;
55 import sun.security.x509.X500Name;
58 * Base class for a Testsuite that makes use of the config variables that define
61 public abstract class ConfiguredTest {
63 static Properties testProps = new Properties();
65 public static Properties getTestProps() {
69 private static boolean envInited = false;
72 * Some password that fulfills the password criteria.
74 public static final String TEST_PASSWORD = "xvXV12°§";
76 public static final String DIFFICULT_CHARS = "ÜÖÄß𐀀";
79 public static void initEnvironmentHook() throws IOException {
83 private static Link l;
85 public static Properties initEnvironment() throws IOException {
86 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
89 synchronized (ConfiguredTest.class) {
91 l = DatabaseConnection.newLink(false);
94 } catch (InterruptedException e) {
97 return generateProps();
100 try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
101 testProps.load(inStream);
103 Properties props = generateProps();
104 ServerConstants.init(props);
105 TimeConditions.init(props);
106 DomainAssessment.init(props);
107 PasswordHash.init(props);
109 if ( !DatabaseConnection.isInited()) {
110 DatabaseConnection.init(testProps);
112 synchronized (ConfiguredTest.class) {
114 l = DatabaseConnection.newLink(false);
117 } catch (InterruptedException e) {
126 public static void closeDBLink() {
127 synchronized (ConfiguredTest.class) {
135 private static Properties generateProps() throws Error {
136 Properties mainProps = new Properties();
137 mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
138 mainProps.setProperty("name.www", testProps.getProperty("name.www"));
139 mainProps.setProperty("name.static", testProps.getProperty("name.static"));
140 mainProps.setProperty("name.api", testProps.getProperty("name.api"));
141 mainProps.setProperty("name.suffix", testProps.getProperty("name.suffix"));
143 mainProps.setProperty("appName", "SomeCA");
144 mainProps.setProperty("appIdentifier", "someca");
146 mainProps.setProperty("https.port", testProps.getProperty("serverPort.https"));
147 mainProps.setProperty("http.port", testProps.getProperty("serverPort.http"));
149 File out = new File("financial.dat");
150 if ( !out.exists()) {
151 try (FileOutputStream fos = new FileOutputStream(out)) {
152 fos.write("google.com\ntwitter.com\n".getBytes("UTF-8"));
153 } catch (IOException e) {
157 mainProps.setProperty("highFinancialValue", out.getAbsolutePath());
158 mainProps.setProperty("scrypt.params", "1;1;1");
162 public static KeyPair generateKeypair() throws GeneralSecurityException {
163 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
164 kpg.initialize(4096);
165 KeyPair keyPair = null;
166 File f = new File("testKeypair");
168 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
169 keyPair = (KeyPair) ois.readObject();
170 } catch (ClassNotFoundException e) {
172 } catch (IOException e) {
176 keyPair = kpg.generateKeyPair();
177 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
178 oos.writeObject(keyPair);
180 } catch (IOException e) {
187 public static KeyPair generateBrokenKeypair() throws GeneralSecurityException {
188 KeyPair keyPair = null;
189 File f = new File("testBrokenKeypair");
191 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
192 keyPair = (KeyPair) ois.readObject();
193 } catch (ClassNotFoundException e) {
195 } catch (IOException e) {
199 // -----BEGIN SHAMELESSLY ADAPTED BLOCK-----
201 * Modified original RSA key generator to use three primes with one
202 * prime set to fixed value to allow simple checking for such faulty
205 * @link sun.security.rsa.RSAKeyPairGenerator#generateKeyPair
208 KeyFactory factory = KeyFactory.getInstance("RSA");
209 Random random = new SecureRandom();
213 // The generated numbers p q and r fall into the
215 // - p: 2^(lp-1) < p < 2^lp
216 // - q: 2^(lq-1) < q < 2^lq
217 // - r: 2^12 < r < 2^13
218 // Thus the generated number has at least lp+lq+11 bit and
219 // can have at most lp+lq+13 bit.
220 // Thus for random selection of p and q the algorithm will
221 // at some point select a number of length n=n/2+lr+(n-n/2-lr)=>n
223 int lp = (keySize + 1) >> 1;
224 int lr = BigInteger.valueOf(r_lv).bitLength();
225 int lq = keySize - lp - lr;
227 BigInteger e = BigInteger.valueOf(7331);
230 while (keyPair == null) {
231 // generate two random primes of size lp/lq
232 BigInteger p, q, r, n;
234 r = BigInteger.valueOf(r_lv);
236 p = BigInteger.probablePrime(lp, random);
237 q = BigInteger.probablePrime(lq, random);
239 // modulus n = p * q * r
240 n = p.multiply(q).multiply(r);
242 // even with correctly sized p, q and r, there is a chance
243 // that n will be one bit short. re-generate the
245 } while (n.bitLength() < keySize);
247 // phi = (p - 1) * (q - 1) * (r - 1) must be relative prime to e
248 // otherwise RSA just won't work ;-)
249 BigInteger p1 = p.subtract(BigInteger.ONE);
250 BigInteger q1 = q.subtract(BigInteger.ONE);
251 BigInteger r1 = r.subtract(BigInteger.ONE);
252 BigInteger phi = p1.multiply(q1).multiply(r1);
254 // generate new p and q until they work. typically
255 if (e.gcd(phi).equals(BigInteger.ONE) == false) {
259 // private exponent d is the inverse of e mod phi
260 BigInteger d = e.modInverse(phi);
262 RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(n, e);
263 RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(n, d);
264 PublicKey publicKey = factory.generatePublic(publicSpec);
265 PrivateKey privateKey = factory.generatePrivate(privateSpec);
266 keyPair = new KeyPair(publicKey, privateKey);
268 // -----END SHAMELESSLY ADAPTED BLOCK-----
270 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
271 oos.writeObject(keyPair);
273 } catch (IOException ioe) {
274 ioe.printStackTrace();
280 public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
281 return generatePEMCSR(kp, dn, new PKCS10Attributes());
284 public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
285 return generatePEMCSR(kp, dn, atts, "SHA512WithRSA");
288 public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
289 PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
290 Signature s = Signature.getInstance(signature);
291 s.initSign(kp.getPrivate());
292 p10.encodeAndSign(new X500Name(dn), s);
293 return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
296 static int count = 0;
298 public static String createRandomIDString() {
299 final char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
300 final int idStringLength = 16;
305 StringBuilder sb = new StringBuilder(idStringLength);
306 for (int i = 0; i < idStringLength; i++) {
307 sb.append(chars[sr.nextInt(chars.length)]);
310 return sb.toString();
313 public static synchronized String createUniqueName() {
314 return "test" + createRandomIDString() + "a" + (count++) + "u";
317 public static CertificateProfile getClientProfile() {
318 return CertificateProfile.getByName("client");
321 public static int countRegex(String text, String pattern) {
322 Pattern p = Pattern.compile(pattern);
323 Matcher m = p.matcher(text);
331 public static void makeAgent(int uid) {
332 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=?, language='en_EN', version='1'")) {
334 ps1.setInt(2, CATSType.AGENT_CHALLENGE.getId());
338 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'")) {
340 ps2.setInt(2, User.getById(uid).getPreferredName().getId());
345 public MailReceiver getMailReceiver() {
346 throw new Error("Feature requires Business or ManagedTest.");
349 public void verify(Domain d) {
351 d.addPing(DomainPingType.EMAIL, "admin");
352 TestMail testMail = getMailReceiver().receive("admin@" + d.getSuffix());
354 // Enforce successful ping :-)
355 d.addPing(DomainPingType.HTTP, "a:b");
356 try (GigiPreparedStatement gps = new GigiPreparedStatement("INSERT INTO `domainPinglog` SET `configId`=(SELECT `id` FROM `pingconfig` WHERE `domainid`=? AND `type`='http'), state='success', needsAction=false")) {
357 gps.setInt(1, d.getId());
360 assertTrue(d.isVerified());
361 } catch (GigiApiException e) {
363 } catch (IOException e) {
368 public static void purgeOnlyDB() throws SQLException, IOException {
369 System.out.println("... resetting Database");
370 long ms = System.currentTimeMillis();
372 DatabaseManager.run(new String[] {
373 testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
374 }, ImportType.TRUNCATE);
375 } catch (ClassNotFoundException e) {
378 System.out.println("Database reset complete in " + (System.currentTimeMillis() - ms) + " ms.");
381 public static String validVerificationDateString() {
382 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
383 Calendar c = Calendar.getInstance();
384 c.setTimeInMillis(System.currentTimeMillis());
385 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
386 return sdf.format(new Date(c.getTimeInMillis()));