From 6020ed3ab8d5ca07b07da3ab9c7d648b372d47f2 Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Sat, 23 Jan 2016 09:16:57 -0800 Subject: [PATCH] Changed the SimpleRegistry The new Simple Registry works according to the RegistryInstance interface --- .gitignore | 2 + voter-registry/.gitignore | 7 +- .../src/main/java/meerkat/SimpleRegistry.java | 60 +++++----- .../util/SimpleRegistryCallBackHandler.java | 60 ++++++++++ .../src/test/java/SimpleRegistryTest.java | 107 ++++++++++++++++-- 5 files changed, 199 insertions(+), 37 deletions(-) create mode 100644 voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java diff --git a/.gitignore b/.gitignore index 6339218..c0cd830 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ out *.project *.classpath bulletin-board-server/local-instances/meerkat.db +/SQLiteDBTest.db +/bulletin-board-server/SQLiteDBTest.db diff --git a/voter-registry/.gitignore b/voter-registry/.gitignore index a3f1a26..2df19de 100644 --- a/voter-registry/.gitignore +++ b/voter-registry/.gitignore @@ -1,3 +1,8 @@ /protoc.exe /bin/ -/comment-info.txt \ No newline at end of file +/comment-info.txt +/main/java/meerkat/manualTests.* +/SQLiteDBTest.db +/bulletin-board-server/SQLiteDBTest.db +/src/main/java/meerkat/manualTests.java +/.gitignore diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index 41e4eb3..e979e8d 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -3,16 +3,23 @@ package meerkat; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; 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; +import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.Crypto; import util.AccurateTimestamp; import util.RegistryTags; import java.io.IOException; import java.text.ParseException; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Random; import static util.CollectionMessagesUtils.*; @@ -30,24 +37,23 @@ public class SimpleRegistry implements RegistryInstance{ this.communicator = communicator; } - private List wantedInformation; - private boolean methodSucceeded; - /** * Creates BulletinBoardMessage with signed basicMessage and UnsignedBulletinBoardMessage that contains the basic message * * @param basicMessage BasicMessage - * @return BulletinBoardAPI.BulletinBoardMessage + * @return BulletinBoardMessage */ - private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage basicMessage) { + private BulletinBoardMessage CreateBulletinBoardMessage(UnsignedBulletinBoardMessage basicMessage) { try { - BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage = - BulletinBoardAPI.BulletinBoardMessage.newBuilder(); + BulletinBoardMessage.Builder bulletinBoardMessage = + BulletinBoardMessage.newBuilder(); Crypto.RerandomizableEncryptedMessage encryptedMessage = signatory.encrypt(basicMessage, signatory.generateRandomness(new Random())); + Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder(); - messageSignature.mergeFrom(encryptedMessage); + messageSignature.setType(Crypto.SignatureType.ECDSA) + .setData(encryptedMessage.toByteString()); bulletinBoardMessage.setMsg(basicMessage); @@ -63,15 +69,15 @@ public class SimpleRegistry implements RegistryInstance{ * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG * * @param tags the tags based on which the messages will be filtered - * @return List + * @return List */ - private List GetRelevantMessages(List tags) { - BulletinBoardAPI.MessageFilterList.Builder filters = - BulletinBoardAPI.MessageFilterList.newBuilder(); + private List GetRelevantMessages(List tags) { + MessageFilterList.Builder filters = + MessageFilterList.newBuilder(); for (String tag : tags) { - BulletinBoardAPI.MessageFilter.Builder idFilter = - BulletinBoardAPI.MessageFilter.newBuilder().setTag(tag); + MessageFilter.Builder idFilter = + MessageFilter.newBuilder().setTag(tag); filters.addFilter(idFilter); } @@ -85,8 +91,8 @@ public class SimpleRegistry implements RegistryInstance{ * @return List */ private List GetRelevantVoterRegistryMessages(List tags) throws InvalidProtocolBufferException { - List relevantMessages = GetRelevantMessages(tags); - List messages = + List relevantMessages = GetRelevantMessages(tags); + List messages = GetUnsignedBulletinBoardMessages(relevantMessages); return ConvertToVoterRegistryMessages(messages); } @@ -97,7 +103,7 @@ public class SimpleRegistry implements RegistryInstance{ * @param Message the massage to post * @return true when the post was successful else false */ - private boolean SafePost(BulletinBoardAPI.BulletinBoardMessage Message) { + private boolean SafePost(BulletinBoardMessage Message) { try { communicator.postMessage(Message); return true; @@ -107,8 +113,8 @@ public class SimpleRegistry implements RegistryInstance{ } public void AddVoter(String voterID, String personalData, RegistryCallBack callback) { - BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage = - BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder(). + UnsignedBulletinBoardMessage.Builder basicMessage = + UnsignedBulletinBoardMessage.newBuilder(). addTag(RegistryTags.ID_TAG + " " + voterID) .addTag(RegistryTags.VOTER_ENTRY_TAG.toString()) .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString()); @@ -119,8 +125,8 @@ public class SimpleRegistry implements RegistryInstance{ } public void AddToGroup(String voterID, String groupID, RegistryCallBack callback) { - BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage = - BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder() + 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) @@ -130,8 +136,8 @@ public class SimpleRegistry implements RegistryInstance{ } public void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callback) { - BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage = - BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder() + 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) @@ -141,8 +147,8 @@ public class SimpleRegistry implements RegistryInstance{ } public void SetVoted(String id, RegistryCallBack callback) { - BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage = - BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder() + UnsignedBulletinBoardMessage.Builder basicMessage = + UnsignedBulletinBoardMessage.newBuilder() .addTag(RegistryTags.ID_TAG + " " + id) .addTag(RegistryTags.VOTE_ACTION_TAG.toString()) .addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString()); diff --git a/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java b/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java new file mode 100644 index 0000000..2013108 --- /dev/null +++ b/voter-registry/src/main/java/util/SimpleRegistryCallBackHandler.java @@ -0,0 +1,60 @@ +package util; + +import meerkat.RegistryCallBack; + +import java.util.List; + +/** + * Handles the return values from called methods or registry + */ +public class SimpleRegistryCallBackHandler implements RegistryCallBack { + /** + * Presents the state of the called registy method + */ + public boolean ActionSucceed; + + /** + * Presents the wanted information from registry + */ + public List WantedInformation; + + public static int counter = 0; + + @Override + public void HandleVoterAdded(boolean succeeded) { + ActionSucceed = succeeded; + counter++; + } + + @Override + public void HandleVoterAddedToGroup(boolean succeeded) { + ActionSucceed = succeeded; + counter++; + } + + @Override + public void HandleVoterRemovedFromGroup(boolean succeeded) { + ActionSucceed = succeeded; + counter++; + } + + @Override + public void HandleSetVoted(boolean succeeded) { + ActionSucceed = succeeded; + counter++; + } + + @Override + public void HandleVoterInfo(List voterInformation) { + ActionSucceed = (voterInformation != null); + WantedInformation = voterInformation; + counter++; + } + + @Override + public void HandleVoterGroups(List groupsVoterIn) { + ActionSucceed = (groupsVoterIn != null); + WantedInformation = groupsVoterIn; + counter++; + } +} diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index b8951e8..83902d0 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -1,59 +1,148 @@ import junit.framework.TestCase; +import meerkat.SimpleRegistry; +import meerkat.bulletinboard.SimpleBulletinBoardClient; +import meerkat.crypto.concrete.ECElGamalEncryption; +import meerkat.crypto.concrete.ECElGamalUtils; +import meerkat.protobuf.BulletinBoardAPI.*; +import meerkat.protobuf.ConcreteCrypto; +import meerkat.protobuf.Voting; +import org.factcenter.qilin.primitives.concrete.ECElGamal; +import org.factcenter.qilin.primitives.concrete.ECGroup; +import util.RegistryTags; +import util.SimpleRegistryCallBackHandler; + +import java.math.BigInteger; +import java.security.spec.InvalidKeySpecException; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; /** * 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 */ 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 void ElGamalSetup(){ + try { + ECGroup group = new ECGroup("secp256k1"); + BigInteger sk = ECElGamal.generateSecretKey(group, random); + ECElGamal.SK key = new ECElGamal.SK(group, sk); + ConcreteCrypto.ElGamalPublicKey serializedPk = ECElGamalUtils.serializePk(group, key); + signatory = new ECElGamalEncryption(); + signatory.init(serializedPk); + } catch (InvalidKeySpecException e) { + assertTrue(false); + } + } + + private void CommunicatorSetup(){ + communicator = new SimpleBulletinBoardClient(); + communicator.init(Voting.BulletinBoardClientParams.newBuilder() + .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) + .setMinRedundancy((float) 1.0) + .build()); + } + + private void RegistryAnswersHandlerSetup(){ + handler = new SimpleRegistryCallBackHandler(); + } + + /** * Initialize SimpleRegistry object */ public void setUp(){ - + ElGamalSetup(); + CommunicatorSetup(); + RegistryAnswersHandlerSetup(); } /** - * Test that add voter creates new correct bulletin board message and add the voter + * Checks if the creation of the registry have been successful + */ + public void testSimpleRegistryCreation(){ + try { + SimpleRegistry registry = 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 */ public void testAddVoter(){ + List ids = new ArrayList<>(); + for (int i = 0 ; i < 10 ; i++ ){ + ids.add("" + (10000000 + random.nextInt(90000000))); + } + SimpleRegistry registry = new SimpleRegistry(signatory, communicator); + + // 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); + } + + // check that the callbacks have been called exactly 10 times + assertEquals(10, SimpleRegistryCallBackHandler.counter); + + + // 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())); + + List messages = communicator.readMessages(filters.build()); + assertTrue(messages != null); + } } + /** * Test that set voted posts creates correct bulletin board message and sets that the user have been voted */ public void testSetVoted(){ - + assertEquals(null, null); } /** * Test that add to group creates correct bulletin board message and adds a user to a group */ public void testAddToGroup(){ - + assertEquals(null, null); } /** * Test that remove from group creates correct bulletin board message and removes the user from a group */ public void testRemoveFromGroup(){ - + assertEquals(null, null); } /** * Test that get groups retrieves the right groups the user are in */ public void testGetGroups(){ - + assertEquals(null, null); } /** * Test that the personal data outputted about the user is right */ public void testGetPersonalIDDetails(){ - + assertEquals(null, null); } - }