Compare commits

...

2 Commits

Author SHA1 Message Date
coah
df26ca27d6 Test absent and unequal string pools 2026-10-01 16:08:32 -05:00
coah
b29ea61a90 Treat absent .data sections as empty string pools 2026-10-01 16:08:27 -05:00
2 changed files with 58 additions and 2 deletions

View File

@@ -7,8 +7,10 @@ BASE = 'build/43U/obj/src/utility/iplESMisc.o'
def pool(path):
e = ELFFile(open(path, 'rb'))
sec = {s.name: s for s in e.iter_sections()}
d = sec['.data'].data()
section = e.get_section_by_name('.data')
if section is None:
return []
d = section.data()
out = []
off = 0
while off < len(d):
@@ -42,6 +44,7 @@ def main():
for j in range(n, max(len(mine), len(base))):
src = mine if j < len(mine) else base
print(' extra %3d %r' % (j, src[j][1][:70]))
return 1
return 0

View File

@@ -0,0 +1,53 @@
import contextlib
import io
import unittest
from unittest.mock import Mock, mock_open, patch
import pool_diff
class PoolDiffTests(unittest.TestCase):
def read_pool(self, data):
elf = Mock()
section = None if data is None else Mock()
if section is not None:
section.data.return_value = data
elf.get_section_by_name.return_value = section
with patch('builtins.open', mock_open(read_data=b'')), patch.object(pool_diff, 'ELFFile', return_value=elf):
result = pool_diff.pool('test.o')
elf.get_section_by_name.assert_called_once_with('.data')
return result
def compare(self, left, right):
with patch.object(pool_diff.sys, 'argv', ['pool_diff.py', 'left.o', 'right.o']), \
patch.object(pool_diff, 'pool', side_effect=[left, right]), \
contextlib.redirect_stdout(io.StringIO()):
return pool_diff.main()
def test_missing_data_has_no_strings(self):
self.assertEqual(self.read_pool(None), [])
def test_empty_data_has_no_strings(self):
self.assertEqual(self.read_pool(b''), [])
def test_existing_string_extraction(self):
self.assertEqual(self.read_pool(b'abc\x00hello\x00world\x00'), [(4, 'hello'), (10, 'world')])
def test_two_missing_pools_match(self):
self.assertEqual(self.compare([], []), 0)
def test_identical_pools_match(self):
self.assertEqual(self.compare([(0, 'hello')], [(0, 'hello')]), 0)
def test_changed_string_fails(self):
self.assertEqual(self.compare([(0, 'hello')], [(0, 'world')]), 1)
def test_missing_left_pool_does_not_hide_strings(self):
self.assertEqual(self.compare([], [(0, 'hello')]), 1)
def test_missing_right_pool_does_not_hide_strings(self):
self.assertEqual(self.compare([(0, 'hello')], []), 1)
if __name__ == '__main__':
unittest.main()