2017-09-20 22:30:38 +10:00
|
|
|
# Eos - Verifiable elections
|
2018-01-11 22:06:21 +11:00
|
|
|
# Copyright © 2017-18 RunasSudo (Yingtong Li)
|
2017-09-20 22:30:38 +10:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-09-24 15:19:07 +10:00
|
|
|
from eos.core.bigint import *
|
2017-09-20 22:30:38 +10:00
|
|
|
from eos.core.objects import *
|
2017-09-28 20:02:20 +10:00
|
|
|
from eos.core.hashing import *
|
2017-12-07 15:32:39 +11:00
|
|
|
from eos.core.tasks import *
|
2017-12-12 20:32:02 +11:00
|
|
|
from eos.core.tasks.direct import *
|
2017-09-20 22:30:38 +10:00
|
|
|
|
2017-09-25 15:19:48 +10:00
|
|
|
# Common library things
|
|
|
|
# ===================
|
|
|
|
|
2017-09-25 11:56:10 +10:00
|
|
|
class EosTestCase:
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
pass
|
|
|
|
|
2017-12-07 15:32:39 +11:00
|
|
|
@classmethod
|
|
|
|
def db_connect_and_reset(cls):
|
|
|
|
db_connect('test')
|
|
|
|
dbinfo.provider.reset_db()
|
|
|
|
|
2017-09-26 21:47:32 +10:00
|
|
|
def assertTrue(self, a):
|
|
|
|
if is_python:
|
|
|
|
self.impl.assertTrue(a)
|
|
|
|
else:
|
|
|
|
if not a:
|
|
|
|
raise Error('Assertion failed: ' + str(a) + ' not True')
|
|
|
|
|
2017-10-05 20:26:40 +11:00
|
|
|
def assertFalse(self, a):
|
|
|
|
if is_python:
|
|
|
|
self.impl.assertFalse(a)
|
|
|
|
else:
|
|
|
|
if not a:
|
|
|
|
raise Error('Assertion failed: ' + str(a) + ' not False')
|
|
|
|
|
2017-09-25 11:56:10 +10:00
|
|
|
def assertEqual(self, a, b):
|
2017-09-25 15:19:48 +10:00
|
|
|
if is_python:
|
|
|
|
self.impl.assertEqual(a, b)
|
|
|
|
else:
|
|
|
|
if a is None:
|
|
|
|
if b is not None:
|
|
|
|
raise Error('Assertion failed: ' + str(a) + ' != ' + str(b))
|
|
|
|
else:
|
|
|
|
if a != b:
|
|
|
|
raise Error('Assertion failed: ' + str(a) + ' != ' + str(b))
|
|
|
|
|
|
|
|
def assertEqualJSON(self, a, b):
|
2017-09-25 18:03:43 +10:00
|
|
|
if isinstance(a, EosObject):
|
|
|
|
a = EosObject.serialise_and_wrap(a)
|
|
|
|
if isinstance(b, EosObject):
|
|
|
|
b = EosObject.serialise_and_wrap(b)
|
2017-09-25 15:19:48 +10:00
|
|
|
self.assertEqual(EosObject.to_json(a), EosObject.to_json(b))
|
|
|
|
|
|
|
|
def py_only(func):
|
|
|
|
func._py_only = True
|
2017-09-25 17:25:06 +10:00
|
|
|
return func
|
2017-09-25 15:19:48 +10:00
|
|
|
def js_only(func):
|
|
|
|
func._js_only = True
|
2017-09-25 17:25:06 +10:00
|
|
|
return func
|
2017-09-25 15:19:48 +10:00
|
|
|
|
|
|
|
# eos.core tests
|
|
|
|
# ==============
|
2017-09-25 11:56:10 +10:00
|
|
|
|
|
|
|
class ObjectTestCase(EosTestCase):
|
2017-09-22 16:57:40 +10:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-09-20 22:30:38 +10:00
|
|
|
class Person(TopLevelObject):
|
|
|
|
name = StringField()
|
2017-12-05 20:10:26 +11:00
|
|
|
address = StringField(default='Default address')
|
2017-09-20 22:30:38 +10:00
|
|
|
def say_hi(self):
|
|
|
|
return 'Hello! My name is ' + self.name
|
|
|
|
|
2017-09-22 16:57:40 +10:00
|
|
|
cls.Person = Person
|
2017-09-20 22:30:38 +10:00
|
|
|
|
2017-09-24 15:19:07 +10:00
|
|
|
def test_basic(self):
|
2017-09-20 22:30:38 +10:00
|
|
|
person1 = self.Person(name='John', address='Address 1')
|
2017-12-05 20:10:26 +11:00
|
|
|
person2 = self.Person(name='James')
|
2017-09-20 22:30:38 +10:00
|
|
|
|
|
|
|
self.assertEqual(person1.address, 'Address 1')
|
2017-12-05 20:10:26 +11:00
|
|
|
self.assertEqual(person2.address, 'Default address')
|
2017-09-20 22:30:38 +10:00
|
|
|
self.assertEqual(person1.say_hi(), 'Hello! My name is John')
|
|
|
|
self.assertEqual(person2.say_hi(), 'Hello! My name is James')
|
2017-09-22 16:57:40 +10:00
|
|
|
|
2017-09-24 15:19:07 +10:00
|
|
|
def test_serialise(self):
|
2017-09-22 16:57:40 +10:00
|
|
|
person1 = self.Person(name='John', address='Address 1')
|
2017-12-07 18:05:38 +11:00
|
|
|
expect1 = {'_ver': person1._ver, 'name': 'John', 'address': 'Address 1'}
|
2017-09-25 15:19:48 +10:00
|
|
|
#expect1a = {'type': 'eos.core.tests.ObjectTestCase.setUpClass.<locals>.Person', 'value': expect1}
|
|
|
|
expect1a = {'type': 'eos.core.tests.Person', 'value': expect1}
|
2017-09-22 16:57:40 +10:00
|
|
|
|
2017-09-25 15:19:48 +10:00
|
|
|
self.assertEqualJSON(person1.serialise(), expect1)
|
|
|
|
self.assertEqualJSON(EosObject.serialise_and_wrap(person1, self.Person), expect1)
|
|
|
|
self.assertEqualJSON(EosObject.serialise_and_wrap(person1), expect1a)
|
2017-09-24 15:19:07 +10:00
|
|
|
|
|
|
|
#self.assertEqual(EosObject.deserialise_and_unwrap(expect1a), person1)
|
2017-09-25 15:19:48 +10:00
|
|
|
self.assertEqualJSON(EosObject.deserialise_and_unwrap(expect1a).serialise(), person1.serialise())
|
2017-09-24 15:19:07 +10:00
|
|
|
|
2017-09-25 15:53:58 +10:00
|
|
|
class HashTestCase(EosTestCase):
|
|
|
|
def test_hash(self):
|
2017-09-28 20:02:20 +10:00
|
|
|
self.assertEqual(SHA256().update_text('Hello World!').hash_as_b64(), 'f4OxZX/x/FO5LcGBSKHWXfwtSx+j1ncoSt3SABJtkGk=')
|
2017-09-25 15:53:58 +10:00
|
|
|
|
2017-09-25 11:56:10 +10:00
|
|
|
class BigIntTestCase(EosTestCase):
|
2017-09-24 15:19:07 +10:00
|
|
|
def test_basic(self):
|
|
|
|
bigint1 = BigInt(5)
|
|
|
|
bigint2 = BigInt('A', 16)
|
|
|
|
bigint3 = BigInt('15')
|
|
|
|
|
|
|
|
self.assertEqual(bigint1, 5)
|
|
|
|
self.assertEqual(bigint2, 10)
|
|
|
|
self.assertEqual(bigint3, 15)
|
|
|
|
|
|
|
|
self.assertEqual(bigint1 + bigint2, 15)
|
|
|
|
self.assertEqual(bigint3 - bigint2, bigint1)
|
2017-09-26 21:09:56 +10:00
|
|
|
self.assertEqual(pow(bigint1, bigint2), 5**10)
|
|
|
|
self.assertEqual(pow(bigint1, bigint2, bigint3), (5**10)%15)
|
|
|
|
self.assertEqual(pow(bigint1, 10, 15), (5**10)%15)
|
2017-12-07 15:32:39 +11:00
|
|
|
|
|
|
|
class TaskTestCase(EosTestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.db_connect_and_reset()
|
|
|
|
|
|
|
|
def test_normal(self):
|
|
|
|
class TaskNormal(Task):
|
|
|
|
result = StringField()
|
|
|
|
def _run(self):
|
|
|
|
self.messages.append('Hello World')
|
|
|
|
self.result = 'Success'
|
|
|
|
|
|
|
|
task = TaskNormal(run_strategy=DirectRunStrategy())
|
|
|
|
task.save()
|
|
|
|
task.run()
|
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
self.assertEqual(task.status, TaskStatus.COMPLETE)
|
2017-12-07 15:32:39 +11:00
|
|
|
self.assertEqual(len(task.messages), 1)
|
|
|
|
self.assertEqual(task.messages[0], 'Hello World')
|
|
|
|
self.assertEqual(task.result, 'Success')
|
|
|
|
|
|
|
|
def test_error(self):
|
|
|
|
class TaskError(Task):
|
|
|
|
def _run(self):
|
|
|
|
raise Exception('Test exception')
|
|
|
|
|
|
|
|
task = TaskError(run_strategy=DirectRunStrategy())
|
|
|
|
task.save()
|
|
|
|
task.run()
|
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
self.assertEqual(task.status, TaskStatus.FAILED)
|
2017-12-07 15:32:39 +11:00
|
|
|
self.assertEqual(len(task.messages), 1)
|
|
|
|
self.assertTrue('Test exception' in task.messages[0])
|