]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ConfiguredTest.java
Suggestions to enhance the SQL call pattern.
[gigi.git] / tests / org / cacert / gigi / testUtils / ConfiguredTest.java
1 package org.cacert.gigi.testUtils;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.security.GeneralSecurityException;
10 import java.security.KeyPair;
11 import java.security.KeyPairGenerator;
12 import java.security.Signature;
13 import java.util.Properties;
14 import java.util.TimeZone;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.database.DatabaseConnection.Link;
20 import org.cacert.gigi.util.PEM;
21 import org.junit.BeforeClass;
22
23 import sun.security.pkcs10.PKCS10;
24 import sun.security.pkcs10.PKCS10Attributes;
25 import sun.security.x509.X500Name;
26
27 /**
28  * Base class for a Testsuite that makes use of the config variables that define
29  * the environment.
30  */
31 public abstract class ConfiguredTest {
32
33     static Properties testProps = new Properties();
34
35     public static Properties getTestProps() {
36         return testProps;
37     }
38
39     private static boolean envInited = false;
40
41     @BeforeClass
42     public static void initEnvironment() throws IOException {
43         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
44         if (envInited) {
45             return;
46         }
47         envInited = true;
48         try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
49             testProps.load(inStream);
50         }
51         if ( !DatabaseConnection.isInited()) {
52             DatabaseConnection.init(testProps);
53             try {
54                 l = DatabaseConnection.newLink(false);
55             } catch (InterruptedException e) {
56                 throw new Error(e);
57             }
58         }
59     }
60
61     public static KeyPair generateKeypair() throws GeneralSecurityException {
62         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
63         kpg.initialize(4096);
64         KeyPair keyPair = null;
65         File f = new File("testKeypair");
66         if (f.exists()) {
67             try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
68                 keyPair = (KeyPair) ois.readObject();
69             } catch (ClassNotFoundException e) {
70                 e.printStackTrace();
71             } catch (IOException e) {
72                 e.printStackTrace();
73             }
74         } else {
75             keyPair = kpg.generateKeyPair();
76             try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
77                 oos.writeObject(keyPair);
78                 oos.close();
79             } catch (IOException e) {
80                 e.printStackTrace();
81             }
82         }
83         return keyPair;
84     }
85
86     public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
87         return generatePEMCSR(kp, dn, new PKCS10Attributes());
88     }
89
90     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
91         return generatePEMCSR(kp, dn, atts, "SHA256WithRSA");
92     }
93
94     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
95         PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
96         Signature s = Signature.getInstance(signature);
97         s.initSign(kp.getPrivate());
98         p10.encodeAndSign(new X500Name(dn), s);
99         return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
100     }
101
102     static int count = 0;
103
104     private static Link l;
105
106     public static String createUniqueName() {
107         return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
108     }
109
110     public static int countRegex(String text, String pattern) {
111         Pattern p = Pattern.compile(pattern);
112         Matcher m = p.matcher(text);
113         int i = 0;
114         while (m.find()) {
115             i++;
116         }
117         return i;
118     }
119 }