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; package meerkat;
import com.google.protobuf.Timestamp; 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.bulletinboard.AsyncBulletinBoardClient;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
import meerkat.protobuf.Crypto; 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.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.List; import java.util.List;
/** /**
@ -27,15 +32,14 @@ import java.util.List;
*/ */
public class Registry implements VoterRegistry{ public class Registry implements VoterRegistry{
protected DigitalSignature signer; protected Collection<DigitalSignature> signers;
protected AsyncBulletinBoardClient bulletinBoardClient ; protected AsyncBulletinBoardClient bulletinBoardClient ;
protected Crypto.Signature signature;
protected InputStream certificateStream; protected InputStream certificateStream;
public Registry(DigitalSignature signer, public void init(Collection<DigitalSignature> signers,
AsyncBulletinBoardClient communicator, AsyncBulletinBoardClient communicator,
InputStream certificateStream) { InputStream certificateStream) {
this.signer = signer; this.signers = signers;
this.bulletinBoardClient = communicator; this.bulletinBoardClient = communicator;
this.certificateStream = certificateStream; this.certificateStream = certificateStream;
} }
@ -47,22 +51,24 @@ public class Registry implements VoterRegistry{
* @param basicMessage BasicMessage * @param basicMessage BasicMessage
* @return BulletinBoardMessage * @return BulletinBoardMessage
*/ */
protected BulletinBoardMessage CreateBulletinBoardMessage(UnsignedBulletinBoardMessage basicMessage) { protected BulletinBoardMessage createBulletinBoardMessage(UnsignedBulletinBoardMessage basicMessage) {
try { try {
BulletinBoardMessage.Builder bulletinBoardMessage = BulletinBoardMessage.newBuilder(); 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); signer.loadVerificationCertificates(certificateStream);
signature = signer.sign(); }
bulletinBoardMessage.setMsg(basicMessage).addSig(signature);
return bulletinBoardMessage.build(); return bulletinBoardMessage.build();
} catch (SignatureException e) { } catch (SignatureException | CertificateException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
public void AddVoter(VoterInfo voterInfo, RegistryCallBack callback) { public void addVoter(VoterInfo voterInfo, RegistryCallBack callback) {
UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder(). UnsignedBulletinBoardMessage.newBuilder().
addTag(RegistryTags.ID_TAG + voterInfo.getId().getId()) addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
@ -70,10 +76,10 @@ public class Registry implements VoterRegistry{
.addTag(RegistryTags.VOTER_DATA_TAG + voterInfo.getInfo()) .addTag(RegistryTags.VOTER_DATA_TAG + voterInfo.getInfo())
.setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()).build()); .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.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder() UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId()) .addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId())
@ -85,33 +91,33 @@ public class Registry implements VoterRegistry{
basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId()); 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.Builder 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()).build()); .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) {{ List<String> GroupsActionsTags = new ArrayList<String>(2) {{
add(RegistryTags.GROUP_ID_TAG + groupID.getId()); add(RegistryTags.GROUP_ID_TAG + groupID.getId());
}}; }};
bulletinBoardClient.readMessages(MessageCollectionUtils.GenerateFiltersFromTags(GroupsActionsTags), bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags),
new LatestMessagesCallBack(callback, signer, signature, certificateStream)); new LatestMessagesCallBack(callback, signers));
} }
public void GetPersonIDDetails(VoterID voterID, RegistryCallBack callback) { public void getPersonIDDetails(VoterID voterID, RegistryCallBack callback) {
List<String> GroupsActionsTags = new ArrayList<String>() {{ List<String> GroupsActionsTags = 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);
}}; }};
bulletinBoardClient.readMessages(MessageCollectionUtils.GenerateFiltersFromTags(GroupsActionsTags), bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags),
new LatestMessagesCallBack(callback, signer, signature, certificateStream)); new LatestMessagesCallBack(callback, signers));
} }
} }

View File

