annotate paraspace/tests/injection_test.py @ 98:c0c127c7b37e

Check and fix issues of map sizes
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 25 Jul 2011 20:37:32 +0800
parents 00cd331f8aa8
children 3898711adb2c
rev   line source
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 from paraspace import dexfile
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
2 import os
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4 def _install_dexfile_4_deptracker():
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
5 global dexfile
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
6 import imp, sys
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
7 from paraspace import dex_deptracker
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
8
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
9 try:
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
10 new_dexfile = imp.load_compiled('dexfile', dexfile.__file__)
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11 except ImportError:
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
12 new_dexfile = imp.load_source('dexfile', dexfile.__file__)
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13 pass
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14 dex_deptracker.dexfile = new_dexfile
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15 dexfile = new_dexfile
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16 dex_deptracker._nest_types = (dexfile.array, dexfile.cond, dexfile.switch)
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
17
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
18 sys.modules['paraspace.dexfile'] = new_dexfile
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 pass
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
22 def _find_map(dex, map_type):
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
23 for map in dex.maps.items.items:
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
24 if map.type == map_type:
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
25 return map
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
26 pass
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
27 pass
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
28
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
29
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
30 def inject_fakefile_to_helloworld_test():
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
31 from paraspace.dex_deptracker import prepare_dep_decls
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
32 from paraspace.injection import dexfile_insert_class
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
33
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 _install_dexfile_4_deptracker()
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
36 all_dep_decls = prepare_dep_decls()
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 srcdir = os.path.dirname(__file__)
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39 srcroot = os.path.join(srcdir, '..', '..')
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41 helloworld_fn = os.path.join(srcroot, 'data', 'helloworld.dex')
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
42 helloworld_dex = dexfile.DEXFile.open(helloworld_fn)
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
43
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
44 classdef_map = _find_map(helloworld_dex, 0x0006)
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
45 saved_classdef_map_sz = classdef_map.size
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
46
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
47 helloworld_linked = \
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
48 dexfile.DEXFile_linked.build_dependencies(helloworld_dex,
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
49 all_dep_decls)
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
50
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
51 fakefile_fn = os.path.join(srcroot, 'data', 'fakefile.dex')
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
52 fakefile_dex = dexfile.DEXFile.open(fakefile_fn)
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
53 fakefile_linked = \
95
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
54 dexfile.DEXFile_linked. \
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
55 build_dependencies(fakefile_dex, all_dep_decls)
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
56
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
57 fakefile_def = fakefile_linked. \
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
58 find_class_name('Lcom/codemud/fakefile/fakefile;')
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
59
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
60 clone = dexfile_insert_class(helloworld_linked, fakefile_def)
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
61 assert clone
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
62 assert clone != fakefile_def
97
00cd331f8aa8 Get unlinked one from a linked one
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
63
00cd331f8aa8 Get unlinked one from a linked one
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
64 helloworld_unlinked = helloworld_linked.get_unlinked()
00cd331f8aa8 Get unlinked one from a linked one
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
65 assert helloworld_unlinked
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
66
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
67 # map size for classdef must be increased by 1
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
68 classdef_map = _find_map(helloworld_unlinked, 0x0006)
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
69 assert classdef_map.size == saved_classdef_map_sz + 1
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
70
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
71 classdata_map = _find_map(helloworld_unlinked, 0x2000)
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
72 assert classdata_map.size == classdef_map.size
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73 pass
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74