From 861f3e9d3791e4acb3db7e4ed25e9fc1930dce22 Mon Sep 17 00:00:00 2001 From: nbaars Date: Sat, 7 Feb 2015 11:37:53 +0100 Subject: [PATCH] Moved loading to separate object. Added a unit test for loading the properties --- .../webgoat/plugins/GlobalProperties.java | 24 ++++++++++ .../webgoat/plugins/PluginFileUtils.java | 5 +- .../org/owasp/webgoat/session/Course.java | 17 ++----- .../webgoat/plugins/GlobalPropertiesTest.java | 47 +++++++++++++++++++ 4 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/owasp/webgoat/plugins/GlobalProperties.java create mode 100644 src/test/java/org/owasp/webgoat/plugins/GlobalPropertiesTest.java diff --git a/src/main/java/org/owasp/webgoat/plugins/GlobalProperties.java b/src/main/java/org/owasp/webgoat/plugins/GlobalProperties.java new file mode 100644 index 000000000..6e478815f --- /dev/null +++ b/src/main/java/org/owasp/webgoat/plugins/GlobalProperties.java @@ -0,0 +1,24 @@ +package org.owasp.webgoat.plugins; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + +public final class GlobalProperties { + + private final Plugin plugin; + + public GlobalProperties(Path pluginDirectory) { + this.plugin = new Plugin(pluginDirectory); + } + + public void loadProperties(Path globalPropertiesPath) { + try { + List filesInDirectory = PluginFileUtils.getFilesInDirectory(globalPropertiesPath); + this.plugin.loadFiles(filesInDirectory, true); + } catch (IOException e) { + throw new IllegalStateException("Unable to load global properties, check your installation for the directory i18n: " + globalPropertiesPath.toString(), e); + } + } + +} diff --git a/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java b/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java index 758119f0e..a3b26a34b 100644 --- a/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java +++ b/src/main/java/org/owasp/webgoat/plugins/PluginFileUtils.java @@ -15,7 +15,7 @@ public class PluginFileUtils { } public static boolean hasParentDirectoryWithName(Path p, String s) { - if (p == null || p.getParent() == null || p.getRoot().equals(p.getParent())) { + if (p == null || p.getParent() == null || p.getParent().equals(p.getRoot())) { return false; } if (p.getParent().getFileName().toString().equals(s)) { @@ -31,8 +31,7 @@ public class PluginFileUtils { return p; } - public static List getFilesInDirectory( Path directory) throws IOException - { + public static List getFilesInDirectory( Path directory) throws IOException { List files = new ArrayList<>(); DirectoryStream dirStream; dirStream = Files.newDirectoryStream(directory); diff --git a/src/main/java/org/owasp/webgoat/session/Course.java b/src/main/java/org/owasp/webgoat/session/Course.java index 24a06a9d2..aac3517b3 100644 --- a/src/main/java/org/owasp/webgoat/session/Course.java +++ b/src/main/java/org/owasp/webgoat/session/Course.java @@ -3,6 +3,7 @@ package org.owasp.webgoat.session; import org.owasp.webgoat.HammerHead; import org.owasp.webgoat.lessons.AbstractLesson; import org.owasp.webgoat.lessons.Category; +import org.owasp.webgoat.plugins.GlobalProperties; import org.owasp.webgoat.plugins.Plugin; import org.owasp.webgoat.plugins.PluginFileUtils; import org.owasp.webgoat.plugins.PluginsLoader; @@ -284,7 +285,6 @@ public class Course { } private void loadLessonFromPlugin(ServletContext context) { - context.getContextPath(); logger.debug("Loading plugins into cache"); String pluginPath = context.getRealPath("plugin_lessons"); String targetPath = context.getRealPath("plugin_extracted"); @@ -292,17 +292,8 @@ public class Course { logger.error("Plugins directory {} not found", pluginPath); return; } - - // Do a one time load of the container properties - String containerPath = context.getRealPath("container//i18n"); - Plugin theContainer = new Plugin(Paths.get(targetPath)); - try { - theContainer.loadFiles(PluginFileUtils.getFilesInDirectory(Paths.get(containerPath)), false); - } catch (IOException io) { - logger.error("Error loading container properties: ", io); - } - - Path pluginDirectory = Paths.get(pluginPath); + new GlobalProperties(Paths.get(targetPath)).loadProperties(Paths.get(context.getRealPath("container//i18n"))); + List plugins = new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)).loadPlugins(true); for (Plugin plugin : plugins) { try { @@ -314,7 +305,7 @@ public class Course { lesson.update(properties); - if (lesson.getHidden() == false) { + if (!lesson.getHidden()) { lessons.add(lesson); } for(Map.Entry lessonPlan : plugin.getLessonPlans().entrySet()) { diff --git a/src/test/java/org/owasp/webgoat/plugins/GlobalPropertiesTest.java b/src/test/java/org/owasp/webgoat/plugins/GlobalPropertiesTest.java new file mode 100644 index 000000000..f1dd80a46 --- /dev/null +++ b/src/test/java/org/owasp/webgoat/plugins/GlobalPropertiesTest.java @@ -0,0 +1,47 @@ +package org.owasp.webgoat.plugins; + +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.junit.Assert.assertNotNull; + +public class GlobalPropertiesTest { + + private Path tempDirectory; + + @Before + public void createTmpDir() throws IOException { + tempDirectory = Files.createTempDirectory(GlobalPropertiesTest.class.getSimpleName()); + tempDirectory.toFile().deleteOnExit(); + } + + @Test + public void propertyFilesShouldBeLoaded() throws IOException { + Path pluginDirectory = Files.createDirectory(Paths.get(tempDirectory.toString(), "plugins")); + Path directory = Files.createDirectory(Paths.get(tempDirectory.toString(), "i18n")); + Path globalProperties = Files.createFile(Paths.get(directory.toString(), "global.properties")); + Files.write(globalProperties, Arrays.asList("test=label for test"), StandardCharsets.UTF_8); + new GlobalProperties(pluginDirectory).loadProperties(directory); + + ClassLoader propertyFilesClassLoader = + ResourceBundleClassLoader.createPropertyFilesClassLoader(this.getClass().getClassLoader()); + assertNotNull(propertyFilesClassLoader.getResourceAsStream("global.properties")); + } + + @Test(expected = IllegalStateException.class) + public void propertyFilesDirectoryNotFoundShouldRaiseError() throws IOException { + Path pluginDirectory = Files.createDirectory(Paths.get(tempDirectory.toString(), "plugins")); + Path directory = Files.createDirectory(Paths.get(tempDirectory.toString(), "i18n")); + Files.delete(directory); + + new GlobalProperties(pluginDirectory).loadProperties(directory); + } + +} \ No newline at end of file