@ -14,57 +14,57 @@ public interface VoterRegistry {
* in case of exception the handleFailure will be called * in case of exception the handleFailure will be called
*/ */
interface RegistryCallBack<T> { interface RegistryCallBack<T> {
void HandleResult(T result); void handleResult(T result);
void HandleFailure(Throwable throwable); void handleFailure(Throwable throwable);
} }
/** /**
* 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 false
* *
* @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 * @return void
*/ */
void AddVoter(VoterInfo voterInfo, RegistryCallBack callBack); void addVoter(VoterInfo voterInfo, RegistryCallBack callBack);
/** /**
* 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 null
* *
* @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 * @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 * 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 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 * @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 * 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 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 * 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 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) * @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.MessageValidator;
import meerkat.VoterRegistry.RegistryCallBack; import meerkat.VoterRegistry.RegistryCallBack;
import meerkat.crypto.DigitalSignature; import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.Crypto; import meerkat.protobuf.Crypto;
import meerkat.util.TimestampComparator; import meerkat.util.TimestampComparator;
import java.io.InputStream;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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 class LatestMessagesCallBack implements FutureCallback<List<BulletinBoardMessage>>, MessageValidator<List<BulletinBoardMessage>> {
public RegistryCallBack callback; public RegistryCallBack callback;
protected DigitalSignature validator; protected Collection<DigitalSignature> validators;
protected Crypto.Signature signature;
protected InputStream certificateStream;
/** /**
* Init MessagesCallBack * init MessagesCallBack
* @param callback voter registry callback object * @param callback voter registry callback object
* @param validator DigitalSignature object * @param validators DigitalSignature object
* @param signature Crypto.Signature object
*/ */
public LatestMessagesCallBack(RegistryCallBack callback, public LatestMessagesCallBack(RegistryCallBack callback, Collection<DigitalSignature> validators) {
DigitalSignature validator,
Crypto.Signature signature,
InputStream certificateStream) {
this.callback = callback; this.callback = callback;
this.validator = validator; this.validators = validators;
this.signature = signature;
this.certificateStream = certificateStream;
} }
/** /**
@ -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 * 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) * the latest tag from this list (in case of personal data)
* *
* @param msg List<BulletinBoardAPI.BulletinBoardMessage> * @param msg List<BulletinBoardAPI.BulletinBoardMessage>
@ -76,10 +66,10 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
public void onSuccess(List<BulletinBoardMessage> msg) { public void onSuccess(List<BulletinBoardMessage> msg) {
if (isAddToGroupsList(msg)) { if (isAddToGroupsList(msg)) {
List<String> groupsOfUser = GetListOfTags(msg, RegistryTags.GROUP_ID_TAG); List<String> groupsOfUser = GetListOfTags(msg, RegistryTags.GROUP_ID_TAG);
callback.HandleResult(groupsOfUser); callback.handleResult(groupsOfUser);
} }
else { else {
callback.HandleResult(Collections.max(msg, (first, second) -> { callback.handleResult(Collections.max(msg, (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());
})); }));
@ -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 * @param t the exception data that have been thrown during the failure of the post method
*/ */
@Override @Override
public void onFailure(Throwable t) { 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 * @param message BulletinBoardMessage instance
* @return true if the message is valid else false * @return true if the message is valid else false
*/ */
private boolean VerifyMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message) private boolean verifyMessage(BulletinBoardMessage message)
{ {
try { try {
validator.loadVerificationCertificates(certificateStream); int counter = 0;
validator.initVerify(signature); for (Crypto.Signature signature : message.getSigList()) {
validator.updateContent(message); for (DigitalSignature digitalSignature : validators) {
return validator.verify(); if (signature.getSignerId().equals(digitalSignature.getSignerID()))
} catch (CertificateException | InvalidKeyException | SignatureException e) { {
digitalSignature.initVerify(signature);
if(digitalSignature.verify())
{
counter++;
}
}
}
}
return counter == validators.size();
} catch (CertificateException | InvalidKeyException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
@ -124,7 +124,7 @@ public class LatestMessagesCallBack implements FutureCallback<List<BulletinBoard
List<BulletinBoardMessage> verifiedMessages = new ArrayList<>(object.size()); List<BulletinBoardMessage> verifiedMessages = new ArrayList<>(object.size());
for (BulletinBoardMessage message : object) for (BulletinBoardMessage message : object)
{ {
if(VerifyMessage(message.getMsg())) if(verifyMessage(message))
{ {
verifiedMessages.add(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 * @param tags the tags based on which the messages will be filtered
* @return MessageFilterList. * @return MessageFilterList.
*/ */
public static MessageFilterList GenerateFiltersFromTags(List<String> tags) { public static MessageFilterList generateFiltersFromTags(List<String> tags) {
MessageFilterList.Builder filters = MessageFilterList.newBuilder(); MessageFilterList.Builder filters = MessageFilterList.newBuilder();
if (tags.isEmpty()){ if (tags.isEmpty()){

View File

@ -16,7 +16,7 @@ public class MessagesCallBack implements FutureCallback<Boolean>
public VoterRegistry.RegistryCallBack callback; public VoterRegistry.RegistryCallBack callback;
/** /**
* Init MessagesCallBack * init MessagesCallBack
* @param callback voter registry callback object * @param callback voter registry callback object
*/ */
public MessagesCallBack(VoterRegistry.RegistryCallBack callback) { 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 * the action succeed
* *
* @param msg the message that the bulletinBoardClient passes to the callback * @param msg the message that the bulletinBoardClient passes to the callback
*/ */
@Override @Override
public void onSuccess(Boolean msg) { 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 * @param t the exception data that have been thrown during the failure of the post method
*/ */
@Override @Override
public void onFailure(Throwable t) { 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_ENTRY_TAG = "VoterEntry: ";
String VOTER_DATA_TAG = "Data: "; String VOTER_DATA_TAG = "Data: ";
String GROUP_ID_TAG = "GroupID: "; String GROUP_ID_TAG = "GroupID: ";
String ADD_TO_GROUP_TAG = "AddToGroups: "; String ADD_TO_GROUP_TAG = "addToGroups: ";
String VOTE_ACTION_TAG = "VoteAction: "; String VOTE_ACTION_TAG = "VoteAction: ";
} }

View File

@ -4,6 +4,7 @@ import junit.framework.TestCase;
import meerkat.Registry; import meerkat.Registry;
import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.AsyncBulletinBoardClient;
import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient;
import meerkat.crypto.DigitalSignature;
import meerkat.crypto.concrete.ECDSASignature; import meerkat.crypto.concrete.ECDSASignature;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; import meerkat.protobuf.BulletinBoardAPI.MessageFilterList;
@ -20,6 +21,7 @@ import java.math.BigInteger;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.Semaphore; import java.util.concurrent.Semaphore;
@ -37,7 +39,7 @@ import static meerkat.util.BulletinBoardUtils.findTagWithPrefix;
*/ */
public class SimpleRegistryTest extends TestCase { public class SimpleRegistryTest extends TestCase {
private ECDSASignature signer; private Collection<DigitalSignature> signers;
private AsyncBulletinBoardClient bulletinBoardClient; private AsyncBulletinBoardClient bulletinBoardClient;
private InputStream certStream; private InputStream certStream;
private SecureRandom random = new SecureRandom(); private SecureRandom random = new SecureRandom();
@ -57,14 +59,14 @@ public class SimpleRegistryTest extends TestCase {
} }
@Override @Override
public void HandleResult(T result) { public void handleResult(T result) {
counter++; counter++;
data = result; data = result;
jobSemaphore.release(); jobSemaphore.release();
} }
@Override @Override
public void HandleFailure(Throwable throwable) { public void handleFailure(Throwable throwable) {
} }
} }
@ -101,7 +103,8 @@ public class SimpleRegistryTest extends TestCase {
private void SetSigner(){ private void SetSigner(){
try { try {
signer = new ECDSASignature(); signers = new ArrayList<>();
ECDSASignature signer = new ECDSASignature();
InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE); InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE);
char[] password = KEYFILE_PASSWORD.toCharArray(); char[] password = KEYFILE_PASSWORD.toCharArray();
@ -109,9 +112,10 @@ public class SimpleRegistryTest extends TestCase {
signer.loadSigningCertificate(keyStore); signer.loadSigningCertificate(keyStore);
keyStream.close(); keyStream.close();
signers.add(signer);
} }
catch (Exception e){ 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); 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 * Checks if the creation of the registry have been successful
*/ */
public void testSimpleRegistryCreation() { public void testSimpleRegistryCreation() {
try { try {
new Registry(signer, bulletinBoardClient, certStream); Registry 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;
} }
@ -173,14 +184,14 @@ public class SimpleRegistryTest extends TestCase {
String data = new BigInteger(130, random).toString(32); String data = new BigInteger(130, random).toString(32);
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 = new Registry(signer, bulletinBoardClient, certStream); Registry registry = GetRegistry();
registry.AddVoter(voterInfo, handler); registry.addVoter(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(1, handler.counter );
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG);}}; 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(); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
bulletinBoardClient.readMessages(filters, bulletinHandler); bulletinBoardClient.readMessages(filters, bulletinHandler);
@ -202,14 +213,14 @@ public class SimpleRegistryTest extends TestCase {
String id = new BigInteger(130, random).toString(32); String id = new BigInteger(130, random).toString(32);
VoterID voterInfo = VoterID.newBuilder().setId(id).build(); VoterID voterInfo = VoterID.newBuilder().setId(id).build();
Registry registry = new Registry(signer, bulletinBoardClient, certStream); Registry registry = GetRegistry();
registry.SetVoted(voterInfo, handler); registry.setVoted(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(1, handler.counter );
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTE_ACTION_TAG);}}; 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(); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
bulletinBoardClient.readMessages(filters, bulletinHandler); bulletinBoardClient.readMessages(filters, bulletinHandler);
@ -234,14 +245,14 @@ 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 = new Registry(signer, bulletinBoardClient, certStream); Registry registry = GetRegistry();
registry.AddToGroups(voterInfo, handler); registry.addToGroups(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter); assertEquals(1, handler.counter);
List<String> tags = new ArrayList<String>(){{add(RegistryTags.ADD_TO_GROUP_TAG);}}; 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(); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
bulletinBoardClient.readMessages(filters, bulletinHandler); bulletinBoardClient.readMessages(filters, bulletinHandler);
@ -270,14 +281,14 @@ public class SimpleRegistryTest extends TestCase {
this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE); this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE);
Registry registry = new Registry(signer, bulletinBoardClient,certStream); Registry registry = GetRegistry();
registry.AddToGroups(voterInfo, handler); registry.addToGroups(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(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(GroupID.newBuilder().setId(groupId).build(), groupsHandler);
jobSemaphore.acquire(1); jobSemaphore.acquire(1);
List<String> userGroups = groupsHandler.data; List<String> userGroups = groupsHandler.data;
@ -296,14 +307,14 @@ 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 = new Registry(signer, bulletinBoardClient, certStream); Registry registry = GetRegistry();
registry.AddVoter(voterInfo, handler); registry.addVoter(voterInfo, handler);
jobSemaphore.acquire(); jobSemaphore.acquire();
assertEquals(1, handler.counter ); assertEquals(1, handler.counter );
DummyRegistryCallBackHandler<BulletinBoardMessage> personalHandler = new DummyRegistryCallBackHandler<>(); DummyRegistryCallBackHandler<BulletinBoardMessage> personalHandler = new DummyRegistryCallBackHandler<>();
registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); registry.getPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler);
jobSemaphore.acquire(1); jobSemaphore.acquire(1);
assertEquals(id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG)); assertEquals(id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG));