<s> from __future__ import absolute_import <EOL> import unittest2 as unittest <EOL> import platform <EOL> import six <EOL> import re <EOL> import json <EOL> import binascii <EOL> import hashlib <EOL> from autobahn . wamp import auth <EOL> PBKDF2_TEST_VECTORS = [ <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:20> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:20> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:16> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:32> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:16> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:32> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT:32> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:X>' * <NUM_LIT:64> , b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT:32> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT:X>' * <NUM_LIT> , b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT:32> , u'<STR_LIT>' ) , <EOL> ] <EOL> if platform . python_implementation ( ) != '<STR_LIT>' : <EOL> PBKDF2_TEST_VECTORS . extend ( <EOL> [ <EOL> ( b'<STR_LIT:password>' , b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT:20> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT>' , b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , u'<STR_LIT>' ) , <EOL> ( b'<STR_LIT>' , b'<STR_LIT>' , <NUM_LIT> , <NUM_LIT:16> , u'<STR_LIT>' ) , <EOL> ] <EOL> ) <EOL> class TestWampAuthHelpers ( unittest . TestCase ) : <EOL> def test_pbkdf2 ( self ) : <EOL> for tv in PBKDF2_TEST_VECTORS : <EOL> result = auth . pbkdf2 ( tv [ <NUM_LIT:0> ] , tv [ <NUM_LIT:1> ] , tv [ <NUM_LIT:2> ] , tv [ <NUM_LIT:3> ] , hashlib . sha1 ) <EOL> self . assertEqual ( type ( result ) , bytes ) <EOL> self . assertEqual ( binascii . hexlify ( result ) . decode ( '<STR_LIT:ascii>' ) , tv [ <NUM_LIT:4> ] ) <EOL> def test_generate_totp_secret_default ( self ) : <EOL> secret = auth . generate_totp_secret ( ) <EOL> self . assertEqual ( type ( secret ) , six . text_type ) <EOL> self . assertEqual ( len ( secret ) , <NUM_LIT:10> * <NUM_LIT:8> / <NUM_LIT:5> ) <EOL> def test_generate_totp_secret_length ( self ) : <EOL> for length in [ <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT:50> ] : <EOL> secret = auth . generate_totp_secret ( length ) <EOL> self . assertEqual ( type ( secret ) , six . text_type ) <EOL> self . assertEqual ( len ( secret ) , length * <NUM_LIT:8> / <NUM_LIT:5> ) <EOL> def test_compute_totp ( self ) : <EOL> pat = re . compile ( u"<STR_LIT>" ) <EOL> secret = u"<STR_LIT>" <EOL> signature = auth . compute_totp ( secret ) <EOL> self . assertEqual ( type ( signature ) , six . text_type ) <EOL> self . assertTrue ( pat . match ( signature ) is not None ) <EOL> def test_compute_totp_offset ( self ) : <EOL> pat = re . compile ( u"<STR_LIT>" ) <EOL> secret = u"<STR_LIT>" <EOL> for offset in range ( - <NUM_LIT:10> , <NUM_LIT:10> ) : <EOL> signature = auth . compute_totp ( secret , offset ) <EOL> self . assertEqual ( type ( signature ) , six . text_type ) <EOL> self . assertTrue ( pat . match ( signature ) is not None ) <EOL> def test_derive_key ( self ) : <EOL> secret = u'<STR_LIT>' <EOL> salt = u'<STR_LIT>' <EOL> key = auth . derive_key ( secret . encode ( '<STR_LIT:utf8>' ) , salt . encode ( '<STR_LIT:utf8>' ) ) <EOL> self . assertEqual ( type ( key ) , bytes ) <EOL> self . assertEqual ( key , b"<STR_LIT>" ) <EOL> def test_generate_wcs_default ( self ) : <EOL> secret = auth . generate_wcs ( ) <EOL> self . assertEqual ( type ( secret ) , bytes ) <EOL> self . assertEqual ( len ( secret ) , <NUM_LIT> ) <EOL> def test_generate_wcs_length ( self ) : <EOL> for length in [ <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:30> , <NUM_LIT> , <NUM_LIT:50> ] : <EOL> secret = auth . generate_wcs ( length ) <EOL> self . assertEqual ( type ( secret ) , bytes ) <EOL> self . assertEqual ( len ( secret ) , length ) <EOL> def test_compute_wcs ( self ) : <EOL> secret = u'<STR_LIT>' <EOL> challenge = json . dumps ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , ensure_ascii = False ) . encode ( '<STR_LIT:utf8>' ) <EOL> signature = auth . compute_wcs ( secret . encode ( '<STR_LIT:utf8>' ) , challenge ) <EOL> self . assertEqual ( type ( signature ) , bytes ) <EOL> self . assertEqual ( signature , b"<STR_LIT>" ) </s>
