import com.google.protobuf.InvalidProtocolBufferException; import junit.framework.TestCase; import meerkat.RegistryMessages; import meerkat.SimpleRegistry; import meerkat.VoterRegistryMessage; import meerkat.bulletinboard.BulletinBoardClient; import meerkat.bulletinboard.SimpleBulletinBoardClient; import meerkat.crypto.concrete.ECDSASignature; import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.Voting; import util.RegistryTags; import java.math.BigInteger; import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; /** * Created by Vladimir Eliezer Tokarev on 1/16/2016. * Tests the Simple Registry contents * NOTE: for most of this tests to pass there should run BulletinBoardServer * that should be reachable on BULLETIN_BOARD_SERVER_ADDRESS */ public class SimpleRegistryTest extends TestCase { private ECDSASignature signatory; private BulletinBoardClient communicator; private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; private SecureRandom random = new SecureRandom(); /** * Creates the communication object (the bulletinBoardClient) */ private void CommunicatorSetup() { communicator = new SimpleBulletinBoardClient(); communicator.init(Voting.BulletinBoardClientParams.newBuilder() .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) .setMinRedundancy((float) 1.0) .build()); } /** * Initialize SimpleRegistry object */ public void setUp() { signatory = new ECDSASignature(); CommunicatorSetup(); } /** * Checks if the creation of the registry have been successful */ public void testSimpleRegistryCreation() { try { new SimpleRegistry(signatory, communicator); } catch (Exception e) { assert false : "While creating the SimpleRegistry exception have been thrown " + e; } } /** * Test that add voter creates new correct bulletin board message and adds the voter */ public void testAddVoter() throws InvalidProtocolBufferException { DummyRegistryCallBackHandler 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(); SimpleRegistry registry = new SimpleRegistry(signatory, communicator); registry.AddVoter(voterInfo, handler); assertEquals(handler.counter , 0); List tags = new ArrayList<>(); tags.add("AddVoter"); BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); List messages = bulletinHandler.getMessages(); assert messages.contains(voterInfo) : "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 { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder() .setId(RegistryTags.ID_TAG + new BigInteger(130, random).toString(32)).build(); SimpleRegistry registry = new SimpleRegistry(signatory, communicator); registry.SetVoted(voterInfo, handler); assertEquals(handler.counter , 0); List tags = new ArrayList<>(); tags.add("SetVoted"); BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); List messages = bulletinHandler.getMessages(); assert messages.contains(voterInfo) : "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 { DummyRegistryCallBackHandler 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 tags = new ArrayList<>(); tags.add("AddToGroup"); BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); List messages = bulletinHandler.getMessages(); assert messages.contains(voterInfo) : "The server don't have the new user added to group."; } /** * Test that remove from group creates correct bulletin board message and removes the user from a group */ public void testGetGroups() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler> 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 tags = new ArrayList<>(); tags.add("AddToGroup"); BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); List messages = bulletinHandler.getMessages(); assert messages.contains(voterInfo) : "The server don't have the new user added to group."; List voterMessages = ConvertToVoterRegistryMessages(handler.data); assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG).contains(voterInfo.getVoterId().getId())); } /** * Test that the personal data outputted about the user is right */ public void testGetPersonalIDDetails() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler> 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(); SimpleRegistry registry = new SimpleRegistry(signatory, communicator); registry.AddVoter(voterInfo, handler); assertEquals(handler.counter , 0); List tags = new ArrayList<>(); tags.add("AddVoter"); BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); communicator.readMessages(filters, bulletinHandler); List messages = bulletinHandler.getMessages(); assert messages.contains(voterInfo) : "The server don't have the new user data."; List voterMessages = ConvertToVoterRegistryMessages(handler.data); assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG).contains(voterInfo.getId().getId())); } }