Skip to content

File should be mocked in Concrete executor #1384

@alisevych

Description

@alisevych

Description

File.read is added to permissions for Java Security Manager.
Some of generated tests contain too much reflection for instantiating objects.

To Reproduce

  1. Install one of the latest plugin build from main in IDEA
  2. Run the 'UTBotJava' project in IntelliJ Idea
  3. Set defaults in Settings->Tools->UnitTestBot
  4. Create a text file sandbox.policy in the following directory: ~/.utbot/ and add the following text to it:
grant {
    permission java.io.FilePermission  "<<ALL FILES>>", "read";
};
  1. Use plugin to generate tests for the following method:
    public int read(File path) throws IOException {
        byte[] bytes = Files.readAllBytes(path.toPath());
        return bytes.length;
    }

Expected behavior

File can be mocked in the generated tests.

Actual behavior

Too complex reflection with system classes is used.

Possible solutions

Create a sample file in test resources and instantiate File with a path to it - for tests with existing File.

Visual proofs (screenshots, logs, images)

    ///region OTHER: ERROR SUITE for method read(java.io.File)

    @Test
    public void testRead1() throws Exception {
        SecurityCheck securityCheck = new SecurityCheck();
        File path = ((File) createInstance("java.io.File"));
        Object windowsPathWithAttributes = createInstance("sun.nio.fs.WindowsPath$WindowsPathWithAttributes");
        Object windowsFileSystem = createInstance("sun.nio.fs.WindowsFileSystem");
        Object windowsFileSystemProvider = createInstance("sun.nio.fs.WindowsFileSystemProvider");
        setField(windowsFileSystem, "sun.nio.fs.WindowsFileSystem", "provider", windowsFileSystemProvider);
        setField(windowsPathWithAttributes, "sun.nio.fs.WindowsPath", "fs", windowsFileSystem);
        setField(windowsPathWithAttributes, "sun.nio.fs.WindowsPath", "type", null);
        WeakCleanableRef weakCleanableRef = ((WeakCleanableRef) createInstance("jdk.internal.ref.CleanerImpl$WeakCleanableRef"));
        setField(windowsPathWithAttributes, "sun.nio.fs.WindowsPath", "pathForWin32Calls", weakCleanableRef);
        setField(path, "java.io.File", "filePath", windowsPathWithAttributes);
        
        /* This test fails because method [org.utbot.examples.securitycheck.SecurityCheck.read] produces [java.lang.UnsupportedOperationException: get]
            java.base/jdk.internal.ref.CleanerImpl$WeakCleanableRef.get(CleanerImpl.java:247)
            java.base/sun.nio.fs.WindowsPath.getPathForWin32Calls(WindowsPath.java:183)
            java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
            java.base/java.nio.file.Files.newByteChannel(Files.java:371)
            java.base/java.nio.file.Files.newByteChannel(Files.java:422)
            java.base/java.nio.file.Files.readAllBytes(Files.java:3206)
            org.utbot.examples.securitycheck.SecurityCheck.read(SecurityCheck.java:19) */
        securityCheck.read(path);
    }

    @Test
    public void testRead2() throws Exception {
        Class vMClazz = Class.forName("jdk.internal.misc.VM");
        int prevInitLevel = ((Integer) getStaticFieldValue(vMClazz, "initLevel"));
        try {
            setStaticField(vMClazz, "initLevel", 2);
            SecurityCheck securityCheck = new SecurityCheck();
            File path = ((File) createInstance("java.io.File"));
            String string = "\u0000\u0000";
            setField(path, "java.io.File", "path", string);
            setField(path, "java.io.File", "filePath", null);
            
            /* This test fails because method [org.utbot.examples.securitycheck.SecurityCheck.read] produces [java.nio.file.InvalidPathException: Illegal char < > at index 0:   ]
                java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
                java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
                java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
                java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
                java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229)
                java.base/java.io.File.toPath(File.java:2329)
                org.utbot.examples.securitycheck.SecurityCheck.read(SecurityCheck.java:19) */
            securityCheck.read(path);
        } finally {
            setStaticField(VM.class, "initLevel", prevInitLevel);
        }
    }

    @Test
    public void testRead3() throws Exception {
        SecurityCheck securityCheck = new SecurityCheck();
        File path = ((File) createInstance("java.io.File"));
        Object windowsPathWithAttributes = createInstance("sun.nio.fs.WindowsPath$WindowsPathWithAttributes");
        Object windowsFileSystem = createInstance("sun.nio.fs.WindowsFileSystem");
        Object windowsFileSystemProvider = createInstance("sun.nio.fs.WindowsFileSystemProvider");
        setField(windowsFileSystem, "sun.nio.fs.WindowsFileSystem", "provider", windowsFileSystemProvider);
        setField(windowsPathWithAttributes, "sun.nio.fs.WindowsPath", "fs", windowsFileSystem);
        Class windowsPathTypeClazz = Class.forName("sun.nio.fs.WindowsPathType");
        Object windowsPathType = getEnumConstantByName(windowsPathTypeClazz, "UNC");
        setField(windowsPathWithAttributes, "sun.nio.fs.WindowsPath", "type", windowsPathType);
        WeakCleanableRef weakCleanableRef = ((WeakCleanableRef) createInstance("jdk.internal.ref.CleanerImpl$WeakCleanableRef"));
        setField(windowsPathWithAttributes, "sun.nio.fs.WindowsPath", "pathForWin32Calls", weakCleanableRef);
        setField(path, "java.io.File", "filePath", windowsPathWithAttributes);
        
        /* This test fails because method [org.utbot.examples.securitycheck.SecurityCheck.read] produces [java.lang.NullPointerException]
            java.base/sun.nio.fs.WindowsPath.getPathForWin32Calls(WindowsPath.java:178)
            java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
            java.base/java.nio.file.Files.newByteChannel(Files.java:371)
            java.base/java.nio.file.Files.newByteChannel(Files.java:422)
            java.base/java.nio.file.Files.readAllBytes(Files.java:3206)
            org.utbot.examples.securitycheck.SecurityCheck.read(SecurityCheck.java:19) */
        securityCheck.read(path);
    }
    ///endregion

Environment

Windows 10 Pro
IntelliJ IDEA 2022.2.3
JDK 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-instrumented-processIssue is related to Instrumented processcomp-symbolic-engineIssue is related to the symbolic execution enginectg-bugIssue is a bugctg-enhancementNew feature, improvement or change request

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions