Changed the SimpleRegistry

The new Simple Registry works according to the RegistryInstance interface
Voter-Registry
Vladimir Eliezer Tokarev 2016-01-23 09:16:57 -08:00
parent 68caeee114
commit 6020ed3ab8
5 changed files with 199 additions and 37 deletions

2
.gitignore vendored
View File

@ -14,3 +14,5 @@ out
*.project
*.classpath
bulletin-board-server/local-instances/meerkat.db
/SQLiteDBTest.db
/bulletin-board-server/SQLiteDBTest.db

View File

@ -1,3 +1,8 @@
/protoc.exe
/bin/
/comment-info.txt
/comment-info.txt
/main/java/meerkat/manualTests.*
/SQLiteDBTest.db
/bulletin-board-server/SQLiteDBTest.db
/src/main/java/meerkat/manualTests.java
/.gitignore

View File

@ -3,16 +3,23 @@ package meerkat;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.bulletinboard.SimpleBulletinBoardClient;
import meerkat.comm.CommunicationException;
import meerkat.crypto.Encryption;
import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.MessageFilter;
import meerkat.protobuf.BulletinBoardAPI.MessageFilterList;
import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage;
import meerkat.protobuf.Crypto;
import util.AccurateTimestamp;
import util.RegistryTags;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import static util.CollectionMessagesUtils.*;
@ -30,24 +37,23 @@ public class SimpleRegistry implements RegistryInstance{
this.communicator = communicator;
}
private List<String> wantedInformation;
private boolean methodSucceeded;
/**
* Creates BulletinBoardMessage with signed basicMessage and UnsignedBulletinBoardMessage that contains the basic message
*
* @param basicMessage BasicMessage
* @return BulletinBoardAPI.BulletinBoardMessage
* @return BulletinBoardMessage
*/
private BulletinBoardAPI.BulletinBoardMessage CreateBulletinBoardMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage basicMessage) {
private BulletinBoardMessage CreateBulletinBoardMessage(UnsignedBulletinBoardMessage basicMessage) {
try {
BulletinBoardAPI.BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardAPI.BulletinBoardMessage.newBuilder();
BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardMessage.newBuilder();
Crypto.RerandomizableEncryptedMessage encryptedMessage =
signatory.encrypt(basicMessage, signatory.generateRandomness(new Random()));
Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder();
messageSignature.mergeFrom(encryptedMessage);
messageSignature.setType(Crypto.SignatureType.ECDSA)
.setData(encryptedMessage.toByteString());
bulletinBoardMessage.setMsg(basicMessage);
@ -63,15 +69,15 @@ public class SimpleRegistry implements RegistryInstance{
* Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG
*
* @param tags the tags based on which the messages will be filtered
* @return List<BulletinBoardAPI.BulletinBoardMessage>
* @return List<BulletinBoardMessage>
*/
private List<BulletinBoardAPI.BulletinBoardMessage> GetRelevantMessages(List<String> tags) {
BulletinBoardAPI.MessageFilterList.Builder filters =
BulletinBoardAPI.MessageFilterList.newBuilder();
private List<BulletinBoardMessage> GetRelevantMessages(List<String> tags) {
MessageFilterList.Builder filters =
MessageFilterList.newBuilder();
for (String tag : tags) {
BulletinBoardAPI.MessageFilter.Builder idFilter =
BulletinBoardAPI.MessageFilter.newBuilder().setTag(tag);
MessageFilter.Builder idFilter =
MessageFilter.newBuilder().setTag(tag);
filters.addFilter(idFilter);
}
@ -85,8 +91,8 @@ public class SimpleRegistry implements RegistryInstance{
* @return List<VoterRegistryMessage>
*/
private List<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<String> tags) throws InvalidProtocolBufferException {
List<BulletinBoardAPI.BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
List<BulletinBoardAPI.UnsignedBulletinBoardMessage> messages =
List<BulletinBoardMessage> relevantMessages = GetRelevantMessages(tags);
List<UnsignedBulletinBoardMessage> messages =
GetUnsignedBulletinBoardMessages(relevantMessages);
return ConvertToVoterRegistryMessages(messages);
}
@ -97,7 +103,7 @@ public class SimpleRegistry implements RegistryInstance{
* @param Message the massage to post
* @return true when the post was successful else false
*/
private boolean SafePost(BulletinBoardAPI.BulletinBoardMessage Message) {
private boolean SafePost(BulletinBoardMessage Message) {
try {
communicator.postMessage(Message);
return true;
@ -107,8 +113,8 @@ public class SimpleRegistry implements RegistryInstance{
}
public void AddVoter(String voterID, String personalData, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder().
addTag(RegistryTags.ID_TAG + " " + voterID)
.addTag(RegistryTags.VOTER_ENTRY_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());
@ -119,8 +125,8 @@ public class SimpleRegistry implements RegistryInstance{
}
public void AddToGroup(String voterID, String groupID, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + voterID)
.addTag(RegistryTags.GROUP_ID_TAG + " " + groupID)
.addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.ADD_TO_GROUP_TAG)
@ -130,8 +136,8 @@ public class SimpleRegistry implements RegistryInstance{
}
public void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + voterID)
.addTag(RegistryTags.GROUP_ID_TAG + " " + groupID)
.addTag(RegistryTags.GROUP_ACTION_TAG + " " + RegistryTags.REMOVE_FROM_GROUP_TAG)
@ -141,8 +147,8 @@ public class SimpleRegistry implements RegistryInstance{
}
public void SetVoted(String id, RegistryCallBack callback) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder basicMessage =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder()
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + " " + id)
.addTag(RegistryTags.VOTE_ACTION_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + " " + AccurateTimestamp.GetCurrentTimestampString());

View File

@ -0,0 +1,60 @@
package util;
import meerkat.RegistryCallBack;
import java.util.List;
/**
* Handles the return values from called methods or registry
*/
public class SimpleRegistryCallBackHandler implements RegistryCallBack {
/**
* Presents the state of the called registy method
*/
public boolean ActionSucceed;
/**
* Presents the wanted information from registry
*/
public List<String> WantedInformation;
public static int counter = 0;
@Override
public void HandleVoterAdded(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleVoterAddedToGroup(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleVoterRemovedFromGroup(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleSetVoted(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleVoterInfo(List<String> voterInformation) {
ActionSucceed = (voterInformation != null);
WantedInformation = voterInformation;
counter++;
}
@Override
public void HandleVoterGroups(List<String> groupsVoterIn) {
ActionSucceed = (groupsVoterIn != null);
WantedInformation = groupsVoterIn;
counter++;
}
}

View File

@ -1,59 +1,148 @@
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();
}
/**
* Test that add voter creates new correct bulletin board message and add the voter
* 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);
}
}