Ejemplo n.º 1
0
    def testLibDependencies(self):
        """Tests the list direct dependencies."""
        # Dependencies:
        #   u -> abc
        #   v -> abc
        #   prog -> u,v
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
                              defined_symbols=['fa', 'fb', 'fc'])
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libu.so'),
                              defined_symbols=['fu'],
                              undefined_symbols=['fa'],
                              used_libs=['abc'])
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libv.so'),
                              defined_symbols=['fv'],
                              undefined_symbols=['fb'],
                              used_libs=['abc'])
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'prog'),
                              undefined_symbols=['fu', 'fv'],
                              used_libs=['u', 'v'],
                              executable=True)

        elf_prog = parseelf.ParseELF(self.tempdir, 'prog', self._ldpaths)
        # Check the direct dependencies.
        self.assertTrue('libu.so' in elf_prog['needed'])
        self.assertTrue('libv.so' in elf_prog['needed'])
        self.assertFalse('libabc.so' in elf_prog['needed'])
Ejemplo n.º 2
0
    def testParsedSymbols(self):
        """Tests the list of imported/exported symbols."""
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
                              defined_symbols=['fa', 'fb', 'fc'])
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libxyz.so'),
                              defined_symbols=['fx', 'fy', 'fz'],
                              undefined_symbols=['fa', 'fb', 'fc'],
                              used_libs=['abc'])

        # Test without symbols.
        elf = parseelf.ParseELF(self.tempdir,
                                'libxyz.so',
                                self._ldpaths,
                                parse_symbols=False)
        self.assertFalse('imp_sym' in elf)
        self.assertFalse('exp_sym' in elf)

        # Test with symbols by default.
        elf = parseelf.ParseELF(self.tempdir,
                                'libxyz.so',
                                self._ldpaths,
                                parse_symbols=True)
        self.assertTrue('imp_sym' in elf)
        self.assertTrue('exp_sym' in elf)
        self.assertEquals(elf['imp_sym'], set(['fa', 'fb', 'fc']))
        self.assertIn('fx', elf['exp_sym'])
        self.assertIn('fy', elf['exp_sym'])
        self.assertIn('fz', elf['exp_sym'])
Ejemplo n.º 3
0
    def testParsedSymbols(self):
        """Tests the list of imported/exported symbols."""
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
                              defined_symbols=['fa', 'fb', 'fc'])
        unittest_lib.BuildELF(os.path.join(self.tempdir, 'libxyz.so'),
                              defined_symbols=['fx', 'fy', 'fz'],
                              undefined_symbols=['fa', 'fb', 'fc'],
                              used_libs=['abc'])

        # Test without symbols.
        elf = parseelf.ParseELF(self.tempdir,
                                'libxyz.so',
                                self._ldpaths,
                                parse_symbols=False)
        self.assertFalse('imp_sym' in elf)
        self.assertFalse('exp_sym' in elf)

        # Test with symbols by default.
        elf = parseelf.ParseELF(self.tempdir, 'libxyz.so', self._ldpaths)
        self.assertTrue('imp_sym' in elf)
        self.assertTrue('exp_sym' in elf)
        self.assertEquals(elf['imp_sym'], set(['fa', 'fb', 'fc']))
        self.assertEquals(
            set(k for k, (_, _, st_shndx) in elf['exp_sym'].iteritems()
                if st_shndx == 'SHT_DYNSYM'), set(['fx', 'fy', 'fz']))
        for sym in ['fx', 'fy', 'fz']:
            self.assertEquals('STB_GLOBAL', elf['exp_sym'][sym][0])
Ejemplo n.º 4
0
  def testSimpleDep(self):
    unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
                          ['func_a', 'func_b', 'func_c'])
    unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'),
                          undefined_symbols=['func_b'],
                          used_libs=['abc'],
                          executable=True)
    dt = dep_tracker.DepTracker(self.tempdir)
    dt.Init()
    dt.ComputeELFFileDeps()

    self.assertEquals(sorted(dt._files.keys()), ['abc_main', 'libabc.so'])
