WALA icon indicating copy to clipboard operation
WALA copied to clipboard

Found bug in JUnitEntryPoints

Open gustaavv opened this issue 2 years ago • 2 comments

When using junit4 annotations, the static method make(IClassHierarchy cha) can not return any entrypoint.

I found that isTestEntryPoint(TypeName typeName) might be wrong:

  private static boolean isTestEntryPoint(TypeName typeName) {
    // WALA uses $ to refers to inner classes. We have to replace "$" by "."
    // to make it a valid class name in Java source code.
    String javaName =
        StringStuff.jvmToReadableType(typeName.getPackage() + "." + typeName.getClassName());

    return TEST_ENTRY_POINT_ANNOTATION_NAMES.contains(javaName);
  }

For the class Lorg/abc/Main, typeName.getPackage() will return org.abc and delete the starting L. While jvmToReadableType depends on this L to parse the className. Without the starting L, jvmToReadableType will return an empty string.

So, I changed the method to the following:

    private static boolean isTestEntryPoint(TypeName typeName) {
        // WALA uses $ to refers to inner classes. We have to replace "$" by "."
        // to make it a valid class name in Java source code.
        String javaName = StringStuff.jvmToReadableType(
                typeName.toString().replace('$', '.'));

        return TEST_ENTRY_POINT_ANNOTATION_NAMES.contains(javaName);
    }

And it works.

Note that in my case, I have no inner classes to test. So I just changed the code by the comment.

gustaavv avatar Mar 09 '24 06:03 gustaavv

Thanks for the report @el-nino2020! Could you submit a pull request with your fix? That would be great.

msridhar avatar Mar 09 '24 16:03 msridhar

In fact, I just copied the source code of JUnitEntryPoints to my project and did the modification above, and it is sufficient for my use. I have not been looking into how to contributing to this project (nor do I have a good knowledge of WALA). Without testing, I think it is imporper to submit a PR. So I made an issue instead of a PR.

gustaavv avatar Mar 10 '24 10:03 gustaavv

When using junit4 annotations, the static method make(IClassHierarchy cha) can not return any entrypoint.

I found that isTestEntryPoint(TypeName typeName) might be wrong:

  private static boolean isTestEntryPoint(TypeName typeName) {
    // WALA uses $ to refers to inner classes. We have to replace "$" by "."
    // to make it a valid class name in Java source code.
    String javaName =
        StringStuff.jvmToReadableType(typeName.getPackage() + "." + typeName.getClassName());

    return TEST_ENTRY_POINT_ANNOTATION_NAMES.contains(javaName);
  }

For the class Lorg/abc/Main, typeName.getPackage() will return org.abc and delete the starting L.

Hm. I don't see where that is removed. I see it added here:

https://github.com/wala/WALA/blob/2f79c10f1188d4b2ef3904e2b5c39c0beebd9e5f/core/src/main/java/com/ibm/wala/types/TypeName.java#L314-L315

khatchad avatar May 28 '24 17:05 khatchad

Ah, but that's in toString(). If the problem is from getPackage(), the package comes from the constructor and can be any atom.

I guess different representations may not be prefixed with L.

khatchad avatar May 30 '24 14:05 khatchad