meerkat-java/voter-registry/src/test/java/SimpleRegistryTest.java

149 lines
4.9 KiB
Java

import junit.framework.TestCase;
import meerkat.SimpleRegistry;
import meerkat.bulletinboard.SimpleBulletinBoardClient;
import meerkat.crypto.concrete.ECElGamalEncryption;
import meerkat.crypto.concrete.ECElGamalUtils;
import meerkat.protobuf.BulletinBoardAPI.*;
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;
import java.math.BigInteger;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 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
* and the sql server and it should be reachable on
* BULLETIN_BOARD_SERVER_ADDRESS location
*/
public class SimpleRegistryTest extends TestCase{
private ECElGamalEncryption signatory;
private SimpleBulletinBoardClient communicator;
private static String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:3306";
private Random random = new Random(0); // Insecure deterministic random for testing.
private SimpleRegistryCallBackHandler handler;
private void ElGamalSetup(){
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) {
assertTrue(false);
}
}
private void CommunicatorSetup(){
communicator = new SimpleBulletinBoardClient();
communicator.init(Voting.BulletinBoardClientParams.newBuilder()
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
.setMinRedundancy((float) 1.0)
.build());
}
private void RegistryAnswersHandlerSetup(){
handler = new SimpleRegistryCallBackHandler();
}
/**
* Initialize SimpleRegistry object
*/
public void setUp(){
ElGamalSetup();
CommunicatorSetup();
RegistryAnswersHandlerSetup();
}
/**
* Checks if the creation of the registry have been successful
*/
public void testSimpleRegistryCreation(){
try {
SimpleRegistry registry = 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(){
List<String> ids = new ArrayList<>();
for (int i = 0 ; i < 10 ; i++ ){
ids.add("" + (10000000 + random.nextInt(90000000)));
}
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
// check that the callbacks have been called and the this been successful
for (String id : ids) {
registry.AddVoter(id, "some personal info", handler);
assertTrue(handler.ActionSucceed);
}
// check that the callbacks have been called exactly 10 times
assertEquals(10, SimpleRegistryCallBackHandler.counter);
// check that the server have the new voters data
for (String id : ids){
MessageFilterList.Builder filters = MessageFilterList.newBuilder()
.addFilter(MessageFilter.newBuilder().setTag(RegistryTags.ID_TAG + " " + id))
.addFilter(MessageFilter.newBuilder().setTag(RegistryTags.VOTER_ENTRY_TAG.toString()));
List<BulletinBoardMessage> messages = communicator.readMessages(filters.build());
assertTrue(messages != null);
}
}
/**
* Test that set voted posts creates correct bulletin board message and sets that the user have been voted
*/
public void testSetVoted(){
assertEquals(null, null);
}
/**
* Test that add to group creates correct bulletin board message and adds a user to a group
*/
public void testAddToGroup(){
assertEquals(null, null);
}
/**
* Test that remove from group creates correct bulletin board message and removes the user from a group
*/
public void testRemoveFromGroup(){
assertEquals(null, null);
}
/**
* Test that get groups retrieves the right groups the user are in
*/
public void testGetGroups(){
assertEquals(null, null);
}
/**
* Test that the personal data outputted about the user is right
*/
public void testGetPersonalIDDetails(){
assertEquals(null, null);
}
}