Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
J
jadx
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
jadx
Commits
db527fbb
Commit
db527fbb
authored
Sep 13, 2014
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: add api for write tests using smali
parent
8f201f1f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
13 deletions
+86
-13
build.gradle
jadx-core/build.gradle
+4
-1
InternalJadxTest.java
jadx-core/src/test/java/jadx/api/InternalJadxTest.java
+31
-12
SmaliTest.java
jadx-core/src/test/java/jadx/api/SmaliTest.java
+51
-0
No files found.
jadx-core/build.gradle
View file @
db527fbb
ext
.
jadxClasspath
=
'clsp-data/android-4.3.jar'
dependencies
{
runtime
files
(
jadxClasspath
)
compile
files
(
'lib/dx-1.8.jar'
)
compile
'org.ow2.asm:asm:5.0.3'
runtime
files
(
jadxClasspath
)
testCompile
'org.smali:smali:2.0.3'
}
task
packTests
(
type:
Jar
)
{
...
...
jadx-core/src/test/java/jadx/api/InternalJadxTest.java
View file @
db527fbb
...
...
@@ -7,6 +7,7 @@ import jadx.core.dex.nodes.ClassNode;
import
jadx.core.dex.nodes.MethodNode
;
import
jadx.core.dex.visitors.DepthTraversal
;
import
jadx.core.dex.visitors.IDexTreeVisitor
;
import
jadx.core.utils.exceptions.JadxException
;
import
jadx.core.utils.files.FileUtils
;
import
java.io.File
;
...
...
@@ -30,21 +31,30 @@ public abstract class InternalJadxTest extends TestUtils {
protected
boolean
outputCFG
=
false
;
protected
boolean
isFallback
=
false
;
protected
boolean
deleteTmp
Jar
=
true
;
protected
boolean
deleteTmp
Files
=
true
;
protected
String
outDir
=
"test-out-tmp"
;
public
ClassNode
getClassNode
(
Class
<?>
clazz
)
{
JadxDecompiler
d
=
new
JadxDecompiler
();
try
{
d
.
loadFile
(
getJarForClass
(
clazz
));
File
jar
=
getJarForClass
(
clazz
);
return
getClassNodeFromFile
(
jar
,
clazz
.
getName
());
}
catch
(
Exception
e
)
{
fail
(
e
.
getMessage
());
}
String
clsName
=
clazz
.
getName
();
return
null
;
}
public
ClassNode
getClassNodeFromFile
(
File
file
,
String
clsName
)
{
JadxDecompiler
d
=
new
JadxDecompiler
();
try
{
d
.
loadFile
(
file
);
}
catch
(
JadxException
e
)
{
fail
(
e
.
getMessage
());
}
ClassNode
cls
=
d
.
getRoot
().
searchClassByName
(
clsName
);
assertNotNull
(
"Class not found: "
+
clsName
,
cls
);
assertEquals
(
cls
.
getFullName
(),
cl
azz
.
getName
()
);
assertEquals
(
cls
.
getFullName
(),
cl
sName
);
cls
.
load
();
for
(
IDexTreeVisitor
visitor
:
getPasses
())
{
...
...
@@ -99,16 +109,26 @@ public abstract class InternalJadxTest extends TestUtils {
String
path
=
cls
.
getPackage
().
getName
().
replace
(
'.'
,
'/'
);
List
<
File
>
list
=
getClassFilesWithInners
(
cls
);
File
temp
=
File
.
createTempFile
(
"jadx-tmp-"
,
System
.
nanoTime
()
+
".jar"
);
File
temp
=
createTempFile
(
".jar"
);
JarOutputStream
jo
=
new
JarOutputStream
(
new
FileOutputStream
(
temp
));
for
(
File
file
:
list
)
{
FileUtils
.
addFileToJar
(
jo
,
file
,
path
+
"/"
+
file
.
getName
());
}
jo
.
close
();
if
(
deleteTmpJar
)
{
temp
.
deleteOnExit
();
}
else
{
System
.
out
.
println
(
"Temporary jar file path: "
+
temp
.
getAbsolutePath
());
return
temp
;
}
protected
File
createTempFile
(
String
suffix
)
{
File
temp
=
null
;
try
{
temp
=
File
.
createTempFile
(
"jadx-tmp-"
,
System
.
nanoTime
()
+
suffix
);
if
(
deleteTmpFiles
)
{
temp
.
deleteOnExit
();
}
else
{
System
.
out
.
println
(
"Temporary file path: "
+
temp
.
getAbsolutePath
());
}
}
catch
(
IOException
e
)
{
fail
(
e
.
getMessage
());
}
return
temp
;
}
...
...
@@ -135,7 +155,6 @@ public abstract class InternalJadxTest extends TestUtils {
return
list
;
}
// Use only for debug purpose
@Deprecated
protected
void
setOutputCFG
()
{
...
...
@@ -151,6 +170,6 @@ public abstract class InternalJadxTest extends TestUtils {
// Use only for debug purpose
@Deprecated
protected
void
notDeleteTmpJar
()
{
this
.
deleteTmp
Jar
=
false
;
this
.
deleteTmp
Files
=
false
;
}
}
jadx-core/src/test/java/jadx/api/SmaliTest.java
0 → 100644
View file @
db527fbb
package
jadx
.
api
;
import
jadx.core.Consts
;
import
jadx.core.dex.nodes.ClassNode
;
import
java.io.File
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.jf.smali.main
;
import
static
org
.
junit
.
Assert
.
fail
;
public
class
SmaliTest
extends
InternalJadxTest
{
private
static
final
String
SMALI_TESTS_PROJECT
=
"jadx-core"
;
private
static
final
String
SMALI_TESTS_DIR
=
"src/test/smali"
;
private
static
final
String
SMALI_TESTS_EXT
=
".smali"
;
protected
ClassNode
getClassNodeFromSmali
(
String
clsName
)
{
File
smaliFile
=
getSmaliFile
(
clsName
);
File
outDex
=
createTempFile
(
".dex"
);
compileSmali
(
smaliFile
,
outDex
);
String
fullClsName
=
Consts
.
DEFAULT_PACKAGE_NAME
+
"."
+
clsName
;
return
getClassNodeFromFile
(
outDex
,
fullClsName
);
}
private
File
getSmaliFile
(
String
clsName
)
{
File
smaliFile
=
new
File
(
SMALI_TESTS_DIR
,
clsName
+
SMALI_TESTS_EXT
);
if
(
smaliFile
.
exists
())
{
return
smaliFile
;
}
smaliFile
=
new
File
(
SMALI_TESTS_PROJECT
,
smaliFile
.
getPath
());
if
(
smaliFile
.
exists
())
{
return
smaliFile
;
}
fail
(
"Smali file not found: "
+
SMALI_TESTS_DIR
+
"/"
+
clsName
+
SMALI_TESTS_EXT
);
return
null
;
}
public
boolean
compileSmali
(
File
input
,
File
output
)
{
List
<
String
>
args
=
new
ArrayList
<
String
>();
args
.
add
(
input
.
getAbsolutePath
());
args
.
add
(
"-o"
);
args
.
add
(
output
.
getAbsolutePath
());
main
.
main
(
args
.
toArray
(
new
String
[
args
.
size
()]));
return
true
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment