2016-01-30 05:30:12 -05:00
|
|
|
import com.google.protobuf.InvalidProtocolBufferException;
|
2016-01-16 09:57:12 -05:00
|
|
|
import junit.framework.TestCase;
|
2016-01-30 05:30:12 -05:00
|
|
|
import meerkat.RegistryCallBack;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.SimpleRegistry;
|
2016-01-30 05:30:12 -05:00
|
|
|
import meerkat.VoterRegistryMessage;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.bulletinboard.SimpleBulletinBoardClient;
|
|
|
|
import meerkat.crypto.concrete.ECElGamalEncryption;
|
|
|
|
import meerkat.crypto.concrete.ECElGamalUtils;
|
2016-01-30 05:30:12 -05:00
|
|
|
import meerkat.protobuf.BulletinBoardAPI;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.protobuf.ConcreteCrypto;
|
|
|
|
import meerkat.protobuf.Voting;
|
|
|
|
import org.factcenter.qilin.primitives.concrete.ECElGamal;
|
|
|
|
import org.factcenter.qilin.primitives.concrete.ECGroup;
|
|
|
|
import util.RegistryTags;
|
|
|
|
import util.SimpleRegistryCallBackHandler;
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.math.BigInteger;
|
2016-01-30 05:30:12 -05:00
|
|
|
import java.security.SecureRandom;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.security.spec.InvalidKeySpecException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-01-30 05:30:12 -05:00
|
|
|
|
|
|
|
import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
|
|
|
|
import static util.CollectionMessagesUtils.GetUnsignedBulletinBoardMessages;
|
2016-01-16 09:57:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Vladimir Eliezer Tokarev on 1/16/2016.
|
|
|
|
* Tests the Simple Registry contents
|
2016-01-23 12:16:57 -05:00
|
|
|
* NOTE: for most of this tests to pass there should run BulletinBoardServer
|
2016-01-30 05:30:12 -05:00
|
|
|
* that should be reachable on BULLETIN_BOARD_SERVER_ADDRESS
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public class SimpleRegistryTest extends TestCase {
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-01-23 12:16:57 -05:00
|
|
|
private ECElGamalEncryption signatory;
|
|
|
|
private SimpleBulletinBoardClient communicator;
|
2016-01-30 05:30:12 -05:00
|
|
|
private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081";
|
|
|
|
private SecureRandom random = new SecureRandom();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
/**
|
|
|
|
* Creates the ElGamal encryption object
|
|
|
|
*/
|
|
|
|
private void ElGamalSetup() {
|
2016-01-23 12:16:57 -05:00
|
|
|
try {
|
|
|
|
ECGroup group = new ECGroup("secp256k1");
|
|
|
|
BigInteger sk = ECElGamal.generateSecretKey(group, random);
|
|
|
|
ECElGamal.SK key = new ECElGamal.SK(group, sk);
|
|
|
|
ConcreteCrypto.ElGamalPublicKey serializedPk = ECElGamalUtils.serializePk(group, key);
|
|
|
|
signatory = new ECElGamalEncryption();
|
|
|
|
signatory.init(serializedPk);
|
|
|
|
} catch (InvalidKeySpecException e) {
|
2016-01-30 05:30:12 -05:00
|
|
|
assertTrue(false);
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
/**
|
|
|
|
* Creates the communication object (the bulletinBoardClient)
|
|
|
|
*/
|
|
|
|
private void CommunicatorSetup() {
|
2016-01-23 12:16:57 -05:00
|
|
|
communicator = new SimpleBulletinBoardClient();
|
|
|
|
communicator.init(Voting.BulletinBoardClientParams.newBuilder()
|
|
|
|
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
|
|
|
|
.setMinRedundancy((float) 1.0)
|
|
|
|
.build());
|
|
|
|
}
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
|
|
|
|
private SimpleRegistryCallBackHandler RegistryAnswersHandlerSetup() {
|
|
|
|
return new SimpleRegistryCallBackHandler();
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
/**
|
2016-01-16 09:57:12 -05:00
|
|
|
* Initialize SimpleRegistry object
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void setUp() {
|
2016-01-23 12:16:57 -05:00
|
|
|
ElGamalSetup();
|
|
|
|
CommunicatorSetup();
|
|
|
|
}
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-01-23 12:16:57 -05:00
|
|
|
/**
|
|
|
|
* Checks if the creation of the registry have been successful
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void testSimpleRegistryCreation() {
|
2016-01-23 12:16:57 -05:00
|
|
|
try {
|
2016-01-30 05:30:12 -05:00
|
|
|
new SimpleRegistry(signatory, communicator);
|
|
|
|
} catch (Exception e) {
|
2016-01-23 12:16:57 -05:00
|
|
|
assert false : "While creating the SimpleRegistry exception have been thrown " + e;
|
|
|
|
}
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-30 05:30:12 -05:00
|
|
|
* Creates list of lists of strings for different testing purposes
|
|
|
|
* @param actionDataNumber number of string in a list
|
|
|
|
* @param numberOfInstances number of lists in a list
|
|
|
|
* @return List<List<String>>
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
private List<List<String>> getActionsData(int actionDataNumber, int numberOfInstances)
|
|
|
|
{
|
|
|
|
System.out.println("- Creating "+ numberOfInstances +" ids and personal data strings for adding different voters.");
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
List<List<String>> actionData = new ArrayList<>();
|
|
|
|
for (int i = 0 ; i < numberOfInstances ; i++ ){
|
|
|
|
List<String> data = new ArrayList<>();
|
|
|
|
for (int j = 0 ; j < actionDataNumber ; j++){
|
|
|
|
data.add(new BigInteger(130, random).toString(32));
|
|
|
|
}
|
|
|
|
actionData.add(data);
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
2016-01-30 05:30:12 -05:00
|
|
|
return actionData;
|
|
|
|
}
|
2016-01-16 09:58:09 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
/**
|
|
|
|
* Create Message filter list from list of tags
|
|
|
|
* @param tags list of string with wanted tags
|
|
|
|
* @return MessageFilterList
|
|
|
|
*/
|
|
|
|
private BulletinBoardAPI.MessageFilterList createFiltersFromTags(List<String> tags){
|
|
|
|
if (tags == null){
|
|
|
|
return BulletinBoardAPI.MessageFilterList.newBuilder().build();
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
BulletinBoardAPI.MessageFilterList.Builder filters = BulletinBoardAPI.MessageFilterList.newBuilder();
|
|
|
|
|
|
|
|
for (String tag : tags) {
|
|
|
|
filters.addFilter(BulletinBoardAPI.MessageFilter.newBuilder()
|
|
|
|
.setTag(tag).setType(BulletinBoardAPI.FilterType.TAG));
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counts the amount of posted messaages that have their tags in voterData
|
|
|
|
* @param votersData List<List<String>> of information about voters (tags etc...)
|
|
|
|
* @param tags List<String> of tags
|
|
|
|
* @return int
|
|
|
|
* @throws InvalidProtocolBufferException
|
|
|
|
*/
|
|
|
|
private int countActualAddedMessages(List<List<String>> votersData, List<String> tags) throws InvalidProtocolBufferException {
|
|
|
|
System.out.println("- Check that the server have the new voters data:");
|
|
|
|
|
|
|
|
BulletinBoardAPI.MessageFilterList filters = createFiltersFromTags(tags);
|
|
|
|
List<VoterRegistryMessage> messages =
|
|
|
|
ConvertToVoterRegistryMessages(GetUnsignedBulletinBoardMessages(communicator.readMessages(filters)));
|
|
|
|
|
|
|
|
System.out.println(messages.size() + " asdasasdasdasasasdaasddas");
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
int addedMessagesCounter = 0;
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
for (List<String> voterData : votersData) {
|
|
|
|
for (VoterRegistryMessage message : messages) {
|
|
|
|
String addedVoterId = message.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG);
|
|
|
|
String addedVoterData = new String(message.base.getData().toByteArray());
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
if (voterData.size() == 2 && addedVoterId.contains(voterData.get(0)) && addedVoterData.contains(voterData.get(1))) {
|
|
|
|
addedMessagesCounter++;
|
|
|
|
}
|
|
|
|
else if (addedVoterId.contains(voterData.get(0)) ){
|
|
|
|
addedMessagesCounter++;
|
|
|
|
}
|
|
|
|
}
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
2016-01-30 05:30:12 -05:00
|
|
|
return addedMessagesCounter;
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
/**
|
|
|
|
* Call methodToCheck voterData as list of params
|
|
|
|
* @param methodToCheck the name of the wanted method
|
|
|
|
* @param numberOfParamsWithoutHandler the amount of params to pass to the wanted method
|
|
|
|
* @param handler object which method will be called as callback from SimpleRegistry
|
|
|
|
* @param registry the simpleRegistry object
|
|
|
|
* @param voterData List of string which is the params to pass to the wantedMethod
|
|
|
|
*/
|
|
|
|
private void CallWantedMethod(String methodToCheck, int numberOfParamsWithoutHandler,
|
|
|
|
SimpleRegistryCallBackHandler handler, SimpleRegistry registry,
|
|
|
|
List<String> voterData) {
|
|
|
|
try {
|
|
|
|
if (numberOfParamsWithoutHandler == 1)
|
|
|
|
{
|
|
|
|
java.lang.reflect.Method method =
|
|
|
|
registry.getClass().getMethod(methodToCheck,String.class, RegistryCallBack.class);
|
|
|
|
method.invoke(registry, voterData.get(0), handler);
|
|
|
|
assertTrue(handler.ActionSucceed);
|
|
|
|
}else {
|
|
|
|
java.lang.reflect.Method method =
|
|
|
|
registry.getClass().getMethod( methodToCheck, String.class, String.class, RegistryCallBack.class);
|
|
|
|
method.invoke(registry, voterData.get(0), voterData.get(1), handler);
|
|
|
|
assertTrue(handler.ActionSucceed);
|
|
|
|
}
|
|
|
|
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new Registry information then sends it to the server then checks the server
|
|
|
|
* have all sent data
|
|
|
|
* @param methodToCheck the name of the method we want to call in SimpleRegistry
|
|
|
|
* @param numberOfParamsToPass the number of params to pass to the methodToCheck
|
|
|
|
* @param numberOfChecks the amount of checks to run
|
|
|
|
* @param tags list of String that represent the tags to filter on the messages
|
|
|
|
* @throws InvalidProtocolBufferException
|
|
|
|
*/
|
|
|
|
private void checkMessagesPostedSuccessfully(String methodToCheck, int numberOfParamsToPass, int numberOfChecks, List<String> tags) throws InvalidProtocolBufferException {
|
|
|
|
SimpleRegistryCallBackHandler handler = RegistryAnswersHandlerSetup();
|
|
|
|
System.out.println("Starting testing the " + methodToCheck + " capability.");
|
|
|
|
List<List<String>> votersData = getActionsData(numberOfParamsToPass, numberOfChecks);
|
|
|
|
|
|
|
|
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
|
|
|
|
|
|
|
|
System.out.println("- Check that all the callbacks have been called successfully after voters adding:");
|
|
|
|
for (List<String> voterData : votersData) {
|
|
|
|
CallWantedMethod(methodToCheck, numberOfParamsToPass, handler, registry, voterData);
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("- Check that for every voter added, the callback have been called:");
|
|
|
|
assertEquals(numberOfChecks, handler.counter);
|
|
|
|
int addedMessagesCounter = countActualAddedMessages(votersData, tags);
|
|
|
|
|
|
|
|
assert addedMessagesCounter == numberOfChecks : "The number of added messages actually is " + addedMessagesCounter;
|
|
|
|
System.out.println("Ended addVoter testing.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that add voter creates new correct bulletin board message and adds the voter
|
|
|
|
*/
|
|
|
|
public void testAddVoter() throws InvalidProtocolBufferException {
|
|
|
|
checkMessagesPostedSuccessfully("AddVoter", 2, 3,
|
|
|
|
new ArrayList<String>(){{add(RegistryTags.VOTER_ENTRY_TAG.toString());}});
|
|
|
|
}
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-16 09:57:12 -05:00
|
|
|
/**
|
|
|
|
* Test that set voted posts creates correct bulletin board message and sets that the user have been voted
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void testSetVoted() throws InvalidProtocolBufferException {
|
|
|
|
checkMessagesPostedSuccessfully("SetVoted", 1, 3,
|
|
|
|
new ArrayList<String>(){{add(RegistryTags.VOTE_ACTION_TAG.toString());}});
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that add to group creates correct bulletin board message and adds a user to a group
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void testRemoveFromGroup() throws InvalidProtocolBufferException {
|
|
|
|
checkMessagesPostedSuccessfully("RemoveFromGroup", 2, 3, null);
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that remove from group creates correct bulletin board message and removes the user from a group
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void testGetGroups() {
|
|
|
|
// print start of gorups testing
|
|
|
|
// Create Registry check creation
|
|
|
|
// Add to X groups remove y group and z group
|
|
|
|
// for every action check that it happened
|
|
|
|
// count the number of callback activated
|
|
|
|
// get the relevant groups
|
|
|
|
// check they are right
|
|
|
|
// print and of groups
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that get groups retrieves the right groups the user are in
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void testAddToGroup() throws InvalidProtocolBufferException {
|
|
|
|
checkMessagesPostedSuccessfully("AddToGroup", 2, 3, null);
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the personal data outputted about the user is right
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void testGetPersonalIDDetails() {
|
|
|
|
assertEquals(1, null);
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
}
|