diff paraspace/tests/dex_deptracker_test.py @ 124:8e42b2816893

Fixing compute_size() and sizeof() for DEX types. - Prevent compute_size() and sizeof() of depend_* to include size of depend-on.
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 07 Aug 2011 22:07:08 +0800
parents 78357afb4a9d
children ff6f869273b7
line wrap: on
line diff
--- a/paraspace/tests/dex_deptracker_test.py	Sat Aug 06 19:09:15 2011 +0800
+++ b/paraspace/tests/dex_deptracker_test.py	Sun Aug 07 22:07:08 2011 +0800
@@ -1,10 +1,65 @@
-from paraspace.dexfile import DEXFile, DEXFile_linked
+from paraspace import dexfile
 from paraspace.dex_deptracker import prepare_dep_decls
 from paraspace.dex_deptracker import restore_dependencies
 import os
 
+
+def _install_dexfile_4_deptracker():
+    global dexfile
+    import imp, sys
+    from paraspace import dex_deptracker
+    from paraspace import injection
+    
+    try:
+        new_dexfile = imp.load_compiled('paraspace.dexfile', dexfile.__file__)
+    except ImportError:
+        new_dexfile = imp.load_source('paraspace.dexfile', dexfile.__file__)
+        pass
+    dex_deptracker.dexfile = new_dexfile
+    dexfile = new_dexfile
+    dex_deptracker._nest_types = (dexfile.array, dexfile.cond, dexfile.switch)
+
+    injection.dex_type_2_array_attr_map = \
+        injection._saved_dex_type_2_array_attr_map
+    
+    sys.modules['paraspace.dexfile'] = new_dexfile
+    pass
+
+
+def update_offset_test():
+    from paraspace.dex_deptracker import collect_all_dep_decls
+    from paraspace.dex_deptracker import build_dependencies
+    from paraspace.dex_deptracker import _install_markers
+    from paraspace.dex_deptracker import _patch_dex_type_markers
+    from paraspace.dex_deptracker import update_offset
+
+    _install_dexfile_4_deptracker()
+
+    all_dep_decls = collect_all_dep_decls()
+    _install_markers(all_dep_decls)
+    _patch_dex_type_markers(all_dep_decls)
+
+    srcdir = os.path.dirname(__file__)
+    srcroot = os.path.join(srcdir, '..', '..')
+    testdatapath = os.path.join(srcroot, 'data', 'testdata1.dex')
+    dex = dexfile.DEXFile.open(testdatapath)
+
+    build_dependencies(dex, all_dep_decls)
+
+    offset0 = dex.typeLists.items[0].value.data_offset
+    dex.typeLists.items[0].value.data_offset = 0
+    offset1 = dex.typeLists.items[1].value.data_offset
+    dex.typeLists.items[1].value.data_offset = 0
+    
+    update_offset(dex, all_dep_decls)
+
+    assert dex.typeLists.items[0].value.data_offset == offset0
+    assert dex.typeLists.items[1].value.data_offset == offset1
+    pass
+
+
 def restore_dependencies_test():
-    from paraspace.dexfile import _DEX_StringId
+    _install_dexfile_4_deptracker()
     
     srcdir = os.path.dirname(__file__)
     srcroot = os.path.join(srcdir, '..', '..')
@@ -13,34 +68,114 @@
     
     decls = prepare_dep_decls()
     
-    hello_dex = DEXFile.open(helloworld_path)
+    hello_dex = dexfile.DEXFile.open(helloworld_path)
     strids_size_saved = hello_dex.header.stringIdsSize
     typeids_off_saved = hello_dex.header.typeIdsOff
     data_off_saved = hello_dex.header.dataOff
     map_off_saved = hello_dex.header.mapOff
     file_sz_saved = hello_dex.header.fileSize
-    for map_item in hello_dex.maps.items.items:
-        print '%d %x' % (map_item.type, map_item.offset)
-        pass
 
-    hello_linked = DEXFile_linked.build_dependencies(hello_dex, decls)
+    hello_linked = dexfile.DEXFile_linked.build_dependencies(hello_dex, decls)
     
     first_strid = hello_linked.stringIds.items[0]
-    strid = _DEX_StringId()
+    strid = dexfile._DEX_StringId()
     strid.stringDataOff = first_strid.stringDataOff
     hello_linked.stringIds.items.append(strid)
 
     restore_dependencies(hello_linked, decls)
 
-    print file_sz_saved, hello_linked.header.fileSize
-    print map_off_saved, hello_linked.header.mapOff
-    for map_item in hello_linked.maps.items.items:
-        print '%d %x' % (map_item.type, map_item.offset)
-        pass
-
     assert hello_linked.header.fileSize == (file_sz_saved + 4)
     assert hello_linked.header.stringIdsSize == (strids_size_saved + 1)
     assert hello_linked.header.typeIdsOff == (typeids_off_saved + 4)
     assert hello_linked.header.dataOff == (data_off_saved + 4)
     assert hello_linked.header.mapOff == (map_off_saved + 4)
     pass
