Skip to content

add some more tests#14

Merged
oleavr merged 33 commits intofrida:masterfrom
giantpune:fix/add_interface_test
Feb 7, 2017
Merged

add some more tests#14
oleavr merged 33 commits intofrida:masterfrom
giantpune:fix/add_interface_test

Conversation

@giantpune
Copy link
Contributor

No description provided.

Copy link
Member

@oleavr oleavr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💣

" var tm = X509TrustManager.$new();" +
"}catch(e){" +
" var MethodTest = Java.use('re.frida.MethodTest');" +
" MethodTest.Fail('couldnt create trustmanager: ' + e);" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use script.getNextMessage() instead (like above), that way we send('some string'); and assert that script.getNextMessage() gives us the same string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

@Test
public void TestNewInterface() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should follow the camelCase() style here and below, and also avoid test as a prefix since it's already implicit from the class. I tried to follow the examples in the JUnit docs for naming, so in this case perhaps something like interfaceCannotBeInstantiated() or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

loadScript("var X509TrustManager = Java.use('javax.net.ssl.X509TrustManager');" +
"try{" +
" var tm = X509TrustManager.$new();" +
"}catch(e){" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the JS we should stick to semistandard, the style we're using in the implementation code. This is almost there, just missing spaces on each side of the curly braces and after catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

loadScript("var c = Java.use('java.lang.Class');" +
"try{" +
" var orig = c.forName.overload('java.lang.String');" +
" c.forName.overload('java.lang.String').implementation = function(s){ orig(s); };" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this test-suite we should avoid hooking things in the framework and only do that as a last resort. It's fine to start there though and refine the test so it's reproducing the same thing. The method in question here seems to be this one:

static Class<?> forName(String className)

And that made me think "uh oh, generics, we have some TODOs in that area", so maybe we can reproduce the same issue by using a locally defined method with the same kind of return type? I'd just throw it into the Badger class at the bottom.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried adding a static Class<?> function to Badger and hooking it. Frida does not crash in the same way as when hooking java.lang.Class. I also tried hooking some different built-ins with similar returns like dalvik.system.VMStack.getStackClass2 and even Made a function in Badger that calls through directly to java.lang.Class.forName() and hooked that. So far, I'm unable to reproduce the same crash without directly hooking java.lang.Class. Maybe we can comment it out for now so it doesn't block the rest of the PR?

Copy link
Member

@oleavr oleavr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if things are looking better on the latest master – released in Frida 9.1.4 this evening – hopefully there will be less weirdness now 🤞

"} catch (e) {" +
" send('couldnt create trustmanager');" +
"}" +
"send('ok');"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be right after var tm = ..., and the assert below should check against the error-case? (As the test is checking that it is invalid to instantiate an interface.) We should however have another test that illustrates how to register a new class implementing an interface in pure JS – or perhaps this test is meant to evolve into that?

}

@Test
public void TestClassForNameOrig() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Malformed camel. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this test should be named like the other tests, without a test prefix, and communicating the expected outcome.

}

@Test
public void TestClassForName() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same two remarks as above.

// this one was just hanging indefinitely during the test, but in an actual app, it was crashing
//! either one of those is bad.
@Test
public void TestMethodInvoke() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same two remarks as above.

}*/

/*@Test
public void TestNativeLibraryLoading() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same two remarks as above.

"} catch (e) {" +
" send('SecretKeySpec: ' + e);" +
"}" +
"send('ok');"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ok feels like it belongs inside the try, at the very end (that is only reached if everything went well).

" c.$init.overload('[B', 'java.lang.String').implementation = function(a, b){ orig.call(this, a, b); };" +

// now look up the function again and call it
" var testConstructor = c.$new( [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], 'AES' );" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semistandard isn't followed here.

}

static Class<?>forName() {
return Badger.class;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off.

throw new IllegalStateException("Already dead");
}

static Class<?>forName() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space between type and method name.

loadScript("var c = Java.use('java.lang.Runtime');" +
"try{" +
" var orig = c.loadLibrary.overload('java.lang.String');" +
" c.loadLibrary.overload('java.lang.String').implementation = function(s){ orig(s); };" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semistandard isn't followed here.

Copy link
Member

@oleavr oleavr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's push the style through the last mile :)


import javax.crypto.Cipher;
import java.io.IOException;
import javax.crypto.Cipher;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import looks like a duplicate.

public void interfaceCannotBeInstantiated() {
loadScript("var X509TrustManager = Java.use('javax.net.ssl.X509TrustManager');" +
"try {" +
" var tm = X509TrustManager.$new();" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that indentation is inside the string, whereas the existing tests puts the indentation outside. I prefer your approach though, so I'll fix the other tests so they match it.

"} catch (e) {" +
" send('couldnt create trustmanager');" +
"}"
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ); should go on the previous line for consistency.

"} catch (e) {" +
" send('class.forName failed. ' + e);" +
"}"
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ); should go on the previous line for consistency.

"} catch (e) {" +
" send('forName failed. ' + e);" +
"}"
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ); should go on the previous line for consistency.

loadScript("var c = Java.use('re.frida.Badger');" +
"try {" +
" var orig = c.forName;" +
" c.forName.implementation = function () { " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads out the same value twice making the code hard to read. (See comment below.)

"try {" +
" var c = Java.use('dalvik.system.VMStack');" +
" var orig = c.getStackClass2;" +
" c.getStackClass2.implementation = function () {" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads out the same value twice making the code hard to read. (See comment below.)

"try {" +
" var c = Java.use('re.frida.Badger');" +
" var orig = c.forNameYo;" +
" c.forNameYo.implementation = function () {" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads out the same value twice making the code hard to read. (See comment below.)


// hook the original
" var orig = c.invoke;" +
" c.invoke.implementation = function (obj, ...args) { " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads out the same value twice making the code hard to read. (See comment below.)

loadScript("var c = Java.use('java.lang.System');" +
"try {" +
" var orig = c.load;" +
" c.load.implementation = function (s) { " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads out the same value twice making the code hard to read. (See comment below.)

public void genericReturnBadger() {
loadScript("var C = Java.use('re.frida.Badger');" +
"try {" +
" var method1 = C.forName;" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space in the JS code.

"var C2 = Java.use('java.lang.Class');" +
"try {" +
// hook the original
" var method1 = C.invoke;" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space in the JS code.

loadScript("var C = Java.use('java.lang.System');" +
"try {" +
" var method1 = C.load;" +
" method1.implementation = function (s) { " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after ,.

public void loadWorks() {
loadScript("var C = Java.use('java.lang.System');" +
"try {" +
" var method1 = C.load;" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space in the JS code.

" var method1 = C.load;" +
" method1.implementation = function (s) { " +
" method1.call(this,s);" +
" };" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

" var method1 = C.load;" +
" method1.implementation = function (s) { " +
" return method1.call(this,s);" +
" };" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

loadScript("var C = Java.use('java.lang.System');" +
"try {" +
" var method1 = C.load;" +
" method1.implementation = function (s) { " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after ,.

" };" +

// now look up the function again and call it
" var now = C.loadLibrary.overload('java.lang.String');" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

loadScript("var C = Java.use('java.lang.Runtime');" +
"try {" +
" var method1 = C.loadLibrary.overload('java.lang.String');" +
" method1.implementation = function (s) {" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after ,.

public void constructorReturnsCorrectType() {
loadScript("var C = Java.use('javax.crypto.spec.SecretKeySpec');" +
"try {" +
" var method1 = C.$init.overload('[B', 'java.lang.String');" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space in the JS code.

Copy link
Member

@oleavr oleavr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants