Correcting arbel peled cr
parent
4fac4bbb8c
commit
a9d96e59b2
|
@ -16,6 +16,26 @@ import java.util.List;
|
|||
*/
|
||||
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
|
||||
* @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)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package meerkat;
|
||||
|
||||
import com.google.protobuf.Timestamp;
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import meerkat.bulletinboard.AsyncBulletinBoardClient;
|
||||
import meerkat.crypto.DigitalSignature;
|
||||
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
|
||||
|
@ -10,17 +10,15 @@ 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 meerkat.util.BulletinBoardUtils;
|
||||
|
||||
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;
|
||||
|
||||
import static meerkat.util.BulletinBoardUtils.signToBulletinBoardMessage;
|
||||
import static meerkat.util.BulletinBoardUtils.signBulletinBoardMessage;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Gives the ability to synchronously manage voters information
|
||||
*/
|
||||
public class Registry implements VoterRegistry{
|
||||
public class AsyncRegistry implements VoterRegistry{
|
||||
|
||||
protected Collection<DigitalSignature> signers;
|
||||
protected AsyncBulletinBoardClient bulletinBoardClient ;
|
||||
|
@ -41,83 +39,53 @@ public class Registry implements VoterRegistry{
|
|||
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
|
||||
public void addVoter(VoterInfo voterInfo, RegistryCallBack callback) {
|
||||
public void addVoter(VoterInfo voterInfo, FutureCallback<Boolean> callback) throws SignatureException {
|
||||
UnsignedBulletinBoardMessage basicMessage =
|
||||
UnsignedBulletinBoardMessage.newBuilder().
|
||||
addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
|
||||
UnsignedBulletinBoardMessage.newBuilder()
|
||||
.addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
|
||||
.addTag(RegistryTags.VOTER_ENTRY_TAG)
|
||||
.addTag(voterInfo.getInfo())
|
||||
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime())
|
||||
.build()).build();
|
||||
.setData(voterInfo.getInfoBytes())
|
||||
.setTimestamp(BulletinBoardUtils.getCurrentTimestampProto())
|
||||
.build();
|
||||
|
||||
try {
|
||||
bulletinBoardClient.postMessage(signToBulletinBoardMessage(basicMessage, signers),
|
||||
new MessagesCallBack(callback));
|
||||
} catch (SignatureException e) {
|
||||
callback.handleFailure(e);
|
||||
}
|
||||
bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signers), callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVoterGroups(VoterRegistryMessage voterGroup, RegistryCallBack callback) {
|
||||
public void setVoterGroups(VoterRegistryMessage voterGroup, FutureCallback<Boolean> callback) throws SignatureException {
|
||||
UnsignedBulletinBoardMessage.Builder basicMessage =
|
||||
UnsignedBulletinBoardMessage.newBuilder()
|
||||
.addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId())
|
||||
.addTag(RegistryTags.ADD_TO_GROUP_TAG)
|
||||
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime())
|
||||
.build());
|
||||
.setTimestamp(BulletinBoardUtils.getCurrentTimestampProto());
|
||||
|
||||
for (GroupID groupId : voterGroup.getGroupIDList())
|
||||
{
|
||||
basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId());
|
||||
}
|
||||
|
||||
try {
|
||||
bulletinBoardClient.postMessage(signToBulletinBoardMessage(basicMessage.build(), signers),
|
||||
new MessagesCallBack(callback));
|
||||
} catch (SignatureException e) {
|
||||
callback.handleFailure(e);
|
||||
}
|
||||
bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage.build(), signers), callback);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVoted(VoterID voterId, RegistryCallBack callback) {
|
||||
public void setVoted(VoterID voterId, FutureCallback<Boolean> callback) throws SignatureException {
|
||||
UnsignedBulletinBoardMessage basicMessage =
|
||||
UnsignedBulletinBoardMessage.newBuilder()
|
||||
.addTag(RegistryTags.ID_TAG + voterId.getId())
|
||||
.addTag(RegistryTags.VOTE_ACTION_TAG)
|
||||
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime())
|
||||
.build()).build();
|
||||
.setTimestamp(BulletinBoardUtils.getCurrentTimestampProto())
|
||||
.build();
|
||||
|
||||
try {
|
||||
bulletinBoardClient.postMessage(signToBulletinBoardMessage(basicMessage, signers),
|
||||
new MessagesCallBack(callback));
|
||||
} catch (SignatureException e) {
|
||||
callback.handleFailure(e);
|
||||
}
|
||||
bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage, signers), callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getGroups(GroupID groupID, RegistryCallBack callback) {
|
||||
public void getGroups(VoterID voterID, FutureCallback<List<String>> callback) {
|
||||
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),
|
||||
|
@ -125,7 +93,7 @@ public class Registry implements VoterRegistry{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void getVoter(VoterID voterID, RegistryCallBack callback) {
|
||||
public void getVoter(VoterID voterID, FutureCallback<VoterInfo> callback) {
|
||||
List<String> addVoterTags = new ArrayList<String>() {{
|
||||
add(RegistryTags.ID_TAG + voterID.getId());
|
||||
add(RegistryTags.VOTER_ENTRY_TAG);
|
||||
|
@ -136,7 +104,7 @@ public class Registry implements VoterRegistry{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void hasVoted(VoterID voterId, RegistryCallBack callBack) {
|
||||
public void hasVoted(VoterID voterId, FutureCallback<Boolean> callBack) {
|
||||
List<String> setVotedTags = new ArrayList<String>() {{
|
||||
add(RegistryTags.ID_TAG + voterId.getId());
|
||||
add(RegistryTags.VOTE_ACTION_TAG);
|
|
@ -1,10 +1,15 @@
|
|||
package meerkat;
|
||||
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import meerkat.bulletinboard.AsyncBulletinBoardClient;
|
||||
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.List;
|
||||
|
||||
/**
|
||||
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
|
||||
|
@ -12,78 +17,77 @@ import java.util.Collection;
|
|||
*/
|
||||
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
|
||||
* @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
|
||||
*/
|
||||
void init(Collection<DigitalSignature> signers, AsyncBulletinBoardClient communicator);
|
||||
|
||||
/**
|
||||
* 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 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
|
||||
* 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 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
|
||||
* 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
|
||||
* @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
|
||||
* 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
|
||||
* @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
|
||||
* Passes wanted data to callback.handleResult if the actions succeeded else null
|
||||
* Retrieves VoterInfo protobuff that represents voter
|
||||
* 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
|
||||
* @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
|
||||
* @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
|
||||
* @return true if voter had voted
|
||||
* @throws SignatureException
|
||||
*/
|
||||
void hasVoted(VoterID voterId, RegistryCallBack callBack);
|
||||
void hasVoted(VoterID voterID, FutureCallback<Boolean> callBack) throws SignatureException;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package meerkat.registry;
|
||||
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import meerkat.VoterRegistry.RegistryCallBack;
|
||||
import meerkat.crypto.DigitalSignature;
|
||||
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
|
||||
import meerkat.protobuf.VoterRegistry;
|
||||
import meerkat.util.TimestampComparator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -23,7 +23,7 @@ import static meerkat.util.BulletinBoardUtils.findTagWithPrefix;
|
|||
* Gets latest data from given List<BulletinBoardMessage>
|
||||
*/
|
||||
public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoardMessage>> {
|
||||
public RegistryCallBack callback;
|
||||
public FutureCallback callback;
|
||||
protected Collection<DigitalSignature> validators;
|
||||
protected String type;
|
||||
|
||||
|
@ -32,7 +32,7 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
|
|||
* @param callback voter registry callback object
|
||||
* @param validators DigitalSignature object
|
||||
*/
|
||||
public LatestMessagesCallBack(RegistryCallBack callback,
|
||||
public LatestMessagesCallBack(FutureCallback callback,
|
||||
Collection<DigitalSignature> validators, String type) {
|
||||
this.callback = callback;
|
||||
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
|
||||
* the latest tag from this list (in case of personal data)
|
||||
*
|
||||
* @param msg List<BulletinBoardAPI.BulletinBoardMessage>
|
||||
* @param messages List<BulletinBoardAPI.BulletinBoardMessage>
|
||||
*/
|
||||
@Override
|
||||
public void onSuccess(List<BulletinBoardMessage> msg) {
|
||||
BulletinBoardMessage lastAddedMessage = Collections.max(msg, (first, second) -> {
|
||||
public void onSuccess(List<BulletinBoardMessage> messages) {
|
||||
BulletinBoardMessage lastAddedMessage = Collections.max(messages, (first, second) -> {
|
||||
TimestampComparator comparator = new TimestampComparator();
|
||||
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){
|
||||
case "getGroups" :
|
||||
callback.handleResult(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG));
|
||||
callback.onSuccess(GetListOfTags(lastMessageList, RegistryTags.GROUP_ID_TAG));
|
||||
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;
|
||||
case "hasVoted" :
|
||||
callback.handleResult(findTagWithPrefix(lastAddedMessage,
|
||||
callback.onSuccess(findTagWithPrefix(lastAddedMessage,
|
||||
RegistryTags.VOTE_ACTION_TAG) != null);
|
||||
break;
|
||||
}
|
||||
|
@ -73,6 +79,6 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
|
|||
*/
|
||||
@Override
|
||||
public void onFailure(Throwable t) {
|
||||
callback.handleFailure(t);
|
||||
callback.onFailure(t);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,12 +22,12 @@ public abstract class MessageCollectionUtils {
|
|||
* @return MessageFilterList.
|
||||
*/
|
||||
public static MessageFilterList generateFiltersFromTags(List<String> tags) {
|
||||
MessageFilterList.Builder filters = MessageFilterList.newBuilder();
|
||||
|
||||
if (tags.isEmpty()){
|
||||
return filters.build();
|
||||
if (tags == null){
|
||||
return MessageFilterList.getDefaultInstance();
|
||||
}
|
||||
|
||||
MessageFilterList.Builder filters = MessageFilterList.newBuilder();
|
||||
|
||||
for (String tag : tags) {
|
||||
MessageFilter filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG).build();
|
||||
filters.addFilter(filter);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -6,11 +6,11 @@ package meerkat.registry;
|
|||
* Have the tags for the registry messages
|
||||
*/
|
||||
public interface RegistryTags {
|
||||
String ID_TAG = "ID: ";
|
||||
String VOTER_ENTRY_TAG = "VoterEntry: ";
|
||||
String GROUP_ID_TAG = "GroupID: ";
|
||||
String ADD_TO_GROUP_TAG = "setVoterGroups: ";
|
||||
String VOTE_ACTION_TAG = "VoteAction: ";
|
||||
public static final String ID_TAG = "ID: ";
|
||||
public static final String VOTER_ENTRY_TAG = "VoterEntry: ";
|
||||
public static final String GROUP_ID_TAG = "GroupID: ";
|
||||
public static final String ADD_TO_GROUP_TAG = "setVoterGroups: ";
|
||||
public static final String VOTE_ACTION_TAG = "VoteAction: ";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import meerkat.Registry;
|
||||
import meerkat.AsyncRegistry;
|
||||
import meerkat.bulletinboard.AsyncBulletinBoardClient;
|
||||
import meerkat.bulletinboard.ThreadedBulletinBoardClient;
|
||||
import meerkat.crypto.DigitalSignature;
|
||||
|
@ -20,6 +20,7 @@ import java.io.InputStream;
|
|||
import java.math.BigInteger;
|
||||
import java.security.KeyStore;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.SignatureException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
@ -51,7 +52,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
public static String KEYFILE_PASSWORD = "secret";
|
||||
Semaphore jobSemaphore;
|
||||
|
||||
class DummyRegistryCallBackHandler<T> implements meerkat.VoterRegistry.RegistryCallBack<T>{
|
||||
class DummyRegistryCallBackHandler<T> implements FutureCallback<T>{
|
||||
public int counter;
|
||||
public T data;
|
||||
|
||||
|
@ -61,14 +62,14 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void handleResult(T result) {
|
||||
public void onSuccess(T result) {
|
||||
counter++;
|
||||
data = result;
|
||||
jobSemaphore.release();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleFailure(Throwable throwable) {
|
||||
public void onFailure(Throwable 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() {
|
||||
bulletinBoardClient = new ThreadedBulletinBoardClient();
|
||||
|
@ -113,9 +110,11 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
|
||||
KeyStore.Builder keyStore = signer.getPKCS12KeyStoreBuilder(keyStream, password);
|
||||
signer.loadSigningCertificate(keyStore);
|
||||
signer.loadVerificationCertificates(getClass().getResourceAsStream(CERT1_PEM_EXAMPLE));
|
||||
|
||||
keyStream.close();
|
||||
signers.add(signer);
|
||||
|
||||
}
|
||||
catch (Exception e){
|
||||
assert false : "The signers creation failed ";
|
||||
|
@ -129,15 +128,13 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
public void setUp() {
|
||||
SetSigner();
|
||||
CommunicatorSetup();
|
||||
createCertificateStream();
|
||||
jobSemaphore = new Semaphore(0);
|
||||
}
|
||||
|
||||
private Registry GetRegistry()
|
||||
private AsyncRegistry GetRegistry()
|
||||
{
|
||||
Registry registry = new Registry();
|
||||
AsyncRegistry registry = new AsyncRegistry();
|
||||
registry.init(signers, bulletinBoardClient);
|
||||
registry.loadCertificateToSigners(certStream);
|
||||
return registry;
|
||||
}
|
||||
|
||||
|
@ -152,7 +149,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
@Test
|
||||
public void simpleRegistryCreation() {
|
||||
try {
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
} catch (Exception 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
|
||||
public void testAddVoter() throws InterruptedException {
|
||||
public void testAddVoter() throws InterruptedException, SignatureException {
|
||||
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
||||
|
||||
String id = generateString();
|
||||
String data = generateString();
|
||||
VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
|
||||
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
registry.addVoter(voterInfo, handler);
|
||||
|
||||
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
|
||||
public void testSetVoted() throws InterruptedException {
|
||||
public void testSetVoted() throws InterruptedException, SignatureException {
|
||||
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
||||
|
||||
String id = generateString();
|
||||
VoterID voterInfo = VoterID.newBuilder().setId(id).build();
|
||||
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
registry.setVoted(voterInfo, handler);
|
||||
|
||||
jobSemaphore.acquire();
|
||||
|
@ -261,7 +258,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
* Test that get groups retrieves the right groups the user are in
|
||||
*/
|
||||
@Test
|
||||
public void testAddToGroup() throws InterruptedException {
|
||||
public void testAddToGroup() throws InterruptedException, SignatureException {
|
||||
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
||||
|
||||
String voterId = generateString();
|
||||
|
@ -270,7 +267,7 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
.setVoterID(VoterID.newBuilder().setId(voterId))
|
||||
.addGroupID(GroupID.newBuilder().setId(groupId)).build();
|
||||
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
registry.setVoterGroups(voterInfo, handler);
|
||||
|
||||
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
|
||||
public void testGetGroups() throws InterruptedException {
|
||||
DummyRegistryCallBackHandler<List<BulletinBoardMessage>> handler =
|
||||
public void testGetGroups() throws InterruptedException, SignatureException {
|
||||
DummyRegistryCallBackHandler<Boolean> handler =
|
||||
new DummyRegistryCallBackHandler<>();
|
||||
|
||||
String voterId = generateString();
|
||||
|
@ -305,14 +302,14 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
|
||||
this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE);
|
||||
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
registry.setVoterGroups(voterInfo, handler);
|
||||
|
||||
jobSemaphore.acquire();
|
||||
assertEquals("The callback handler hasn't been called yet", 1, handler.counter );
|
||||
|
||||
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);
|
||||
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
|
||||
public void testGetVoter() throws InterruptedException {
|
||||
DummyRegistryCallBackHandler<List<BulletinBoardMessage>> handler =
|
||||
public void testGetVoter() throws InterruptedException, SignatureException {
|
||||
DummyRegistryCallBackHandler<Boolean> handler =
|
||||
new DummyRegistryCallBackHandler<>();
|
||||
|
||||
String id = generateString();
|
||||
|
@ -332,20 +329,20 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
VoterInfo voterInfo = VoterInfo.newBuilder().
|
||||
setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
|
||||
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
registry.addVoter(voterInfo, handler);
|
||||
|
||||
jobSemaphore.acquire();
|
||||
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);
|
||||
|
||||
jobSemaphore.acquire(1);
|
||||
assertEquals("The voter id doesn't match the created on ",
|
||||
id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG));
|
||||
assertTrue("The voter personal data can't be found.",
|
||||
findTagWithPrefix(personalHandler.data, data) != null);
|
||||
id, personalHandler.data.getId().getId());
|
||||
String personalData = personalHandler.data.getInfo();
|
||||
assertTrue("The voter personal data can't be found.", data.equals(personalData));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -353,12 +350,12 @@ public class SimpleRegistryTest /**extends TestCase**/ {
|
|||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testHasVoted () throws InterruptedException {
|
||||
public void testHasVoted () throws InterruptedException, SignatureException {
|
||||
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
||||
String id = generateString();
|
||||
VoterID voterInfo = VoterID.newBuilder().setId(id).build();
|
||||
|
||||
Registry registry = GetRegistry();
|
||||
AsyncRegistry registry = GetRegistry();
|
||||
registry.setVoted(voterInfo, handler);
|
||||
jobSemaphore.acquire();
|
||||
assertEquals("The callback handler hasn't been called yet", 1, handler.counter);
|
||||
|
|
Loading…
Reference in New Issue