Correcting arbel peled cr

Voter-Registry
Vladimir Eliezer Tokarev 2016-03-11 06:02:09 -08:00
parent 4fac4bbb8c
commit a9d96e59b2
8 changed files with 136 additions and 203 deletions

View File

@ -16,6 +16,26 @@ import java.util.List;
*/ */
public class BulletinBoardUtils { public class BulletinBoardUtils {
/**
* Creates BulletinBoardMessage with UnsignedBulletinBoardMessage and its signature
* signed by all given DigitalSignatures
*
* @param unsignedMessage BasicMessage
* @param signers collection of DigitalSignature which will sign the
* UnsignedBulletinBoardMessage message
* @return BulletinBoardMessage
*/
public static BulletinBoardMessage signBulletinBoardMessage(UnsignedBulletinBoardMessage unsignedMessage, Collection<DigitalSignature> signers) throws SignatureException {
BulletinBoardMessage.Builder bulletinBoardMessage = BulletinBoardMessage.newBuilder();
bulletinBoardMessage.setMsg(unsignedMessage);
for (DigitalSignature signer : signers) {
signer.updateContent(unsignedMessage);
Crypto.Signature signature = signer.sign();
bulletinBoardMessage.addSig(signature);
}
return bulletinBoardMessage.build();
}
/** /**
* Searches the tags in the message for one that begins with given prefix * Searches the tags in the message for one that begins with given prefix
* @param message is the message to search * @param message is the message to search
@ -33,24 +53,7 @@ public class BulletinBoardUtils {
} }
/**
* Creates BulletinBoardMessage with UnsignedBulletinBoardMessage and its signature
* signed by all given DigitalSignatures
*
* @param unsignedMessage BasicMessage
* @param signers collection of DigitalSignature which will sign the
* UnsignedBulletinBoardMessage message
* @return BulletinBoardMessage
*/
public static BulletinBoardMessage signToBulletinBoardMessage(UnsignedBulletinBoardMessage unsignedMessage, Collection<DigitalSignature> signers) throws SignatureException {
BulletinBoardMessage.Builder bulletinBoardMessage = BulletinBoardMessage.newBuilder();
for (DigitalSignature signer : signers) {
signer.updateContent(unsignedMessage);
Crypto.Signature signature = signer.sign();
bulletinBoardMessage.setMsg(unsignedMessage).addSig(signature);
}
return bulletinBoardMessage.build();
}
/** /**
* Gets list of tags values from given messages (tagName values) * Gets list of tags values from given messages (tagName values)

View File

@ -1,6 +1,6 @@
package meerkat; package meerkat;
import com.google.protobuf.Timestamp; import com.google.common.util.concurrent.FutureCallback;
import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.AsyncBulletinBoardClient;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
@ -10,17 +10,15 @@ import meerkat.protobuf.VoterRegistry.VoterInfo;
import meerkat.protobuf.VoterRegistry.VoterRegistryMessage; import meerkat.protobuf.VoterRegistry.VoterRegistryMessage;
import meerkat.registry.LatestMessagesCallBack; import meerkat.registry.LatestMessagesCallBack;
import meerkat.registry.MessageCollectionUtils; import meerkat.registry.MessageCollectionUtils;
import meerkat.registry.MessagesCallBack;
import meerkat.registry.RegistryTags; import meerkat.registry.RegistryTags;
import meerkat.util.BulletinBoardUtils;
import java.io.InputStream;
import java.security.SignatureException; import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import static meerkat.util.BulletinBoardUtils.signToBulletinBoardMessage; import static meerkat.util.BulletinBoardUtils.signBulletinBoardMessage;
/** /**
* TODO : add ability to use DB of certificates * TODO : add ability to use DB of certificates
@ -30,7 +28,7 @@ import static meerkat.util.BulletinBoardUtils.signToBulletinBoardMessage;
* Created by Vladimir Eliezer Tokarev on 1/8/2016. * Created by Vladimir Eliezer Tokarev on 1/8/2016.
* Gives the ability to synchronously manage voters information * Gives the ability to synchronously manage voters information
*/ */
public class Registry implements VoterRegistry{ public class AsyncRegistry implements VoterRegistry{
protected Collection<DigitalSignature> signers; protected Collection<DigitalSignature> signers;
protected AsyncBulletinBoardClient bulletinBoardClient ; protected AsyncBulletinBoardClient bulletinBoardClient ;
@ -41,83 +39,53 @@ public class Registry implements VoterRegistry{
this.bulletinBoardClient = communicator; this.bulletinBoardClient = communicator;
} }
/**
* Loads all the verification certificate to all the signers
* @param certificateStream the certificate for the validation
*/
public void loadCertificateToSigners(InputStream certificateStream)
{
try {
for (DigitalSignature signer: signers)
{
signer.loadVerificationCertificates(certificateStream);
}
} catch (CertificateException e) {
e.printStackTrace();
}
}
@Override @Override
public void addVoter(VoterInfo voterInfo, RegistryCallBack callback) { public void addVoter(VoterInfo voterInfo, FutureCallback<Boolean> callback) throws SignatureException {
UnsignedBulletinBoardMessage basicMessage = UnsignedBulletinBoardMessage basicMessage =
UnsignedBulletinBoardMessage.newBuilder(). UnsignedBulletinBoardMessage.newBuilder()
addTag(RegistryTags.ID_TAG + voterInfo.getId().getId()) .addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
.addTag(RegistryTags.VOTER_ENTRY_TAG) .addTag(RegistryTags.VOTER_ENTRY_TAG)
.addTag(voterInfo.getInfo()) .setData(voterInfo.getInfoBytes())
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()) .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto())
.build()).build(); .build();
try { bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signers), callback);
bulletinBoardClient.postMessage(signToBulletinBoardMessage(basicMessage, signers),
new MessagesCallBack(callback));
} catch (SignatureException e) {
callback.handleFailure(e);
}
} }
@Override @Override
public void setVoterGroups(VoterRegistryMessage voterGroup, RegistryCallBack callback) { public void setVoterGroups(VoterRegistryMessage voterGroup, FutureCallback<Boolean> callback) throws SignatureException {
UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder() UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId()) .addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId())
.addTag(RegistryTags.ADD_TO_GROUP_TAG) .addTag(RegistryTags.ADD_TO_GROUP_TAG)
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()) .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto());
.build());
for (GroupID groupId : voterGroup.getGroupIDList()) for (GroupID groupId : voterGroup.getGroupIDList())
{ {
basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId()); basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId());
} }
try { bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage.build(), signers), callback);
bulletinBoardClient.postMessage(signToBulletinBoardMessage(basicMessage.build(), signers),
new MessagesCallBack(callback));
} catch (SignatureException e) {
callback.handleFailure(e);
}
} }
@Override @Override
public void setVoted(VoterID voterId, RegistryCallBack callback) { public void setVoted(VoterID voterId, FutureCallback<Boolean> callback) throws SignatureException {
UnsignedBulletinBoardMessage basicMessage = UnsignedBulletinBoardMessage basicMessage =
UnsignedBulletinBoardMessage.newBuilder() UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterId.getId()) .addTag(RegistryTags.ID_TAG + voterId.getId())
.addTag(RegistryTags.VOTE_ACTION_TAG) .addTag(RegistryTags.VOTE_ACTION_TAG)
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()) .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto())
.build()).build(); .build();
try { bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signers), callback);
bulletinBoardClient.postMessage(signToBulletinBoardMessage(basicMessage, signers),
new MessagesCallBack(callback));
} catch (SignatureException e) {
callback.handleFailure(e);
}
} }
@Override @Override
public void getGroups(GroupID groupID, RegistryCallBack callback) { public void getGroups(VoterID voterID, FutureCallback<List<String>> callback) {
List<String> GroupsActionsTags = new ArrayList<String>(2) {{ List<String> GroupsActionsTags = new ArrayList<String>(2) {{
add(RegistryTags.GROUP_ID_TAG + groupID.getId()); add(RegistryTags.ADD_TO_GROUP_TAG);
}}; }};
bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags),
@ -125,7 +93,7 @@ public class Registry implements VoterRegistry{
} }
@Override @Override
public void getVoter(VoterID voterID, RegistryCallBack callback) { public void getVoter(VoterID voterID, FutureCallback<VoterInfo> callback) {
List<String> addVoterTags = new ArrayList<String>() {{ List<String> addVoterTags = new ArrayList<String>() {{
add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.ID_TAG + voterID.getId());
add(RegistryTags.VOTER_ENTRY_TAG); add(RegistryTags.VOTER_ENTRY_TAG);
@ -136,7 +104,7 @@ public class Registry implements VoterRegistry{
} }
@Override @Override
public void hasVoted(VoterID voterId, RegistryCallBack callBack) { public void hasVoted(VoterID voterId, FutureCallback<Boolean> callBack) {
List<String> setVotedTags = new ArrayList<String>() {{ List<String> setVotedTags = new ArrayList<String>() {{
add(RegistryTags.ID_TAG + voterId.getId()); add(RegistryTags.ID_TAG + voterId.getId());
add(RegistryTags.VOTE_ACTION_TAG); add(RegistryTags.VOTE_ACTION_TAG);

View File

@ -1,10 +1,15 @@
package meerkat; package meerkat;
import com.google.common.util.concurrent.FutureCallback;
import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.AsyncBulletinBoardClient;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.VoterRegistry.*; import meerkat.protobuf.VoterRegistry.VoterID;
import meerkat.protobuf.VoterRegistry.VoterInfo;
import meerkat.protobuf.VoterRegistry.VoterRegistryMessage;
import java.security.SignatureException;
import java.util.Collection; import java.util.Collection;
import java.util.List;
/** /**
* Created by Vladimir Eliezer Tokarev on 1/22/2016. * Created by Vladimir Eliezer Tokarev on 1/22/2016.
@ -12,78 +17,77 @@ import java.util.Collection;
*/ */
public interface VoterRegistry { public interface VoterRegistry {
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* This interface will handle the end of methods of RegistryInstance
* in case of exception the handleFailure will be called
*/
interface RegistryCallBack<T> {
void handleResult(T result);
void handleFailure(Throwable throwable);
}
/** /**
* Initialize the voter registry * Initialize the voter registry
* @param signers object that will sign the messages before sent them * @param signers collection of singers every object will sign every output message
* @param communicator the object which communicates with the BulletinBoardServer * @param communicator the object which communicates with the BulletinBoardServer
*/ */
void init(Collection<DigitalSignature> signers, AsyncBulletinBoardClient communicator); void init(Collection<DigitalSignature> signers, AsyncBulletinBoardClient communicator);
/** /**
* Adds new voter to the bulletin-board * 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
* call the onFailure method of the callback with the arisen error
* *
* @param voterInfo protobuff object that represents voter information * @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 * @throws SignatureException
*/ */
void addVoter(VoterInfo voterInfo, RegistryCallBack callBack); void addVoter(VoterInfo voterInfo, FutureCallback<Boolean> callBack) throws SignatureException;
/** /**
* Adding given voter to given group * 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
* call the onFailure method of the callback with the arisen error
* *
* @param voterGroup protobuff object that is coupling of voterId to groupId * @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 * @throws SignatureException
*/ */
void setVoterGroups(VoterRegistryMessage voterGroup, RegistryCallBack callBack); void setVoterGroups(VoterRegistryMessage voterGroup, FutureCallback<Boolean> callBack) throws SignatureException;
/** /**
* Sets that the voter have voted * 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
* call the onFailure method of the callback with the arisen error
* *
* @param voterId id tag string * @param voterId protobuff object that represent the voter that have voted
* @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 * @throws SignatureException
*/ */
void setVoted(VoterID voterId, RegistryCallBack callBack); void setVoted(VoterID voterId, FutureCallback<Boolean> callBack) throws SignatureException;
/** /**
* Requests all the groups that the given id voter is in * 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
* call the onFailure method of the callback with the arisen error
* *
* @param groupID id tag string * @param voterID protobuff object that represent the voter
* @param callBack when the adding voter done callBack.handleResult will be called * @param callBack when the adding voter done callBack.handleResult will be called
* @throws SignatureException
*/ */
void getGroups(GroupID groupID, RegistryCallBack callBack); void getGroups(VoterID voterID, FutureCallback<List<String>> callBack) throws SignatureException;
/** /**
* Retrieves list of strings that represents voter * Retrieves VoterInfo protobuff 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
* call the onFailure method of the callback with the arisen error
* *
* @param voterID id tag string * @param voterID protobuff object that represent the voter
* @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) * @throws SignatureException
*/ */
void getVoter(VoterID voterID, RegistryCallBack callBack); void getVoter(VoterID voterID, FutureCallback<VoterInfo> callBack) throws SignatureException;
/** /**
* Checks if the given voter (by his id) have already voted * Checks if the given voter (by his id) have already voted
* @param voterId the id of the the voter * passes true of the voter have been voted or not else
* call the onFailure method of the callback with the arisen error
*
* @param voterID protobuff object that represent the voter
* @param callBack method that will be called with the when the result will be found * @param callBack method that will be called with the when the result will be found
* @return true if voter had voted * @throws SignatureException
*/ */
void hasVoted(VoterID voterId, RegistryCallBack callBack); void hasVoted(VoterID voterID, FutureCallback<Boolean> callBack) throws SignatureException;
} }

View File

@ -1,9 +1,9 @@
package meerkat.registry; package meerkat.registry;
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
import meerkat.VoterRegistry.RegistryCallBack;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.VoterRegistry;
import meerkat.util.TimestampComparator; import meerkat.util.TimestampComparator;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,7 +23,7 @@ import static meerkat.util.BulletinBoardUtils.findTagWithPrefix;
* Gets latest data from given List<BulletinBoardMessage> * Gets latest data from given List<BulletinBoardMessage>
*/ */
public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoardMessage>> { public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoardMessage>> {
public RegistryCallBack callback; public FutureCallback callback;
protected Collection<DigitalSignature> validators; protected Collection<DigitalSignature> validators;
protected String type; protected String type;
@ -32,7 +32,7 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
* @param callback voter registry callback object * @param callback voter registry callback object
* @param validators DigitalSignature object * @param validators DigitalSignature object
*/ */
public LatestMessagesCallBack(RegistryCallBack callback, public LatestMessagesCallBack(FutureCallback callback,
Collection<DigitalSignature> validators, String type) { Collection<DigitalSignature> validators, String type) {
this.callback = callback; this.callback = callback;
this.validators = validators; this.validators = validators;
@ -44,24 +44,30 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
* 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) * the latest tag from this list (in case of personal data)
* *
* @param msg List<BulletinBoardAPI.BulletinBoardMessage> * @param messages List<BulletinBoardAPI.BulletinBoardMessage>
*/ */
@Override @Override
public void onSuccess(List<BulletinBoardMessage> msg) { public void onSuccess(List<BulletinBoardMessage> messages) {
BulletinBoardMessage lastAddedMessage = Collections.max(msg, (first, second) -> { BulletinBoardMessage lastAddedMessage = Collections.max(messages, (first, second) -> {
TimestampComparator comparator = new TimestampComparator(); TimestampComparator comparator = new TimestampComparator();
return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp()); return comparator.compare(first.getMsg().getTimestamp(), second.getMsg().getTimestamp());
}); });
List<BulletinBoardMessage> messages = new ArrayList<BulletinBoardMessage>(){{add(lastAddedMessage);}}; List<BulletinBoardMessage> lastMessageList = new ArrayList<BulletinBoardMessage>(){{add(lastAddedMessage);}};
switch (type){ switch (type){
case "getGroups" : case "getGroups" :
callback.handleResult(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG)); callback.onSuccess(GetListOfTags(lastMessageList, RegistryTags.GROUP_ID_TAG));
break; break;
case "getVoter" : callback.handleResult(lastAddedMessage); case "getVoter" :
VoterRegistry.VoterInfo info = VoterRegistry.VoterInfo.newBuilder()
.setId(VoterRegistry.VoterID.newBuilder()
.setId(findTagWithPrefix(lastAddedMessage, RegistryTags.ID_TAG)))
.setInfo(lastAddedMessage.getMsg().getData().toStringUtf8()).build();
callback.onSuccess(info);
break; break;
case "hasVoted" : case "hasVoted" :
callback.handleResult(findTagWithPrefix(lastAddedMessage, callback.onSuccess(findTagWithPrefix(lastAddedMessage,
RegistryTags.VOTE_ACTION_TAG) != null); RegistryTags.VOTE_ACTION_TAG) != null);
break; break;
} }
@ -73,6 +79,6 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
*/ */
@Override @Override
public void onFailure(Throwable t) { public void onFailure(Throwable t) {
callback.handleFailure(t); callback.onFailure(t);
} }
} }

View File

@ -22,12 +22,12 @@ public abstract class MessageCollectionUtils {
* @return MessageFilterList. * @return MessageFilterList.
*/ */
public static MessageFilterList generateFiltersFromTags(List<String> tags) { public static MessageFilterList generateFiltersFromTags(List<String> tags) {
MessageFilterList.Builder filters = MessageFilterList.newBuilder(); if (tags == null){
return MessageFilterList.getDefaultInstance();
if (tags.isEmpty()){
return filters.build();
} }
MessageFilterList.Builder filters = MessageFilterList.newBuilder();
for (String tag : tags) { for (String tag : tags) {
MessageFilter filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG).build(); MessageFilter filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG).build();
filters.addFilter(filter); filters.addFilter(filter);

View File

@ -1,45 +0,0 @@
package meerkat.registry;
import com.google.common.util.concurrent.FutureCallback;
import meerkat.VoterRegistry;
/**
* TODO: add logging
*/
/**
* Created by Vladimir Eliezer Tokarev on 2/19/2016.
* Handles the the after post state of VoterRegistry methods (that uses bulletinBoardClient to communicate with the server)
*/
public class MessagesCallBack implements FutureCallback<Boolean>
{
public VoterRegistry.RegistryCallBack callback;
/**
* init MessagesCallBack
* @param callback voter registry callback object
*/
public MessagesCallBack(VoterRegistry.RegistryCallBack callback) {
this.callback = callback;
}
/**
* 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);
}
/**
* 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);
}
}

View File

@ -6,11 +6,11 @@ package meerkat.registry;
* Have the tags for the registry messages * Have the tags for the registry messages
*/ */
public interface RegistryTags { public interface RegistryTags {
String ID_TAG = "ID: "; public static final String ID_TAG = "ID: ";
String VOTER_ENTRY_TAG = "VoterEntry: "; public static final String VOTER_ENTRY_TAG = "VoterEntry: ";
String GROUP_ID_TAG = "GroupID: "; public static final String GROUP_ID_TAG = "GroupID: ";
String ADD_TO_GROUP_TAG = "setVoterGroups: "; public static final String ADD_TO_GROUP_TAG = "setVoterGroups: ";
String VOTE_ACTION_TAG = "VoteAction: "; public static final String VOTE_ACTION_TAG = "VoteAction: ";
} }

View File

@ -1,5 +1,5 @@
import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.FutureCallback;
import meerkat.Registry; import meerkat.AsyncRegistry;
import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.AsyncBulletinBoardClient;
import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
@ -20,6 +20,7 @@ import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.SignatureException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -51,7 +52,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
public static String KEYFILE_PASSWORD = "secret"; public static String KEYFILE_PASSWORD = "secret";
Semaphore jobSemaphore; Semaphore jobSemaphore;
class DummyRegistryCallBackHandler<T> implements meerkat.VoterRegistry.RegistryCallBack<T>{ class DummyRegistryCallBackHandler<T> implements FutureCallback<T>{
public int counter; public int counter;
public T data; public T data;
@ -61,14 +62,14 @@ public class SimpleRegistryTest /**extends TestCase**/ {
} }
@Override @Override
public void handleResult(T result) { public void onSuccess(T result) {
counter++; counter++;
data = result; data = result;
jobSemaphore.release(); jobSemaphore.release();
} }
@Override @Override
public void handleFailure(Throwable throwable) { public void onFailure(Throwable throwable) {
System.out.print(throwable); System.out.print(throwable);
} }
} }
@ -90,10 +91,6 @@ public class SimpleRegistryTest /**extends TestCase**/ {
} }
} }
private void createCertificateStream()
{
this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE);
}
private void CommunicatorSetup() { private void CommunicatorSetup() {
bulletinBoardClient = new ThreadedBulletinBoardClient(); bulletinBoardClient = new ThreadedBulletinBoardClient();
@ -113,9 +110,11 @@ public class SimpleRegistryTest /**extends TestCase**/ {
KeyStore.Builder keyStore = signer.getPKCS12KeyStoreBuilder(keyStream, password); KeyStore.Builder keyStore = signer.getPKCS12KeyStoreBuilder(keyStream, password);
signer.loadSigningCertificate(keyStore); signer.loadSigningCertificate(keyStore);
signer.loadVerificationCertificates(getClass().getResourceAsStream(CERT1_PEM_EXAMPLE));
keyStream.close(); keyStream.close();
signers.add(signer); signers.add(signer);
} }
catch (Exception e){ catch (Exception e){
assert false : "The signers creation failed "; assert false : "The signers creation failed ";
@ -129,15 +128,13 @@ public class SimpleRegistryTest /**extends TestCase**/ {
public void setUp() { public void setUp() {
SetSigner(); SetSigner();
CommunicatorSetup(); CommunicatorSetup();
createCertificateStream();
jobSemaphore = new Semaphore(0); jobSemaphore = new Semaphore(0);
} }
private Registry GetRegistry() private AsyncRegistry GetRegistry()
{ {
Registry registry = new Registry(); AsyncRegistry registry = new AsyncRegistry();
registry.init(signers, bulletinBoardClient); registry.init(signers, bulletinBoardClient);
registry.loadCertificateToSigners(certStream);
return registry; return registry;
} }
@ -152,7 +149,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
@Test @Test
public void simpleRegistryCreation() { public void simpleRegistryCreation() {
try { try {
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
} catch (Exception e) { } catch (Exception e) {
assert false : "While creating the registry exception have been thrown " + e; assert false : "While creating the registry exception have been thrown " + e;
} }
@ -204,14 +201,14 @@ public class SimpleRegistryTest /**extends TestCase**/ {
* Test that add voter creates new correct bulletin board message and adds the voter * Test that add voter creates new correct bulletin board message and adds the voter
*/ */
@Test @Test
public void testAddVoter() throws InterruptedException { public void testAddVoter() throws InterruptedException, SignatureException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
String id = generateString(); String id = generateString();
String data = generateString(); String data = generateString();
VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build(); VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
registry.addVoter(voterInfo, handler); registry.addVoter(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -233,13 +230,13 @@ public class SimpleRegistryTest /**extends TestCase**/ {
* Test that set voted posts creates correct bulletin board message and sets that the user have been voted * Test that set voted posts creates correct bulletin board message and sets that the user have been voted
*/ */
@Test @Test
public void testSetVoted() throws InterruptedException { public void testSetVoted() throws InterruptedException, SignatureException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
String id = generateString(); String id = generateString();
VoterID voterInfo = VoterID.newBuilder().setId(id).build(); VoterID voterInfo = VoterID.newBuilder().setId(id).build();
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
registry.setVoted(voterInfo, handler); registry.setVoted(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -261,7 +258,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
* Test that get groups retrieves the right groups the user are in * Test that get groups retrieves the right groups the user are in
*/ */
@Test @Test
public void testAddToGroup() throws InterruptedException { public void testAddToGroup() throws InterruptedException, SignatureException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
String voterId = generateString(); String voterId = generateString();
@ -270,7 +267,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
.setVoterID(VoterID.newBuilder().setId(voterId)) .setVoterID(VoterID.newBuilder().setId(voterId))
.addGroupID(GroupID.newBuilder().setId(groupId)).build(); .addGroupID(GroupID.newBuilder().setId(groupId)).build();
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
registry.setVoterGroups(voterInfo, handler); registry.setVoterGroups(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
@ -293,8 +290,8 @@ public class SimpleRegistryTest /**extends TestCase**/ {
* Test that remove from group creates correct bulletin board message and removes the user from a group * Test that remove from group creates correct bulletin board message and removes the user from a group
*/ */
@Test @Test
public void testGetGroups() throws InterruptedException { public void testGetGroups() throws InterruptedException, SignatureException {
DummyRegistryCallBackHandler<List<BulletinBoardMessage>> handler = DummyRegistryCallBackHandler<Boolean> handler =
new DummyRegistryCallBackHandler<>(); new DummyRegistryCallBackHandler<>();
String voterId = generateString(); String voterId = generateString();
@ -305,14 +302,14 @@ public class SimpleRegistryTest /**extends TestCase**/ {
this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE); this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE);
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
registry.setVoterGroups(voterInfo, handler); registry.setVoterGroups(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals("The callback handler hasn't been called yet", 1, handler.counter ); assertEquals("The callback handler hasn't been called yet", 1, handler.counter );
DummyRegistryCallBackHandler<List<String>> groupsHandler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<List<String>> groupsHandler = new DummyRegistryCallBackHandler<>();
registry.getGroups(GroupID.newBuilder().setId(groupId).build(), groupsHandler); registry.getGroups(VoterID.newBuilder().setId(groupId).build(), groupsHandler);
jobSemaphore.acquire(1); jobSemaphore.acquire(1);
List<String> userGroups = groupsHandler.data; List<String> userGroups = groupsHandler.data;
@ -323,8 +320,8 @@ public class SimpleRegistryTest /**extends TestCase**/ {
* Test that the personal data outputted about the user is right * Test that the personal data outputted about the user is right
*/ */
@Test @Test
public void testGetVoter() throws InterruptedException { public void testGetVoter() throws InterruptedException, SignatureException {
DummyRegistryCallBackHandler<List<BulletinBoardMessage>> handler = DummyRegistryCallBackHandler<Boolean> handler =
new DummyRegistryCallBackHandler<>(); new DummyRegistryCallBackHandler<>();
String id = generateString(); String id = generateString();
@ -332,20 +329,20 @@ public class SimpleRegistryTest /**extends TestCase**/ {
VoterInfo voterInfo = VoterInfo.newBuilder(). VoterInfo voterInfo = VoterInfo.newBuilder().
setId(VoterID.newBuilder().setId(id)).setInfo(data).build(); setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
registry.addVoter(voterInfo, handler); registry.addVoter(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals("The callback handler hasn't been called yet", 1, handler.counter ); assertEquals("The callback handler hasn't been called yet", 1, handler.counter );
DummyRegistryCallBackHandler<BulletinBoardMessage> personalHandler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<VoterInfo> personalHandler = new DummyRegistryCallBackHandler<>();
registry.getVoter(VoterID.newBuilder().setId(id).build(), personalHandler); registry.getVoter(VoterID.newBuilder().setId(id).build(), personalHandler);
jobSemaphore.acquire(1); jobSemaphore.acquire(1);
assertEquals("The voter id doesn't match the created on ", assertEquals("The voter id doesn't match the created on ",
id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG)); id, personalHandler.data.getId().getId());
assertTrue("The voter personal data can't be found.", String personalData = personalHandler.data.getInfo();
findTagWithPrefix(personalHandler.data, data) != null); assertTrue("The voter personal data can't be found.", data.equals(personalData));
} }
/** /**
@ -353,12 +350,12 @@ public class SimpleRegistryTest /**extends TestCase**/ {
* @throws InterruptedException * @throws InterruptedException
*/ */
@Test @Test
public void testHasVoted () throws InterruptedException { public void testHasVoted () throws InterruptedException, SignatureException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
String id = generateString(); String id = generateString();
VoterID voterInfo = VoterID.newBuilder().setId(id).build(); VoterID voterInfo = VoterID.newBuilder().setId(id).build();
Registry registry = GetRegistry(); AsyncRegistry registry = GetRegistry();
registry.setVoted(voterInfo, handler); registry.setVoted(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals("The callback handler hasn't been called yet", 1, handler.counter); assertEquals("The callback handler hasn't been called yet", 1, handler.counter);