1 #!/usr/bin/env python 2 # Copyright 2014 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import os 7 import sys 8 import unittest 9 10 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) 11 PARENT_DIR = os.path.dirname(SCRIPT_DIR) 12 DATA_DIR = os.path.join(SCRIPT_DIR, 'data') 13 14 sys.path.append(PARENT_DIR) 15 16 import elf 17 18 19 class TestIsDynamicElf(unittest.TestCase): 20 def test_arm(self): 21 static_nexe = os.path.join(DATA_DIR, 'test_static_arm.nexe') 22 self.assertFalse(elf.IsDynamicElf(static_nexe, False)) 23 24 def test_x86_32(self): 25 dyn_nexe = os.path.join(DATA_DIR, 'test_dynamic_x86_32.nexe') 26 static_nexe = os.path.join(DATA_DIR, 'test_static_x86_32.nexe') 27 self.assertTrue(elf.IsDynamicElf(dyn_nexe, False)) 28 self.assertFalse(elf.IsDynamicElf(static_nexe, False)) 29 30 def test_x86_64(self): 31 dyn_nexe = os.path.join(DATA_DIR, 'test_dynamic_x86_64.nexe') 32 static_nexe = os.path.join(DATA_DIR, 'test_static_x86_64.nexe') 33 self.assertTrue(elf.IsDynamicElf(dyn_nexe, True)) 34 self.assertFalse(elf.IsDynamicElf(static_nexe, True)) 35 36 37 class TestParseElfHeader(unittest.TestCase): 38 def test_invalid_elf(self): 39 self.assertRaises(elf.Error, elf.ParseElfHeader, __file__) 40 41 def test_arm_elf_parse(self): 42 """Test parsing of ARM elf header.""" 43 static_nexe = os.path.join(DATA_DIR, 'test_static_arm.nexe') 44 arch, dynamic = elf.ParseElfHeader(static_nexe) 45 self.assertEqual(arch, 'arm') 46 self.assertFalse(dynamic) 47 48 def test_x86_32_elf_parse(self): 49 """Test parsing of x86-32 elf header.""" 50 dyn_nexe = os.path.join(DATA_DIR, 'test_dynamic_x86_32.nexe') 51 static_nexe = os.path.join(DATA_DIR, 'test_static_x86_32.nexe') 52 53 arch, dynamic = elf.ParseElfHeader(dyn_nexe) 54 self.assertEqual(arch, 'x86-32') 55 self.assertTrue(dynamic) 56 57 arch, dynamic = elf.ParseElfHeader(static_nexe) 58 self.assertEqual(arch, 'x86-32') 59 self.assertFalse(dynamic) 60 61 def test_x86_64_elf_parse(self): 62 """Test parsing of x86-64 elf header.""" 63 dyn_nexe = os.path.join(DATA_DIR, 'test_dynamic_x86_64.nexe') 64 static_nexe = os.path.join(DATA_DIR, 'test_static_x86_64.nexe') 65 66 arch, dynamic = elf.ParseElfHeader(dyn_nexe) 67 self.assertEqual(arch, 'x86-64') 68 self.assertTrue(dynamic) 69 70 arch, dynamic = elf.ParseElfHeader(static_nexe) 71 self.assertEqual(arch, 'x86-64') 72 self.assertFalse(dynamic) 73 74 75 if __name__ == '__main__': 76 unittest.main() 77