import java.io.BufferedReader;
import java.io.DataOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
-import java.security.GeneralSecurityException;
import java.security.KeyManagementException;
-import java.security.KeyPair;
-import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PrivateKey;
-import java.security.Signature;
import java.security.cert.X509Certificate;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Properties;
import org.cacert.gigi.DevelLauncher;
import org.cacert.gigi.GigiApiException;
import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.database.GigiResultSet;
import org.cacert.gigi.dbObjects.EmailAddress;
+import org.cacert.gigi.dbObjects.ObjectCache;
import org.cacert.gigi.dbObjects.User;
import org.cacert.gigi.localisation.Language;
import org.cacert.gigi.pages.account.MyDetails;
import org.cacert.gigi.pages.main.RegisterPage;
import org.cacert.gigi.testUtils.TestEmailReciever.TestMail;
import org.cacert.gigi.util.DatabaseManager;
-import org.cacert.gigi.util.PEM;
import org.cacert.gigi.util.ServerConstants;
import org.cacert.gigi.util.SimpleSigner;
+import org.cacert.gigi.util.DatabaseManager.ImportType;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
-import sun.security.pkcs10.PKCS10;
-import sun.security.pkcs10.PKCS10Attributes;
-import sun.security.x509.X500Name;
+public class ManagedTest extends ConfiguredTest {
-public class ManagedTest {
+ static {
+ System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
+ }
/**
* Some password that fullfills the password criteria.
return url;
}
- static Properties testProps = new Properties();
-
- public static Properties getTestProps() {
- return testProps;
- }
-
static {
InitTruststore.run();
HttpURLConnection.setFollowRedirects(false);
}
@BeforeClass
- public static void connectToServer() {
+ public static void initEnvironment() {
try {
- testProps.load(new FileInputStream("config/test.properties"));
- if ( !DatabaseConnection.isInited()) {
- DatabaseConnection.init(testProps);
- }
+ ConfiguredTest.initEnvironment();
+
purgeDatabase();
String type = testProps.getProperty("type");
Properties mainProps = generateMainProps();
try {
DatabaseManager.run(new String[] {
testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
- }, true);
+ }, ImportType.TRUNCATE);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(" in " + (System.currentTimeMillis() - ms) + " ms");
+ String type = testProps.getProperty("type");
+ ObjectCache.clearAllCaches();
+ if (type.equals("local")) {
+ URL u = new URL("https://" + getServerName() + "/manage");
+ u.openConnection().getHeaderField("Location");
+ }
}
private static Properties generateMainProps() {
String[] parts = verifyLink.split("\\?");
URL u = new URL("https://" + getServerName() + "/verify?" + parts[1]);
u.openStream().close();
- ;
- PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM users where email=?");
+
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM users where email=?");
ps.setString(1, email);
- ResultSet rs = ps.executeQuery();
+ GigiResultSet rs = ps.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
throw new Error(e);
} catch (IOException e) {
throw new Error(e);
- } catch (SQLException e) {
- throw new Error(e);
}
}
*/
public static int createAssuranceUser(String firstName, String lastName, String email, String password) {
int uid = createVerifiedUser(firstName, lastName, email, password);
- try {
- PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?");
- ps.setInt(1, uid);
- ps.setInt(2, 0);
- ps.execute();
- ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'");
- ps.setInt(1, uid);
- ps.setInt(2, uid);
- ps.execute();
-
- } catch (SQLException e) {
- throw new Error(e);
- }
+ GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?");
+ ps.setInt(1, uid);
+ ps.setInt(2, 0);
+ ps.execute();
+ ps = DatabaseConnection.getInstance().prepare("INSERT INTO `notary` SET `from`=?, `to`=?, points='100'");
+ ps.setInt(1, uid);
+ ps.setInt(2, uid);
+ ps.execute();
return uid;
}
- static int count = 0;
-
- public static String createUniqueName() {
- return "test" + System.currentTimeMillis() + "a" + (count++) + "u";
- }
-
private static String stripCookie(String headerField) {
return headerField.substring(0, headerField.indexOf(';'));
}
return m.group(1);
}
- public static KeyPair generateKeypair() throws GeneralSecurityException {
- KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
- kpg.initialize(4096);
- KeyPair keyPair = null;
- File f = new File("testKeypair");
- if (f.exists()) {
- try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
- keyPair = (KeyPair) ois.readObject();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- keyPair = kpg.generateKeyPair();
- try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
- oos.writeObject(keyPair);
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return keyPair;
- }
-
- public static String generatePEMCSR(KeyPair kp, String dn) throws GeneralSecurityException, IOException {
- return generatePEMCSR(kp, dn, new PKCS10Attributes());
- }
-
- public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts) throws GeneralSecurityException, IOException {
- return generatePEMCSR(kp, dn, atts, "SHA256WithRSA");
- }
-
- public static String generatePEMCSR(KeyPair kp, String dn, PKCS10Attributes atts, String signature) throws GeneralSecurityException, IOException {
- PKCS10 p10 = new PKCS10(kp.getPublic(), atts);
- Signature s = Signature.getInstance(signature);
- s.initSign(kp.getPrivate());
- p10.encodeAndSign(new X500Name(dn), s);
- return PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
- }
-
public static String executeBasicWebInteraction(String cookie, String path, String query) throws MalformedURLException, UnsupportedEncodingException, IOException {
return executeBasicWebInteraction(cookie, path, query, 0);
}
}
public static EmailAddress createVerifiedEmail(User u) throws InterruptedException, GigiApiException {
- EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
+ EmailAddress adrr = new EmailAddress(u, createUniqueName() + "test@test.tld");
adrr.insert(Language.getInstance(Locale.ENGLISH));
TestMail testMail = getMailReciever().recieve();
assertEquals(adrr.getAddress(), testMail.getTo());