Commit 960cb0ed1e2c5a4f816c630416bf2a4730ac7409
- Diff rendering mode:
- inline
- side by side
tests/tc_exceptions.rb
(24 / 15)
|   | |||
| 28 | 28 | class TC_Exceptions < Test::Unit::TestCase | |
| 29 | 29 | TestDir = Pathname.new(Elf::BaseTest::TestDir + "invalid/") | |
| 30 | 30 | ||
| 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 | ||
| 35 | 36 | file = File.new(TestDir + "nonelf") | |
| 36 | fileno_before = file.fileno | ||
| 37 | @fileno_before = file.fileno | ||
| 37 | 38 | file.close | |
| 39 | end | ||
| 38 | 40 | ||
| 39 | assert_raise exception_class do | ||
| 40 | Elf::File.new(TestDir + subpath) | ||
| 41 | end | ||
| 42 | |||
| 41 | def teardown | ||
| 43 | 42 | file = File.new(TestDir + "nonelf") | |
| 44 | fileno_after = file.fileno | ||
| 43 | @fileno_after = file.fileno | ||
| 45 | 44 | file.close | |
| 46 | 45 | ||
| 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!") | ||
| 49 | 48 | end | |
| 50 | 49 | ||
| 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 | |||
| 51 | 57 | # Test behaviour when a file is requested that is not present. | |
| 52 | 58 | # | |
| 53 | 59 | # Expected behaviour: Errno::ENOENT exception is raised | |
| … | … | ||
| 176 | 176 | begin | |
| 177 | 177 | elf = Elf::File.new(TestDir + "unknown_section_type") | |
| 178 | 178 | elf[11] # We need an explicit request for the corrupted section | |
| 179 | elf.close | ||
| 180 | 179 | rescue Elf::Section::UnknownType => e | |
| 181 | 180 | assert_equal(0x0000ff02, e.type_id, | |
| 182 | 181 | "Wrong type_id reported for unknown section type") | |
| … | … | ||
| 187 | 187 | assert_equal(1, e.section_name, | |
| 188 | 188 | "Wrong section_name reported for unknown section type") | |
| 189 | 189 | return | |
| 190 | ensure | ||
| 191 | elf.close | ||
| 190 | 192 | end | |
| 191 | 193 | ||
| 192 | 194 | flunk("Elf::Section::UnknownType exception not received.") | |
| … | … | ||
| 200 | 200 | # Expected behaviour: Elf::File::MissingStringTable exception is | |
| 201 | 201 | # raised | |
| 202 | 202 | def test_missing_string_table_request | |
| 203 | elf = Elf::File.new(TestDir + "unknown_section_type") | ||
| 204 | |||
| 203 | 205 | assert_raise Elf::File::MissingStringTable do | |
| 204 | elf = Elf::File.new(TestDir + "unknown_section_type") | ||
| 205 | 206 | elf[".symtab"] | |
| 206 | elf.close | ||
| 207 | 207 | end | |
| 208 | |||
| 209 | elf.close | ||
| 208 | 210 | end | |
| 209 | 211 | ||
| 210 | 212 | # Test behaviour when a file lacks a string table and a section is |

