<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division <EOL> import os <EOL> from twisted import plugin <EOL> from twisted . trial import unittest <EOL> from twisted . cred import credentials , checkers , error , strcred <EOL> from twisted . plugins import cred_file , cred_anonymous <EOL> from twisted . python import usage , compat <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . python . fakepwd import UserDatabase <EOL> from twisted . python . reflect import requireModule <EOL> if compat . _PY3 : <EOL> from io import StringIO <EOL> else : <EOL> from io import BytesIO as StringIO <EOL> try : <EOL> import crypt <EOL> except ImportError : <EOL> crypt = None <EOL> try : <EOL> import pwd <EOL> except ImportError : <EOL> pwd = None <EOL> try : <EOL> import spwd <EOL> except ImportError : <EOL> spwd = None <EOL> def getInvalidAuthType ( ) : <EOL> """<STR_LIT>""" <EOL> invalidAuthType = '<STR_LIT>' <EOL> while ( invalidAuthType in <EOL> [ factory . authType for factory in strcred . findCheckerFactories ( ) ] ) : <EOL> invalidAuthType += '<STR_LIT:_>' <EOL> return invalidAuthType <EOL> class PublicAPITests ( unittest . TestCase ) : <EOL> def test_emptyDescription ( self ) : <EOL> """<STR_LIT>""" <EOL> iat = getInvalidAuthType ( ) <EOL> self . assertRaises ( strcred . InvalidAuthType , strcred . makeChecker , iat ) <EOL> self . assertRaises ( <EOL> strcred . InvalidAuthType , strcred . findCheckerFactory , iat ) <EOL> def test_invalidAuthType ( self ) : <EOL> """<STR_LIT>""" <EOL> iat = getInvalidAuthType ( ) <EOL> self . assertRaises ( strcred . InvalidAuthType , strcred . makeChecker , iat ) <EOL> self . assertRaises ( <EOL> strcred . InvalidAuthType , strcred . findCheckerFactory , iat ) <EOL> class StrcredFunctionsTests ( unittest . TestCase ) : <EOL> def test_findCheckerFactories ( self ) : <EOL> """<STR_LIT>""" <EOL> availablePlugins = list ( strcred . findCheckerFactories ( ) ) <EOL> for plg in plugin . getPlugins ( strcred . ICheckerFactory ) : <EOL> self . assertIn ( plg , availablePlugins ) <EOL> def test_findCheckerFactory ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertIdentical ( strcred . findCheckerFactory ( '<STR_LIT:file>' ) , <EOL> cred_file . theFileCheckerFactory ) <EOL> class MemoryCheckerTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . admin = credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alice = credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> self . badPass = credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . badUser = credentials . UsernamePassword ( '<STR_LIT:x>' , '<STR_LIT>' ) <EOL> self . checker = strcred . makeChecker ( '<STR_LIT>' ) <EOL> def test_isChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( self . checker ) ) <EOL> self . assertIn ( credentials . IUsernamePassword , <EOL> self . checker . credentialInterfaces ) <EOL> def test_badFormatArgString ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( strcred . InvalidAuthArgumentString , <EOL> strcred . makeChecker , '<STR_LIT>' ) <EOL> def test_memoryCheckerSucceeds ( self ) : <EOL> """<STR_LIT>""" <EOL> def _gotAvatar ( username ) : <EOL> self . assertEqual ( username , self . admin . username ) <EOL> return ( self . checker <EOL> . requestAvatarId ( self . admin ) <EOL> . addCallback ( _gotAvatar ) ) <EOL> def test_memoryCheckerFailsUsername ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertFailure ( self . checker . requestAvatarId ( self . badUser ) , <EOL> error . UnauthorizedLogin ) <EOL> def test_memoryCheckerFailsPassword ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertFailure ( self . checker . requestAvatarId ( self . badPass ) , <EOL> error . UnauthorizedLogin ) <EOL> class AnonymousCheckerTests ( unittest . TestCase ) : <EOL> def test_isChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> checker = strcred . makeChecker ( '<STR_LIT>' ) <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( checker ) ) <EOL> self . assertIn ( credentials . IAnonymous , checker . credentialInterfaces ) <EOL> def testAnonymousAccessSucceeds ( self ) : <EOL> """<STR_LIT>""" <EOL> checker = strcred . makeChecker ( '<STR_LIT>' ) <EOL> request = checker . requestAvatarId ( credentials . Anonymous ( ) ) <EOL> def _gotAvatar ( avatar ) : <EOL> self . assertIdentical ( checkers . ANONYMOUS , avatar ) <EOL> return request . addCallback ( _gotAvatar ) <EOL> class UnixCheckerTests ( unittest . TestCase ) : <EOL> users = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> } <EOL> def _spwd ( self , username ) : <EOL> return ( username , crypt . crypt ( self . users [ username ] , '<STR_LIT>' ) , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:7> , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> def setUp ( self ) : <EOL> self . admin = credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alice = credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT:foo>' ) <EOL> self . badPass = credentials . UsernamePassword ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . badUser = credentials . UsernamePassword ( '<STR_LIT:x>' , '<STR_LIT>' ) <EOL> self . checker = strcred . makeChecker ( '<STR_LIT>' ) <EOL> if pwd : <EOL> database = UserDatabase ( ) <EOL> for username , password in self . users . items ( ) : <EOL> database . addUser ( <EOL> username , crypt . crypt ( password , '<STR_LIT>' ) , <EOL> <NUM_LIT:1000> , <NUM_LIT:1000> , username , '<STR_LIT>' + username , '<STR_LIT>' ) <EOL> self . patch ( pwd , '<STR_LIT>' , database . getpwnam ) <EOL> if spwd : <EOL> self . _spwd_getspnam = spwd . getspnam <EOL> spwd . getspnam = self . _spwd <EOL> def tearDown ( self ) : <EOL> if spwd : <EOL> spwd . getspnam = self . _spwd_getspnam <EOL> def test_isChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( self . checker ) ) <EOL> self . assertIn ( credentials . IUsernamePassword , <EOL> self . checker . credentialInterfaces ) <EOL> def test_unixCheckerSucceeds ( self ) : <EOL> """<STR_LIT>""" <EOL> def _gotAvatar ( username ) : <EOL> self . assertEqual ( username , self . admin . username ) <EOL> return ( self . checker <EOL> . requestAvatarId ( self . admin ) <EOL> . addCallback ( _gotAvatar ) ) <EOL> def test_unixCheckerFailsUsername ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertFailure ( self . checker . requestAvatarId ( self . badUser ) , <EOL> error . UnauthorizedLogin ) <EOL> def test_unixCheckerFailsPassword ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertFailure ( self . checker . requestAvatarId ( self . badPass ) , <EOL> error . UnauthorizedLogin ) <EOL> if None in ( pwd , spwd , crypt ) : <EOL> availability = [ ] <EOL> for module , name in ( ( pwd , "<STR_LIT>" ) , ( spwd , "<STR_LIT>" ) , ( crypt , "<STR_LIT>" ) ) : <EOL> if module is None : <EOL> availability += [ name ] <EOL> for method in ( test_unixCheckerSucceeds , <EOL> test_unixCheckerFailsUsername , <EOL> test_unixCheckerFailsPassword ) : <EOL> method . skip = ( "<STR_LIT>" + <EOL> "<STR_LIT:U+002CU+0020>" . join ( availability ) ) <EOL> class FileDBCheckerTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . admin = credentials . UsernamePassword ( b'<STR_LIT>' , b'<STR_LIT>' ) <EOL> self . alice = credentials . UsernamePassword ( b'<STR_LIT>' , b'<STR_LIT:foo>' ) <EOL> self . badPass = credentials . UsernamePassword ( b'<STR_LIT>' , b'<STR_LIT>' ) <EOL> self . badUser = credentials . UsernamePassword ( b'<STR_LIT:x>' , b'<STR_LIT>' ) <EOL> self . filename = self . mktemp ( ) <EOL> FilePath ( self . filename ) . setContent ( b'<STR_LIT>' ) <EOL> self . checker = strcred . makeChecker ( '<STR_LIT>' + self . filename ) <EOL> def _fakeFilename ( self ) : <EOL> filename = '<STR_LIT>' <EOL> while os . path . exists ( filename ) : <EOL> filename += '<STR_LIT:_>' <EOL> return filename <EOL> def test_isChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( self . checker ) ) <EOL> self . assertIn ( credentials . IUsernamePassword , <EOL> self . checker . credentialInterfaces ) <EOL> def test_fileCheckerSucceeds ( self ) : <EOL> """<STR_LIT>""" <EOL> def _gotAvatar ( username ) : <EOL> self . assertEqual ( username , self . admin . username ) <EOL> return ( self . checker <EOL> . requestAvatarId ( self . admin ) <EOL> . addCallback ( _gotAvatar ) ) <EOL> def test_fileCheckerFailsUsername ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertFailure ( self . checker . requestAvatarId ( self . badUser ) , <EOL> error . UnauthorizedLogin ) <EOL> def test_fileCheckerFailsPassword ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertFailure ( self . checker . requestAvatarId ( self . badPass ) , <EOL> error . UnauthorizedLogin ) <EOL> def test_failsWithEmptyFilename ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( ValueError , strcred . makeChecker , '<STR_LIT:file>' ) <EOL> self . assertRaises ( ValueError , strcred . makeChecker , '<STR_LIT>' ) <EOL> def test_warnWithBadFilename ( self ) : <EOL> """<STR_LIT>""" <EOL> oldOutput = cred_file . theFileCheckerFactory . errorOutput <EOL> newOutput = StringIO ( ) <EOL> cred_file . theFileCheckerFactory . errorOutput = newOutput <EOL> strcred . makeChecker ( '<STR_LIT>' + self . _fakeFilename ( ) ) <EOL> cred_file . theFileCheckerFactory . errorOutput = oldOutput <EOL> self . assertIn ( cred_file . invalidFileWarning , newOutput . getvalue ( ) ) <EOL> class SSHCheckerTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> skip = None <EOL> if requireModule ( '<STR_LIT>' ) is None : <EOL> skip = '<STR_LIT>' <EOL> if requireModule ( '<STR_LIT>' ) is None : <EOL> skip = '<STR_LIT>' <EOL> def test_isChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> sshChecker = strcred . makeChecker ( '<STR_LIT>' ) <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( sshChecker ) ) <EOL> self . assertIn ( <EOL> credentials . ISSHPrivateKey , sshChecker . credentialInterfaces ) <EOL> class DummyOptions ( usage . Options , strcred . AuthOptionMixin ) : <EOL> """<STR_LIT>""" <EOL> class CheckerOptionsTests ( unittest . TestCase ) : <EOL> def test_createsList ( self ) : <EOL> """<STR_LIT>""" <EOL> options = DummyOptions ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( options [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> options = DummyOptions ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( options [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> def test_invalidAuthError ( self ) : <EOL> """<STR_LIT>""" <EOL> options = DummyOptions ( ) <EOL> invalidParameter = getInvalidAuthType ( ) <EOL> self . assertRaises ( <EOL> usage . UsageError , <EOL> options . parseOptions , [ '<STR_LIT>' , invalidParameter ] ) <EOL> self . assertRaises ( <EOL> usage . UsageError , <EOL> options . parseOptions , [ '<STR_LIT>' , invalidParameter ] ) <EOL> def test_createsDictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> options = DummyOptions ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> chd = options [ '<STR_LIT>' ] <EOL> self . assertEqual ( len ( chd [ credentials . IAnonymous ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( chd [ credentials . IUsernamePassword ] ) , <NUM_LIT:1> ) <EOL> chdAnonymous = chd [ credentials . IAnonymous ] [ <NUM_LIT:0> ] <EOL> chdUserPass = chd [ credentials . IUsernamePassword ] [ <NUM_LIT:0> ] <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( chdAnonymous ) ) <EOL> self . assertTrue ( checkers . ICredentialsChecker . providedBy ( chdUserPass ) ) <EOL> self . assertIn ( credentials . IAnonymous , <EOL> chdAnonymous . credentialInterfaces ) <EOL> self . assertIn ( credentials . IUsernamePassword , <EOL> chdUserPass . credentialInterfaces ) <EOL> def test_credInterfacesProvidesLists ( self ) : <EOL> """<STR_LIT>""" <EOL> options = DummyOptions ( ) <EOL> options . parseOptions ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( <EOL> options [ '<STR_LIT>' ] , <EOL> options [ '<STR_LIT>' ] [ credentials . IUsernamePassword ] ) <EOL> def test_listDoesNotDisplayDuplicates ( self ) : <EOL> """<STR_LIT>""" <EOL> authTypes = [ ] <EOL> options = DummyOptions ( ) <EOL> for cf in options . _checkerFactoriesForOptHelpAuth ( ) : <EOL> self . assertNotIn ( cf . authType , authTypes ) <EOL> authTypes . append ( cf . authType ) <EOL> def test_displaysListCorrectly ( self ) : <EOL> """<STR_LIT>""" <EOL> newStdout = StringIO ( ) <EOL> options = DummyOptions ( ) <EOL> options . authOutput = newStdout <EOL> self . assertRaises ( SystemExit , options . parseOptions , [ '<STR_LIT>' ] ) <EOL> for checkerFactory in strcred . findCheckerFactories ( ) : <EOL> self . assertIn ( checkerFactory . authType , newStdout . getvalue ( ) ) <EOL> def test_displaysHelpCorrectly ( self ) : <EOL> """<STR_LIT>""" <EOL> newStdout = StringIO ( ) <EOL> options = DummyOptions ( ) <EOL> options . authOutput = newStdout <EOL> self . assertRaises ( <EOL> SystemExit , options . parseOptions , [ '<STR_LIT>' , '<STR_LIT:file>' ] ) <EOL> for line in cred_file . theFileCheckerFactory . authHelp : <EOL> if line . strip ( ) : <EOL> self . assertIn ( line . strip ( ) , newStdout . getvalue ( ) ) <EOL> def test_unexpectedException ( self ) : <EOL> """<STR_LIT>""" <EOL> options = DummyOptions ( ) <EOL> err = self . assertRaises ( usage . UsageError , options . parseOptions , <EOL> [ '<STR_LIT>' , '<STR_LIT:file>' ] ) <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> class OptionsForUsernamePassword ( usage . Options , strcred . AuthOptionMixin ) : <EOL> supportedInterfaces = ( credentials . IUsernamePassword , ) <EOL> class OptionsForUsernameHashedPassword ( usage . Options , strcred . AuthOptionMixin ) : <EOL> supportedInterfaces = ( credentials . IUsernameHashedPassword , ) <EOL> class OptionsSupportsAllInterfaces ( usage . Options , strcred . AuthOptionMixin ) : <EOL> supportedInterfaces = None <EOL> class OptionsSupportsNoInterfaces ( usage . Options , strcred . AuthOptionMixin ) : <EOL> supportedInterfaces = [ ] <EOL> class LimitingInterfacesTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . filename = self . mktemp ( ) <EOL> with open ( self . filename , '<STR_LIT:wb>' ) as f : <EOL> f . write ( b'<STR_LIT>' ) <EOL> self . goodChecker = checkers . FilePasswordDB ( self . filename ) <EOL> self . badChecker = checkers . FilePasswordDB ( <EOL> self . filename , hash = self . _hash ) <EOL> self . anonChecker = checkers . AllowAnonymousAccess ( ) <EOL> def _hash ( self , networkUsername , networkPassword , storedPassword ) : <EOL> """<STR_LIT>""" <EOL> return networkPassword <EOL> def test_supportsInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsForUsernamePassword ( ) <EOL> self . assertTrue ( <EOL> options . supportsInterface ( credentials . IUsernamePassword ) ) <EOL> self . assertFalse ( <EOL> options . supportsInterface ( credentials . IAnonymous ) ) <EOL> self . assertRaises ( <EOL> strcred . UnsupportedInterfaces , options . addChecker , <EOL> self . anonChecker ) <EOL> def test_supportsAllInterfaces ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsSupportsAllInterfaces ( ) <EOL> self . assertTrue ( <EOL> options . supportsInterface ( credentials . IUsernamePassword ) ) <EOL> self . assertTrue ( <EOL> options . supportsInterface ( credentials . IAnonymous ) ) <EOL> def test_supportsCheckerFactory ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsForUsernamePassword ( ) <EOL> fileCF = cred_file . theFileCheckerFactory <EOL> anonCF = cred_anonymous . theAnonymousCheckerFactory <EOL> self . assertTrue ( options . supportsCheckerFactory ( fileCF ) ) <EOL> self . assertFalse ( options . supportsCheckerFactory ( anonCF ) ) <EOL> def test_canAddSupportedChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsForUsernamePassword ( ) <EOL> options . addChecker ( self . goodChecker ) <EOL> iface = options . supportedInterfaces [ <NUM_LIT:0> ] <EOL> self . assertIdentical ( <EOL> options [ '<STR_LIT>' ] [ iface ] [ <NUM_LIT:0> ] , self . goodChecker ) <EOL> self . assertIdentical ( options [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , self . goodChecker ) <EOL> self . assertEqual ( len ( options [ '<STR_LIT>' ] [ iface ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( options [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> def test_failOnAddingUnsupportedChecker ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsForUsernameHashedPassword ( ) <EOL> self . assertRaises ( strcred . UnsupportedInterfaces , <EOL> options . addChecker , self . badChecker ) <EOL> def test_unsupportedInterfaceError ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsSupportsNoInterfaces ( ) <EOL> authType = cred_anonymous . theAnonymousCheckerFactory . authType <EOL> self . assertRaises ( <EOL> usage . UsageError , <EOL> options . parseOptions , [ '<STR_LIT>' , authType ] ) <EOL> def test_helpAuthLimitsOutput ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsForUsernamePassword ( ) <EOL> for factory in options . _checkerFactoriesForOptHelpAuth ( ) : <EOL> invalid = True <EOL> for interface in factory . credentialInterfaces : <EOL> if options . supportsInterface ( interface ) : <EOL> invalid = False <EOL> if invalid : <EOL> raise strcred . UnsupportedInterfaces ( ) <EOL> def test_helpAuthTypeLimitsOutput ( self ) : <EOL> """<STR_LIT>""" <EOL> options = OptionsForUsernamePassword ( ) <EOL> invalidFactory = None <EOL> for factory in strcred . findCheckerFactories ( ) : <EOL> if not options . supportsCheckerFactory ( factory ) : <EOL> invalidFactory = factory <EOL> break <EOL> self . assertNotIdentical ( invalidFactory , None ) <EOL> newStdout = StringIO ( ) <EOL> options . authOutput = newStdout <EOL> self . assertRaises ( SystemExit , options . parseOptions , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertIn ( strcred . notSupportedWarning , newStdout . getvalue ( ) ) <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ] </s>
