readelf: print addend for RELA relocations without symbol (#292)
[pyelftools.git] / test / test_utils.py
index 54a09bb4f2ab4c57a1b479b5e93e0a2e7605c8fa..23669e73091254774df74e513d6caed4a578e3d2 100644 (file)
@@ -1,56 +1,68 @@
-import sys, unittest
-from cStringIO import StringIO
+#-------------------------------------------------------------------------------
+# elftools tests
+#
+# Eli Bendersky (eliben@gmail.com)
+# This code is in the public domain
+#-------------------------------------------------------------------------------
+import unittest
 from random import randint
 
-sys.path.extend(['.', '..'])
-from elftools.common.utils import (parse_cstring_from_stream,
+from elftools.common.py3compat import int2byte, BytesIO
+from elftools.common.utils import (parse_cstring_from_stream, merge_dicts,
         preserve_stream_pos)
 
 
 class Test_parse_cstring_from_stream(unittest.TestCase):
-    def _make_random_string(self, n):
-        return ''.join(chr(randint(32, 127)) for i in range(n))
-        
+    def _make_random_bytes(self, n):
+        return b''.join(int2byte(randint(32, 127)) for i in range(n))
+
     def test_small1(self):
-        sio = StringIO('abcdefgh\x0012345')
-        self.assertEqual(parse_cstring_from_stream(sio), 'abcdefgh')
-        self.assertEqual(parse_cstring_from_stream(sio, 2), 'cdefgh')
-        self.assertEqual(parse_cstring_from_stream(sio, 8), '')
+        sio = BytesIO(b'abcdefgh\x0012345')
+        self.assertEqual(parse_cstring_from_stream(sio), b'abcdefgh')
+        self.assertEqual(parse_cstring_from_stream(sio, 2), b'cdefgh')
+        self.assertEqual(parse_cstring_from_stream(sio, 8), b'')
 
     def test_small2(self):
-        sio = StringIO('12345\x006789\x00abcdefg\x00iii')
-        self.assertEqual(parse_cstring_from_stream(sio), '12345')
-        self.assertEqual(parse_cstring_from_stream(sio, 5), '')
-        self.assertEqual(parse_cstring_from_stream(sio, 6), '6789')
+        sio = BytesIO(b'12345\x006789\x00abcdefg\x00iii')
+        self.assertEqual(parse_cstring_from_stream(sio), b'12345')
+        self.assertEqual(parse_cstring_from_stream(sio, 5), b'')
+        self.assertEqual(parse_cstring_from_stream(sio, 6), b'6789')
 
     def test_large1(self):
-        text = 'i' * 400 + '\x00' + 'bb'
-        sio = StringIO(text)
-        self.assertEqual(parse_cstring_from_stream(sio), 'i' * 400)
-        self.assertEqual(parse_cstring_from_stream(sio, 150), 'i' * 250)
+        text = b'i' * 400 + b'\x00' + b'bb'
+        sio = BytesIO(text)
+        self.assertEqual(parse_cstring_from_stream(sio), b'i' * 400)
+        self.assertEqual(parse_cstring_from_stream(sio, 150), b'i' * 250)
 
     def test_large2(self):
-        text = self._make_random_string(5000) + '\x00' + 'jujajaja'
-        sio = StringIO(text)
+        text = self._make_random_bytes(5000) + b'\x00' + b'jujajaja'
+        sio = BytesIO(text)
         self.assertEqual(parse_cstring_from_stream(sio), text[:5000])
         self.assertEqual(parse_cstring_from_stream(sio, 2348), text[2348:5000])
 
 
-class Test_preserve_stream_pos(object):
+class Test_preserve_stream_pos(unittest.TestCase):
     def test_basic(self):
-        sio = StringIO('abcdef')
+        sio = BytesIO(b'abcdef')
         with preserve_stream_pos(sio):
             sio.seek(4)
-        self.assertEqual(stream.tell(), 0)
+        self.assertEqual(sio.tell(), 0)
 
         sio.seek(5)
         with preserve_stream_pos(sio):
             sio.seek(0)
-        self.assertEqual(stream.tell(), 5)
+        self.assertEqual(sio.tell(), 5)
 
 
-if __name__ == '__main__':
-    unittest.main()
+class Test_merge_dicts(unittest.TestCase):
+    def test_basic(self):
+        md = merge_dicts({10: 20, 20: 30}, {30: 40, 50: 60})
+        self.assertEqual(md, {10: 20, 20: 30, 30: 40, 50: 60})
 
+    def test_keys_resolve(self):
+        md = merge_dicts({10: 20, 20: 30}, {20: 40, 50: 60})
+        self.assertEqual(md, {10: 20, 20: 40, 50: 60})
 
 
+if __name__ == '__main__':
+    unittest.main()