mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-10-06 11:17:02 +02:00
# Description of Changes Please provide a summary of the changes, including: Refactor test imports and update Gradle file to include all Java files from the src directory --- ## Checklist ### General - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [x] I have performed a self-review of my own code - [x] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details.
83 lines
3.0 KiB
Java
83 lines
3.0 KiB
Java
package stirling.software.SPDF.utils;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
import java.io.IOException;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import stirling.software.SPDF.model.api.converters.HTMLToPdfRequest;
|
|
|
|
public class FileToPdfTest {
|
|
|
|
/**
|
|
* Test the HTML to PDF conversion. This test expects an IOException when an empty HTML input is
|
|
* provided.
|
|
*/
|
|
@Test
|
|
public void testConvertHtmlToPdf() {
|
|
HTMLToPdfRequest request = new HTMLToPdfRequest();
|
|
byte[] fileBytes = new byte[0]; // Sample file bytes (empty input)
|
|
String fileName = "test.html"; // Sample file name indicating an HTML file
|
|
boolean disableSanitize = false; // Flag to control sanitization
|
|
|
|
// Expect an IOException to be thrown due to empty input
|
|
Throwable thrown =
|
|
assertThrows(
|
|
IOException.class,
|
|
() ->
|
|
FileToPdf.convertHtmlToPdf(
|
|
"/path/", request, fileBytes, fileName, disableSanitize));
|
|
assertNotNull(thrown);
|
|
}
|
|
|
|
/**
|
|
* Test sanitizeZipFilename with null or empty input. It should return an empty string in these
|
|
* cases.
|
|
*/
|
|
@Test
|
|
public void testSanitizeZipFilename_NullOrEmpty() {
|
|
assertEquals("", FileToPdf.sanitizeZipFilename(null));
|
|
assertEquals("", FileToPdf.sanitizeZipFilename(" "));
|
|
}
|
|
|
|
/**
|
|
* Test sanitizeZipFilename to ensure it removes path traversal sequences. This includes
|
|
* removing both forward and backward slash sequences.
|
|
*/
|
|
@Test
|
|
public void testSanitizeZipFilename_RemovesTraversalSequences() {
|
|
String input = "../some/../path/..\\to\\file.txt";
|
|
String expected = "some/path/to/file.txt";
|
|
|
|
// Print output for debugging purposes
|
|
System.out.println("sanitizeZipFilename " + FileToPdf.sanitizeZipFilename(input));
|
|
System.out.flush();
|
|
|
|
// Expect that the method replaces backslashes with forward slashes
|
|
// and removes path traversal sequences
|
|
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
}
|
|
|
|
/** Test sanitizeZipFilename to ensure that it removes leading drive letters and slashes. */
|
|
@Test
|
|
public void testSanitizeZipFilename_RemovesLeadingDriveAndSlashes() {
|
|
String input = "C:\\folder\\file.txt";
|
|
String expected = "folder/file.txt";
|
|
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
|
|
input = "/folder/file.txt";
|
|
expected = "folder/file.txt";
|
|
assertEquals(expected, FileToPdf.sanitizeZipFilename(input));
|
|
}
|
|
|
|
/** Test sanitizeZipFilename to verify that safe filenames remain unchanged. */
|
|
@Test
|
|
public void testSanitizeZipFilename_NoChangeForSafeNames() {
|
|
String input = "folder/subfolder/file.txt";
|
|
assertEquals(input, FileToPdf.sanitizeZipFilename(input));
|
|
}
|
|
}
|