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.sql.Timestamp;
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.Properties;
28 import java.util.Random;
29 import java.util.TimeZone;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
36 import club.wpia.gigi.Gigi;
37 import club.wpia.gigi.GigiApiException;
38 import club.wpia.gigi.database.DatabaseConnection;
39 import club.wpia.gigi.database.DatabaseConnection.Link;
40 import club.wpia.gigi.database.GigiPreparedStatement;
41 import club.wpia.gigi.database.SQLFileManager.ImportType;
42 import club.wpia.gigi.dbObjects.CATS;
43 import club.wpia.gigi.dbObjects.CATS.CATSType;
44 import club.wpia.gigi.dbObjects.CertificateProfile;
45 import club.wpia.gigi.dbObjects.Domain;
46 import club.wpia.gigi.dbObjects.DomainPingType;
47 import club.wpia.gigi.dbObjects.User;
48 import club.wpia.gigi.passwords.PasswordStrengthChecker;
49 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
50 import club.wpia.gigi.util.DatabaseManager;
51 import club.wpia.gigi.util.DomainAssessment;
52 import club.wpia.gigi.util.Notary;
53 import club.wpia.gigi.util.PEM;
54 import club.wpia.gigi.util.PasswordHash;
55 import club.wpia.gigi.util.ServerConstants;
56 import club.wpia.gigi.util.TimeConditions;
57 import sun.security.pkcs10.PKCS10;
58 import sun.security.pkcs10.PKCS10Attributes;
59 import sun.security.x509.X500Name;
62 * Base class for a Testsuite that makes use of the config variables that define
65 public abstract class ConfiguredTest {
67 static Properties testProps = new Properties();
69 public static Properties getTestProps() {
73 private static boolean envInited = false;
76 * Some password that fulfills the password criteria.
78 public static final String TEST_PASSWORD = "xvXV12°§";
80 public static final String DIFFICULT_CHARS = "ÜÖÄß𐀀";
83 public static void initEnvironmentHook() throws IOException {
87 private static Link l;
89 public static Properties initEnvironment() throws IOException {
90 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
93 synchronized (ConfiguredTest.class) {
95 l = DatabaseConnection.newLink(false);
98 } catch (InterruptedException e) {
101 return generateProps();
104 try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
105 testProps.load(inStream);
107 Properties props = generateProps();
108 ServerConstants.init(props);
109 TimeConditions.init(props);
110 DomainAssessment.init(props);
111 PasswordHash.init(props);
112 Gigi.setPasswordChecker(new PasswordStrengthChecker());
114 if ( !DatabaseConnection.isInited()) {
115 DatabaseConnection.init(testProps);
117 synchronized (ConfiguredTest.class) {
119 l = DatabaseConnection.newLink(false);
122 } catch (InterruptedException e) {
131 public static void closeDBLink() {
132 synchronized (ConfiguredTest.class) {
140 private static Properties generateProps() throws Error {
141 Properties mainProps = new Properties();
142 mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
143 mainProps.setProperty("name.www", testProps.getProperty("name.www"));
144 mainProps.setProperty("name.static", testProps.getProperty("name.static"));
145 mainProps.setProperty("name.api", testProps.getProperty("name.api"));
146 mainProps.setProperty("name.suffix", testProps.getProperty("name.suffix"));
148 mainProps.setProperty("appName", "SomeCA");
149 mainProps.setProperty("appIdentifier", "someca");
151 mainProps.setProperty("https.port", testProps.getProperty("serverPort.https"));
152 mainProps.setProperty("http.port", testProps.getProperty("serverPort.http"));
154 File out = new File("financial.dat");
155 if ( !out.exists()) {
156 try (FileOutputStream fos = new FileOutputStream(out)) {
157 fos.write("google.com\ntwitter.com\n".getBytes("UTF-8"));
158 } catch (IOException e) {
162 mainProps.setProperty("highFinancialValue", out.getAbsolutePath());
163 mainProps.setProperty("scrypt.params", "1;1;1");
167 public static KeyPair generateKeypair() throws GeneralSecurityException {
168 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
169 kpg.initialize(4096);
170 KeyPair keyPair = null;
171 File f = new File("testKeypair");
173 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
174 keyPair = (KeyPair) ois.readObject();
175 } catch (ClassNotFoundException e) {
177 } catch (IOException e) {
181 keyPair = kpg.generateKeyPair();
182 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
183 oos.writeObject(keyPair);
185 } catch (IOException e) {
192 public static KeyPair generateBrokenKeypair() throws GeneralSecurityException {
193 KeyPair keyPair = null;
194 File f = new File("testBrokenKeypair");
196 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
197 keyPair = (KeyPair) ois.readObject();
198 } catch (ClassNotFoundException e) {
200 } catch (IOException e) {
204 // -----BEGIN SHAMELESSLY ADAPTED BLOCK-----
206 * Modified original RSA key generator to use three primes with one
207 * prime set to fixed value to allow simple checking for such faulty
210 * @link sun.security.rsa.RSAKeyPairGenerator#generateKeyPair
213 KeyFactory factory = KeyFactory.getInstance("RSA");
214 Random random = new SecureRandom();
218 // The generated numbers p q and r fall into the
220 // - p: 2^(lp-1) < p < 2^lp
221 // - q: 2^(lq-1) < q < 2^lq
222 // - r: 2^12 < r < 2^13
223 // Thus the generated number has at least lp+lq+11 bit and
224 // can have at most lp+lq+13 bit.
225 // Thus for random selection of p and q the algorithm will
226 // at some point select a number of length n=n/2+lr+(n-n/2-lr)=>n
228 int lp = (keySize + 1) >> 1;
229 int lr = BigInteger.valueOf(r_lv).bitLength();
230 int lq = keySize - lp - lr;
232 BigInteger e = BigInteger.valueOf(7331);
235 while (keyPair == null) {
236 // generate two random primes of size lp/lq
237 BigInteger p, q, r, n;
239 r = BigInteger.valueOf(r_lv);
241 p = BigInteger.probablePrime(lp, random);
242 q = BigInteger.probablePrime(lq, random);
244 // modulus n = p * q * r
245 n = p.multiply(q).multiply(r);
247 // even with correctly sized p, q and r, there is a chance
248 // that n will be one bit short. re-generate the
250 } while (n.bitLength() < keySize);
252 // phi = (p - 1) * (q - 1) * (r - 1) must be relative prime to e
253 // otherwise RSA just won't work ;-)
254 BigInteger p1 = p.subtract(BigInteger.ONE);
255 BigInteger q1 = q.subtract(BigInteger.ONE);
256 BigInteger r1 = r.subtract(BigInteger.ONE);
257 BigInteger phi = p1.multiply(q1).multiply(r1);
259 // generate new p and q until they work. typically
260 if (e.gcd(phi).equals(BigInteger.ONE) == false) {
264 // private exponent d is the inverse of e mod phi
265 BigInteger d = e.modInverse(phi);
267 RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(n, e);
268 RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(n, d);
269 PublicKey publicKey = factory.generatePublic(publicSpec);
270 PrivateKey privateKey = factory.generatePrivate(privateSpec);
271 keyPair = new KeyPair(publicKey, privateKey);
273 // -----END SHAMELESSLY ADAPTED BLOCK-----
275 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
276 oos.writeObject(keyPair);
278 } catch (IOException ioe) {
279 ioe.printStackTrace();
285 public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
286 return generatePEMCSR(kp, dn, new PKCS10Attributes());
289 public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
290 return generatePEMCSR(kp, dn, atts, "SHA512WithRSA");
293 public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
294 PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
295 Signature s = Signature.getInstance(signature);
296 s.initSign(kp.getPrivate());
297 p10.encodeAndSign(new X500Name(dn), s);
298 return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
301 static int count = 0;
303 public static String createRandomIDString() {
304 final char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
305 final int idStringLength = 16;
310 StringBuilder sb = new StringBuilder(idStringLength);
311 for (int i = 0; i < idStringLength; i++) {
312 sb.append(chars[sr.nextInt(chars.length)]);
315 return sb.toString();
318 public static synchronized String createUniqueName() {
319 return "test" + createRandomIDString() + "a" + (count++) + "u";
322 public static CertificateProfile getClientProfile() {
323 return CertificateProfile.getByName("client");
326 public static int countRegex(String text, String pattern) {
327 Pattern p = Pattern.compile(pattern);
328 Matcher m = p.matcher(text);
336 public static void makeAgent(int uid) {
337 addChallenge(uid, CATSType.AGENT_CHALLENGE);
341 public static void addChallenge(int uid, CATSType ct) {
342 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=?, language='en_EN', version='1'")) {
344 ps1.setInt(2, ct.getId());
349 public static void addChallengeInPast(int uid, CATSType ct) {
350 try (GigiPreparedStatement ps1 = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=?, pass_date=?, language='en_EN', version='1'")) {
352 ps1.setInt(2, ct.getId());
353 ps1.setTimestamp(3, new Timestamp(new Date(System.currentTimeMillis() - 24L * 60 * 60 * (CATS.TEST_MONTHS + 1) * 31 * 1000L).getTime()));
358 public static void add100Points(int uid) {
359 try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'")) {
361 ps2.setInt(2, User.getById(uid).getPreferredName().getId());
366 public MailReceiver getMailReceiver() {
367 throw new Error("Feature requires Business or ManagedTest.");
370 public void verify(Domain d) {
372 d.addPing(DomainPingType.EMAIL, "admin");
373 TestMail testMail = getMailReceiver().receive("admin@" + d.getSuffix());
375 // Enforce successful ping :-)
376 d.addPing(DomainPingType.HTTP, "a:b");
377 try (GigiPreparedStatement gps = new GigiPreparedStatement("INSERT INTO `domainPinglog` SET `configId`=(SELECT `id` FROM `pingconfig` WHERE `domainid`=? AND `type`='http'), state='success', needsAction=false")) {
378 gps.setInt(1, d.getId());
381 assertTrue(d.isVerified());
382 } catch (GigiApiException e) {
384 } catch (IOException e) {
389 public static void purgeOnlyDB() throws SQLException, IOException {
390 System.out.println("... resetting Database");
391 long ms = System.currentTimeMillis();
393 DatabaseManager.run(new String[] {
394 testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
395 }, ImportType.TRUNCATE);
396 } catch (ClassNotFoundException e) {
399 System.out.println("Database reset complete in " + (System.currentTimeMillis() - ms) + " ms.");
402 public static String validVerificationDateString() {
403 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
404 Calendar c = Calendar.getInstance();
405 c.setTimeInMillis(System.currentTimeMillis());
406 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
407 return sdf.format(new Date(c.getTimeInMillis()));