<s> """<STR_LIT>""" <EOL> import datetime <EOL> import os <EOL> import struct <EOL> import mox <EOL> import stubout <EOL> from google . apputils import app <EOL> from google . apputils import basetest <EOL> from simian . auth import base <EOL> import base_test <EOL> def GetRandomInt ( ) : <EOL> """<STR_LIT>""" <EOL> b = os . urandom ( <NUM_LIT:16> ) <EOL> a = struct . unpack ( '<STR_LIT>' , b ) <EOL> return ( a [ <NUM_LIT:0> ] << <NUM_LIT:64> ) + a [ <NUM_LIT:1> ] <EOL> class Auth1Test ( base_test . AuthTestingBase ) : <EOL> """<STR_LIT>""" <EOL> def GetTestClass ( self ) : <EOL> return base . Auth1 ( ) <EOL> def testStep1 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( base . base64 , '<STR_LIT>' ) <EOL> sn = GetRandomInt ( ) <EOL> cn = GetRandomInt ( ) <EOL> self . ba . Nonce ( ) . AndReturn ( sn ) <EOL> m = '<STR_LIT>' % ( cn , sn ) <EOL> self . ba . _AssembleMessage ( str ( cn ) , str ( sn ) ) . AndReturn ( m ) <EOL> self . ba . Sign ( m ) . AndReturn ( '<STR_LIT>' % m ) <EOL> base . base64 . urlsafe_b64encode ( '<STR_LIT>' % m ) . AndReturn ( '<STR_LIT>' ) <EOL> self . ba . _AssembleMessage ( m , '<STR_LIT>' ) . AndReturn ( '<STR_LIT>' % m ) <EOL> self . ba . SessionSetCnSn ( cn , sn ) <EOL> self . mox . ReplayAll ( ) <EOL> self . AssertState ( base . State . INPUT ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . ba . Input ( n = str ( cn ) ) <EOL> self . AssertState ( base . State . OUTPUT ) <EOL> self . assertEqual ( self . ba . Output ( ) , self . _MakeMsg ( cn , sn , '<STR_LIT>' ) ) <EOL> self . AssertState ( base . State . INPUT ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . mox . VerifyAll ( ) <EOL> def testStep2Success ( self ) : <EOL> """<STR_LIT>""" <EOL> auth_token = '<STR_LIT>' <EOL> m = '<STR_LIT>' <EOL> s = '<STR_LIT>' <EOL> s_b64 = '<STR_LIT>' <EOL> c = '<STR_LIT>' <EOL> c_b64 = '<STR_LIT>' <EOL> cn = '<STR_LIT>' <EOL> sn = '<STR_LIT>' <EOL> uuid = '<STR_LIT>' <EOL> mock_cert = self . mox . CreateMockAnything ( ) <EOL> mock_cert . GetPublicKey ( ) . AndReturn ( mock_cert ) <EOL> mock_cert . SetRequiredIssuer ( self . ba . _required_issuer ) . AndReturn ( None ) <EOL> mock_cert . CheckAll ( ) . AndReturn ( None ) <EOL> mock_cert . GetSubject ( ) . AndReturn ( uuid ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( base . base64 , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . ba . _SplitMessage ( m , <NUM_LIT:3> ) . AndReturn ( [ c_b64 , cn , sn ] ) <EOL> base . base64 . urlsafe_b64decode ( str ( s_b64 ) ) . AndReturn ( s ) <EOL> base . base64 . urlsafe_b64decode ( str ( c_b64 ) ) . AndReturn ( c ) <EOL> self . ba . LoadOtherCert ( c ) . AndReturn ( mock_cert ) <EOL> self . ba . VerifyDataSignedWithCert ( m , s , mock_cert ) . AndReturn ( True ) <EOL> self . ba . VerifyCertSignedByCA ( mock_cert ) . AndReturn ( True ) <EOL> self . ba . SessionVerifyKnownCnSn ( cn , sn ) . AndReturn ( True ) <EOL> self . ba . SessionCreateAuthToken ( uuid ) . AndReturn ( auth_token ) <EOL> self . ba . SessionDelCn ( cn ) . AndReturn ( True ) <EOL> self . mox . ReplayAll ( ) <EOL> self . AssertState ( base . State . INPUT ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . ba . Input ( m = m , s = s_b64 ) <EOL> self . AssertState ( base . State . OUTPUT ) <EOL> self . assertEqual ( self . ba . Output ( ) , auth_token ) <EOL> self . AssertAuthState ( base . AuthState . OK ) <EOL> self . AssertState ( base . State . INPUT ) <EOL> self . mox . VerifyAll ( ) <EOL> def testStep2SplitMessageFailure ( self ) : <EOL> """<STR_LIT>""" <EOL> m = '<STR_LIT>' <EOL> s_b64 = '<STR_LIT>' <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . ba . _SplitMessage ( m , <NUM_LIT:3> ) . AndRaise ( base . MessageError ) <EOL> self . ba . AuthFail ( ) . AndReturn ( None ) <EOL> self . mox . ReplayAll ( ) <EOL> self . AssertState ( base . State . INPUT ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . ba . Input ( m = m , s = s_b64 ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . AssertState ( base . State . INPUT ) <EOL> self . mox . VerifyAll ( ) <EOL> class Auth1ClientTest ( base_test . AuthTestingBase ) : <EOL> """<STR_LIT>""" <EOL> def GetTestClass ( self ) : <EOL> return base . Auth1Client ( ) <EOL> def testStep0 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba . _session , '<STR_LIT>' ) <EOL> self . ba . Nonce ( ) . AndReturn ( <NUM_LIT:1> ) <EOL> self . ba . _session . Set ( '<STR_LIT>' , '<STR_LIT:1>' ) . AndReturn ( None ) <EOL> self . mox . ReplayAll ( ) <EOL> self . AssertState ( self . ba . DefaultState ( ) ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . ba . Input ( ) <EOL> self . AssertState ( base . State . OUTPUT ) <EOL> self . assertEqual ( '<STR_LIT:1>' , self . ba . Output ( ) ) <EOL> self . AssertState ( self . ba . DefaultState ( ) ) <EOL> self . mox . VerifyAll ( ) <EOL> def testStep1 ( self ) : <EOL> """<STR_LIT>""" <EOL> cn = '<STR_LIT>' <EOL> sn = '<STR_LIT>' <EOL> s_b64 = '<STR_LIT>' <EOL> s = '<STR_LIT>' <EOL> m = '<STR_LIT>' % ( cn , sn , s ) <EOL> self . ba . _ca_cert = '<STR_LIT>' <EOL> self . ba . _cert_str = '<STR_LIT>' <EOL> self . ba . _session . Set ( '<STR_LIT>' , cn ) <EOL> self . ba . _server_cert_pem = '<STR_LIT>' <EOL> c_b64 = '<STR_LIT>' <EOL> msg = '<STR_LIT::>' . join ( ( c_b64 , cn , sn ) ) <EOL> cn_sn_m = '<STR_LIT>' % ( cn , sn ) <EOL> mock_server_cert = self . mox . CreateMockAnything ( ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( base . base64 , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( base . base64 , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . ba . _session , '<STR_LIT>' ) <EOL> self . ba . _SplitMessage ( m , <NUM_LIT:3> ) . AndReturn ( ( cn , sn , s_b64 ) ) <EOL> base . base64 . urlsafe_b64decode ( s_b64 ) . AndReturn ( s ) <EOL> self . ba . LoadOtherCert ( self . ba . _server_cert_pem ) . AndReturn ( mock_server_cert ) <EOL> self . ba . VerifyCertSignedByCA ( mock_server_cert ) . AndReturn ( True ) <EOL> self . ba . _session . Get ( '<STR_LIT>' ) . AndReturn ( cn ) <EOL> self . ba . _AssembleMessage ( cn , sn ) . AndReturn ( cn_sn_m ) <EOL> self . ba . VerifyDataSignedWithCert ( <EOL> cn_sn_m , s , mock_server_cert ) . AndReturn ( True ) <EOL> base . base64 . urlsafe_b64encode ( self . ba . _cert_str ) . AndReturn ( c_b64 ) <EOL> self . ba . _AssembleMessage ( c_b64 , cn , sn ) . AndReturn ( msg ) <EOL> self . ba . Sign ( msg ) . AndReturn ( s ) <EOL> base . base64 . urlsafe_b64encode ( s ) . AndReturn ( s_b64 ) <EOL> expect_output = { '<STR_LIT:m>' : msg , '<STR_LIT:s>' : s_b64 } <EOL> self . mox . ReplayAll ( ) <EOL> self . AssertState ( self . ba . DefaultState ( ) ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . ba . Input ( m = m ) <EOL> self . AssertState ( base . State . OUTPUT ) <EOL> self . assertEqual ( expect_output , self . ba . Output ( ) ) <EOL> self . AssertState ( self . ba . DefaultState ( ) ) <EOL> self . mox . VerifyAll ( ) <EOL> def testStep3 ( self ) : <EOL> """<STR_LIT>""" <EOL> token = base . Auth1 . TOKEN <EOL> self . mox . StubOutWithMock ( self . ba . _session , '<STR_LIT>' ) <EOL> self . ba . _session . DeleteById ( '<STR_LIT>' ) <EOL> self . mox . ReplayAll ( ) <EOL> self . AssertState ( self . ba . DefaultState ( ) ) <EOL> self . AssertAuthState ( base . AuthState . UNKNOWN ) <EOL> self . ba . Input ( t = token ) <EOL> self . AssertAuthState ( base . AuthState . OK ) <EOL> self . AssertState ( self . ba . DefaultState ( ) ) <EOL> self . mox . VerifyAll ( ) <EOL> def main ( unused_argv ) : <EOL> basetest . main ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( ) </s>
