예제 #1
0
    def test_fail_to_load_magic_raises_exception(self, mock_magic):
        mock_magic.return_value.load.return_value = 1

        with self.assertRaises(RuntimeError) as raised:
            pluginhandler._find_dependencies('.')

        self.assertEqual(
            raised.exception.__str__(), 'Cannot load magic header detection')
예제 #2
0
    def test_no_find_dependencies_of_non_dynamically_linked(
            self, mock_dependencies, mock_magic):
        workdir = os.path.join(os.getcwd(), 'workdir')
        os.makedirs(workdir)

        statically_linked_elf_path = os.path.join(workdir, 'statically-linked')
        open(statically_linked_elf_path, 'w').close()

        mock_ms = Mock()
        mock_magic.return_value = mock_ms
        mock_ms.load.return_value = 0
        mock_ms.file.return_value = (
            'ELF 64-bit LSB executable, x86-64, version 1 (SYSV), '
            'statically linked, for GNU/Linux 2.6.32, '
            'BuildID[sha1]=XYZ, stripped')

        dependencies = pluginhandler._find_dependencies(workdir)

        mock_ms.file.assert_called_once_with(
            bytes(statically_linked_elf_path, 'utf-8'))

        self.assertFalse(
            mock_dependencies.called,
            'statically linked files should not have library dependencies')

        self.assertFalse(dependencies)
예제 #3
0
    def test_find_dependencies(self, mock_dependencies, mock_magic):
        workdir = os.path.join(os.getcwd(), 'workdir')
        os.makedirs(workdir)

        linked_elf_path = os.path.join(workdir, 'linked')
        open(linked_elf_path, 'w').close()

        mock_ms = Mock()
        mock_magic.return_value = mock_ms
        mock_ms.load.return_value = 0
        mock_ms.file.return_value = (
            'ELF 64-bit LSB executable, x86-64, version 1 (SYSV), '
            'dynamically linked interpreter /lib64/ld-linux-x86-64.so.2, '
            'for GNU/Linux 2.6.32, BuildID[sha1]=XYZ, stripped')

        mock_dependencies.return_value = ['/usr/lib/libDepends.so']

        dependencies = pluginhandler._find_dependencies(workdir)

        mock_ms.file.assert_called_once_with(bytes(linked_elf_path, 'utf-8'))
        self.assertEqual(dependencies, {'/usr/lib/libDepends.so'})
예제 #4
0
    def test_no_find_dependencies_of_symlinks(
            self, mock_dependencies, mock_magic):
        workdir = os.path.join(os.getcwd(), 'workdir')
        os.makedirs(workdir)

        symlinked_path = os.path.join(workdir, 'symlinked')
        os.symlink('/bin/dash', symlinked_path)

        mock_ms = Mock()
        mock_magic.return_value = mock_ms
        mock_ms.load.return_value = 0

        dependencies = pluginhandler._find_dependencies(workdir)

        self.assertFalse(
            mock_ms.file.called, 'magic is not needed for symlinks')

        self.assertFalse(
            mock_dependencies.called,
            'statically linked files should not have library dependencies')

        self.assertFalse(
            dependencies,
            'statically linked files should not have library dependencies')
예제 #5
0
    def test_no_find_dependencies_of_non_elf_files(
            self, mock_dependencies, mock_magic):
        workdir = os.path.join(os.getcwd(), 'workdir')
        os.makedirs(workdir)

        non_elf_path = os.path.join(workdir, 'non-elf')
        open(non_elf_path, 'w').close()

        mock_ms = Mock()
        mock_magic.return_value = mock_ms
        mock_ms.load.return_value = 0
        mock_ms.file.return_value = 'JPEG image data, Exif standard: ...'

        dependencies = pluginhandler._find_dependencies(workdir)

        mock_ms.file.assert_called_once_with(bytes(non_elf_path, 'utf-8'))

        self.assertFalse(
            mock_dependencies.called,
            'non elf files should not have library dependencies')

        self.assertFalse(
            dependencies,
            'non elf files should not have library dependencies')