Example #1
0
    def run_and_parse(self, program, args, print_output=False):
        """ run single program with single file and parse output """
        return_code = None
        with OutputCapture() as capturer:       # capture stdout
            try:
                return_code = program(args)
            except Exception:
                return_code = 1   # would result in non-zero exit
            except SystemExit as se:
                return_code = se.code or 0   # se.code can be None
        if return_code is not 0:
            if print_output:
                print('Command failed ({0}) -- not parsing output'
                      .format(return_code))
            return []    # no need to test

        self.assertNotEqual(return_code, None,
                            msg='self-test fail: return_code not set')

        # now test output
        if print_output:
            print(capturer.buffer.getvalue())
        capturer.buffer.seek(0)    # re-set position in file-like stream
        try:
            json_data = json.load(capturer.buffer)
        except ValueError:
            self.fail('Invalid json:\n' + capturer.buffer.getvalue())
        self.assertNotEqual(len(json_data), 0, msg='Output was empty')
        return json_data
Example #2
0
    def write_and_run(self, sample_text):
        """ helper for test_texts: save text to file, run through msodde """
        filename = None
        handle = 0
        try:
            handle, filename = mkstemp(prefix='oletools-test-csv-', text=True)
            os.write(handle, sample_text.encode('ascii'))
            os.close(handle)
            handle = 0
            args = [
                filename,
            ]
            if self.DO_DEBUG:
                args += ['-l', 'debug']

            with OutputCapture() as capturer:
                capturer.reload_module(msodde)  # re-create logger
                ret_code = msodde.main(args)
            self.assertEqual(
                ret_code, 0, 'checking sample resulted in '
                'error:\n' + sample_text)
            return capturer

        except Exception:
            raise
        finally:
            if handle:
                os.close(handle)
                handle = 0  # just in case
            if filename:
                if self.DO_DEBUG:
                    print('keeping for debug purposes: {0}'.format(filename))
                else:
                    os.remove(filename)
                filename = None  # just in case
Example #3
0
 def test_no_dde(self):
     """ check that no dde links appear on stdout """
     with OutputCapture() as capturer:
         msodde.main([join(BASE_DIR, 'msodde-doc', 'harmless-clean.doc')])
     self.assertEqual(len(self.get_dde_from_output(capturer)),
                      0,
                      msg='Found dde links in output for doc file')
Example #4
0
 def test_clean_rtf_ddeonly(self):
     """ find no dde links in rtf spec """
     filename = 'RTF-Spec-1.7.rtf'
     with OutputCapture() as capturer:
         msodde.main(['-d', join(BASE_DIR, 'msodde', filename)])
     self.assertEqual(len(self.get_dde_from_output(capturer)), 0,
                      msg='Found dde links in output of ' + filename)
Example #5
0
 def test_with_dde_utf16le(self):
     """ check that dde links appear on stdout """
     filename = 'dde-test-from-office2013-utf_16le-korean.doc'
     with OutputCapture() as capturer:
         msodde.main([join(BASE_DIR, 'msodde', filename)])
     self.assertNotEqual(len(self.get_dde_from_output(capturer)), 0,
                         msg='Found no dde links in output of ' + filename)
Example #6
0
 def test_with_dde(self):
     """ check that dde links appear on stdout """
     with OutputCapture() as capturer:
         msodde.main(
             [join(BASE_DIR, 'msodde-doc', 'dde-test-from-office2003.doc')])
     self.assertNotEqual(len(self.get_dde_from_output(capturer)),
                         0,
                         msg='Found no dde links in output for doc file')
Example #7
0
 def test_excel(self):
     """ check that dde links are found in excel 2007+ files """
     expect = ['DDE-Link cmd /c calc.exe', ]
     for extn in 'xlsx', 'xlsm', 'xlsb':
         with OutputCapture() as capturer:
             msodde.main([join(BASE_DIR, 'msodde', 'dde-test.' + extn), ])
         self.assertEqual(expect, self.get_dde_from_output(capturer),
                          msg='unexpected output for dde-test.{0}: {1}'
                              .format(extn, capturer.get_data()))
Example #8
0
 def test_file(self):
     """ test simple small example file """
     filename = join(DATA_BASE_DIR, 'msodde', 'dde-in-csv.csv')
     with OutputCapture() as capturer:
         capturer.reload_module(msodde)    # re-create logger
         ret_code = msodde.main([filename, ])
     self.assertEqual(ret_code, 0)
     links = self.get_dde_from_output(capturer)
     self.assertEqual(len(links), 1)
     self.assertEqual(links[0],
                      r"cmd '/k \..\..\..\Windows\System32\calc.exe'")
Example #9
0
 def test_excel(self):
     """ check that dde links are found in excel 2007+ files """
     expect = [
         'DDE-Link cmd /c calc.exe',
     ]
     for extn in 'xlsx', 'xlsm':  # not yet: 'xlsb'
         with OutputCapture() as capturer:
             msodde.main([
                 join(BASE_DIR, 'msodde', 'dde-test.' + extn),
             ])
         self.assertEqual(expect, self.get_dde_from_output(capturer))
Example #10
0
 def test_xml(self):
     """ check that dde in xml from word / excel is found """
     for name_part in 'excel2003', 'word2003', 'word2007':
         filename = 'dde-in-' + name_part + '.xml'
         with OutputCapture() as capturer:
             msodde.main([
                 join(BASE_DIR, 'msodde', filename),
             ])
         links = self.get_dde_from_output(capturer)
         self.assertEqual(
             len(links), 1,
             'found {0} dde-links in {1}'.format(len(links), filename))
         self.assertTrue('cmd' in links[0],
                         'no "cmd" in dde-link for {0}'.format(filename))
         self.assertTrue('calc' in links[0],
                         'no "calc" in dde-link for {0}'.format(filename))
Example #11
0
 def test_clean_rtf_blacklist(self):
     """ find a lot of hyperlinks in rtf spec """
     filename = 'RTF-Spec-1.7.rtf'
     with OutputCapture() as capturer:
         msodde.main([join(BASE_DIR, 'msodde', filename)])
     self.assertEqual(len(self.get_dde_from_output(capturer)), 1413)