<s> """<STR_LIT>""" <EOL> from grr . lib import flags <EOL> from grr . lib import test_lib <EOL> from grr . lib . checks import checks_test_lib <EOL> from grr . lib . rdfvalues import config_file as rdf_config <EOL> from grr . parsers import linux_pam_parser <EOL> ETC_PAM_CONF_EMPTY = """<STR_LIT>""" <EOL> ETC_PAM_CONF_SIMPLE = """<STR_LIT>""" <EOL> ETC_PAM_CONF_COMPLEX = ETC_PAM_CONF_SIMPLE + """<STR_LIT>""" <EOL> ETC_PAMD_FILT_INCLUDE = """<STR_LIT>""" <EOL> ETC_PAMD_FULL_INCLUDE = ETC_PAMD_FILT_INCLUDE <EOL> ETC_PAMD_SSH = """<STR_LIT>""" <EOL> ETC_PAMD_TELNET = """<STR_LIT>""" <EOL> ETC_PAMD_EXTERNAL = """<STR_LIT>""" <EOL> TELNET_ONLY_CONFIG = { '<STR_LIT>' : ETC_PAMD_TELNET } <EOL> TELNET_ONLY_CONFIG_EXPECTED = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> TELNET_WITH_PAMCONF = { '<STR_LIT>' : ETC_PAM_CONF_EMPTY , <EOL> '<STR_LIT>' : ETC_PAMD_TELNET } <EOL> TELNET_WITH_PAMCONF_EXPECTED = TELNET_ONLY_CONFIG_EXPECTED <EOL> PAM_CONF_SIMPLE = { '<STR_LIT>' : ETC_PAM_CONF_SIMPLE } <EOL> PAM_CONF_SIMPLE_EXPECTED = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> PAM_CONF_OVERRIDE = { '<STR_LIT>' : ETC_PAM_CONF_SIMPLE , <EOL> '<STR_LIT>' : ETC_PAMD_TELNET } <EOL> PAM_CONF_OVERRIDE_EXPECTED = PAM_CONF_SIMPLE_EXPECTED <EOL> PAM_CONF_OVERRIDE_COMPLEX = { '<STR_LIT>' : ETC_PAM_CONF_COMPLEX , <EOL> '<STR_LIT>' : ETC_PAMD_SSH , <EOL> '<STR_LIT>' : ETC_PAMD_FULL_INCLUDE , <EOL> '<STR_LIT>' : ETC_PAMD_FILT_INCLUDE , <EOL> '<STR_LIT>' : ETC_PAMD_TELNET } <EOL> PAM_CONF_OVERRIDE_COMPLEX_EXPECTED = PAM_CONF_SIMPLE_EXPECTED + [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> PAM_CONF_TYPICAL = { '<STR_LIT>' : ETC_PAM_CONF_EMPTY , <EOL> '<STR_LIT>' : ETC_PAMD_SSH , <EOL> '<STR_LIT>' : ETC_PAMD_FULL_INCLUDE , <EOL> '<STR_LIT>' : ETC_PAMD_FILT_INCLUDE , <EOL> '<STR_LIT>' : ETC_PAMD_TELNET } <EOL> PAM_CONF_TYPICAL_EXPECTED = TELNET_ONLY_CONFIG_EXPECTED + [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ] <EOL> PAM_CONF_EXTERNAL_REF = { '<STR_LIT>' : ETC_PAM_CONF_EMPTY , <EOL> '<STR_LIT>' : ETC_PAMD_EXTERNAL } <EOL> PAM_CONF_EXTERNAL_REF_EXPECTED = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> PAM_CONF_EXTERNAL_REF_ERRORS = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class LinuxPAMParserTest ( test_lib . GRRBaseTest ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( LinuxPAMParserTest , self ) . setUp ( ) <EOL> self . kb = checks_test_lib . HostCheckTest . SetKnowledgeBase ( ) [ '<STR_LIT>' ] <EOL> def _EntryToTuple ( self , entry ) : <EOL> return ( entry . service , entry . type , entry . control , <EOL> entry . module_path , entry . module_args ) <EOL> def _EntriesToTuples ( self , entries ) : <EOL> return [ self . _EntryToTuple ( x ) for x in entries ] <EOL> def testParseMultiple ( self ) : <EOL> """<STR_LIT>""" <EOL> parser = linux_pam_parser . PAMParser ( ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> TELNET_ONLY_CONFIG ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> self . assertItemsEqual ( TELNET_ONLY_CONFIG_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertEqual ( [ ] , out [ <NUM_LIT:0> ] . external_config ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> TELNET_WITH_PAMCONF ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> entry = out [ <NUM_LIT:0> ] . entries [ <NUM_LIT:0> ] <EOL> self . assertEqual ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> self . _EntryToTuple ( entry ) ) <EOL> self . assertItemsEqual ( TELNET_WITH_PAMCONF_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertEqual ( [ ] , out [ <NUM_LIT:0> ] . external_config ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> PAM_CONF_SIMPLE ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> self . assertItemsEqual ( PAM_CONF_SIMPLE_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertEqual ( [ ] , out [ <NUM_LIT:0> ] . external_config ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> PAM_CONF_OVERRIDE ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> self . assertItemsEqual ( PAM_CONF_OVERRIDE_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertEqual ( [ ] , out [ <NUM_LIT:0> ] . external_config ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> PAM_CONF_OVERRIDE_COMPLEX ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> self . assertItemsEqual ( PAM_CONF_OVERRIDE_COMPLEX_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertEqual ( [ ] , out [ <NUM_LIT:0> ] . external_config ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> PAM_CONF_TYPICAL ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> self . assertItemsEqual ( PAM_CONF_TYPICAL_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertEqual ( [ ] , out [ <NUM_LIT:0> ] . external_config ) <EOL> stats , file_objs = checks_test_lib . HostCheckTest . GenStatFileData ( <EOL> PAM_CONF_EXTERNAL_REF ) <EOL> out = list ( parser . ParseMultiple ( stats , file_objs , self . kb ) ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( isinstance ( out [ <NUM_LIT:0> ] , rdf_config . PamConfig ) ) <EOL> self . assertItemsEqual ( PAM_CONF_EXTERNAL_REF_EXPECTED , <EOL> self . _EntriesToTuples ( out [ <NUM_LIT:0> ] . entries ) ) <EOL> self . assertItemsEqual ( PAM_CONF_EXTERNAL_REF_ERRORS , <EOL> list ( out [ <NUM_LIT:0> ] . external_config ) ) <EOL> def main ( args ) : <EOL> test_lib . main ( args ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> flags . StartMain ( main ) </s>
