diff --git a/voter-registry/build.gradle b/voter-registry/build.gradle index 5ccac88..729465a 100644 --- a/voter-registry/build.gradle +++ b/voter-registry/build.gradle @@ -51,6 +51,9 @@ dependencies { // Google protobufs compile 'com.google.protobuf:protobuf-java:3.+' + // Depend on test resources from meerkat-common + testCompile project(path: ':meerkat-common', configuration: 'testOutput') + testCompile 'junit:junit:4.+' runtime 'org.codehaus.groovy:groovy:2.4.+' diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index d193523..40959c8 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -42,10 +42,11 @@ public class SimpleRegistry implements VoterRegistry{ BulletinBoardMessage.newBuilder(); signatory.updateContent(basicMessage); + Crypto.Signature signature = signatory.sign(); + bulletinBoardMessage.addSig(signature); bulletinBoardMessage.setMsg(basicMessage); - bulletinBoardMessage.addSig(signature); return bulletinBoardMessage.build(); } catch (SignatureException e) { diff --git a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java index cf594c4..f7f4ddc 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java @@ -28,9 +28,9 @@ public class VoterRegistryMessage { * @param tagName the name of the tag * @return string */ - public String GetWantedTagFromBasicMessage(RegistryTags tagName) { + public String GetWantedTagFromBasicMessage(String tagName) { for (String tag : base.getTagList()) { - if (tag.contains(tagName.toString())) { + if (tag.contains(tagName)) { return tag; } } diff --git a/voter-registry/src/test/java/DummyBulletinBoardCallBackHandler.java b/voter-registry/src/test/java/DummyBulletinBoardCallBackHandler.java deleted file mode 100644 index 5aa655d..0000000 --- a/voter-registry/src/test/java/DummyBulletinBoardCallBackHandler.java +++ /dev/null @@ -1,51 +0,0 @@ -import meerkat.bulletinboard.BulletinBoardClient; -import meerkat.protobuf.BulletinBoardAPI; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by Vladimir Eliezer Tokarev on 2/19/2016. - * Class that gives the ability of retrieve data from BulletinBoardClient synchronously - */ -public class DummyBulletinBoardCallBackHandler implements - BulletinBoardClient.ClientCallback> { - private List messages; - - public DummyBulletinBoardCallBackHandler() - { - messages = new ArrayList<>(); - } - - /** - * Sets the received messages to be messages - * @param msg List passed by BulletinBoardClient - */ - @Override - public void handleCallback(List msg) { - messages = msg; - } - - /** - * Sets the messages to be null because during the BulletinBoardAction was an error - * @param t the information of the excretion thrown during the process - */ - @Override - public void handleFailure(Throwable t) { - messages = null; - } - - /** - * Method that waits that the messages will arrive from bulletin board client - * @return List - */ - public List getMessages(){ - while(messages.isEmpty()){ - if (messages == null) - { - return null; - } - } - return messages; - } -} diff --git a/voter-registry/src/test/java/DummyRegistryCallBackHandler.java b/voter-registry/src/test/java/DummyRegistryCallBackHandler.java deleted file mode 100644 index 66daf8d..0000000 --- a/voter-registry/src/test/java/DummyRegistryCallBackHandler.java +++ /dev/null @@ -1,32 +0,0 @@ -import meerkat.VoterRegistry; - -/** - * Created by Vladimir Eliezer Tokarev on 2/19/2016. - * Object that gives the ability of synchronously access to SimpleVoterRegistry returned values - */ -public class DummyRegistryCallBackHandler implements VoterRegistry.RegistryCallBack{ - /** - * The amount of times HandleResult had been called - */ - public int counter; - - /** - * The data the VoterRegistry have returned - */ - public T data; - - public DummyRegistryCallBackHandler() - { - counter=0; - } - - /** - * Sets the retrieved data into data field and increases the counter - * @param result the answer VoterRegistry passed - */ - @Override - public void HandleResult(T result) { - counter++; - data = result; - } -} diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index e95e301..2150c82 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -2,21 +2,31 @@ import com.google.protobuf.InvalidProtocolBufferException; import junit.framework.TestCase; import meerkat.RegistryMessages; import meerkat.SimpleRegistry; +import meerkat.VoterRegistry; import meerkat.VoterRegistryMessage; import meerkat.bulletinboard.BulletinBoardClient; -import meerkat.bulletinboard.SimpleBulletinBoardClient; +import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.crypto.concrete.ECDSASignature; import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.Voting; import util.RegistryTags; +import java.io.InputStream; import java.math.BigInteger; +import java.security.KeyStore; import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Semaphore; import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; +/** + * TODO: Have to do more relevant prints (print what happens) + * TODO: repair all the other methods + */ + + /** * Created by Vladimir Eliezer Tokarev on 1/16/2016. * Tests the Simple Registry contents @@ -25,28 +35,86 @@ import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; */ public class SimpleRegistryTest extends TestCase { - private ECDSASignature signatory; + private ECDSASignature signer; private BulletinBoardClient communicator; - private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; private SecureRandom random = new SecureRandom(); + public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12"; + public static String KEYFILE_PASSWORD = "secret"; + Semaphore jobSemaphore; + + class DummyRegistryCallBackHandler implements VoterRegistry.RegistryCallBack{ + public int counter; + public T data; + + public DummyRegistryCallBackHandler() + { + counter=0; + } + + @Override + public void HandleResult(T result) { + counter++; + data = result; + jobSemaphore.release(); + } + } + + public class DummyBulletinBoardCallBackHandler implements BulletinBoardClient.ClientCallback> { + public List messages; + + @Override + public void handleCallback(List msg) + { + messages = msg; + jobSemaphore.release(); + } + + @Override + public void handleFailure(Throwable t) throws Throwable { + messages = null; + jobSemaphore.release(); + throw t; + } + } + /** * Creates the communication object (the bulletinBoardClient) */ private void CommunicatorSetup() { - communicator = new SimpleBulletinBoardClient(); + communicator = new ThreadedBulletinBoardClient(); + String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; communicator.init(Voting.BulletinBoardClientParams.newBuilder() .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) .setMinRedundancy((float) 1.0) .build()); } + /** + * Creates the signer object which is the ECDSASignature + */ + private void SetSigner(){ + try { + signer = new ECDSASignature(); + InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE); + char[] password = KEYFILE_PASSWORD.toCharArray(); + + KeyStore.Builder keyStore = signer.getPKCS12KeyStoreBuilder(keyStream, password); + signer.loadSigningCertificate(keyStore); + keyStream.close(); + } + catch (Exception e){ + assert false : "The signer creation failed "; + } + } + /** * Initialize SimpleRegistry object */ public void setUp() { - signatory = new ECDSASignature(); + SetSigner(); CommunicatorSetup(); + jobSemaphore = new Semaphore(0); } /** @@ -54,95 +122,124 @@ public class SimpleRegistryTest extends TestCase { */ public void testSimpleRegistryCreation() { try { - new SimpleRegistry(signatory, communicator); + new SimpleRegistry(signer, communicator); } catch (Exception e) { assert false : "While creating the SimpleRegistry exception have been thrown " + e; } } + /** + * Counts the amount of messages from messages that have all the wanted tags inside + * @param messages List + * @param tags List + * @return integer that represent the amount of messages with wanted tags + */ + private int countMessagesWithTags(List messages, List tags) + { + int counter = 0 ; + + for (VoterRegistryMessage message :messages) { + int wantedTagsCounter = 0; + + for (String tag : tags) { + if(message.GetWantedTagFromBasicMessage(tag)!=null){ + wantedTagsCounter++; + } + } + + if(wantedTagsCounter == tags.size()) + { + counter++; + } + } + return counter; + } + + /** * Test that add voter creates new correct bulletin board message and adds the voter */ - public void testAddVoter() throws InvalidProtocolBufferException { - DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); + public void testAddVoter() throws InvalidProtocolBufferException, InterruptedException { + DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). setId(RegistryMessages.VoterID.newBuilder() - .setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32))) + .setId(new BigInteger(130, random).toString(32))) .setInfo(new BigInteger(130, random).toString(32)).build(); - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddVoter(voterInfo, handler); - assertEquals(handler.counter , 0); - - List tags = new ArrayList<>(); - tags.add("AddVoter"); + jobSemaphore.acquire(); + assertEquals(handler.counter , 1); + List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); - List messages = bulletinHandler.getMessages(); + jobSemaphore.acquire(); - assert messages.contains(voterInfo) : "The server don't have the new user data."; + int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); + assert counter == 1 : "The server don't have the new user data."; } /** * Test that set voted posts creates correct bulletin board message and sets that the user have been voted */ - public void testSetVoted() throws InvalidProtocolBufferException { + public void testSetVoted() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder() - .setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)).build(); + .setId(new BigInteger(130, random).toString(32)).build(); - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.SetVoted(voterInfo, handler); + jobSemaphore.acquire(); + assertEquals(handler.counter , 0); - List tags = new ArrayList<>(); - tags.add("SetVoted"); - + List tags = new ArrayList(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); - List messages = bulletinHandler.getMessages(); + jobSemaphore.acquire(); - assert messages.contains(voterInfo) : "The server don't have the new user id."; + int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); + assert counter == 1 : "The server don't have the new user id."; } - - /** * Test that get groups retrieves the right groups the user are in */ - public void testAddToGroup() throws InvalidProtocolBufferException { + public void testAddToGroup() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder() .setVoterId(RegistryMessages.VoterID.newBuilder() - .setId((RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)))) + .setId((new BigInteger(130, random).toString(32)))) .setGroupId(RegistryMessages.GroupID.newBuilder() - .setId((RegistryTags.GROUP_ID_TAG + new BigInteger(130, random).toString(32)))).build(); + .setId((new BigInteger(130, random).toString(32)))).build(); - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddToGroup(voterInfo, handler); + jobSemaphore.acquire(); + assertEquals(handler.counter , 0); - List tags = new ArrayList<>(); - tags.add("AddToGroup"); - + List tags = new ArrayList(){{ add(RegistryTags.GROUP_ACTION_TAG .toString() + + RegistryTags.ADD_TO_GROUP_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); - List messages = bulletinHandler.getMessages(); + jobSemaphore.acquire(); - assert messages.contains(voterInfo) : "The server don't have the new user added to group."; + int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); + assert counter == 1 : "The server don't have the new user added to group."; } /** @@ -154,27 +251,27 @@ public class SimpleRegistryTest extends TestCase { RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder() .setVoterId(RegistryMessages.VoterID.newBuilder() - .setId((RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)))) + .setId((new BigInteger(130, random).toString(32)))) .setGroupId(RegistryMessages.GroupID.newBuilder() - .setId((RegistryTags.GROUP_ID_TAG + new BigInteger(130, random).toString(32)))).build(); + .setId((new BigInteger(130, random).toString(32)))).build(); - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddToGroup(voterInfo, handler); + jobSemaphore.acquire(); + assertEquals(handler.counter , 0); - List tags = new ArrayList<>(); - tags.add("AddToGroup"); - + List tags = new ArrayList(){{ add(RegistryTags.GROUP_ACTION_TAG .toString() + + RegistryTags.ADD_TO_GROUP_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); - List messages = bulletinHandler.getMessages(); + jobSemaphore.acquire(); - assert messages.contains(voterInfo) : "The server don't have the new user added to group."; List voterMessages = ConvertToVoterRegistryMessages(handler.data); - assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG).contains(voterInfo.getVoterId().getId())); + assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG.toString()).contains(voterInfo.getVoterId().getId())); } @@ -183,29 +280,31 @@ public class SimpleRegistryTest extends TestCase { * Test that the personal data outputted about the user is right */ public void testGetPersonalIDDetails() throws InvalidProtocolBufferException, InterruptedException { - DummyRegistryCallBackHandler> handler = new DummyRegistryCallBackHandler<>(); + DummyRegistryCallBackHandler> handler = + new DummyRegistryCallBackHandler<>(); + + String id = new BigInteger(130, random).toString(32); + String data = new BigInteger(130, random).toString(32); RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). - setId(RegistryMessages.VoterID.newBuilder() - .setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32))) - .setInfo(new BigInteger(130, random).toString(32)).build(); + setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddVoter(voterInfo, handler); + jobSemaphore.acquire(); + assertEquals(handler.counter , 0); - List tags = new ArrayList<>(); - tags.add("AddVoter"); - + List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); - List messages = bulletinHandler.getMessages(); + jobSemaphore.acquire(); - assert messages.contains(voterInfo) : "The server don't have the new user data."; List voterMessages = ConvertToVoterRegistryMessages(handler.data); - assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG).contains(voterInfo.getId().getId())); + int counter = countMessagesWithTags(voterMessages, new ArrayList(){{add(id);add(data);}}); + assert counter == 1 : "The method doesnt retreived the wanted information."; } }