+
+
+def sizoef_after_build_dep_test():
+    from paraspace.dex_deptracker import update_offset
+    
+    _install_dexfile_4_deptracker()
+
+    srcdir = os.path.dirname(__file__)
+    srcroot = os.path.join(srcdir, '..', '..')
+    datadir = os.path.join(srcroot, 'data')
+    helloworld_path = os.path.join(datadir, 'helloworld.dex')
+    
+    decls = prepare_dep_decls()
+    
+    hello_dex = dexfile.DEXFile.open(helloworld_path)
+    update_offset(hello_dex, decls)
+    
+    get_sz = lambda x: x.sizeof(x)
+    assert get_sz(hello_dex.header) == 112
+    assert get_sz(hello_dex.stringIds) == 0x168
+    assert get_sz(hello_dex.typeIds) == (0x250-0x1d8)
+    assert get_sz(hello_dex.protoIds) == (0x31c-0x250)
+    assert get_sz(hello_dex.fieldIds) == (0x354-0x31c)
+    assert get_sz(hello_dex.methodIds) == (0x46c-0x354)
+    assert get_sz(hello_dex.classDefs) == (0x58c-0x46c)
+    assert get_sz(hello_dex.annotationSetItems) == (0x5dc-0x58c)
+    assert get_sz(hello_dex.codeItems) == (0x8b8-0x5dc)
+    assert get_sz(hello_dex.annotationsDirectoryItems) == (0x928-0x8b8)
+    assert get_sz(hello_dex.typeLists) == (0x98a-0x928)
+    assert get_sz(hello_dex.stringDataItems) == (0xe7d-0x98a)
+    assert get_sz(hello_dex.debugInfoItems) == (0xf31-0xe7d)
+    assert get_sz(hello_dex.annotationItems) == (0xf82-0xf31)
+    assert get_sz(hello_dex.encodedArrayItems) == (0xf9d-0xf82)
+    assert get_sz(hello_dex.classDatas) == (0x1024-0xf9d-1)
+    assert hello_dex.maps.sizeof(hello_dex.maps) == 0xd0
+
+    hello_dex.compute_size()
+    unlinked_sz = hello_dex.sizeof(hello_dex)
+    assert unlinked_sz == hello_dex.header.fileSize
+
+    hello_linked = dexfile.DEXFile_linked.build_dependencies(hello_dex, decls)
+
+    hello_linked.compute_size()
+    linked_sz = hello_linked.sizeof(hello_linked)
+    
+    assert get_sz(hello_linked.header) == 112
+    assert get_sz(hello_linked.stringIds) == 0x168
+    assert get_sz(hello_linked.typeIds) == (0x250-0x1d8)
+    assert get_sz(hello_linked.protoIds) == (0x31c-0x250)
+    assert get_sz(hello_linked.fieldIds) == (0x354-0x31c)
+    assert get_sz(hello_linked.methodIds) == (0x46c-0x354)
+    assert get_sz(hello_linked.classDefs) == (0x58c-0x46c)
+    assert get_sz(hello_linked.annotationSetItems) == (0x5dc-0x58c)
+    assert get_sz(hello_linked.codeItems) == (0x8b8-0x5dc)
+    assert get_sz(hello_linked.annotationsDirectoryItems) == (0x928-0x8b8)
+    assert get_sz(hello_linked.typeLists) == (0x98a-0x928)
+    assert get_sz(hello_linked.stringDataItems) == (0xe7d-0x98a)
+    assert get_sz(hello_linked.debugInfoItems) == (0xf31-0xe7d)
+    assert get_sz(hello_linked.annotationItems) == (0xf82-0xf31)
+    assert get_sz(hello_linked.encodedArrayItems) == (0xf9d-0xf82)
+
+    assert linked_sz == unlinked_sz
+    
+    first_strid = hello_linked.stringIds.items[0]
+    strid = dexfile._DEX_StringId()
+    strid.stringDataOff = first_strid.stringDataOff
+    hello_linked.stringIds.items.append(strid)
+    
+    hello_linked.compute_size()
+    linked_sz = hello_linked.sizeof(hello_linked)
+    
+    assert get_sz(hello_linked.header) == 112
+    assert get_sz(hello_linked.stringIds) == 0x16c
+    assert get_sz(hello_linked.typeIds) == (0x250-0x1d8)
+    assert get_sz(hello_linked.protoIds) == (0x31c-0x250)
+    assert get_sz(hello_linked.fieldIds) == (0x354-0x31c)
+    assert get_sz(hello_linked.methodIds) == (0x46c-0x354)
+    assert get_sz(hello_linked.classDefs) == (0x58c-0x46c)
+    assert get_sz(hello_linked.annotationSetItems) == (0x5dc-0x58c)
+    assert get_sz(hello_linked.codeItems) == (0x8b8-0x5dc)
+    assert get_sz(hello_linked.annotationsDirectoryItems) == (0x928-0x8b8)
+    assert get_sz(hello_linked.typeLists) == (0x98a-0x928)
+    assert get_sz(hello_linked.stringDataItems) == (0xe7d-0x98a)
+    assert get_sz(hello_linked.debugInfoItems) == (0xf31-0xe7d)
+    assert get_sz(hello_linked.annotationItems) == (0xf82-0xf31)
+    assert get_sz(hello_linked.encodedArrayItems) == (0xf9d-0xf82)
+
+    assert linked_sz == (unlinked_sz + 4)
+    pass