Ejemplo n.º 5
0
 def testNotIsLib(self):
     """Tests the 'is_lib' attribute is inferred correctly for executables."""
     unittest_lib.BuildELF(os.path.join(self.tempdir, 'abc_main'),
                           executable=True)
     elf = parseelf.ParseELF(self.tempdir, 'abc_main', self._ldpaths)
     self.assertTrue('is_lib' in elf)
     self.assertFalse(elf['is_lib'])
Ejemplo n.º 6
0
 def testIsLib(self):
     """Tests the 'is_lib' attribute is inferred correctly for libs."""
     unittest_lib.BuildELF(os.path.join(self.tempdir, 'liba.so'),
                           ['func_a'])
     elf = parseelf.ParseELF(self.tempdir, 'liba.so', self._ldpaths)
     self.assertTrue('is_lib' in elf)
     self.assertTrue(elf['is_lib'])
Ejemplo n.º 7
0
 def testRelativeLibPaths(self):
   """Test that the paths reported by ParseELF are relative to root."""
   unittest_lib.BuildELF(os.path.join(self.tempdir, 'liba.so'), ['fa'])
   unittest_lib.BuildELF(os.path.join(self.tempdir, 'prog'),
                         undefined_symbols=['fa'], used_libs=['a'],
                         executable=True)
   elf = parseelf.ParseELF(self.tempdir, 'prog', self._ldpaths)
   for lib in elf['libs'].values():
     for path in ('realpath', 'path'):
       if lib[path] is None:
         continue
       self.assertFalse(lib[path].startswith('/'))
       self.assertFalse(lib[path].startswith(self.tempdir))
       # Linked lib paths should be relative to the working directory or is the
       # ld dynamic loader.
       self.assertTrue(lib[path] == elf['interp'] or
                       os.path.exists(os.path.join(self.tempdir, lib[path])))
    def testBinaryELFFiles(self):
        """Test decoding ELF files."""
        liba_so = os.path.join(self.tempdir, 'liba.so')
        unittest_lib.BuildELF(liba_so, ['func_a'])
        self.assertEqual('binary/elf/dynamic-so',
                         filetype.FileTypeDecoder.DecodeFile(liba_so))

        prog = os.path.join(self.tempdir, 'prog')
        unittest_lib.BuildELF(prog,
                              undefined_symbols=['func_a'],
                              used_libs=['a'],
                              executable=True)
        self.assertEqual('binary/elf/dynamic-bin',
                         filetype.FileTypeDecoder.DecodeFile(prog))

        prog_static = os.path.join(self.tempdir, 'prog_static')
        unittest_lib.BuildELF(prog_static, executable=True, static=True)
        self.assertEqual('binary/elf/static',
                         filetype.FileTypeDecoder.DecodeFile(prog_static))
Ejemplo n.º 9
0
  def testFiletypeSet(self):
    """Tests that the 'ftype' member is set for ELF files first."""
    unittest_lib.BuildELF(os.path.join(self.tempdir, 'libabc.so'),
                          ['func_a', 'func_b', 'func_c'])
    osutils.WriteFile(os.path.join(self.tempdir, 'pyscript'),
                      "#!/usr/bin/python\nimport sys\nsys.exit(42)\n")
    dt = dep_tracker.DepTracker(self.tempdir)
    dt.Init()

    # ComputeELFFileDeps() should compute the file type of ELF files so we
    # don't need to parse them again.
    dt.ComputeELFFileDeps()
    self.assertTrue('ftype' in dt._files['libabc.so'])
    self.assertFalse('ftype' in dt._files['pyscript'])

    # ComputeFileTypes() shold compute the file type of every file.
    dt.ComputeFileTypes()
    self.assertTrue('ftype' in dt._files['libabc.so'])
    self.assertTrue('ftype' in dt._files['pyscript'])