Changed the signatures of methods according to java standarts (camelCaseConvention).

Voter-Registry
Vladimir Eliezer Tokarev 2016-03-05 02:36:35 -08:00
parent 4b31e87d07
commit da614c13ab
7 changed files with 119 additions and 102 deletions

View File

@ -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<DigitalSignature> 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<DigitalSignature> 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<String> GroupsActionsTags = new ArrayList<String>(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<String> GroupsActionsTags = new ArrayList<String>() {{
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));
}
}

View File

@ -14,57 +14,57 @@ public interface VoterRegistry {
* in case of exception the handleFailure will be called
*/
interface RegistryCallBack<T> {
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);
}

View File

@ -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<List<BulletinBoardMessage>>, MessageValidator<List<BulletinBoardMessage>> {
public RegistryCallBack callback;
protected DigitalSignature validator;
protected Crypto.Signature signature;
protected InputStream certificateStream;
protected Collection<DigitalSignature> 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<DigitalSignature> 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<List<BulletinBoard
/**
* Checks if the list of messages is of type GROUP_ID_TAG if it is then calls
* HandleResult with mapping of the latest groups, else calls to HandleResult with
* handleResult with mapping of the latest groups, else calls to handleResult with
* the latest tag from this list (in case of personal data)
*
* @param msg List<BulletinBoardAPI.BulletinBoardMessage>
@ -76,10 +66,10 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
public void onSuccess(List<BulletinBoardMessage> msg) {
if (isAddToGroupsList(msg)) {
List<String> 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<List<BulletinBoard
}
/**
* 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);
}
/**
@ -100,14 +90,24 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
* @param message BulletinBoardMessage instance
* @return true if the message is valid else false
*/
private boolean VerifyMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message)
private boolean verifyMessage(BulletinBoardMessage message)
{
try {
validator.loadVerificationCertificates(certificateStream);
validator.initVerify(signature);
validator.updateContent(message);
return validator.verify();
} catch (CertificateException | InvalidKeyException | SignatureException e) {
int counter = 0;
for (Crypto.Signature signature : message.getSigList()) {
for (DigitalSignature digitalSignature : validators) {
if (signature.getSignerId().equals(digitalSignature.getSignerID()))
{
digitalSignature.initVerify(signature);
if(digitalSignature.verify())
{
counter++;
}
}
}
}
return counter == validators.size();
} catch (CertificateException | InvalidKeyException e) {
e.printStackTrace();
return false;
}
@ -124,7 +124,7 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
List<BulletinBoardMessage> verifiedMessages = new ArrayList<>(object.size());
for (BulletinBoardMessage message : object)
{
if(VerifyMessage(message.getMsg()))
if(verifyMessage(message))
{
verifiedMessages.add(message);
}

View File

@ -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<String> tags) {
public static MessageFilterList generateFiltersFromTags(List<String> tags) {
MessageFilterList.Builder filters = MessageFilterList.newBuilder();
if (tags.isEmpty()){

View File

@ -16,7 +16,7 @@ public class MessagesCallBack implements FutureCallback<Boolean>
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<Boolean>
}
/**
* 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);
}
}

View File

@ -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: ";
}

View File

@ -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<DigitalSignature> 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<String> tags = new ArrayList<String>(){{ 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<String> tags = new ArrayList<String>(){{ 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<String> tags = new ArrayList<String>(){{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<List<String>> groupsHandler = new DummyRegistryCallBackHandler<>();
registry.GetGroups(GroupID.newBuilder().setId(groupId).build(), groupsHandler);
registry.getGroups(GroupID.newBuilder().setId(groupId).build(), groupsHandler);
jobSemaphore.acquire(1);
List<String> 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<BulletinBoardMessage> 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));