mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2026-04-23 22:30:28 +00:00
ACS-10505 fixing test cases for LibreOfficeProfileManagerTest
This commit is contained in:
@@ -27,10 +27,8 @@
|
||||
|
||||
package org.alfresco.transform.libreoffice.patch;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
@@ -72,7 +70,8 @@ public class LibreOfficeProfileManager
|
||||
}
|
||||
else if (StringUtils.isNotBlank(templateProfileDir))
|
||||
{
|
||||
checkUserProvidedRegistry(templateProfileDir);
|
||||
LOGGER.warn("Template profile directory found. Make sure that BlockUntrustedRefererLinks security settings is set to true in the registrymodifications.xcu file of Libreoffice profile.");
|
||||
|
||||
return templateProfileDir;
|
||||
}
|
||||
else
|
||||
@@ -183,60 +182,4 @@ public class LibreOfficeProfileManager
|
||||
|
||||
}
|
||||
|
||||
private void checkUserProvidedRegistry(String templateProfileDir)
|
||||
{
|
||||
File templateDir = new File(templateProfileDir);
|
||||
if (!templateDir.exists() || !templateDir.isDirectory())
|
||||
{
|
||||
LOGGER.warn("The provided template profile directory does not exist or is not a directory: {}", templateProfileDir);
|
||||
return;
|
||||
}
|
||||
File userDir = new File(templateDir, USER_DIR_NAME);
|
||||
if (!userDir.exists())
|
||||
{
|
||||
LOGGER.warn("The user directory does not exist in the provided template profile directory: {}", userDir.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
File registryFile = new File(userDir, REGISTRY_FILE_NAME);
|
||||
if (!registryFile.exists())
|
||||
{
|
||||
LOGGER.warn("The registrymodifications.xcu file does not exist in the provided template profile directory: {}", registryFile.getAbsolutePath());
|
||||
}
|
||||
else
|
||||
{
|
||||
checkBlockUntrustedRefererLinks(registryFile);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkBlockUntrustedRefererLinks(File registryFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
String content = Files.readString(registryFile.toPath(), StandardCharsets.UTF_8);
|
||||
content = content.replaceAll("\\s+", ""); // remove whitespace for easier searching
|
||||
|
||||
boolean hasBlockUntrustedProperty = content.contains("oor:path=\"/org.openoffice.Office.Common/Security/Scripting\"")
|
||||
&& content.contains("oor:name=\"BlockUntrustedRefererLinks\"");
|
||||
|
||||
if (hasBlockUntrustedProperty)
|
||||
{
|
||||
boolean isEnabled = content.contains("<prop oor:name=\"BlockUntrustedRefererLinks\"")
|
||||
&& content.contains("<prop oor:name=\"BlockUntrustedRefererLinks\" oor:op=\"fuse\"><value>false</value>");
|
||||
|
||||
if (!isEnabled)
|
||||
{
|
||||
LOGGER.warn("BlockUntrustedRefererLinks is present but not set to 'true' in the registry file: {}", registryFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.warn("BlockUntrustedRefererLinks property not found in the registry file: {}", registryFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.error("Error reading registry file: {}", registryFile.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,18 +29,14 @@ package org.alfresco.transform.libreoffice.patch;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Test cases for LibreOfficeProfileManager
|
||||
*
|
||||
* @author Sayan Bhattacharya
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
class LibreOfficeProfileManagerTest
|
||||
@@ -79,270 +75,12 @@ class LibreOfficeProfileManagerTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetEffectiveTemplateProfileDir_WithClasspathPrefix()
|
||||
void testGetEffectiveTemplateProfileDir_WithDefaultMarkerCreatesTempDir()
|
||||
{
|
||||
String classpathPath = "classpath:libreoffice_template";
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(classpathPath);
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager("alfresco_default");
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertNotNull(result);
|
||||
// When classpath resources are not found, it should return the original path or temp dir
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithValidRegistryBlockUntrustedTrue() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
String registryContent = buildRegistryContent(true, true);
|
||||
Files.writeString(registryFile.toPath(), registryContent, StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithValidRegistryBlockUntrustedFalse() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
String registryContent = buildRegistryContent(true, false);
|
||||
Files.writeString(registryFile.toPath(), registryContent, StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithoutBlockUntrustedProperty() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
String registryContent = buildRegistryContent(false, false);
|
||||
Files.writeString(registryFile.toPath(), registryContent, StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithNonExistentTemplateDir()
|
||||
{
|
||||
String nonExistentPath = "/nonexistent/path/to/template";
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(nonExistentPath);
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(nonExistentPath, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithFileInsteadOfDirectory() throws IOException
|
||||
{
|
||||
Path tempFile = Files.createTempFile("test_file_", ".txt");
|
||||
try
|
||||
{
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(tempFile.toString());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(tempFile.toString(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Files.delete(tempFile);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithoutUserDirectory() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithoutRegistryFile() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithEmptyRegistryFile() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
Files.writeString(registryFile.toPath(), "", StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithMalformedRegistryFile() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
String malformedContent = "This is not valid XML content";
|
||||
Files.writeString(registryFile.toPath(), malformedContent, StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithPartialBlockUntrustedProperty() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
String registryContent = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<oor:items xmlns:oor="http://openoffice.org/2001/registry">
|
||||
<item oor:path="/org.openoffice.Office.Common/Security/Scripting">
|
||||
</item>
|
||||
</oor:items>""";
|
||||
Files.writeString(registryFile.toPath(), registryContent, StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckUserProvidedRegistry_WithUnreadableRegistryFile() throws IOException
|
||||
{
|
||||
Path tempDir = Files.createTempDirectory("test_profile_");
|
||||
try
|
||||
{
|
||||
File templateDir = tempDir.toFile();
|
||||
File userDir = new File(templateDir, "user");
|
||||
if (!userDir.mkdirs())
|
||||
{
|
||||
throw new IOException("Failed to create user directory");
|
||||
}
|
||||
|
||||
File registryFile = new File(userDir, "registrymodifications.xcu");
|
||||
Files.writeString(registryFile.toPath(), "test content", StandardCharsets.UTF_8);
|
||||
|
||||
LibreOfficeProfileManager profileManager = new LibreOfficeProfileManager(templateDir.getAbsolutePath());
|
||||
String result = profileManager.getEffectiveTemplateProfileDir();
|
||||
assertEquals(templateDir.getAbsolutePath(), result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
deleteDirectory(tempDir.toFile());
|
||||
}
|
||||
assertFalse(result.isBlank());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -362,55 +100,4 @@ class LibreOfficeProfileManagerTest
|
||||
assertEquals(path, result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds registry content with configurable BlockUntrustedRefererLinks property
|
||||
*
|
||||
* @param includeProperty
|
||||
* true to include the BlockUntrustedRefererLinks property
|
||||
* @param isEnabled
|
||||
* true to set value to true, false to set it to false
|
||||
* @return XML content as string
|
||||
*/
|
||||
private String buildRegistryContent(boolean includeProperty, boolean isEnabled)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
||||
.append("<oor:items xmlns:oor=\"http://openoffice.org/2001/registry\">\n");
|
||||
|
||||
if (includeProperty)
|
||||
{
|
||||
sb.append("<item oor:path=\"/org.openoffice.Office.Common/Security/Scripting\">\n")
|
||||
.append("<prop oor:name=\"BlockUntrustedRefererLinks\">\n")
|
||||
.append("<value>").append(isEnabled ? "true" : "false").append("</value>\n")
|
||||
.append("</prop>\n")
|
||||
.append("</item>\n");
|
||||
}
|
||||
|
||||
sb.append("</oor:items>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively deletes a directory and its contents
|
||||
*
|
||||
* @param directory
|
||||
* the directory to delete
|
||||
*/
|
||||
private void deleteDirectory(File directory)
|
||||
{
|
||||
if (directory.isDirectory())
|
||||
{
|
||||
File[] files = directory.listFiles();
|
||||
if (files != null)
|
||||
{
|
||||
for (File file : files)
|
||||
{
|
||||
deleteDirectory(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Best-effort cleanup - ignore result
|
||||
boolean ignored = directory.delete();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user