2 # Copyright (c) 2012 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.
11 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
12 PARENT_DIR
= os
.path
.dirname(SCRIPT_DIR
)
13 DATA_DIR
= os
.path
.join(SCRIPT_DIR
, 'data')
15 sys
.path
.append(PARENT_DIR
)
21 class TestIsDynamicElf(unittest
.TestCase
):
23 static_nexe
= os
.path
.join(DATA_DIR
, 'test_static_arm.nexe')
24 self
.assertFalse(create_nmf
.IsDynamicElf(static_nexe
, False))
26 def test_x86_32(self
):
27 dyn_nexe
= os
.path
.join(DATA_DIR
, 'test_dynamic_x86_32.nexe')
28 static_nexe
= os
.path
.join(DATA_DIR
, 'test_static_x86_32.nexe')
29 self
.assertTrue(create_nmf
.IsDynamicElf(dyn_nexe
, False))
30 self
.assertFalse(create_nmf
.IsDynamicElf(static_nexe
, False))
32 def test_x86_64(self
):
33 dyn_nexe
= os
.path
.join(DATA_DIR
, 'test_dynamic_x86_64.nexe')
34 static_nexe
= os
.path
.join(DATA_DIR
, 'test_static_x86_64.nexe')
35 self
.assertTrue(create_nmf
.IsDynamicElf(dyn_nexe
, True))
36 self
.assertFalse(create_nmf
.IsDynamicElf(static_nexe
, True))
39 class TestParseElfHeader(unittest
.TestCase
):
40 def test_invalid_elf(self
):
41 self
.assertRaises(create_nmf
.Error
, create_nmf
.ParseElfHeader
, __file__
)
43 def test_arm_elf_parse(self
):
44 """Test parsing of ARM elf header."""
45 static_nexe
= os
.path
.join(DATA_DIR
, 'test_static_arm.nexe')
46 arch
, dynamic
= create_nmf
.ParseElfHeader(static_nexe
)
47 self
.assertEqual(arch
, 'arm')
48 self
.assertFalse(dynamic
)
50 def test_x86_32_elf_parse(self
):
51 """Test parsing of x86-32 elf header."""
52 dyn_nexe
= os
.path
.join(DATA_DIR
, 'test_dynamic_x86_32.nexe')
53 static_nexe
= os
.path
.join(DATA_DIR
, 'test_static_x86_32.nexe')
55 arch
, dynamic
= create_nmf
.ParseElfHeader(dyn_nexe
)
56 self
.assertEqual(arch
, 'x86-32')
57 self
.assertTrue(dynamic
)
59 arch
, dynamic
= create_nmf
.ParseElfHeader(static_nexe
)
60 self
.assertEqual(arch
, 'x86-32')
61 self
.assertFalse(dynamic
)
63 def test_x86_64_elf_parse(self
):
64 """Test parsing of x86-64 elf header."""
65 dyn_nexe
= os
.path
.join(DATA_DIR
, 'test_dynamic_x86_64.nexe')
66 static_nexe
= os
.path
.join(DATA_DIR
, 'test_static_x86_64.nexe')
68 arch
, dynamic
= create_nmf
.ParseElfHeader(dyn_nexe
)
69 self
.assertEqual(arch
, 'x86-64')
70 self
.assertTrue(dynamic
)
72 arch
, dynamic
= create_nmf
.ParseElfHeader(static_nexe
)
73 self
.assertEqual(arch
, 'x86-64')
74 self
.assertFalse(dynamic
)
77 class TestNmfUtils(unittest
.TestCase
):
78 """Tests for the main NmfUtils class in create_nmf."""
82 chrome_src
= os
.path
.dirname(os
.path
.dirname(os
.path
.dirname(PARENT_DIR
)))
83 toolchain
= os
.path
.join(chrome_src
, 'native_client', 'toolchain')
84 self
.toolchain
= os
.path
.join(toolchain
, '%s_x86' % getos
.GetPlatform())
85 self
.objdump
= os
.path
.join(self
.toolchain
, 'bin', 'i686-nacl-objdump')
87 self
.objdump
+= '.exe'
88 self
.dyn_nexe
= os
.path
.join(DATA_DIR
, 'test_dynamic_x86_32.nexe')
89 self
.dyn_deps
= set(['libc.so.51fe1ff9', 'runnable-ld.so',
90 'libgcc_s.so.1', 'libpthread.so.51fe1ff9'])
94 shutil
.rmtree(self
.tempdir
)
97 self
.tempdir
= tempfile
.mkdtemp()
99 def CreateNmfUtils(self
):
100 libdir
= os
.path
.join(self
.toolchain
, 'x86_64-nacl', 'lib32')
101 return create_nmf
.NmfUtils([self
.dyn_nexe
],
103 objdump
=self
.objdump
)
105 def testGetNeededStatic(self
):
106 nexe
= os
.path
.join(DATA_DIR
, 'test_static_x86_32.nexe')
107 nmf
= create_nmf
.NmfUtils([nexe
])
108 needed
= nmf
.GetNeeded()
110 # static nexe should have exactly one needed file
111 self
.assertEqual(len(needed
), 1)
112 self
.assertEqual(needed
.keys()[0], nexe
)
114 # arch of needed file should be x86-32
115 archfile
= needed
.values()[0]
116 self
.assertEqual(archfile
.arch
, 'x86-32')
118 def testGetNeededDynamic(self
):
119 nmf
= self
.CreateNmfUtils()
120 needed
= nmf
.GetNeeded()
121 names
= needed
.keys()
123 # this nexe has 5 dependancies
124 expected
= set(self
.dyn_deps
)
125 expected
.add(os
.path
.basename(self
.dyn_nexe
))
127 basenames
= set(os
.path
.basename(n
) for n
in names
)
128 self
.assertEqual(expected
, basenames
)
130 def testStageDependencies(self
):
132 nmf
= self
.CreateNmfUtils()
135 nmf
.StageDependencies(self
.tempdir
)
137 # Verify directory contents
138 contents
= set(os
.listdir(self
.tempdir
))
139 expectedContents
= set((os
.path
.basename(self
.dyn_nexe
), 'lib32'))
140 self
.assertEqual(contents
, expectedContents
)
142 contents
= set(os
.listdir(os
.path
.join(self
.tempdir
, 'lib32')))
143 expectedContents
= self
.dyn_deps
144 self
.assertEqual(contents
, expectedContents
)
147 if __name__
== '__main__':