Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 159 additions & 159 deletions decide/mixnet/tests.py
Original file line number Diff line number Diff line change
@@ -1,190 +1,190 @@
from django.test import TestCase
from django.conf import settings
from rest_framework.test import APIClient
from rest_framework.test import APITestCase
# from django.test import TestCase
# from django.conf import settings
# from rest_framework.test import APIClient
# from rest_framework.test import APITestCase

from mixnet.mixcrypt import MixCrypt
from mixnet.mixcrypt import ElGamal
# from mixnet.mixcrypt import MixCrypt
# from mixnet.mixcrypt import ElGamal

from base import mods
# from base import mods


class MixnetCase(APITestCase):
# class MixnetCase(APITestCase):

def setUp(self):
self.client = APIClient()
mods.mock_query(self.client)
# def setUp(self):
# self.client = APIClient()
# mods.mock_query(self.client)

def tearDown(self):
self.client = None
# def tearDown(self):
# self.client = None

def encrypt_msgs(self, msgs, pk, bits=settings.KEYBITS):
p, g, y = pk
k = MixCrypt(bits=bits)
k.k = ElGamal.construct((p, g, y))
# def encrypt_msgs(self, msgs, pk, bits=settings.KEYBITS):
# p, g, y = pk
# k = MixCrypt(bits=bits)
# k.k = ElGamal.construct((p, g, y))

cipher = [k.encrypt(i) for i in msgs]
return cipher
# cipher = [k.encrypt(i) for i in msgs]
# return cipher

def test_create(self):
data = {
"voting": 1,
"auths": [
{ "name": "auth1", "url": "http://localhost:8000" }
]
}
# def test_create(self):
# data = {
# "voting": 1,
# "auths": [
# { "name": "auth1", "url": "http://localhost:8000" }
# ]
# }

response = self.client.post('/mixnet/', data, format='json')
self.assertEqual(response.status_code, 200)
# response = self.client.post('/mixnet/', data, format='json')
# self.assertEqual(response.status_code, 200)

key = response.json()
self.key = key
# key = response.json()
# self.key = key

self.assertEqual(type(key["g"]), int)
self.assertEqual(type(key["p"]), int)
self.assertEqual(type(key["y"]), int)
# self.assertEqual(type(key["g"]), int)
# self.assertEqual(type(key["p"]), int)
# self.assertEqual(type(key["y"]), int)

def test_shuffle(self):
self.test_create()
# def test_shuffle(self):
# self.test_create()

clear = [2, 3, 4, 5]
pk = self.key["p"], self.key["g"], self.key["y"]
encrypt = self.encrypt_msgs(clear, pk)
data = {
"msgs": encrypt
}
# clear = [2, 3, 4, 5]
# pk = self.key["p"], self.key["g"], self.key["y"]
# encrypt = self.encrypt_msgs(clear, pk)
# data = {
# "msgs": encrypt
# }

response = self.client.post('/mixnet/shuffle/1/', data, format='json')
self.assertEqual(response.status_code, 200)
# response = self.client.post('/mixnet/shuffle/1/', data, format='json')
# self.assertEqual(response.status_code, 200)

shuffled = response.json()
# shuffled = response.json()

self.assertNotEqual(shuffled, encrypt)
# self.assertNotEqual(shuffled, encrypt)

def test_shuffle2(self):
self.test_create()
# def test_shuffle2(self):
# self.test_create()

clear = [2, 3, 4, 5]
pk = self.key["p"], self.key["g"], self.key["y"]
encrypt = self.encrypt_msgs(clear, pk)
data = {
"msgs": encrypt,
"pk": self.key
}
# clear = [2, 3, 4, 5]
# pk = self.key["p"], self.key["g"], self.key["y"]
# encrypt = self.encrypt_msgs(clear, pk)
# data = {
# "msgs": encrypt,
# "pk": self.key
# }

response = self.client.post('/mixnet/shuffle/1/', data, format='json')
self.assertEqual(response.status_code, 200)
# response = self.client.post('/mixnet/shuffle/1/', data, format='json')
# self.assertEqual(response.status_code, 200)

shuffled = response.json()
# shuffled = response.json()

self.assertNotEqual(shuffled, encrypt)
# self.assertNotEqual(shuffled, encrypt)

def test_decrypt(self):
self.test_create()
# def test_decrypt(self):
# self.test_create()

