From 003720839cf43a378956dd3af443c479d0d8be96 Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Sat, 30 Jan 2016 02:30:12 -0800 Subject: [PATCH] Added testing to the SimpleRegistryTest class The Test checking that SetVoter AddVoter AddToGroup RemoveFromGroup methods working. --- .../src/main/java/meerkat/SimpleRegistry.java | 35 +-- .../java/meerkat/VoterRegistryMessage.java | 6 +- .../src/main/java/util/RegistryTags.java | 2 +- .../util/SimpleRegistryCallBackHandler.java | 2 +- .../src/test/java/SimpleRegistryTest.java | 224 ++++++++++++++---- 5 files changed, 199 insertions(+), 70 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index e979e8d..9f65967 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -6,6 +6,7 @@ import meerkat.bulletinboard.SimpleBulletinBoardClient; import meerkat.comm.CommunicationException; import meerkat.crypto.Encryption; +import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.MessageFilter; import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; @@ -77,7 +78,7 @@ public class SimpleRegistry implements RegistryInstance{ for (String tag : tags) { MessageFilter.Builder idFilter = - MessageFilter.newBuilder().setTag(tag); + MessageFilter.newBuilder().setTag(tag).setType(BulletinBoardAPI.FilterType.TAG); filters.addFilter(idFilter); } @@ -115,9 +116,9 @@ public class SimpleRegistry implements RegistryInstance{ public void AddVoter(String voterID, String personalData, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder(). - addTag(RegistryTags.ID_TAG + " " + voterID) + addTag(RegistryTags.ID_TAG + voterID) .addTag(RegistryTags.VOTER_ENTRY_TAG.toString()) - .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString()); + .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); basicMessage.setData(ByteString.copyFrom(personalData.getBytes())); @@ -127,10 +128,10 @@ public class SimpleRegistry implements RegistryInstance{ public void AddToGroup(String voterID, String groupID, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder() - .addTag(RegistryTags.ID_TAG + " " + voterID) - .addTag(RegistryTags.GROUP_ID_TAG + " " + groupID) - .addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG) - .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString()); + .addTag(RegistryTags.ID_TAG + voterID) + .addTag(RegistryTags.GROUP_ID_TAG + groupID) + .addTag(RegistryTags.GROUP_ACTION_TAG .toString() + RegistryTags.ADD_TO_GROUP_TAG) + .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); callback.HandleVoterAddedToGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build()))); } @@ -138,10 +139,10 @@ public class SimpleRegistry implements RegistryInstance{ public void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder() - .addTag(RegistryTags.ID_TAG + " " + voterID) - .addTag(RegistryTags.GROUP_ID_TAG + " " + groupID) - .addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG) - .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString()); + .addTag(RegistryTags.ID_TAG + voterID) + .addTag(RegistryTags.GROUP_ID_TAG + groupID) + .addTag(RegistryTags.GROUP_ACTION_TAG .toString()+ RegistryTags.REMOVE_FROM_GROUP_TAG) + .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); callback.HandleVoterRemovedFromGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build()))); } @@ -149,9 +150,9 @@ public class SimpleRegistry implements RegistryInstance{ public void SetVoted(String id, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder() - .addTag(RegistryTags.ID_TAG + " " + id) + .addTag(RegistryTags.ID_TAG + id) .addTag(RegistryTags.VOTE_ACTION_TAG.toString()) - .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString()); + .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString()); callback.HandleVoterAddedToGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build()))); } @@ -159,9 +160,9 @@ public class SimpleRegistry implements RegistryInstance{ public void GetGroups(String id, RegistryCallBack callback) { try { List GroupsActionsTags = new ArrayList() {{ - add(RegistryTags.ID_TAG + " " + id); - add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG); - add(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG); + add(RegistryTags.ID_TAG + id); + add(RegistryTags.GROUP_ACTION_TAG .toString() + RegistryTags.REMOVE_FROM_GROUP_TAG); + add(RegistryTags.GROUP_ACTION_TAG.toString() + RegistryTags.ADD_TO_GROUP_TAG); }}; List voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags); @@ -176,7 +177,7 @@ public class SimpleRegistry implements RegistryInstance{ public void GetPersonIDDetails(String id, RegistryCallBack callback) { try { List GroupsActionsTags = new ArrayList() {{ - add(RegistryTags.ID_TAG + " " + id); + add(RegistryTags.ID_TAG + id); add(RegistryTags.VOTER_ENTRY_TAG.toString()); }}; List voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags); diff --git a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java index a67bfee..b93cc73 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistryMessage.java @@ -17,7 +17,9 @@ public class VoterRegistryMessage { public BulletinBoardAPI.UnsignedBulletinBoardMessage base; public VoterRegistryMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message) { - base = BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getTagList()).build(); + BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder unsignedBase = BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getTagList()); + unsignedBase.setData(message.getData()); + base = unsignedBase.build(); } /** @@ -72,7 +74,7 @@ public class VoterRegistryMessage { * * @return List of strings */ - public List tagsToStringList(){ + public List tagsToStringList() { return base.getTagList(); } } diff --git a/voter-registry/src/main/java/util/RegistryTags.java b/voter-registry/src/main/java/util/RegistryTags.java index 1769ee0..a5406c7 100644 --- a/voter-registry/src/main/java/util/RegistryTags.java +++ b/voter-registry/src/main/java/util/RegistryTags.java @@ -8,7 +8,7 @@ public enum RegistryTags { ID_TAG("ID:"), VOTER_ENTRY_TAG("Voter Entry"), - GROUP_ID_TAG("Group ID:"), + GROUP_ID_TAG("GroupID:"), GROUP_ACTION_TAG("Group Action:"), REMOVE_FROM_GROUP_TAG("Remove From Group"), ADD_TO_GROUP_TAG("Add To Group"), diff --git a/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java b/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java index 2013108..cd78cd1 100644 --- a/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java +++ b/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java @@ -18,7 +18,7 @@ public class SimpleRegistryCallBackHandler implements RegistryCallBack { */ public List WantedInformation; - public static int counter = 0; + public int counter = 0; @Override public void HandleVoterAdded(boolean succeeded) { diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 83902d0..868b57b 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -1,9 +1,12 @@ +import com.google.protobuf.InvalidProtocolBufferException; import junit.framework.TestCase; +import meerkat.RegistryCallBack; import meerkat.SimpleRegistry; +import meerkat.VoterRegistryMessage; import meerkat.bulletinboard.SimpleBulletinBoardClient; import meerkat.crypto.concrete.ECElGamalEncryption; import meerkat.crypto.concrete.ECElGamalUtils; -import meerkat.protobuf.BulletinBoardAPI.*; +import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.ConcreteCrypto; import meerkat.protobuf.Voting; import org.factcenter.qilin.primitives.concrete.ECElGamal; @@ -11,29 +14,33 @@ import org.factcenter.qilin.primitives.concrete.ECGroup; import util.RegistryTags; import util.SimpleRegistryCallBackHandler; +import java.lang.reflect.InvocationTargetException; import java.math.BigInteger; +import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.util.ArrayList; import java.util.List; -import java.util.Random; + +import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; +import static util.CollectionMessagesUtils.GetUnsignedBulletinBoardMessages; /** * Created by Vladimir Eliezer Tokarev on 1/16/2016. * Tests the Simple Registry contents - * * NOTE: for most of this tests to pass there should run BulletinBoardServer - * and the sql server and it should be reachable on - * BULLETIN_BOARD_SERVER_ADDRESS location + * that should be reachable on BULLETIN_BOARD_SERVER_ADDRESS */ -public class SimpleRegistryTest extends TestCase{ +public class SimpleRegistryTest extends TestCase { private ECElGamalEncryption signatory; private SimpleBulletinBoardClient communicator; - private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:3306"; - private Random random = new Random(0); // Insecure deterministic random for testing. - private SimpleRegistryCallBackHandler handler; + private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; + private SecureRandom random = new SecureRandom(); - private void ElGamalSetup(){ + /** + * Creates the ElGamal encryption object + */ + private void ElGamalSetup() { try { ECGroup group = new ECGroup("secp256k1"); BigInteger sk = ECElGamal.generateSecretKey(group, random); @@ -42,11 +49,14 @@ public class SimpleRegistryTest extends TestCase{ signatory = new ECElGamalEncryption(); signatory.init(serializedPk); } catch (InvalidKeySpecException e) { - assertTrue(false); + assertTrue(false); } } - private void CommunicatorSetup(){ + /** + * Creates the communication object (the bulletinBoardClient) + */ + private void CommunicatorSetup() { communicator = new SimpleBulletinBoardClient(); communicator.init(Voting.BulletinBoardClientParams.newBuilder() .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) @@ -54,95 +64,211 @@ public class SimpleRegistryTest extends TestCase{ .build()); } - private void RegistryAnswersHandlerSetup(){ - handler = new SimpleRegistryCallBackHandler(); + + private SimpleRegistryCallBackHandler RegistryAnswersHandlerSetup() { + return new SimpleRegistryCallBackHandler(); } - /** + /** * Initialize SimpleRegistry object */ - public void setUp(){ + public void setUp() { ElGamalSetup(); CommunicatorSetup(); - RegistryAnswersHandlerSetup(); } /** * Checks if the creation of the registry have been successful */ - public void testSimpleRegistryCreation(){ + public void testSimpleRegistryCreation() { try { - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); - }catch (Exception e){ + new SimpleRegistry(signatory, communicator); + } catch (Exception e) { assert false : "While creating the SimpleRegistry exception have been thrown " + e; } } /** - * Test that add voter creates new correct bulletin board message and adds the voter + * Creates list of lists of strings for different testing purposes + * @param actionDataNumber number of string in a list + * @param numberOfInstances number of lists in a list + * @return List> */ - public void testAddVoter(){ - List ids = new ArrayList<>(); + private List> getActionsData(int actionDataNumber, int numberOfInstances) + { + System.out.println("- Creating "+ numberOfInstances +" ids and personal data strings for adding different voters."); - for (int i = 0 ; i < 10 ; i++ ){ - ids.add("" + (10000000 + random.nextInt(90000000))); + List> actionData = new ArrayList<>(); + for (int i = 0 ; i < numberOfInstances ; i++ ){ + List data = new ArrayList<>(); + for (int j = 0 ; j < actionDataNumber ; j++){ + data.add(new BigInteger(130, random).toString(32)); + } + actionData.add(data); } - SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + return actionData; + } - // check that the callbacks have been called and the this been successful - for (String id : ids) { - registry.AddVoter(id, "some personal info", handler); - assertTrue(handler.ActionSucceed); + /** + * Create Message filter list from list of tags + * @param tags list of string with wanted tags + * @return MessageFilterList + */ + private BulletinBoardAPI.MessageFilterList createFiltersFromTags(List tags){ + if (tags == null){ + return BulletinBoardAPI.MessageFilterList.newBuilder().build(); } - // check that the callbacks have been called exactly 10 times - assertEquals(10, SimpleRegistryCallBackHandler.counter); + BulletinBoardAPI.MessageFilterList.Builder filters = BulletinBoardAPI.MessageFilterList.newBuilder(); + for (String tag : tags) { + filters.addFilter(BulletinBoardAPI.MessageFilter.newBuilder() + .setTag(tag).setType(BulletinBoardAPI.FilterType.TAG)); + } - // check that the server have the new voters data - for (String id : ids){ - MessageFilterList.Builder filters = MessageFilterList.newBuilder() - .addFilter(MessageFilter.newBuilder().setTag(RegistryTags.ID_TAG + " " + id)) - .addFilter(MessageFilter.newBuilder().setTag(RegistryTags.VOTER_ENTRY_TAG.toString())); + return filters.build(); + } - List messages = communicator.readMessages(filters.build()); - assertTrue(messages != null); + /** + * Counts the amount of posted messaages that have their tags in voterData + * @param votersData List> of information about voters (tags etc...) + * @param tags List of tags + * @return int + * @throws InvalidProtocolBufferException + */ + private int countActualAddedMessages(List> votersData, List tags) throws InvalidProtocolBufferException { + System.out.println("- Check that the server have the new voters data:"); + + BulletinBoardAPI.MessageFilterList filters = createFiltersFromTags(tags); + List messages = + ConvertToVoterRegistryMessages(GetUnsignedBulletinBoardMessages(communicator.readMessages(filters))); + + System.out.println(messages.size() + " asdasasdasdasasasdaasddas"); + + int addedMessagesCounter = 0; + + for (List voterData : votersData) { + for (VoterRegistryMessage message : messages) { + String addedVoterId = message.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG); + String addedVoterData = new String(message.base.getData().toByteArray()); + + if (voterData.size() == 2 && addedVoterId.contains(voterData.get(0)) && addedVoterData.contains(voterData.get(1))) { + addedMessagesCounter++; + } + else if (addedVoterId.contains(voterData.get(0)) ){ + addedMessagesCounter++; + } + } + } + return addedMessagesCounter; + } + + /** + * Call methodToCheck voterData as list of params + * @param methodToCheck the name of the wanted method + * @param numberOfParamsWithoutHandler the amount of params to pass to the wanted method + * @param handler object which method will be called as callback from SimpleRegistry + * @param registry the simpleRegistry object + * @param voterData List of string which is the params to pass to the wantedMethod + */ + private void CallWantedMethod(String methodToCheck, int numberOfParamsWithoutHandler, + SimpleRegistryCallBackHandler handler, SimpleRegistry registry, + List voterData) { + try { + if (numberOfParamsWithoutHandler == 1) + { + java.lang.reflect.Method method = + registry.getClass().getMethod(methodToCheck,String.class, RegistryCallBack.class); + method.invoke(registry, voterData.get(0), handler); + assertTrue(handler.ActionSucceed); + }else { + java.lang.reflect.Method method = + registry.getClass().getMethod( methodToCheck, String.class, String.class, RegistryCallBack.class); + method.invoke(registry, voterData.get(0), voterData.get(1), handler); + assertTrue(handler.ActionSucceed); + } + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); } } + /** + * Creates new Registry information then sends it to the server then checks the server + * have all sent data + * @param methodToCheck the name of the method we want to call in SimpleRegistry + * @param numberOfParamsToPass the number of params to pass to the methodToCheck + * @param numberOfChecks the amount of checks to run + * @param tags list of String that represent the tags to filter on the messages + * @throws InvalidProtocolBufferException + */ + private void checkMessagesPostedSuccessfully(String methodToCheck, int numberOfParamsToPass, int numberOfChecks, List tags) throws InvalidProtocolBufferException { + SimpleRegistryCallBackHandler handler = RegistryAnswersHandlerSetup(); + System.out.println("Starting testing the " + methodToCheck + " capability."); + List> votersData = getActionsData(numberOfParamsToPass, numberOfChecks); + + SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + + System.out.println("- Check that all the callbacks have been called successfully after voters adding:"); + for (List voterData : votersData) { + CallWantedMethod(methodToCheck, numberOfParamsToPass, handler, registry, voterData); + } + + System.out.println("- Check that for every voter added, the callback have been called:"); + assertEquals(numberOfChecks, handler.counter); + int addedMessagesCounter = countActualAddedMessages(votersData, tags); + + assert addedMessagesCounter == numberOfChecks : "The number of added messages actually is " + addedMessagesCounter; + System.out.println("Ended addVoter testing."); + } + + /** + * Test that add voter creates new correct bulletin board message and adds the voter + */ + public void testAddVoter() throws InvalidProtocolBufferException { + checkMessagesPostedSuccessfully("AddVoter", 2, 3, + new ArrayList(){{add(RegistryTags.VOTER_ENTRY_TAG.toString());}}); + } /** * Test that set voted posts creates correct bulletin board message and sets that the user have been voted */ - public void testSetVoted(){ - assertEquals(null, null); + public void testSetVoted() throws InvalidProtocolBufferException { + checkMessagesPostedSuccessfully("SetVoted", 1, 3, + new ArrayList(){{add(RegistryTags.VOTE_ACTION_TAG.toString());}}); } /** * Test that add to group creates correct bulletin board message and adds a user to a group */ - public void testAddToGroup(){ - assertEquals(null, null); + public void testRemoveFromGroup() throws InvalidProtocolBufferException { + checkMessagesPostedSuccessfully("RemoveFromGroup", 2, 3, null); } /** * Test that remove from group creates correct bulletin board message and removes the user from a group */ - public void testRemoveFromGroup(){ - assertEquals(null, null); + public void testGetGroups() { + // print start of gorups testing + // Create Registry check creation + // Add to X groups remove y group and z group + // for every action check that it happened + // count the number of callback activated + // get the relevant groups + // check they are right + // print and of groups } /** * Test that get groups retrieves the right groups the user are in */ - public void testGetGroups(){ - assertEquals(null, null); + public void testAddToGroup() throws InvalidProtocolBufferException { + checkMessagesPostedSuccessfully("AddToGroup", 2, 3, null); } /** * Test that the personal data outputted about the user is right */ - public void testGetPersonalIDDetails(){ - assertEquals(null, null); + public void testGetPersonalIDDetails() { + assertEquals(1, null); } }