From da614c13abd124ad7e40ded3ee7db1e8fa52b3fb Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Sat, 5 Mar 2016 02:36:35 -0800 Subject: [PATCH] Changed the signatures of methods according to java standarts (camelCaseConvention). --- .../src/main/java/meerkat/Registry.java | 64 ++++++++++--------- .../src/main/java/meerkat/VoterRegistry.java | 34 +++++----- .../registry/LatestMessagesCallBack.java | 56 ++++++++-------- .../registry/MessageCollectionUtils.java | 2 +- .../meerkat/registry/MessagesCallBack.java | 10 +-- .../java/meerkat/registry/RegistryTags.java | 2 +- .../src/test/java/SimpleRegistryTest.java | 53 +++++++++------ 7 files changed, 119 insertions(+), 102 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/Registry.java b/voter-registry/src/main/java/meerkat/Registry.java index ee00759..d97d2c7 100644 --- a/voter-registry/src/main/java/meerkat/Registry.java +++ b/voter-registry/src/main/java/meerkat/Registry.java @@ -1,20 +1,25 @@ package meerkat; import com.google.protobuf.Timestamp; -import meerkat.registry.MessagesCallBack; -import meerkat.registry.MessageCollectionUtils; -import meerkat.registry.RegistryTags; -import meerkat.registry.LatestMessagesCallBack; import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.Crypto; -import meerkat.protobuf.VoterRegistry.*; +import meerkat.protobuf.VoterRegistry.GroupID; +import meerkat.protobuf.VoterRegistry.VoterID; +import meerkat.protobuf.VoterRegistry.VoterInfo; +import meerkat.protobuf.VoterRegistry.VoterRegistryMessage; +import meerkat.registry.LatestMessagesCallBack; +import meerkat.registry.MessageCollectionUtils; +import meerkat.registry.MessagesCallBack; +import meerkat.registry.RegistryTags; import java.io.InputStream; import java.security.SignatureException; +import java.security.cert.CertificateException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -27,15 +32,14 @@ import java.util.List; */ public class Registry implements VoterRegistry{ - protected DigitalSignature signer; + protected Collection signers; protected AsyncBulletinBoardClient bulletinBoardClient ; - protected Crypto.Signature signature; protected InputStream certificateStream; - public Registry(DigitalSignature signer, - AsyncBulletinBoardClient communicator, - InputStream certificateStream) { - this.signer = signer; + public void init(Collection signers, + AsyncBulletinBoardClient communicator, + InputStream certificateStream) { + this.signers = signers; this.bulletinBoardClient = communicator; this.certificateStream = certificateStream; } @@ -47,22 +51,24 @@ public class Registry implements VoterRegistry{ * @param basicMessage BasicMessage * @return BulletinBoardMessage */ - protected BulletinBoardMessage CreateBulletinBoardMessage(UnsignedBulletinBoardMessage basicMessage) { + protected BulletinBoardMessage createBulletinBoardMessage(UnsignedBulletinBoardMessage basicMessage) { try { BulletinBoardMessage.Builder bulletinBoardMessage = BulletinBoardMessage.newBuilder(); + for (DigitalSignature signer : signers) { + signer.updateContent(basicMessage); + Crypto.Signature signature = signer.sign(); + bulletinBoardMessage.setMsg(basicMessage).addSig(signature); - signer.updateContent(basicMessage); - signature = signer.sign(); - bulletinBoardMessage.setMsg(basicMessage).addSig(signature); - + signer.loadVerificationCertificates(certificateStream); + } return bulletinBoardMessage.build(); - } catch (SignatureException e) { + } catch (SignatureException | CertificateException e) { e.printStackTrace(); return null; } } - public void AddVoter(VoterInfo voterInfo, RegistryCallBack callback) { + public void addVoter(VoterInfo voterInfo, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder(). addTag(RegistryTags.ID_TAG + voterInfo.getId().getId()) @@ -70,10 +76,10 @@ public class Registry implements VoterRegistry{ .addTag(RegistryTags.VOTER_DATA_TAG + voterInfo.getInfo()) .setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()).build()); - bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); + bulletinBoardClient.postMessage(createBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); } - public void AddToGroups(VoterRegistryMessage voterGroup, RegistryCallBack callback) { + public void addToGroups(VoterRegistryMessage voterGroup, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder() .addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId()) @@ -85,33 +91,33 @@ public class Registry implements VoterRegistry{ basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId()); } - bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); + bulletinBoardClient.postMessage(createBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); } - public void SetVoted(VoterID voterId, RegistryCallBack callback) { + public void setVoted(VoterID voterId, RegistryCallBack callback) { UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder() .addTag(RegistryTags.ID_TAG + voterId.getId()) .addTag(RegistryTags.VOTE_ACTION_TAG) .setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()).build()); - bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); + bulletinBoardClient.postMessage(createBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); } - public void GetGroups(GroupID groupID, RegistryCallBack callback) { + public void getGroups(GroupID groupID, RegistryCallBack callback) { List GroupsActionsTags = new ArrayList(2) {{ add(RegistryTags.GROUP_ID_TAG + groupID.getId()); }}; - bulletinBoardClient.readMessages(MessageCollectionUtils.GenerateFiltersFromTags(GroupsActionsTags), - new LatestMessagesCallBack(callback, signer, signature, certificateStream)); + bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), + new LatestMessagesCallBack(callback, signers)); } - public void GetPersonIDDetails(VoterID voterID, RegistryCallBack callback) { + public void getPersonIDDetails(VoterID voterID, RegistryCallBack callback) { List GroupsActionsTags = new ArrayList() {{ add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.VOTER_ENTRY_TAG); }}; - bulletinBoardClient.readMessages(MessageCollectionUtils.GenerateFiltersFromTags(GroupsActionsTags), - new LatestMessagesCallBack(callback, signer, signature, certificateStream)); + bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), + new LatestMessagesCallBack(callback, signers)); } } diff --git a/voter-registry/src/main/java/meerkat/VoterRegistry.java b/voter-registry/src/main/java/meerkat/VoterRegistry.java index 4247fdd..25d7822 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistry.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistry.java @@ -14,57 +14,57 @@ public interface VoterRegistry { * in case of exception the handleFailure will be called */ interface RegistryCallBack { - void HandleResult(T result); - void HandleFailure(Throwable throwable); + void handleResult(T result); + void handleFailure(Throwable throwable); } /** * Adds new voter to the bulletin-board - * Passes true to callBack.HandleResult if the actions succeeded else false + * Passes true to callBack.handleResult if the actions succeeded else false * * @param voterInfo protobuff object that represents voter information - * @param callBack when the adding voter done callBack.HandleResult will be called + * @param callBack when the adding voter done callBack.handleResult will be called * @return void */ - void AddVoter(VoterInfo voterInfo, RegistryCallBack callBack); + void addVoter(VoterInfo voterInfo, RegistryCallBack callBack); /** * Adding given voter to given group - * Passes the group to callBack.HandleResult if the actions succeeded else null + * Passes the group to callBack.handleResult if the actions succeeded else null * * @param voterGroup protobuff object that is coupling of voterId to groupId - * @param callBack when the adding voter done callBack.HandleResult will be called + * @param callBack when the adding voter done callBack.handleResult will be called * @return true if the adding action succeeded else return false */ - void AddToGroups(VoterRegistryMessage voterGroup, RegistryCallBack callBack); + void addToGroups(VoterRegistryMessage voterGroup, RegistryCallBack callBack); /** * Sets that the voter have voted - * Passes true to callBack.HandleResult if the actions succeeded else false + * Passes true to callBack.handleResult if the actions succeeded else false * * @param voterId id tag string - * @param callBack when the adding voter done callBack.HandleResult will be called + * @param callBack when the adding voter done callBack.handleResult will be called * @return true if the set voted succeed else false */ - void SetVoted(VoterID voterId, RegistryCallBack callBack); + void setVoted(VoterID voterId, RegistryCallBack callBack); /** * Requests all the groups that the given id voter is in - * Passes wanted groups to callback.HandleResult if the actions succeeded else null + * Passes wanted groups to callback.handleResult if the actions succeeded else null * * @param groupID id tag string - * @param callBack when the adding voter done callBack.HandleResult will be called + * @param callBack when the adding voter done callBack.handleResult will be called */ - void GetGroups(GroupID groupID, RegistryCallBack callBack); + void getGroups(GroupID groupID, RegistryCallBack callBack); /** * Retrieves list of strings that represents voter - * Passes wanted data to callback.HandleResult if the actions succeeded else null + * Passes wanted data to callback.handleResult if the actions succeeded else null * * @param voterID id tag string - * @param callBack when the adding voter done callBack.HandleResult will be called + * @param callBack when the adding voter done callBack.handleResult will be called * @return list of strings (empty list if the lookup failed) */ - void GetPersonIDDetails(VoterID voterID, RegistryCallBack callBack); + void getPersonIDDetails(VoterID voterID, RegistryCallBack callBack); } diff --git a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java index 0b9ad7e..0d081b1 100644 --- a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java +++ b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java @@ -4,16 +4,14 @@ import com.google.common.util.concurrent.FutureCallback; import meerkat.MessageValidator; import meerkat.VoterRegistry.RegistryCallBack; import meerkat.crypto.DigitalSignature; -import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.Crypto; import meerkat.util.TimestampComparator; -import java.io.InputStream; import java.security.InvalidKeyException; -import java.security.SignatureException; import java.security.cert.CertificateException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; @@ -29,24 +27,16 @@ import static meerkat.util.BulletinBoardUtils.GetListOfTags; */ public class LatestMessagesCallBack implements FutureCallback>, MessageValidator> { public RegistryCallBack callback; - protected DigitalSignature validator; - protected Crypto.Signature signature; - protected InputStream certificateStream; + protected Collection validators; /** - * Init MessagesCallBack + * init MessagesCallBack * @param callback voter registry callback object - * @param validator DigitalSignature object - * @param signature Crypto.Signature object + * @param validators DigitalSignature object */ - public LatestMessagesCallBack(RegistryCallBack callback, - DigitalSignature validator, - Crypto.Signature signature, - InputStream certificateStream) { + public LatestMessagesCallBack(RegistryCallBack callback, Collection validators) { this.callback = callback; - this.validator = validator; - this.signature = signature; - this.certificateStream = certificateStream; + this.validators = validators; } /** @@ -67,7 +57,7 @@ public class LatestMessagesCallBack implements FutureCallback @@ -76,10 +66,10 @@ public class LatestMessagesCallBack implements FutureCallback msg) { if (isAddToGroupsList(msg)) { List groupsOfUser = GetListOfTags(msg, RegistryTags.GROUP_ID_TAG); - callback.HandleResult(groupsOfUser); + callback.handleResult(groupsOfUser); } else { - callback.HandleResult(Collections.max(msg, (first, second) -> { + callback.handleResult(Collections.max(msg, (first, second) -> { TimestampComparator comparator = new TimestampComparator(); return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp()); })); @@ -87,12 +77,12 @@ public class LatestMessagesCallBack implements FutureCallback verifiedMessages = new ArrayList<>(object.size()); for (BulletinBoardMessage message : object) { - if(VerifyMessage(message.getMsg())) + if(verifyMessage(message)) { verifiedMessages.add(message); } diff --git a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java index 98c02c7..bf7ee23 100644 --- a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java +++ b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java @@ -21,7 +21,7 @@ public abstract class MessageCollectionUtils { * @param tags the tags based on which the messages will be filtered * @return MessageFilterList. */ - public static MessageFilterList GenerateFiltersFromTags(List tags) { + public static MessageFilterList generateFiltersFromTags(List tags) { MessageFilterList.Builder filters = MessageFilterList.newBuilder(); if (tags.isEmpty()){ diff --git a/voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java index a8fe184..84d2ecb 100644 --- a/voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java +++ b/voter-registry/src/main/java/meerkat/registry/MessagesCallBack.java @@ -16,7 +16,7 @@ public class MessagesCallBack implements FutureCallback public VoterRegistry.RegistryCallBack callback; /** - * Init MessagesCallBack + * init MessagesCallBack * @param callback voter registry callback object */ public MessagesCallBack(VoterRegistry.RegistryCallBack callback) { @@ -24,22 +24,22 @@ public class MessagesCallBack implements FutureCallback } /** - * Calls the callback HandleResult method with passed object from bulletinBoardClient when + * Calls the callback handleResult method with passed object from bulletinBoardClient when * the action succeed * * @param msg the message that the bulletinBoardClient passes to the callback */ @Override public void onSuccess(Boolean msg) { - callback.HandleResult(msg); + callback.handleResult(msg); } /** - * Calls the callback HandleResult method with false because the post method failed + * Calls the callback handleResult method with false because the post method failed * @param t the exception data that have been thrown during the failure of the post method */ @Override public void onFailure(Throwable t) { - callback.HandleFailure(t); + callback.handleFailure(t); } } diff --git a/voter-registry/src/main/java/meerkat/registry/RegistryTags.java b/voter-registry/src/main/java/meerkat/registry/RegistryTags.java index df1910a..375e367 100644 --- a/voter-registry/src/main/java/meerkat/registry/RegistryTags.java +++ b/voter-registry/src/main/java/meerkat/registry/RegistryTags.java @@ -10,7 +10,7 @@ public interface RegistryTags { String VOTER_ENTRY_TAG = "VoterEntry: "; String VOTER_DATA_TAG = "Data: "; String GROUP_ID_TAG = "GroupID: "; - String ADD_TO_GROUP_TAG = "AddToGroups: "; + String ADD_TO_GROUP_TAG = "addToGroups: "; String VOTE_ACTION_TAG = "VoteAction: "; } diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 6bafb5d..12a9197 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -4,6 +4,7 @@ import junit.framework.TestCase; import meerkat.Registry; import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient; +import meerkat.crypto.DigitalSignature; import meerkat.crypto.concrete.ECDSASignature; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; @@ -20,6 +21,7 @@ import java.math.BigInteger; import java.security.KeyStore; import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.concurrent.Semaphore; @@ -37,7 +39,7 @@ import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; */ public class SimpleRegistryTest extends TestCase { - private ECDSASignature signer; + private Collection signers; private AsyncBulletinBoardClient bulletinBoardClient; private InputStream certStream; private SecureRandom random = new SecureRandom(); @@ -57,14 +59,14 @@ public class SimpleRegistryTest extends TestCase { } @Override - public void HandleResult(T result) { + public void handleResult(T result) { counter++; data = result; jobSemaphore.release(); } @Override - public void HandleFailure(Throwable throwable) { + public void handleFailure(Throwable throwable) { } } @@ -101,7 +103,8 @@ public class SimpleRegistryTest extends TestCase { private void SetSigner(){ try { - signer = new ECDSASignature(); + signers = new ArrayList<>(); + ECDSASignature signer = new ECDSASignature(); InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE); char[] password = KEYFILE_PASSWORD.toCharArray(); @@ -109,9 +112,10 @@ public class SimpleRegistryTest extends TestCase { signer.loadSigningCertificate(keyStore); keyStream.close(); + signers.add(signer); } catch (Exception e){ - assert false : "The signer creation failed "; + assert false : "The signers creation failed "; } } @@ -125,12 +129,19 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore = new Semaphore(0); } + private Registry GetRegistry() + { + Registry registry = new Registry(); + registry.init(signers, bulletinBoardClient, certStream); + return registry; + } + /** * Checks if the creation of the registry have been successful */ public void testSimpleRegistryCreation() { try { - new Registry(signer, bulletinBoardClient, certStream); + Registry registry = GetRegistry(); } catch (Exception e) { assert false : "While creating the registry exception have been thrown " + e; } @@ -173,14 +184,14 @@ public class SimpleRegistryTest extends TestCase { String data = new BigInteger(130, random).toString(32); VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build(); - Registry registry = new Registry(signer, bulletinBoardClient, certStream); - registry.AddVoter(voterInfo, handler); + Registry registry = GetRegistry(); + registry.addVoter(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG);}}; - MessageFilterList filters = MessageCollectionUtils.GenerateFiltersFromTags(tags); + MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); bulletinBoardClient.readMessages(filters, bulletinHandler); @@ -202,14 +213,14 @@ public class SimpleRegistryTest extends TestCase { String id = new BigInteger(130, random).toString(32); VoterID voterInfo = VoterID.newBuilder().setId(id).build(); - Registry registry = new Registry(signer, bulletinBoardClient, certStream); - registry.SetVoted(voterInfo, handler); + Registry registry = GetRegistry(); + registry.setVoted(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTE_ACTION_TAG);}}; - MessageFilterList filters = MessageCollectionUtils.GenerateFiltersFromTags(tags); + MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); bulletinBoardClient.readMessages(filters, bulletinHandler); @@ -234,14 +245,14 @@ public class SimpleRegistryTest extends TestCase { .setVoterID(VoterID.newBuilder().setId(voterId)) .addGroupID(GroupID.newBuilder().setId(groupId)).build(); - Registry registry = new Registry(signer, bulletinBoardClient, certStream); - registry.AddToGroups(voterInfo, handler); + Registry registry = GetRegistry(); + registry.addToGroups(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter); List tags = new ArrayList(){{add(RegistryTags.ADD_TO_GROUP_TAG);}}; - MessageFilterList filters = MessageCollectionUtils.GenerateFiltersFromTags(tags); + MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); bulletinBoardClient.readMessages(filters, bulletinHandler); @@ -270,14 +281,14 @@ public class SimpleRegistryTest extends TestCase { this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE); - Registry registry = new Registry(signer, bulletinBoardClient,certStream); - registry.AddToGroups(voterInfo, handler); + Registry registry = GetRegistry(); + registry.addToGroups(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter ); DummyRegistryCallBackHandler> groupsHandler = new DummyRegistryCallBackHandler<>(); - registry.GetGroups(GroupID.newBuilder().setId(groupId).build(), groupsHandler); + registry.getGroups(GroupID.newBuilder().setId(groupId).build(), groupsHandler); jobSemaphore.acquire(1); List userGroups = groupsHandler.data; @@ -296,14 +307,14 @@ public class SimpleRegistryTest extends TestCase { VoterInfo voterInfo = VoterInfo.newBuilder(). setId(VoterID.newBuilder().setId(id)).setInfo(data).build(); - Registry registry = new Registry(signer, bulletinBoardClient, certStream); - registry.AddVoter(voterInfo, handler); + Registry registry = GetRegistry(); + registry.addVoter(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter ); DummyRegistryCallBackHandler personalHandler = new DummyRegistryCallBackHandler<>(); - registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); + registry.getPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); jobSemaphore.acquire(1); assertEquals(id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG));