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