Skip to content

Disable NPE checks for non-public library fields #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Damtev opened this issue Jun 30, 2022 · 1 comment · Fixed by #353
Closed

Disable NPE checks for non-public library fields #358

Damtev opened this issue Jun 30, 2022 · 1 comment · Fixed by #353
Assignees
Labels
comp-symbolic-engine Issue is related to the symbolic execution engine

Comments

@Damtev
Copy link
Member

Damtev commented Jun 30, 2022

Description

For now, the engine considers method invocations or field access on null values as producing NullPointerException. Previously in #226 such NPE checks were disabled for final fields from library classes because it often leads to the generation of tests with exceptions that are unexpected for a user with high usage of Java reflection. It is suggested to extend such a strategy to non-public fields from library classes too, for the same reasons.

Expected behavior

Consider generating tests for such a method:

public class DateExample {
    boolean foo(Date date) {
        return date.getTime() == 100;
    }
}

Only such 3 tests are expected by user:

@Test
@DisplayName("foo: return date.getTime() == 100 : False -> return date.getTime() == 100")
public void testFoo_DateGetTimeNotEquals100() {
    DateExample dateExample = new DateExample();
    Date date = new Date(-155L);

    boolean actual = dateExample.foo(date);

    assertFalse(actual);
}

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> return date.getTime() == 100")
public void testFoo_DateGetTimeEquals100() {
    DateExample dateExample = new DateExample();
    Date date = new Date(100L);

    boolean actual = dateExample.foo(date);

    assertTrue(actual);
}

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> ThrowNullPointerException")
public void testFoo_DateGetTime() {
    DateExample dateExample = new DateExample();

    assertThrows(NullPointerException.class, () -> dateExample.foo(null));
}

But for now, UtBot also generates at least 2 such tests:

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> ThrowNullPointerException")
public void testFoo_ThrowNullPointerException() throws Exception {
    DateExample dateExample = new DateExample();
    Date date = ((Date) createInstance("java.util.Date"));
    Object immutableGregorianDate = createInstance("sun.util.calendar.ImmutableGregorianDate");
    setField(immutableGregorianDate, "date", null);
    setField(date, "cdate", immutableGregorianDate);

    assertThrows(NullPointerException.class, () -> dateExample.foo(date));
}

@Test
@DisplayName("foo: return date.getTime() == 100 : True -> ThrowNullPointerException")
public void testFoo_ThrowNullPointerException_1() throws ClassNotFoundException, Exception {
    Class dateClazz = Class.forName("java.util.Date");
    BaseCalendar prevGcal = ((BaseCalendar) getStaticFieldValue(dateClazz, "gcal"));
    BaseCalendar prevJcal = ((BaseCalendar) getStaticFieldValue(dateClazz, "jcal"));
    try {
        setStaticField(dateClazz, "gcal", null);
        setStaticField(dateClazz, "jcal", null);
        DateExample dateExample = new DateExample();
        Date date = ((Date) createInstance("java.util.Date"));
        Object immutableGregorianDate = createInstance("sun.util.calendar.ImmutableGregorianDate");
        LocalGregorianCalendar.Date date1 = ((LocalGregorianCalendar.Date) createInstance("sun.util.calendar.LocalGregorianCalendar$Date"));
        setField(date1, "zoneinfo", null);
        setField(date1, "normalized", false);
        setField(date1, "millis", 0);
        setField(date1, "seconds", 0);
        setField(date1, "minutes", 0);
        setField(date1, "hours", 0);
        setField(date1, "dayOfMonth", 0);
        setField(date1, "month", 0);
        setField(date1, "gregorianYear", 11564545);
        setField(immutableGregorianDate, "date", date1);
        setField(date, "cdate", immutableGregorianDate);

        dateExample.foo(date);
    } finally {
        setStaticField(Date.class, "gcal", prevGcal);
        setStaticField(Date.class, "jcal", prevJcal);
    }
}

These tests SHOULD NOT be generated (in the plugin, at least).

Environment

No mocks, Java.

Potential alternatives

Unknown.

Context

We can possibly lose some branches with such disabling.

@Damtev Damtev added the comp-symbolic-engine Issue is related to the symbolic execution engine label Jun 30, 2022
@Damtev Damtev self-assigned this Jun 30, 2022
@Damtev Damtev moved this to Todo in UTBot Java Jun 30, 2022
@dtim
Copy link
Collaborator

dtim commented Jun 30, 2022

Disabling NPE branches for non-public fields (and for final fields as well) affects the fields declared in library classes. While it is OK for JDK classes, it also prevents NPE checks for any third-party libraries, including potentially buggy code. Protected fields may be initialized to null by derived classes as well by any users of these classes using existing constructors or static methods. If the class does not perform necessary checks, NPE may be thrown.

Attached file: a sample project with a (buggy) library dependency packaged as a jar.

ProjectWithLib.zip

Repository owner moved this from Todo to Done in UTBot Java Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-symbolic-engine Issue is related to the symbolic execution engine
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants