2014-07-15 18:32:07 +02:00
|
|
|
#
|
|
|
|
|
# SelfTest/Math/test_Numbers.py: Self-test for Numbers module
|
|
|
|
|
#
|
|
|
|
|
# ===================================================================
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
|
|
|
|
# All rights reserved.
|
|
|
|
|
#
|
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
|
# are met:
|
|
|
|
|
#
|
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
# notice, this list of conditions and the following disclaimer in
|
|
|
|
|
# the documentation and/or other materials provided with the
|
|
|
|
|
# distribution.
|
|
|
|
|
#
|
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
|
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
|
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
|
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
# ===================================================================
|
|
|
|
|
|
|
|
|
|
"""Self-test for Math.Numbers"""
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
from Crypto.SelfTest.st_common import list_test_cases
|
|
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
from Crypto.Util.py3compat import *
|
|
|
|
|
|
2014-07-15 18:32:07 +02:00
|
|
|
|
|
|
|
|
class TestNatural(unittest.TestCase):
|
|
|
|
|
|
2014-09-21 22:07:19 +02:00
|
|
|
def setUp(self):
|
|
|
|
|
if not hasattr(self, "Natural"):
|
|
|
|
|
from Crypto.Math.Numbers import Natural as NaturalDefault
|
|
|
|
|
self.Natural = NaturalDefault
|
|
|
|
|
|
|
|
|
|
def Naturals(self, *arg):
|
|
|
|
|
return map(self.Natural, arg)
|
|
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
def test_init_and_equality(self):
|
2014-09-21 22:07:19 +02:00
|
|
|
Natural = self.Natural
|
|
|
|
|
|
2014-07-15 18:32:07 +02:00
|
|
|
a = Natural(23)
|
2014-09-10 07:47:08 +02:00
|
|
|
d = Natural(a)
|
|
|
|
|
self.assertRaises(ValueError, Natural, 1.0)
|
|
|
|
|
self.assertRaises(ValueError, Natural, -1)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
c = Natural(10)
|
|
|
|
|
self.failUnless(a == a)
|
|
|
|
|
self.failUnless(a == d)
|
|
|
|
|
self.failIf(a == c)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
|
|
|
|
def test_conversion_to_bytes(self):
|
2014-09-21 22:07:19 +02:00
|
|
|
Natural = self.Natural
|
|
|
|
|
|
2014-07-15 18:32:07 +02:00
|
|
|
a = Natural(0x17)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.assertEqual(b("\x17"), a.to_bytes())
|
2014-07-15 18:32:07 +02:00
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
c = Natural(0xFFFF)
|
|
|
|
|
self.assertEqual(b("\xFF\xFF"), c.to_bytes())
|
|
|
|
|
self.assertEqual(b("\x00\xFF\xFF"), c.to_bytes(3))
|
|
|
|
|
self.assertRaises(ValueError, c.to_bytes, 1)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
def test_conversion_to_int(self):
|
2014-09-21 22:07:19 +02:00
|
|
|
Natural = self.Natural
|
|
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
a = Natural(23)
|
|
|
|
|
self.assertEqual(int(a), 23)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
2014-09-21 22:07:19 +02:00
|
|
|
f = Natural(2 ** 1000)
|
|
|
|
|
self.assertEqual(int(f), 2 ** 1000)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
def test_equality_with_ints(self):
|
2014-09-21 22:07:19 +02:00
|
|
|
a = self.Natural(23)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.failUnless(a == 23)
|
|
|
|
|
self.failIf(a == 24)
|
|
|
|
|
|
|
|
|
|
def test_conversion_from_bytes(self):
|
2014-09-21 22:07:19 +02:00
|
|
|
Natural = self.Natural
|
|
|
|
|
|
2014-09-10 07:47:08 +02:00
|
|
|
a = Natural.from_bytes(b("\x00"))
|
|
|
|
|
self.assertEqual(0, a)
|
|
|
|
|
|
|
|
|
|
a = Natural.from_bytes(b("\x00\x00"))
|
|
|
|
|
self.assertEqual(0, a)
|
|
|
|
|
|
|
|
|
|
c = Natural.from_bytes(b("\xFF\xFF"))
|
|
|
|
|
self.assertEqual(0xFFFF, c)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
|
|
|
|
def test_inequality(self):
|
|
|
|
|
# Test Natural!=Natural and Natural!=int
|
2014-09-21 22:07:19 +02:00
|
|
|
a, d, c = self.Naturals(89, 89, 90)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.failUnless(a != c)
|
|
|
|
|
self.failUnless(a != 90)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.failIf(a != d)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.failIf(a != 89)
|
|
|
|
|
|
|
|
|
|
def test_less_than(self):
|
|
|
|
|
# Test Natural<Natural and Natural<int
|
2014-09-21 22:07:19 +02:00
|
|
|
a, d, c = self.Naturals(13, 13, 14)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.failUnless(a < c)
|
|
|
|
|
self.failUnless(a < 14)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.failIf(a < d)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.failIf(a < 13)
|
|
|
|
|
|
|
|
|
|
def test_addition(self):
|
|
|
|
|
# Test Natural+Natural and Natural+int
|
2014-09-21 22:07:19 +02:00
|
|
|
a, d = self.Naturals(7, 90)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.assertEqual(a + d, 97)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.assertEqual(a + 90, 97)
|
|
|
|
|
self.assertEqual(a + (-7), 0)
|
|
|
|
|
self.assertRaises(ValueError, lambda: a + (-8))
|
|
|
|
|
|
|
|
|
|
def test_subtraction(self):
|
|
|
|
|
# Test Natural-Natural and Natural-int
|
2014-09-21 22:07:19 +02:00
|
|
|
a, d = self.Naturals(7, 90)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.assertEqual(d - a, 83)
|
|
|
|
|
self.assertEqual(d - 7, 83)
|
|
|
|
|
self.assertEqual(d - (-7), 97)
|
|
|
|
|
self.assertRaises(ValueError, lambda: a - d)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.assertRaises(ValueError, lambda: a - 90)
|
|
|
|
|
|
|
|
|
|
def test_remainder(self):
|
|
|
|
|
# Test Natural%Natural and Natural%int
|
2014-09-21 22:07:19 +02:00
|
|
|
a, d = self.Naturals(23, 5)
|
2014-09-10 07:47:08 +02:00
|
|
|
self.assertEqual(a % d, 3)
|
2014-07-15 18:32:07 +02:00
|
|
|
self.assertEqual(a % 5, 3)
|
|
|
|
|
self.assertRaises(ZeroDivisionError, lambda: a % 0)
|
|
|
|
|
|
2014-09-21 21:28:59 +02:00
|
|
|
def test_exponentiation(self):
|
2014-09-21 22:07:19 +02:00
|
|
|
a, d, e = self.Naturals(23, 5, 17)
|
2014-09-21 21:28:59 +02:00
|
|
|
|
|
|
|
|
self.assertEqual(pow(a, d, e), 7)
|
|
|
|
|
self.assertEqual(pow(a, 5, e), 7)
|
|
|
|
|
self.assertEqual(pow(a, d, 17), 7)
|
|
|
|
|
self.assertEqual(pow(a, 5, 17), 7)
|
|
|
|
|
self.assertEqual(pow(a, 0, 17), 1)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, pow, a, 5, 0)
|
|
|
|
|
self.assertRaises(ValueError, pow, a, 5, -4)
|
|
|
|
|
self.assertRaises(ValueError, pow, a, -3, 8)
|
2014-07-15 18:32:07 +02:00
|
|
|
|
2014-09-21 22:07:19 +02:00
|
|
|
|
|
|
|
|
class TestNaturalInt(TestNatural):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
from Crypto.Math._Numbers_int import Natural as NaturalInt
|
|
|
|
|
self.Natural = NaturalInt
|
|
|
|
|
TestNatural.setUp(self)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestNaturalGMP(TestNatural):
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
from Crypto.Math._Numbers_gmp import Natural as NaturalGMP
|
|
|
|
|
self.Natural = NaturalGMP
|
|
|
|
|
TestNatural.setUp(self)
|
|
|
|
|
|
|
|
|
|
|
2014-07-15 18:32:07 +02:00
|
|
|
def get_tests(config={}):
|
|
|
|
|
tests = []
|
2014-09-21 22:07:19 +02:00
|
|
|
tests += list_test_cases(TestNaturalInt)
|
|
|
|
|
try:
|
|
|
|
|
from Crypto.Math._Numbers_gmp import Natural as NaturalGMP
|
|
|
|
|
tests += list_test_cases(TestNaturalGMP)
|
|
|
|
|
except ImportError:
|
|
|
|
|
print "Skipping GMP tests"
|
2014-07-15 18:32:07 +02:00
|
|
|
tests += list_test_cases(TestNatural)
|
|
|
|
|
return tests
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
suite = lambda: unittest.TestSuite(get_tests())
|
|
|
|
|
unittest.main(defaultTest='suite')
|