]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/testUtils/ConfiguredTest.java
add: show more certificates on the "roots" page
[gigi.git] / tests / club / wpia / gigi / testUtils / ConfiguredTest.java
1 package club.wpia.gigi.testUtils;
2
3 import static org.junit.Assert.*;
4
5 import java.io.File;
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;
24
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27
28 import club.wpia.gigi.GigiApiException;
29 import club.wpia.gigi.database.DatabaseConnection;
30 import club.wpia.gigi.database.DatabaseConnection.Link;
31 import club.wpia.gigi.database.GigiPreparedStatement;
32 import club.wpia.gigi.database.SQLFileManager.ImportType;
33 import club.wpia.gigi.dbObjects.CATS.CATSType;
34 import club.wpia.gigi.dbObjects.CertificateProfile;
35 import club.wpia.gigi.dbObjects.Domain;
36 import club.wpia.gigi.dbObjects.DomainPingType;
37 import club.wpia.gigi.dbObjects.User;
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;
49
50 /**
51  * Base class for a Testsuite that makes use of the config variables that define
52  * the environment.
53  */
54 public abstract class ConfiguredTest {
55
56     static Properties testProps = new Properties();
57
58     public static Properties getTestProps() {
59         return testProps;
60     }
61
62     private static boolean envInited = false;
63
64     /**
65      * Some password that fulfills the password criteria.
66      */
67     public static final String TEST_PASSWORD = "xvXV12°§";
68
69     public static final String DIFFICULT_CHARS = "ÜÖÄß𐀀";
70
71     @BeforeClass
72     public static void initEnvironmentHook() throws IOException {
73         initEnvironment();
74     }
75
76     private static Link l;
77
78     public static Properties initEnvironment() throws IOException {
79         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
80         if (envInited) {
81             try {
82                 synchronized (ConfiguredTest.class) {
83                     if (l == null) {
84                         l = DatabaseConnection.newLink(false);
85                     }
86                 }
87             } catch (InterruptedException e) {
88                 throw new Error(e);
89             }
90             return generateProps();
91         }
92         envInited = true;
93         try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
94             testProps.load(inStream);
95         }
96         Properties props = generateProps();
97         ServerConstants.init(props);
98         TimeConditions.init(props);
99         DomainAssessment.init(props);
100         PasswordHash.init(props);
101
102         if ( !DatabaseConnection.isInited()) {
103             DatabaseConnection.init(testProps);
104             try {
105                 synchronized (ConfiguredTest.class) {
106                     if (l == null) {
107                         l = DatabaseConnection.newLink(false);
108                     }
109                 }
110             } catch (InterruptedException e) {
111                 throw new Error(e);
112             }
113         }
114
115         return props;
116     }
117
118     @AfterClass
119     public static void closeDBLink() {
120         synchronized (ConfiguredTest.class) {
121             if (l != null) {
122                 l.close();
123                 l = null;
124             }
125         }
126     }
127
128     private static Properties generateProps() throws Error {
129         Properties mainProps = new Properties();
130         mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
131         mainProps.setProperty("name.www", testProps.getProperty("name.www"));
132         mainProps.setProperty("name.static", testProps.getProperty("name.static"));
133         mainProps.setProperty("name.api", testProps.getProperty("name.api"));
134         mainProps.setProperty("name.suffix", testProps.getProperty("name.suffix"));
135
136         mainProps.setProperty("appName", "SomeCA");
137         mainProps.setProperty("appIdentifier", "someca");
138
139         mainProps.setProperty("https.port", testProps.getProperty("serverPort.https"));
140         mainProps.setProperty("http.port", testProps.getProperty("serverPort.http"));
141
142         File out = new File("financial.dat");
143         if ( !out.exists()) {
144             try (FileOutputStream fos = new FileOutputStream(out)) {
145                 fos.write("google.com\ntwitter.com\n".getBytes("UTF-8"));
146             } catch (IOException e) {
147                 throw new Error(e);
148             }
149         }
150         mainProps.setProperty("highFinancialValue", out.getAbsolutePath());
151         mainProps.setProperty("scrypt.params", "1;1;1");
152         return mainProps;
153     }
154
155     public static KeyPair generateKeypair() throws GeneralSecurityException {
156         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
157         kpg.initialize(4096);
158         KeyPair keyPair = null;
159         File f = new File("testKeypair");
160         if (f.exists()) {
161             try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
162                 keyPair = (KeyPair) ois.readObject();
163             } catch (ClassNotFoundException e) {
164                 e.printStackTrace();
165             } catch (IOException e) {
166                 e.printStackTrace();
167             }
168         } else {
169             keyPair = kpg.generateKeyPair();
170             try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
171                 oos.writeObject(keyPair);
172                 oos.close();
173             } catch (IOException e) {
174                 e.printStackTrace();
175             }
176         }
177         return keyPair;
178     }
179
180     public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
181         return generatePEMCSR(kp, dn, new PKCS10Attributes());
182     }
183
184     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
185         return generatePEMCSR(kp, dn, atts, "SHA512WithRSA");
186     }
187
188     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
189         PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
190         Signature s = Signature.getInstance(signature);
191         s.initSign(kp.getPrivate());
192         p10.encodeAndSign(new X500Name(dn), s);
193         return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
194     }
195
196     static int count = 0;
197
198     public static String createRandomIDString() {
199         final char[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
200         final int idStringLength = 16;
201
202         Random sr;
203         sr = new Random();
204
205         StringBuilder sb = new StringBuilder(idStringLength);
206         for (int i = 0; i < idStringLength; i++) {
207             sb.append(chars[sr.nextInt(chars.length)]);
208         }
209
210         return sb.toString();
211     }
212
213     public static synchronized String createUniqueName() {
214         return "test" + createRandomIDString() + "a" + (count++) + "u";
215     }
216
217     public static CertificateProfile getClientProfile() {
218         return CertificateProfile.getByName("client");
219     }
220
221     public static int countRegex(String text, String pattern) {
222         Pattern p = Pattern.compile(pattern);
223         Matcher m = p.matcher(text);
224         int i = 0;
225         while (m.find()) {
226             i++;
227         }
228         return i;
229     }
230
231     public static void makeAgent(int uid) {
232         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=?, language='en_EN', version='1'")) {
233             ps1.setInt(1, uid);
234             ps1.setInt(2, CATSType.AGENT_CHALLENGE.getId());
235             ps1.execute();
236         }
237
238         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'")) {
239             ps2.setInt(1, uid);
240             ps2.setInt(2, User.getById(uid).getPreferredName().getId());
241             ps2.execute();
242         }
243     }
244
245     public MailReceiver getMailReceiver() {
246         throw new Error("Feature requires Business or ManagedTest.");
247     }
248
249     public void verify(Domain d) {
250         try {
251             d.addPing(DomainPingType.EMAIL, "admin");
252             TestMail testMail = getMailReceiver().receive();
253             testMail.verify();
254             assertTrue(d.isVerified());
255         } catch (GigiApiException e) {
256             throw new Error(e);
257         } catch (IOException e) {
258             throw new Error(e);
259         }
260     }
261
262     public static void purgeOnlyDB() throws SQLException, IOException {
263         System.out.println("... resetting Database");
264         long ms = System.currentTimeMillis();
265         try {
266             DatabaseManager.run(new String[] {
267                     testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
268             }, ImportType.TRUNCATE);
269         } catch (ClassNotFoundException e) {
270             e.printStackTrace();
271         }
272         System.out.println("Database reset complete in " + (System.currentTimeMillis() - ms) + " ms.");
273     }
274
275     public static String validVerificationDateString() {
276         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
277         Calendar c = Calendar.getInstance();
278         c.setTimeInMillis(System.currentTimeMillis());
279         c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
280         return sdf.format(new Date(c.getTimeInMillis()));
281     }
282 }