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-02-19 11:41:02 -05:00
|
|
|
import meerkat.RegistryMessages;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.SimpleRegistry;
|
2016-02-20 10:48:10 -05:00
|
|
|
import meerkat.VoterRegistry;
|
2016-01-30 05:30:12 -05:00
|
|
|
import meerkat.VoterRegistryMessage;
|
2016-02-19 11:41:02 -05:00
|
|
|
import meerkat.bulletinboard.BulletinBoardClient;
|
2016-02-20 10:48:10 -05:00
|
|
|
import meerkat.bulletinboard.ThreadedBulletinBoardClient;
|
2016-02-19 11:41:02 -05:00
|
|
|
import meerkat.crypto.concrete.ECDSASignature;
|
2016-01-30 05:30:12 -05:00
|
|
|
import meerkat.protobuf.BulletinBoardAPI;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.protobuf.Voting;
|
|
|
|
import util.RegistryTags;
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
import java.io.InputStream;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.math.BigInteger;
|
2016-02-20 10:48:10 -05:00
|
|
|
import java.security.KeyStore;
|
2016-01-30 05:30:12 -05:00
|
|
|
import java.security.SecureRandom;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-02-20 10:48:10 -05:00
|
|
|
import java.util.concurrent.Semaphore;
|
2016-01-30 05:30:12 -05:00
|
|
|
|
|
|
|
import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
/**
|
2016-02-20 11:54:22 -05:00
|
|
|
* TODO: add logs prints for the tests to be clear what they are
|
2016-02-20 10:48:10 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
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-02-20 10:48:10 -05:00
|
|
|
private ECDSASignature signer;
|
2016-02-19 11:41:02 -05:00
|
|
|
private BulletinBoardClient communicator;
|
2016-01-30 05:30:12 -05:00
|
|
|
private SecureRandom random = new SecureRandom();
|
2016-02-20 10:48:10 -05:00
|
|
|
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
|
2016-02-20 11:54:22 -05:00
|
|
|
public void handleFailure(Throwable t){
|
2016-02-20 10:48:10 -05:00
|
|
|
messages = null;
|
|
|
|
jobSemaphore.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-20 10:48:10 -05:00
|
|
|
communicator = new ThreadedBulletinBoardClient();
|
|
|
|
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081";
|
2016-01-23 12:16:57 -05:00
|
|
|
communicator.init(Voting.BulletinBoardClientParams.newBuilder()
|
|
|
|
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
|
|
|
|
.setMinRedundancy((float) 1.0)
|
|
|
|
.build());
|
|
|
|
}
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
/**
|
|
|
|
* 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 ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-20 10:48:10 -05:00
|
|
|
SetSigner();
|
2016-01-23 12:16:57 -05:00
|
|
|
CommunicatorSetup();
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore = new Semaphore(0);
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
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-02-20 10:48:10 -05:00
|
|
|
new SimpleRegistry(signer, communicator);
|
2016-01-30 05:30:12 -05:00
|
|
|
} 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-02-20 10:48:10 -05:00
|
|
|
/**
|
|
|
|
* 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) {
|
2016-02-20 11:54:22 -05:00
|
|
|
if(message.GetWantedTagFromBasicMessage(tag)!=null){
|
|
|
|
wantedTagsCounter++;
|
|
|
|
}
|
2016-02-20 10:48:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(wantedTagsCounter == tags.size())
|
|
|
|
{
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-16 09:57:12 -05:00
|
|
|
/**
|
2016-02-19 11:41:02 -05:00
|
|
|
* Test that add voter creates new correct bulletin board message and adds the voter
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-02-20 10:48:10 -05:00
|
|
|
public void testAddVoter() throws InvalidProtocolBufferException, InterruptedException {
|
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
String id = new BigInteger(130, random).toString(32);
|
|
|
|
String data = new BigInteger(130, random).toString(32);
|
2016-02-19 11:41:02 -05:00
|
|
|
RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder().
|
2016-02-20 11:54:22 -05:00
|
|
|
setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build();
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.AddVoter(voterInfo, handler);
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter );
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}};
|
2016-02-19 09:17:43 -05:00
|
|
|
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
|
2016-02-19 11:41:02 -05:00
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
communicator.readMessages(filters, bulletinHandler);
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
tags.add(id);
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags);
|
|
|
|
assert counter == 1 : "The server don't have the new user data.";
|
2016-01-30 05:30:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-19 11:41:02 -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
|
|
|
*/
|
2016-02-20 10:48:10 -05:00
|
|
|
public void testSetVoted() throws InvalidProtocolBufferException, InterruptedException {
|
2016-02-19 11:41:02 -05:00
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
String id = new BigInteger(130, random).toString(32);
|
|
|
|
RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder().setId(id).build();
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.SetVoted(voterInfo, handler);
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter );
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}};
|
2016-02-19 11:41:02 -05:00
|
|
|
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
|
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
communicator.readMessages(filters, bulletinHandler);
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
tags.add(id);
|
2016-02-20 10:48:10 -05:00
|
|
|
int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags);
|
|
|
|
assert counter == 1 : "The server don't have the new user id.";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-30 12:19:25 -05:00
|
|
|
* Test that get groups retrieves the right groups the user are in
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-02-20 10:48:10 -05:00
|
|
|
public void testAddToGroup() throws InvalidProtocolBufferException, InterruptedException {
|
2016-02-19 11:41:02 -05:00
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
String voterId = new BigInteger(130, random).toString(32);
|
|
|
|
String groupId = new BigInteger(130, random).toString(32);
|
2016-02-19 11:41:02 -05:00
|
|
|
RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder()
|
2016-02-20 11:54:22 -05:00
|
|
|
.setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId))
|
|
|
|
.setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build();
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.AddToGroup(voterInfo, handler);
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.GROUP_ACTION_TAG .toString()
|
|
|
|
+ RegistryTags.ADD_TO_GROUP_TAG.toString());}};
|
2016-02-19 11:41:02 -05:00
|
|
|
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
|
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
communicator.readMessages(filters, bulletinHandler);
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
tags.add(voterId);
|
|
|
|
tags.add(groupId);
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags);
|
|
|
|
assert counter == 1 : "The server don't have the new user added to group.";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-30 12:19:25 -05:00
|
|
|
* Test that remove from group creates correct bulletin board message and removes the user from a group
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-01-30 12:19:25 -05:00
|
|
|
public void testGetGroups() throws InvalidProtocolBufferException, InterruptedException {
|
2016-02-19 11:41:02 -05:00
|
|
|
DummyRegistryCallBackHandler<List<BulletinBoardAPI.BulletinBoardMessage>> handler =
|
|
|
|
new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
String voterId = new BigInteger(130, random).toString(32);
|
|
|
|
String groupId = new BigInteger(130, random).toString(32);
|
2016-02-19 11:41:02 -05:00
|
|
|
RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder()
|
2016-02-20 11:54:22 -05:00
|
|
|
.setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId))
|
|
|
|
.setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build();
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.AddToGroup(voterInfo, handler);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter );
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
DummyRegistryCallBackHandler<List<String>> groupsHandler = new DummyRegistryCallBackHandler<>();
|
|
|
|
registry.GetGroups(RegistryMessages.GroupID.newBuilder().setId(groupId).build(), groupsHandler);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
List<String> userGroups = groupsHandler.data;
|
|
|
|
assert userGroups.contains(RegistryTags.GROUP_ID_TAG + groupId) :
|
|
|
|
"The simple voter registry object does not retrieved right user groups";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the personal data outputted about the user is right
|
|
|
|
*/
|
2016-01-30 12:19:25 -05:00
|
|
|
public void testGetPersonalIDDetails() throws InvalidProtocolBufferException, InterruptedException {
|
2016-02-20 10:48:10 -05:00
|
|
|
DummyRegistryCallBackHandler<List<BulletinBoardAPI.BulletinBoardMessage>> handler =
|
|
|
|
new DummyRegistryCallBackHandler<>();
|
|
|
|
|
|
|
|
String id = new BigInteger(130, random).toString(32);
|
|
|
|
String data = new BigInteger(130, random).toString(32);
|
2016-02-19 11:41:02 -05:00
|
|
|
RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder().
|
2016-02-20 10:48:10 -05:00
|
|
|
setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build();
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
SimpleRegistry registry = new SimpleRegistry(signer, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.AddVoter(voterInfo, handler);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter );
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
DummyRegistryCallBackHandler<VoterRegistryMessage> personalHandler = new DummyRegistryCallBackHandler<>();
|
|
|
|
registry.GetPersonIDDetails(RegistryMessages.VoterID.newBuilder().setId(id).build(), personalHandler);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(RegistryTags.ID_TAG + id,
|
|
|
|
personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG.toString()));
|
|
|
|
assertEquals(data,personalHandler.data.base.getData().toStringUtf8());
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
}
|