Commit 960cb0ed1e2c5a4f816c630416bf2a4730ac7409

Improve the exceptions tests so that we check for leaked descriptors in all
cases.

The only two cases that happened to hit, were bugs of the tests themselves
which are now fixed.
  
2828class TC_Exceptions < Test::Unit::TestCase
2929 TestDir = Pathname.new(Elf::BaseTest::TestDir + "invalid/")
3030
31 # Helper to check for exceptions on opening a file. It not only
32 # ensures that the correct exception class is given, but also that
33 # the opening action didn't leak any file descriptor!
34 def helper_open_exception(exception_class, subpath)
31 # Define setup and teardown functions to make sure that no
32 # descriptors are leaked during the tests. We don't want descriptors
33 # to leak when exception happens, otherwise we likely have a bug in
34 # the code.
35 def setup
3536 file = File.new(TestDir + "nonelf")
36 fileno_before = file.fileno
37 @fileno_before = file.fileno
3738 file.close
39 end
3840
39 assert_raise exception_class do
40 Elf::File.new(TestDir + subpath)
41 end
42
41 def teardown
4342 file = File.new(TestDir + "nonelf")
44 fileno_after = file.fileno
43 @fileno_after = file.fileno
4544 file.close
4645
47 assert_equal(fileno_before, fileno_after,
48 "Descriptor leaked by the Elf::File.new call")
46 assert_equal(@fileno_before, @fileno_after,
47 "Descriptor leaked!")
4948 end
5049
50 # Helper to check for exceptions on opening a file.
51 def helper_open_exception(exception_class, subpath)
52 assert_raise exception_class do
53 Elf::File.new(TestDir + subpath)
54 end
55 end
56
5157 # Test behaviour when a file is requested that is not present.
5258 #
5359 # Expected behaviour: Errno::ENOENT exception is raised
176176 begin
177177 elf = Elf::File.new(TestDir + "unknown_section_type")
178178 elf[11] # We need an explicit request for the corrupted section
179 elf.close
180179 rescue Elf::Section::UnknownType => e
181180 assert_equal(0x0000ff02, e.type_id,
182181 "Wrong type_id reported for unknown section type")
187187 assert_equal(1, e.section_name,
188188 "Wrong section_name reported for unknown section type")
189189 return
190 ensure
191 elf.close
190192 end
191193
192194 flunk("Elf::Section::UnknownType exception not received.")
200200 # Expected behaviour: Elf::File::MissingStringTable exception is
201201 # raised
202202 def test_missing_string_table_request
203 elf = Elf::File.new(TestDir + "unknown_section_type")
204
203205 assert_raise Elf::File::MissingStringTable do
204 elf = Elf::File.new(TestDir + "unknown_section_type")
205206 elf[".symtab"]
206 elf.close
207207 end
208
209 elf.close
208210 end
209211
210212 # Test behaviour when a file lacks a string table and a section is