SignableTest.java

/*
 * Copyright 2023 Emmanuel Bourg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.jsign;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.Vector;

import org.apache.commons.io.FileUtils;
import org.junit.Assume;
import org.junit.Test;

import net.jsign.appx.APPXFile;
import net.jsign.pe.PEFile;
import net.jsign.script.VBScript;

import static org.junit.Assert.*;

public class SignableTest {

    @Test
    public void testOfWithUnsupportedFormat() {
        Exception e = assertThrows(UnsupportedOperationException.class, () -> Signable.of(new File("pom.xml")));
        assertEquals("message", "Unsupported file: pom.xml", e.getMessage());
    }

    @Test
    public void testOfWithMixedCaseExtension() throws Exception {
        FileUtils.copyFile(new File("target/test-classes/hello-world.vbs"), new File("target/test-classes/hello-world-renamed.VBS"));
        try (Signable signable = Signable.of(new File("target/test-classes/hello-world-renamed.VBS"))) {
            assertTrue(signable instanceof VBScript);
        }

        FileUtils.copyFile(new File("target/test-classes/minimal.appxbundle"), new File("target/test-classes/minimal-renamed.AppxBundle"));
        try (Signable signable = Signable.of(new File("target/test-classes/minimal-renamed.AppxBundle"))) {
            assertTrue(signable instanceof APPXFile);
        }
    }

    @Test
    public void testOfWithCustomClassLoader() throws Exception {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

        try {
            Thread.currentThread().setContextClassLoader(new ClassLoader() {
                @Override
                public Enumeration<URL> getResources(String name) {
                    // this classloader can't see META-INF/services/net.jsign.spi.SignableProvider
                    return new Vector<URL>().elements();
                }
            });

            try (Signable signable = Signable.of(new File("target/test-classes/wineyes.exe"))) {
                assertTrue(signable instanceof PEFile);
            };

        } finally {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }

    @Test
    public void testFileDescriptorLeak() throws Exception {
        Assume.assumeTrue(System.getProperty("os.name").startsWith("Linux"));

        File file = new File("pom.xml");
        File tmp = new File("target/test-classes/pom-tmp.xml");

        FileUtils.copyFile(file, tmp);

        assertFalse("The file descriptor should be closed before probing the file", isFileOpen(tmp));
        assertThrows(UnsupportedOperationException.class, () -> Signable.of(tmp));
        assertFalse("The file descriptor should be closed after probing the file", isFileOpen(tmp));
    }

    /**
     * Checks if the current process has an open file descriptor for the specified file (Linux only).
     */
    private boolean isFileOpen(File file) throws IOException {
        Path canonicalPath = file.getCanonicalFile().toPath();
        //String pid = String.valueOf(ProcessHandle.current().pid()); // Java 9+
        String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];

        Process process = null;
        try {
            process = new ProcessBuilder("lsfd", "-p", pid, "-Q", "TYPE == \"REG\"", "-o", "NAME", "--noheadings").start();

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    try {
                        Path opened = Paths.get(line).toRealPath();
                        if (Files.isSameFile(opened, canonicalPath)) {
                            return true;
                        }
                    } catch (IOException e) {
                        // Ignore non-file entries
                    }
                }
            }
        } finally {
            if (process != null) {
                process.destroy();
            }
        }

        return false;
    }
}