]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/testUtils/ConfiguredTest.java
fix: typo in receive
[gigi.git] / tests / org / cacert / gigi / testUtils / ConfiguredTest.java
1 package org.cacert.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.util.Properties;
17 import java.util.TimeZone;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.cacert.gigi.GigiApiException;
22 import org.cacert.gigi.database.DatabaseConnection;
23 import org.cacert.gigi.database.DatabaseConnection.Link;
24 import org.cacert.gigi.database.GigiPreparedStatement;
25 import org.cacert.gigi.database.SQLFileManager.ImportType;
26 import org.cacert.gigi.dbObjects.CATS.CATSType;
27 import org.cacert.gigi.dbObjects.Domain;
28 import org.cacert.gigi.dbObjects.DomainPingType;
29 import org.cacert.gigi.testUtils.TestEmailReceiver.TestMail;
30 import org.cacert.gigi.util.DatabaseManager;
31 import org.cacert.gigi.util.DomainAssessment;
32 import org.cacert.gigi.util.PEM;
33 import org.cacert.gigi.util.ServerConstants;
34 import org.junit.BeforeClass;
35
36 import sun.security.pkcs10.PKCS10;
37 import sun.security.pkcs10.PKCS10Attributes;
38 import sun.security.x509.X500Name;
39
40 /**
41  * Base class for a Testsuite that makes use of the config variables that define
42  * the environment.
43  */
44 public abstract class ConfiguredTest {
45
46     static Properties testProps = new Properties();
47
48     public static Properties getTestProps() {
49         return testProps;
50     }
51
52     private static boolean envInited = false;
53
54     /**
55      * Some password that fulfills the password criteria.
56      */
57     public static final String TEST_PASSWORD = "xvXV12°§";
58
59     public static final String DIFFICULT_CHARS = "ÜÖÄß𐀀";
60
61     @BeforeClass
62     public static void initEnvironmentHook() throws IOException {
63         initEnvironment();
64     }
65
66     public static Properties initEnvironment() throws IOException {
67         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
68         if (envInited) {
69             return generateProps();
70         }
71         envInited = true;
72         try (FileInputStream inStream = new FileInputStream("config/test.properties")) {
73             testProps.load(inStream);
74         }
75         Properties props = generateProps();
76         ServerConstants.init(props);
77         DomainAssessment.init(props);
78
79         if ( !DatabaseConnection.isInited()) {
80             DatabaseConnection.init(testProps);
81             try {
82                 l = DatabaseConnection.newLink(false);
83             } catch (InterruptedException e) {
84                 throw new Error(e);
85             }
86         }
87         return props;
88
89     }
90
91     private static Properties generateProps() throws Error {
92         Properties mainProps = new Properties();
93         mainProps.setProperty("name.secure", testProps.getProperty("name.secure"));
94         mainProps.setProperty("name.www", testProps.getProperty("name.www"));
95         mainProps.setProperty("name.static", testProps.getProperty("name.static"));
96         mainProps.setProperty("name.api", testProps.getProperty("name.api"));
97
98         mainProps.setProperty("https.port", testProps.getProperty("serverPort.https"));
99         mainProps.setProperty("http.port", testProps.getProperty("serverPort.http"));
100
101         File out = new File("financial.dat");
102         if ( !out.exists()) {
103             try (FileOutputStream fos = new FileOutputStream(out)) {
104                 fos.write("google.com\ntwitter.com\n".getBytes("UTF-8"));
105             } catch (IOException e) {
106                 throw new Error(e);
107             }
108         }
109         mainProps.setProperty("highFinancialValue", out.getAbsolutePath());
110         return mainProps;
111     }
112
113     public static KeyPair generateKeypair() throws GeneralSecurityException {
114         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
115         kpg.initialize(4096);
116         KeyPair keyPair = null;
117         File f = new File("testKeypair");
118         if (f.exists()) {
119             try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
120                 keyPair = (KeyPair) ois.readObject();
121             } catch (ClassNotFoundException e) {
122                 e.printStackTrace();
123             } catch (IOException e) {
124                 e.printStackTrace();
125             }
126         } else {
127             keyPair = kpg.generateKeyPair();
128             try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
129                 oos.writeObject(keyPair);
130                 oos.close();
131             } catch (IOException e) {
132                 e.printStackTrace();
133             }
134         }
135         return keyPair;
136     }
137
138     public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
139         return generatePEMCSR(kp, dn, new PKCS10Attributes());
140     }
141
142     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
143         return generatePEMCSR(kp, dn, atts, "SHA256WithRSA");
144     }
145
146     public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
147         PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
148         Signature s = Signature.getInstance(signature);
149         s.initSign(kp.getPrivate());
150         p10.encodeAndSign(new X500Name(dn), s);
151         return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
152     }
153
154     static int count = 0;
155
156     private static Link l;
157
158     public static String createUniqueName() {
159         return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
160     }
161
162     public static int countRegex(String text, String pattern) {
163         Pattern p = Pattern.compile(pattern);
164         Matcher m = p.matcher(text);
165         int i = 0;
166         while (m.find()) {
167             i++;
168         }
169         return i;
170     }
171
172     public static void makeAssurer(int uid) {
173         try (GigiPreparedStatement ps1 = new GigiPreparedStatement("INSERT INTO cats_passed SET user_id=?, variant_id=?, language='en_EN', version=1")) {
174             ps1.setInt(1, uid);
175             ps1.setInt(2, CATSType.ASSURER_CHALLENGE.getId());
176             ps1.execute();
177         }
178
179         try (GigiPreparedStatement ps2 = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'")) {
180             ps2.setInt(1, uid);
181             ps2.setInt(2, uid);
182             ps2.execute();
183         }
184     }
185
186     public MailReceiver getMailReceiver() {
187         throw new Error("Feature requires Business or ManagedTest.");
188     }
189
190     public void verify(Domain d) {
191         try {
192             d.addPing(DomainPingType.EMAIL, "admin");
193             TestMail testMail = getMailReceiver().receive();
194             testMail.verify();
195             assertTrue(d.isVerified());
196         } catch (GigiApiException e) {
197             throw new Error(e);
198         } catch (IOException e) {
199             throw new Error(e);
200         }
201     }
202
203     public static void purgeOnlyDB() throws SQLException, IOException {
204         System.out.print("... resetting Database");
205         long ms = System.currentTimeMillis();
206         try {
207             DatabaseManager.run(new String[] {
208                     testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
209             }, ImportType.TRUNCATE);
210         } catch (ClassNotFoundException e) {
211             e.printStackTrace();
212         }
213         System.out.println(" in " + (System.currentTimeMillis() - ms) + " ms");
214     }
215 }