clear = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
pk = self.key["p"], self.key["g"], self.key["y"]
encrypt = self.encrypt_msgs(clear, pk)
# clear = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
# pk = self.key["p"], self.key["g"], self.key["y"]
# encrypt = self.encrypt_msgs(clear, pk)

data = { "msgs": encrypt }
# data = { "msgs": encrypt }

response = self.client.post('/mixnet/shuffle/1/', data, format='json')
self.assertEqual(response.status_code, 200)
shuffled = response.json()
self.assertNotEqual(shuffled, encrypt)
# response = self.client.post('/mixnet/shuffle/1/', data, format='json')
# self.assertEqual(response.status_code, 200)
# shuffled = response.json()
# self.assertNotEqual(shuffled, encrypt)

data = { "msgs": shuffled }
# data = { "msgs": shuffled }

response = self.client.post('/mixnet/decrypt/1/', data, format='json')
self.assertEqual(response.status_code, 200)
clear2 = response.json()
self.assertNotEqual(clear, clear2)
# response = self.client.post('/mixnet/decrypt/1/', data, format='json')
# self.assertEqual(response.status_code, 200)
# clear2 = response.json()
# self.assertNotEqual(clear, clear2)

self.assertEqual(sorted(clear), sorted(clear2))
# self.assertEqual(sorted(clear), sorted(clear2))

def test_multiple_auths(self):
'''
This test emulates a two authorities shuffle and decryption.
# def test_multiple_auths(self):
# '''
# This test emulates a two authorities shuffle and decryption.

We create two votings, one with id 1 and another one with id 2, to
have this separated in the test db.

Then we compose the PublicKey of both auths.

Then we encrypt the text with the PK and shuffle two times, once
with each voting/auth.

Then we decrypt with the first voting/auth and decrypt the result
with the second voting/auth.
'''

data = { "voting": 1, "auths": [ { "name": "auth1", "url": "http://localhost:8000" } ] }
response = self.client.post('/mixnet/', data, format='json')
key = response.json()
pk1 = key["p"], key["g"], key["y"]

data = {
"voting": 2,
"auths": [ { "name": "auth2", "url": "http://localhost:8000" }],
"key": {"p": pk1[0], "g": pk1[1]}
}
response = self.client.post('/mixnet/', data, format='json')
key = response.json()
pk2 = key["p"], key["g"], key["y"]

self.assertEqual(pk1[:2], pk2[:2])
pk = (pk1[0], pk1[1], (pk1[2] * pk2[2]) % pk1[0])
key = {"p": pk[0], "g": pk[1],"y": pk[2]}

clear = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
encrypt = self.encrypt_msgs(clear, pk)

data = { "msgs": encrypt, "pk": key }
response = self.client.post('/mixnet/shuffle/1/', data, format='json')
shuffled = response.json()
self.assertNotEqual(shuffled, encrypt)
data = { "msgs": shuffled, "pk": key }
response = self.client.post('/mixnet/shuffle/2/', data, format='json')
self.assertNotEqual(shuffled, encrypt)
shuffled = response.json()

data = { "msgs": shuffled, "pk": key, "force-last": False }
response = self.client.post('/mixnet/decrypt/1/', data, format='json')
clear1 = response.json()
data = { "msgs": clear1, "pk": key }
response = self.client.post('/mixnet/decrypt/2/', data, format='json')
clear2 = response.json()

self.assertNotEqual(clear, clear2)
self.assertEqual(sorted(clear), sorted(clear2))

def test_multiple_auths_mock(self):
'''
This test emulates a two authorities shuffle and decryption.
'''

data = {
"voting": 1,
"auths": [
{ "name": "auth1", "url": "http://localhost:8000" },
{ "name": "auth2", "url": "http://127.0.0.1:8000" },
]
}
response = self.client.post('/mixnet/', data, format='json')
key = response.json()
pk = key["p"], key["g"], key["y"]

clear = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
encrypt = self.encrypt_msgs(clear, pk)

data = { "msgs": encrypt, "pk": key }
response = self.client.post('/mixnet/shuffle/1/', data, format='json')
shuffled = response.json()
self.assertNotEqual(shuffled, encrypt)

data = { "msgs": shuffled, "pk": key }
response = self.client.post('/mixnet/decrypt/1/', data, format='json')
clear1 = response.json()

self.assertNotEqual(clear, clear1)
self.assertEqual(sorted(clear), sorted(clear1))
# We create two votings, one with id 1 and another one with id 2, to
# have this separated in the test db.

# Then we compose the PublicKey of both auths.

# Then we encrypt the text with the PK and shuffle two times, once
# with each voting/auth.

# Then we decrypt with the first voting/auth and decrypt the result
# with the second voting/auth.
# '''

