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-01-30 05:30:12 -05:00
|
|
|
import meerkat.VoterRegistryMessage;
|
2016-02-19 11:41:02 -05:00
|
|
|
import meerkat.bulletinboard.BulletinBoardClient;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.bulletinboard.SimpleBulletinBoardClient;
|
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;
|
|
|
|
|
|
|
|
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.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-01-30 05:30:12 -05:00
|
|
|
|
|
|
|
import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages;
|
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-19 11:41:02 -05:00
|
|
|
private ECDSASignature signatory;
|
|
|
|
private BulletinBoardClient 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 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
|
|
|
/**
|
2016-01-16 09:57:12 -05:00
|
|
|
* Initialize SimpleRegistry object
|
|
|
|
*/
|
2016-01-30 05:30:12 -05:00
|
|
|
public void setUp() {
|
2016-02-19 11:41:02 -05:00
|
|
|
signatory = new ECDSASignature();
|
2016-01-23 12:16:57 -05:00
|
|
|
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-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-19 11:41:02 -05:00
|
|
|
public void testAddVoter() throws InvalidProtocolBufferException {
|
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
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();
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
|
|
|
|
registry.AddVoter(voterInfo, handler);
|
|
|
|
|
|
|
|
assertEquals(handler.counter , 0);
|
|
|
|
|
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
tags.add("AddVoter");
|
2016-01-30 05:30:12 -05:00
|
|
|
|
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-19 11:41:02 -05:00
|
|
|
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
|
|
|
|
|
|
|
|
assert messages.contains(voterInfo) : "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-19 11:41:02 -05:00
|
|
|
public void testSetVoted() throws InvalidProtocolBufferException {
|
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
|
|
|
|
RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder()
|
|
|
|
.setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)).build();
|
2016-01-30 05:30:12 -05:00
|
|
|
|
|
|
|
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.SetVoted(voterInfo, handler);
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
assertEquals(handler.counter , 0);
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
tags.add("SetVoted");
|
2016-01-30 05:30:12 -05:00
|
|
|
|
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-19 11:41:02 -05:00
|
|
|
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
assert messages.contains(voterInfo) : "The server don't have the new user id.";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
|
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-01-30 12:19:25 -05:00
|
|
|
public void testAddToGroup() throws InvalidProtocolBufferException {
|
2016-02-19 11:41:02 -05:00
|
|
|
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))))
|
|
|
|
.setGroupId(RegistryMessages.GroupID.newBuilder()
|
|
|
|
.setId((RegistryTags.GROUP_ID_TAG + new BigInteger(130, random).toString(32)))).build();
|
|
|
|
|
|
|
|
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
|
|
|
|
registry.AddToGroup(voterInfo, handler);
|
|
|
|
|
|
|
|
assertEquals(handler.counter , 0);
|
|
|
|
|
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
tags.add("AddToGroup");
|
|
|
|
|
|
|
|
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
|
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
communicator.readMessages(filters, bulletinHandler);
|
|
|
|
|
|
|
|
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
|
|
|
|
|
|
|
|
assert messages.contains(voterInfo) : "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<>();
|
|
|
|
|
|
|
|
RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder()
|
|
|
|
.setVoterId(RegistryMessages.VoterID.newBuilder()
|
|
|
|
.setId((RegistryTags.ID_TAG + new BigInteger(130, random).toString(32))))
|
|
|
|
.setGroupId(RegistryMessages.GroupID.newBuilder()
|
|
|
|
.setId((RegistryTags.GROUP_ID_TAG + new BigInteger(130, random).toString(32)))).build();
|
2016-01-30 12:19:25 -05:00
|
|
|
|
|
|
|
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.AddToGroup(voterInfo, handler);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
assertEquals(handler.counter , 0);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
tags.add("AddToGroup");
|
|
|
|
|
|
|
|
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
|
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
communicator.readMessages(filters, bulletinHandler);
|
|
|
|
|
|
|
|
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
|
|
|
|
|
|
|
|
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()));
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
2016-01-30 12:19:25 -05:00
|
|
|
|
|
|
|
|
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-19 11:41:02 -05:00
|
|
|
DummyRegistryCallBackHandler<List<BulletinBoardAPI.BulletinBoardMessage>> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
|
|
|
|
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();
|
2016-01-30 12:19:25 -05:00
|
|
|
|
|
|
|
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
|
2016-02-19 11:41:02 -05:00
|
|
|
registry.AddVoter(voterInfo, handler);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
assertEquals(handler.counter , 0);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
tags.add("AddVoter");
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-19 11:41:02 -05:00
|
|
|
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
|
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
communicator.readMessages(filters, bulletinHandler);
|
|
|
|
|
|
|
|
List<BulletinBoardAPI.BulletinBoardMessage> messages = bulletinHandler.getMessages();
|
|
|
|
|
|
|
|
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()));
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
}
|