diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java index 6d344fe..032ea27 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/httpserver/BulletinBoardHttpServer.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard.httpserver; +import java.io.File; import java.io.IOException; import javax.servlet.ServletConfig; @@ -15,6 +16,8 @@ import meerkat.protobuf.BulletinBoardAPI.*; public class BulletinBoardHttpServer extends HttpServlet { + public final static File DEFAULT_MEERKAT_DB = new File("local-instances/meerkat.db"); + /** * Auto-generated UID. */ @@ -28,7 +31,7 @@ public class BulletinBoardHttpServer extends HttpServlet { bbs = new SQLiteBulletinBoardServer(); try { - bbs.init(); + bbs.init(DEFAULT_MEERKAT_DB); } catch (CommunicationException e) { // TODO Log error throw new ServletException("Servlet failed to initialize: " + e.getMessage()); diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java index 777d3bc..4cb8f38 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/BulletinBoardSQLServer.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard.sqlserver; +import java.io.File; import java.util.Arrays; import java.util.List; @@ -38,7 +39,7 @@ public abstract class BulletinBoardSQLServer implements BulletinBoardServer{ * 2. Call this procedure */ @Override - public void init() throws CommunicationException { + public void init(File meerkatDB) throws CommunicationException { // TODO write signature reading part. digest = new SHA256Digest(); diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java index dd86549..95f1948 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/SQLiteBulletinBoardServer.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard.sqlserver; +import java.io.File; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -40,14 +41,14 @@ public class SQLiteBulletinBoardServer extends BulletinBoardSQLServer { } @Override - public void init() throws CommunicationException { + public void init(File meerkatDB) throws CommunicationException { try{ - - connection = DriverManager.getConnection("jdbc:sqlite:local-instances/meerkat.db"); + String dbString = "jdbc:sqlite:" + meerkatDB.getPath(); + connection = DriverManager.getConnection(dbString); createSchema(); - super.init(); + super.init(meerkatDB); } catch (SQLException e) { diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java index ed162e1..1b8883a 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -18,16 +18,18 @@ import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessageList; import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; import meerkat.rest.Constants; +import java.io.File; + @Path("/sqlserver") -public class BulletinBoardWebApp implements BulletinBoardServer{ +public class BulletinBoardWebApp implements BulletinBoardServer { BulletinBoardServer bulletinBoard; @PostConstruct @Override - public void init() throws CommunicationException { + public void init(File meerkatDB) throws CommunicationException { bulletinBoard = new SQLiteBulletinBoardServer(); - bulletinBoard.init(); + bulletinBoard.init(meerkatDB); } @Path("postmessage") diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java index ff1e48f..e0801af 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardServerTest.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard; +import java.io.File; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; @@ -12,22 +13,11 @@ import meerkat.bulletinboard.sqlserver.SQLiteBulletinBoardServer; public class BulletinBoardServerTest { @Test - public void testAllServers(){ + public void testAllServers() throws Exception { GenericBulletinBoardServerTest bbst = new GenericBulletinBoardServerTest(); - try { - try{ - Path path = Paths.get("local-instances/meerkat.db"); - Files.delete(path); - } catch(NoSuchFileException e){} - - bbst.init(SQLiteBulletinBoardServer.class); - bbst.bulkTest(); - bbst.close(); - - } catch (Exception e) { - System.err.println(e.getMessage() + e.getClass()); - assert false; - } - + + bbst.init(SQLiteBulletinBoardServer.class); + bbst.bulkTest(); + bbst.close(); } } diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java index 8b51b65..36a998b 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/GenericBulletinBoardServerTest.java @@ -1,5 +1,6 @@ package meerkat.bulletinboard; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; @@ -12,6 +13,7 @@ import java.security.SignatureException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.List; +import java.util.Random; import com.google.protobuf.ByteString; @@ -30,7 +32,7 @@ public class GenericBulletinBoardServerTest { private BulletinBoardServer bulletinBoardServer; private ECDSASignature signers[]; - private SecureRandom random; + private Random random; private static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12"; private static String KEYFILE_PASSWORD = "secret"; @@ -41,7 +43,8 @@ public class GenericBulletinBoardServerTest { public void init(Class cls) throws InstantiationException, IllegalAccessException, CertificateException, KeyStoreException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, CommunicationException{ bulletinBoardServer = (BulletinBoardServer) cls.newInstance(); - bulletinBoardServer.init(); + + bulletinBoardServer.init(File.createTempFile("meerkat-test", "db")); signers = new ECDSASignature[2]; signers[0] = new ECDSASignature(); @@ -55,11 +58,11 @@ public class GenericBulletinBoardServerTest { signers[0].loadVerificationCertificates(getClass().getResourceAsStream(CERT1_PEM_EXAMPLE)); - random = new SecureRandom(); + random = new Random(0); // We use insecure randomness in tests for repeatability } private byte randomByte(){ - return (byte) ((Math.random() * 256) - 128); + return (byte) random.nextInt(); } private String randomString(){ diff --git a/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java b/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java index fe19d6e..298c290 100644 --- a/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java +++ b/meerkat-common/src/main/java/meerkat/bulletinboard/BulletinBoardServer.java @@ -3,6 +3,8 @@ package meerkat.bulletinboard; import meerkat.comm.CommunicationException; import meerkat.protobuf.BulletinBoardAPI.*; +import java.io.File; + /** * Created by Arbel on 07/11/15. * @@ -17,7 +19,7 @@ public interface BulletinBoardServer{ * It also establishes the connection to the DB. * @throws CommunicationException on DB connection error. */ - public void init() throws CommunicationException; + public void init(File meerkatDB) throws CommunicationException; /** * Post a message to bulletin board.