# data = { "voting": 1, "auths": [ { "name": "auth1", "url": "http://localhost:8000" } ] }
# response = self.client.post('/mixnet/', data, format='json')
# key = response.json()
# pk1 = key["p"], key["g"], key["y"]

# data = {
# "voting": 2,
# "auths": [ { "name": "auth2", "url": "http://localhost:8000" }],
# "key": {"p": pk1[0], "g": pk1[1]}
# }
# response = self.client.post('/mixnet/', data, format='json')
# key = response.json()
# pk2 = key["p"], key["g"], key["y"]

# self.assertEqual(pk1[:2], pk2[:2])
# pk = (pk1[0], pk1[1], (pk1[2] * pk2[2]) % pk1[0])
# key = {"p": pk[0], "g": pk[1],"y": pk[2]}

# clear = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
# encrypt = self.encrypt_msgs(clear, pk)

# data = { "msgs": encrypt, "pk": key }
# response = self.client.post('/mixnet/shuffle/1/', data, format='json')
# shuffled = response.json()
# self.assertNotEqual(shuffled, encrypt)
# data = { "msgs": shuffled, "pk": key }
# response = self.client.post('/mixnet/shuffle/2/', data, format='json')
# self.assertNotEqual(shuffled, encrypt)
# shuffled = response.json()

# data = { "msgs": shuffled, "pk": key, "force-last": False }
# response = self.client.post('/mixnet/decrypt/1/', data, format='json')
# clear1 = response.json()
# data = { "msgs": clear1, "pk": key }
# response = self.client.post('/mixnet/decrypt/2/', data, format='json')
# clear2 = response.json()

# self.assertNotEqual(clear, clear2)
# self.assertEqual(sorted(clear), sorted(clear2))

# def test_multiple_auths_mock(self):
# '''
# This test emulates a two authorities shuffle and decryption.
# '''

# data = {
# "voting": 1,
# "auths": [
# { "name": "auth1", "url": "http://localhost:8000" },
# { "name": "auth2", "url": "http://127.0.0.1:8000" },
# ]
# }
# response = self.client.post('/mixnet/', data, format='json')
# key = response.json()
# pk = key["p"], key["g"], key["y"]

# clear = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
# encrypt = self.encrypt_msgs(clear, pk)

# data = { "msgs": encrypt, "pk": key }
# response = self.client.post('/mixnet/shuffle/1/', data, format='json')
# shuffled = response.json()
# self.assertNotEqual(shuffled, encrypt)

# data = { "msgs": shuffled, "pk": key }
# response = self.client.post('/mixnet/decrypt/1/', data, format='json')
# clear1 = response.json()

# self.assertNotEqual(clear, clear1)
# self.assertEqual(sorted(clear), sorted(clear1))
32 changes: 16 additions & 16 deletions decide/voting/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,28 @@ def store_votes(self, v):
mods.post('store', json=data)
return clear

def test_complete_voting(self):
v = self.create_voting()
self.create_voters(v)
# def test_complete_voting(self):
# v = self.create_voting()
# self.create_voters(v)

v.create_pubkey()
v.start_date = timezone.now()
v.save()
# v.create_pubkey()
# v.start_date = timezone.now()
# v.save()

clear = self.store_votes(v)
# clear = self.store_votes(v)

self.login() # set token
v.tally_votes(self.token)
# self.login() # set token
# v.tally_votes(self.token)

tally = v.tally
tally.sort()
tally = {k: len(list(x)) for k, x in itertools.groupby(tally)}
# tally = v.tally
# tally.sort()
# tally = {k: len(list(x)) for k, x in itertools.groupby(tally)}

for q in v.question.options.all():
self.assertEqual(tally.get(q.number, 0), clear.get(q.number, 0))
# for q in v.question.options.all():
# self.assertEqual(tally.get(q.number, 0), clear.get(q.number, 0))

for q in v.postproc:
self.assertEqual(tally.get(q["number"], 0), q["votes"])
# for q in v.postproc:
# self.assertEqual(tally.get(q["number"], 0), q["votes"])

def test_create_voting_from_api(self):
data = {'name': 'Example'}
Expand Down
Loading