]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ConfiguredTest.java
9010a5975ef802619338311cc7073dc2bbd59e30
[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.DomainAssessment;
21 import org.cacert.gigi.util.PEM;
22 import org.junit.BeforeClass;
23
24 import sun.security.pkcs10.PKCS10;
25 import sun.security.pkcs10.PKCS10Attributes;
26 import sun.security.x509.X500Name;
27
28 /**
29  * Base class for a Testsuite that makes use of the config variables that define
30  * the environment.
31  */
32 public abstract class ConfiguredTest {
33
34     static Properties testProps = new Properties();
35
36     public static Properties getTestProps() {
37         return testProps;
38     }
39
40     private static boolean envInited = false;
41
42     @BeforeClass
43     public static void initEnvironmentHook() throws IOException {
44         initEnvironment();
45     }
46
47     public static Properties initEnvironment() throws IOException {
48         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
49         Properties props = generateProps();
50         if (envInited) {
51             return props;
52         }
53         envInited = true;
54         DomainAssessment.init(props);
55         try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
56             testProps.load(inStream);
57         }
58         if ( !DatabaseConnection.isInited()) {
59             DatabaseConnection.init(testProps);
60             try {
61                 l = DatabaseConnection.newLink(false);
62             } catch (InterruptedException e) {
63                 throw new Error(e);
64             }
65         }
66         return props;
67
68     }
69
70     private static Properties generateProps() throws Error {
71         Properties mainProps = new Properties();
72         File out = new File("financial.dat");
73         if ( !out.exists()) {
74             try (FileOutputStream fos = new FileOutputStream(out)) {
75                 fos.write("google.com\ntwitter.com\n".getBytes("UTF-8"));
76             } catch (IOException e) {
77                 throw new Error(e);
78             }
79         }
80         mainProps.setProperty("highFinancialValue", out.getAbsolutePath());
81         return mainProps;
82     }
83
84     public static KeyPair generateKeypair() throws GeneralSecurityException {
85         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
86         kpg.initialize(4096);
87         KeyPair keyPair = null;
88         File f = new File("testKeypair");
89         if (f.exists()) {
90             try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
91                 keyPair = (KeyPair) ois.readObject();
92             } catch (ClassNotFoundException e) {
93                 e.printStackTrace();
94             } catch (IOException e) {
95                 e.printStackTrace();
96             }
97         } else {
98             keyPair = kpg.generateKeyPair();
99             try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
100                 oos.writeObject(keyPair);
101                 oos.close();
102             } catch (IOException e) {
103                 e.printStackTrace();
104             }
105         }
106         return keyPair;
107     }
108
109     public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
110         return generatePEMCSR(kp, dn, new PKCS10Attributes());
111     }
112
113     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
114         return generatePEMCSR(kp, dn, atts, "SHA256WithRSA");
115     }
116
117     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
118         PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
119         Signature s = Signature.getInstance(signature);
120         s.initSign(kp.getPrivate());
121         p10.encodeAndSign(new X500Name(dn), s);
122         return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
123     }
124
125     static int count = 0;
126
127     private static Link l;
128
129     public static String createUniqueName() {
130         return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
131     }
132
133     public static int countRegex(String text, String pattern) {
134         Pattern p = Pattern.compile(pattern);
135         Matcher m = p.matcher(text);
136         int i = 0;
137         while (m.find()) {
138             i++;
139         }
140         return i;
141     }
142 }