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