def test_run(): init_testenv() # Read in and process hosts baseline test i/p file hosts = sccm.SccmHosts() hosts.read(mydir='data/df_sys_base.csv') hosts.save() gbls.ad_vip_grps = 'data/ps-ad-vip.csv' # Invoke Input plugin for customized I/P data plugin1 = gbls.plugin_manager.getPluginByName(gbls.PLUGINIP) plugin1.plugin_object.modify_hosts(hosts) df_hosts = hosts.get() df_hosts.to_pickle('data/df_sys_base.pck') print('Hosts file initialized') # Read in sccm software inventory files sft = sccm.SccmSoft() sft.read( mydir_x86='data/df_v_gs_add_rem_base_x86.csv', mydir_x64='data/df_v_gs_add_rem_base_x64.csv', ) df_sft = sft.get() df_sft.to_pickle('data/df_v_gs_add_rem_base.pck') print('Software file initialized') # use edited CPE file to produce a packed dataframe for baseline # comparison # Convert the i/p XML file to a dataframe cpe = nvd.NvdCpe() cpe.read(my_cpe='data/official-cpe-dictionary_v2.3.base.xml') df_cpe = cpe.get() df_cpe.to_pickle('data/df_cpe4_base.pck') print('NVD CPE file initialized') # use CVE I/P file to produce a packed dataframe for baseline # comparison cve = nvd.NvdCve() df_cve = cve.read(my_dir='data/') df_cve = cve.get() df_cve.to_pickle("data/df_cve_base.pck") print('NVD CVE file initialized') # Force error see o/p assert False
def test_cve_read(self, init_testenv): """ Test the CVE parsing function """ if init_testenv != "Initialized": exit('nvd - TestNvdCve initialization failed, exiting') # Fire up the code to be tested cve = nvd.NvdCve() cve.read(my_dir="data/") df_cve_processed = cve.get() # load up base dframe for comparison cve.load(mypck="data/df_cve_base.pck") df_cve_base = cve.get() # Check calculated dframe against base dframe assert df_cve_base.equals(df_cve_processed)
def init_matching(self): print('init_matching: Initialize for matching.') sft = sccm.SccmSoft() sft.load(mydir='data/df_match_sccm.pck') df_sft = sft.get() print ('Match tests: Software inventory file initialized') cpe = nvd.NvdCpe() cpe.load(mypck='data/df_match_cpe4.pck') df_cpe = cpe.get() print ('Match tests: NVD CPE file initialized') # Load cve dframe cve = nvd.NvdCve() cve.load(mypck="data/df_cve_base.pck") df_cve = cve.get() print ('Match tests: NVD CVE file initialized') # Vendor matching baseline comparison dframe match_vendor_base = matchven.MatchVendor() match_vendor_base.load(mypck='data/df_match_vendor_baseline.pck') df_match_vendor_base = match_vendor_base.get() print ('Match tests: Vendor match baseline dframe initialized') # Software matching baseline comparison dframe match_soft_base = matchsft.MatchSoft() match_soft_base.load(mypck='data/df_match_sft_baseline.pck') df_match_sft_base = match_soft_base.get() print ('Match tests: Software match baseline dframe initialized') return ( df_sft, df_cpe, df_cve, df_match_vendor_base, df_match_sft_base )
def test_cve_download(self, init_testenv): """ Test the CVE download function over multiple yrs / conditions """ if init_testenv != "Initialized": exit('nvd - TestNvdCve initialization failed, exiting') def mock_http(my_url, my_file, my_content_type): """ Set up a mock http file download """ # Read flat file as a string to return as mock http response print("Entering mock_http: {0}, {1}, {2}".format( my_url, my_file, my_content_type)) try: with open(my_file, "rb") as myfile: mybuf = myfile.read() # print 1st 50 lines i/p if '.zip' in my_file: print("mock_http: Download of {0}".format(my_file)) else: print("mock_http:\n{0}\n\n".format(mybuf[:1000])) except Exception as e: print(e) mybuf = None # Set up mock http response to return test file responses.add(responses.GET, my_url, body=mybuf, status=200, content_type=my_content_type) def set_fnames_urls(my_index): """Set the filenames and urls for a given yr""" if my_index < 0 or my_index > 1: print "set_fnames_urls: index must be either 0 or 1" assert False return (None, None, None) # Determine yr being processed now = datetime.datetime.now() my_yr = now.year yr_processed = my_yr - my_index # Target meta file meta_dest_filename = (gbls.nvddir + gbls.nvd_meta_filename + str(yr_processed)) # Base meta file (used for comparison) meta_base_filename = "data/cve_meta_base" + str(my_index) # URL for mock download of CVE meta file url_meta = (gbls.url_meta_base + str(yr_processed) + gbls.url_meta_end) # Set up mock http download of meta file mock_http(url_meta, meta_base_filename, "text/plain") # URL to read the corresponding CVE XML feed file url_xml = (gbls.url_xml_base + str(yr_processed) + gbls.url_xml_end) # Target cve xml file cve_filename = (gbls.nvdcve + str(yr_processed) + '.xml') print('set_fnames_urls:\n {0}, {1}, {2}\n' ' {3}\n'.format(meta_base_filename, meta_dest_filename, cve_filename, url_xml)) return (meta_base_filename, meta_dest_filename, cve_filename, url_xml) ###### # Initialize for test: will read files for last 2 yrs # # Mock downloads of meta files for both yrs will occur. # # "Last yr" will be set to not download (i.e. meta files equal). if a # mock download nonetheless occurs this will throw an error. # # "Current yr" will be set cause a mock download of the corresponding # zipped XML file. ###### gbls.num_nvd_files = 2 # *** Set up last yr *** # Initialize filenames and urls (meta_base_filename, meta_dest_filename, cve_filename, url_xml) = set_fnames_urls(1) # Ensure that meta files match so now download copyfile(meta_base_filename, meta_dest_filename) # Set url for cve xml download to throw error exception = HTTPError( 'NIST Meta file check failed' '- Should have been no download of CVE XML files') responses.add(responses.GET, url_xml, body=exception) # *** Set up current year *** # Initialize filenames and urls (meta_base_filename, meta_dest_filename, cve_filename, url_xml) = set_fnames_urls(0) # Remove target meta file. This will force download of cve xml file try: os.remove(meta_dest_filename) except OSError: pass # Set up mock http download of meta file mock_http(url_xml, 'data/cve_xml_base.zip', 'application/x-zip-compressed') # Fire up the code to be tested cve = nvd.NvdCve() cve.download_cve() # Check that extracted file matches the baseline version assert filecmp.cmp(cve_filename, 'data/cve_xml_base', False)
def test_run(): init_testenv() # initialize data for match tests cpe = nvd.NvdCpe() cpe.read(my_cpe='data/match_official-cpe-dictionary_v2.3.xml') df_cpe = cpe.get() df_cpe.to_pickle('data/df_match_cpe4.pck') print ('Match tests: NVD CPE file initialized') sft = sccm.SccmSoft() sft.read( mydir_x86='data/df_match_sccm_86.csv', mydir_x64='data/df_match_sccm_64.csv', ) df_sft = sft.get() df_sft.to_pickle('data/df_match_sccm.pck') print ('Match tests: Software file initialized') # Run vendor matching match_vendor = matchven.MatchVendor() match_vendor.match( df_cpe, df_sft ) df_match_vendor = match_vendor.get() df_match_vendor.to_pickle('data/df_match_vendor_baseline.pck') print ('Match tests: Vendor match dframe initialized') # Run software matching. # - First load cve dframe cve_base = nvd.NvdCve() cve_base.load(mypck="data/df_cve_base.pck") df_cve = cve_base.get() match_soft = matchsft.MatchSoft() match_soft.match( df_match_vendor, df_sft, df_cpe ) df_match_sft = match_soft.get() df_match_sft.to_pickle('data/df_match_sft_baseline.pck') print ('Match tests: Software match dframe initialized') # Match vulns to software # - First load hosts dframe hosts_base = sccm.SccmHosts() hosts_base.load(mydir="data/df_sys_base.pck") df_hosts = hosts_base.get() match_vulns = vulns.MatchVulns() match_vulns.data_merge( df_cve, df_match_sft, df_sft, df_hosts ) df_match_vulns = match_vulns.get() df_match_vulns.to_pickle('data/df_match_vulns_baseline.pck') print ('Match tests: Vuln match dframe initialized') # Force error see o/p assert False