test_number.py :  » Security » PyCrypto » pycrypto-2.0.1 » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Security » PyCrypto 
PyCrypto » pycrypto 2.0.1 » test » test_number.py
#
# Test script for Crypto.Util.number.
#

__revision__ = "$Id: test_number.py,v 1.4 2003/04/04 18:21:35 akuchling Exp $"

from sancho.unittest import TestScenario,parse_args,run_scenarios
from Crypto.Util import number

tested_modules = [ "Crypto.Util.number" ]

class NumberTest (TestScenario):

    def setup (self):
        pass

    def shutdown (self):
        pass

    def check_getRandomNumber (self):
        "Check generation of N-bit random numbers"
        def f(N):
            return '\xff' * N

        self.test_val('number.getRandomNumber(1, f)', 1)
        self.test_val('number.getRandomNumber(4, f)', 15)
        self.test_val('number.getRandomNumber(8, f)', 255)
        self.test_val('number.getRandomNumber(12, f)', 4095)
        self.test_val('number.getRandomNumber(16, f)', 65535)

    def check_GCD (self):
        "Check GCD computation"
        self.test_val('number.GCD(1, 5)', 1)
        self.test_val('number.GCD(2, 6)', 2)
        self.test_val('number.GCD(16, 12)', 4)

    def check_inverse (self):
        "Check computation of inverses"
        self.test_val('number.inverse(9, 10)', 9)
        self.test_val('number.inverse(1, 2)', 1)
        self.test_val('number.inverse(529, 10502)', 3097)

    def check_getPrime (self):
        "Check generation of primes"
        def f(n):
            return '\xff' * n
        self.test_val('number.getPrime(1, f)', 3)
        self.test_val('number.getPrime(2, f)', 3)
        self.test_val('number.getPrime(8, f)', 257)
        self.test_val('number.getPrime(75, f)', 37778931862957161709601L)

    def check_isPrime (self):
        "Check verification of primes"
        self.test_bool('number.isPrime(1)', want_true=0)
        self.test_bool('number.isPrime(2)')
        self.test_bool('number.isPrime(3)')
        self.test_bool('number.isPrime(4)', want_true=0)
        self.test_bool('number.isPrime(37778931862957161709601L)')
        self.test_bool('number.isPrime(37778931862957161709603L)',
                       want_true=0)

    def check_longbytes (self):
        "Check conversion between bytes and integers"
        self.test_val('number.long_to_bytes(1)', '\x01')
        self.test_val('number.long_to_bytes(1, 2)', '\x00\x01')
        self.test_val('number.long_to_bytes(511)', '\x01\xff')

        self.test_val('number.bytes_to_long("\x01")', 1)
        self.test_val('number.bytes_to_long("\xff\x01")', 0xff01)
        self.test_val('number.bytes_to_long("\x12\x34\x01")', 0x123401)

    def check_size (self):
        "Check measurement of number sizes"
        self.test_val('number.size(1)', 1)
        self.test_val('number.size(15)', 4)
        self.test_val('number.size(255)', 8)
        self.test_val('number.size(256)', 9)


# class NumberTest


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.