Fixed bulletin-board server tests (use temporary db instead of assuming one exists at specific path)
							parent
							
								
									ad121a7bfd
								
							
						
					
					
						commit
						ed28e2eb1b
					
				|  | @ -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()); | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
|  | @ -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) { | ||||
| 			 | ||||
|  |  | |||
|  | @ -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") | ||||
|  |  | |||
|  | @ -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(); | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -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(){ | ||||
|  |  | |||
|  | @ -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. | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue