Refactoring the code of voter-registry tests according to Arbel Peled CR

Voter-Registry
Vladimir Eliezer Tokarev 2016-02-20 07:48:10 -08:00
parent 8c0e7f57bb
commit 3347a6c42d
6 changed files with 161 additions and 141 deletions

View File

@ -51,6 +51,9 @@ dependencies {
// Google protobufs
compile 'com.google.protobuf:protobuf-java:3.+'
// Depend on test resources from meerkat-common
testCompile project(path: ':meerkat-common', configuration: 'testOutput')
testCompile 'junit:junit:4.+'
runtime 'org.codehaus.groovy:groovy:2.4.+'

View File

@ -42,10 +42,11 @@ public class SimpleRegistry implements VoterRegistry{
BulletinBoardMessage.newBuilder();
signatory.updateContent(basicMessage);
Crypto.Signature signature = signatory.sign();
bulletinBoardMessage.addSig(signature);
bulletinBoardMessage.setMsg(basicMessage);
bulletinBoardMessage.addSig(signature);
return bulletinBoardMessage.build();
} catch (SignatureException e) {

View File

@ -28,9 +28,9 @@ public class VoterRegistryMessage {
* @param tagName the name of the tag
* @return string
*/
public String GetWantedTagFromBasicMessage(RegistryTags tagName) {
public String GetWantedTagFromBasicMessage(String tagName) {
for (String tag : base.getTagList()) {
if (tag.contains(tagName.toString())) {
if (tag.contains(tagName)) {
return tag;
}
}

View File

@ -1,51 +0,0 @@
import meerkat.bulletinboard.BulletinBoardClient;
import meerkat.protobuf.BulletinBoardAPI;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Vladimir Eliezer Tokarev on 2/19/2016.
* Class that gives the ability of retrieve data from BulletinBoardClient synchronously
*/
public class DummyBulletinBoardCallBackHandler implements
BulletinBoardClient.ClientCallback<List<BulletinBoardAPI.BulletinBoardMessage>> {
private List<BulletinBoardAPI.BulletinBoardMessage> messages;
public DummyBulletinBoardCallBackHandler()
{
messages = new ArrayList<>();
}
/**
* Sets the received messages to be messages
* @param msg List<BulletinBoardAPI.BulletinBoardMessage> passed by BulletinBoardClient
*/
@Override
public void handleCallback(List<BulletinBoardAPI.BulletinBoardMessage> msg) {
messages = msg;
}
/**
* Sets the messages to be null because during the BulletinBoardAction was an error
* @param t the information of the excretion thrown during the process
*/
@Override
public void handleFailure(Throwable t) {
messages = null;
}
/**
* Method that waits that the messages will arrive from bulletin board client
* @return List<BulletinBoardAPI.BulletinBoardMessage>
*/
public List<BulletinBoardAPI.BulletinBoardMessage> getMessages(){
while(messages.isEmpty()){
if (messages == null)
{
return null;
}
}
return messages;
}
}

View File

@ -1,32 +0,0 @@
import meerkat.VoterRegistry;
/**
* Created by Vladimir Eliezer Tokarev on 2/19/2016.
* Object that gives the ability of synchronously access to SimpleVoterRegistry returned values
*/
public class DummyRegistryCallBackHandler<T> implements VoterRegistry.RegistryCallBack<T>{
/**
* The amount of times HandleResult had been called
*/
public int counter;
/**
* The data the VoterRegistry have returned
*/
public T data;
public DummyRegistryCallBackHandler()
{
counter=0;
}
/**
* Sets the retrieved data into data field and increases the counter
* @param result the answer VoterRegistry passed
*/
@Override
public void HandleResult(T result) {
counter++;
data = result;
}
}

View File

@ -2,21 +2,31 @@ import com.google.protobuf.InvalidProtocolBufferException;
import junit.framework.TestCase;
import meerkat.RegistryMessages;
import meerkat.SimpleRegistry;
import meerkat.VoterRegistry;
import meerkat.VoterRegistryMessage;
import meerkat.bulletinboard.BulletinBoardClient;
import meerkat.bulletinboard.SimpleBulletinBoardClient;
import meerkat.bulletinboard.ThreadedBulletinBoardClient;
import meerkat.crypto.concrete.ECDSASignature;
import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.Voting;
import util.RegistryTags;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
/**
* TODO: Have to do more relevant prints (print what happens)
* TODO: repair all the other methods
*/
/**
* Created by Vladimir Eliezer Tokarev on 1/16/2016.
* Tests the Simple Registry contents
@ -25,28 +35,86 @@ import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
*/
public class SimpleRegistryTest extends TestCase {
private ECDSASignature signatory;
private ECDSASignature signer;
private BulletinBoardClient communicator;
private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081";
private SecureRandom random = new SecureRandom();
public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12";
public static String KEYFILE_PASSWORD = "secret";
Semaphore jobSemaphore;
class DummyRegistryCallBackHandler<T> implements VoterRegistry.RegistryCallBack<T>{
public int counter;
public T data;
public DummyRegistryCallBackHandler()
{
counter=0;
}
@Override
public void HandleResult(T result) {
counter++;
data = result;
jobSemaphore.release();
}
}
public class DummyBulletinBoardCallBackHandler implements BulletinBoardClient.ClientCallback<List<BulletinBoardAPI.BulletinBoardMessage>> {
public List<BulletinBoardAPI.BulletinBoardMessage> messages;
@Override
public void handleCallback(List<BulletinBoardAPI.BulletinBoardMessage> msg)
{
messages = msg;
jobSemaphore.release();
}
@Override
public void handleFailure(Throwable t) throws Throwable {
messages = null;
jobSemaphore.release();
throw t;
}
}
/**
* Creates the communication object (the bulletinBoardClient)
*/
private void CommunicatorSetup() {
communicator = new SimpleBulletinBoardClient();
communicator = new ThreadedBulletinBoardClient();
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081";
communicator.init(Voting.BulletinBoardClientParams.newBuilder()
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
.setMinRedundancy((float) 1.0)
.build());
}
/**
* Creates the signer object which is the ECDSASignature
*/
private void SetSigner(){
try {
signer = new ECDSASignature();
InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE);
char[] password = KEYFILE_PASSWORD.toCharArray();
KeyStore.Builder keyStore = signer.getPKCS12KeyStoreBuilder(keyStream, password);
signer.loadSigningCertificate(keyStore);
keyStream.close();
}
catch (Exception e){
assert false : "The signer creation failed ";
}
}
/**
* Initialize SimpleRegistry object
*/
public void setUp() {
signatory = new ECDSASignature();
SetSigner();
CommunicatorSetup();
jobSemaphore = new Semaphore(0);
}
/**
@ -54,95 +122,124 @@ public class SimpleRegistryTest extends TestCase {
*/
public void testSimpleRegistryCreation() {
try {
new SimpleRegistry(signatory, communicator);
new SimpleRegistry(signer, communicator);
} catch (Exception e) {
assert false : "While creating the SimpleRegistry exception have been thrown " + e;
}
}
/**
* Counts the amount of messages from messages that have all the wanted tags inside
* @param messages List<VoterRegistryMessage>
* @param tags List<RegistryTags>
* @return integer that represent the amount of messages with wanted tags
*/
private int countMessagesWithTags(List<VoterRegistryMessage> messages, List<String> tags)
{
int counter = 0 ;
for (VoterRegistryMessage message :messages) {
int wantedTagsCounter = 0;
for (String tag : tags) {
if(message.GetWantedTagFromBasicMessage(tag)!=null){
wantedTagsCounter++;
}
}
if(wantedTagsCounter == tags.size())
{
counter++;
}
}
return counter;
}
/**
* Test that add voter creates new correct bulletin board message and adds the voter
*/
public void testAddVoter() throws InvalidProtocolBufferException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
public void testAddVoter() throws InvalidProtocolBufferException, InterruptedException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder().
setId(RegistryMessages.VoterID.newBuilder()
.setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)))
.setId(new BigInteger(130, random).toString(32)))
.setInfo(new BigInteger(130, random).toString(32)).build();
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
registry.AddVoter(voterInfo, handler);
assertEquals(handler.counter , 0);
List<String> tags = new ArrayList<>();
tags.add("AddVoter");
jobSemaphore.acquire();
assertEquals(handler.counter , 1);
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler);
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
jobSemaphore.acquire();
assert messages.contains(voterInfo) : "The server don't have the new user data.";
int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags);
assert counter == 1 : "The server don't have the new user data.";
}
/**
* Test that set voted posts creates correct bulletin board message and sets that the user have been voted
*/
public void testSetVoted() throws InvalidProtocolBufferException {
public void testSetVoted() throws InvalidProtocolBufferException, InterruptedException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder()
.setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)).build();
.setId(new BigInteger(130, random).toString(32)).build();
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
registry.SetVoted(voterInfo, handler);
jobSemaphore.acquire();
assertEquals(handler.counter , 0);
List<String> tags = new ArrayList<>();
tags.add("SetVoted");
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler);
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
jobSemaphore.acquire();
assert messages.contains(voterInfo) : "The server don't have the new user id.";
int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags);
assert counter == 1 : "The server don't have the new user id.";
}
/**
* Test that get groups retrieves the right groups the user are in
*/
public void testAddToGroup() throws InvalidProtocolBufferException {
public void testAddToGroup() throws InvalidProtocolBufferException, InterruptedException {
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder()
.setVoterId(RegistryMessages.VoterID.newBuilder()
.setId((RegistryTags.ID_TAG + new BigInteger(130, random).toString(32))))
.setId((new BigInteger(130, random).toString(32))))
.setGroupId(RegistryMessages.GroupID.newBuilder()
.setId((RegistryTags.GROUP_ID_TAG + new BigInteger(130, random).toString(32)))).build();
.setId((new BigInteger(130, random).toString(32)))).build();
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
registry.AddToGroup(voterInfo, handler);
jobSemaphore.acquire();
assertEquals(handler.counter , 0);
List<String> tags = new ArrayList<>();
tags.add("AddToGroup");
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.GROUP_ACTION_TAG .toString()
+ RegistryTags.ADD_TO_GROUP_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler);
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
jobSemaphore.acquire();
assert messages.contains(voterInfo) : "The server don't have the new user added to group.";
int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags);
assert counter == 1 : "The server don't have the new user added to group.";
}
/**
@ -154,27 +251,27 @@ public class SimpleRegistryTest extends TestCase {
RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder()
.setVoterId(RegistryMessages.VoterID.newBuilder()
.setId((RegistryTags.ID_TAG + new BigInteger(130, random).toString(32))))
.setId((new BigInteger(130, random).toString(32))))
.setGroupId(RegistryMessages.GroupID.newBuilder()
.setId((RegistryTags.GROUP_ID_TAG + new BigInteger(130, random).toString(32)))).build();
.setId((new BigInteger(130, random).toString(32)))).build();
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
registry.AddToGroup(voterInfo, handler);
jobSemaphore.acquire();
assertEquals(handler.counter , 0);
List<String> tags = new ArrayList<>();
tags.add("AddToGroup");
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.GROUP_ACTION_TAG .toString()
+ RegistryTags.ADD_TO_GROUP_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler);
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
jobSemaphore.acquire();
assert messages.contains(voterInfo) : "The server don't have the new user added to group.";
List<VoterRegistryMessage> voterMessages = ConvertToVoterRegistryMessages(handler.data);
assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG).contains(voterInfo.getVoterId().getId()));
assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG.toString()).contains(voterInfo.getVoterId().getId()));
}
@ -183,29 +280,31 @@ public class SimpleRegistryTest extends TestCase {
* Test that the personal data outputted about the user is right
*/
public void testGetPersonalIDDetails() throws InvalidProtocolBufferException, InterruptedException {
DummyRegistryCallBackHandler<List<BulletinBoardAPI.BulletinBoardMessage>> handler = new DummyRegistryCallBackHandler<>();
DummyRegistryCallBackHandler<List<BulletinBoardAPI.BulletinBoardMessage>> handler =
new DummyRegistryCallBackHandler<>();
String id = new BigInteger(130, random).toString(32);
String data = new BigInteger(130, random).toString(32);
RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder().
setId(RegistryMessages.VoterID.newBuilder()
.setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)))
.setInfo(new BigInteger(130, random).toString(32)).build();
setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build();
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
registry.AddVoter(voterInfo, handler);
jobSemaphore.acquire();
assertEquals(handler.counter , 0);
List<String> tags = new ArrayList<>();
tags.add("AddVoter");
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler);
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
jobSemaphore.acquire();
assert messages.contains(voterInfo) : "The server don't have the new user data.";
List<VoterRegistryMessage> voterMessages = ConvertToVoterRegistryMessages(handler.data);
assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG).contains(voterInfo.getId().getId()));
int counter = countMessagesWithTags(voterMessages, new ArrayList<String>(){{add(id);add(data);}});
assert counter == 1 : "The method doesnt retreived the wanted information.";
}
}