diff options
author | Jiri Vanek <[email protected]> | 2013-02-27 17:00:48 +0100 |
---|---|---|
committer | Jiri Vanek <[email protected]> | 2013-02-27 17:00:48 +0100 |
commit | 59a94f89ce7543d5a9788040de31d20fdf642c23 (patch) | |
tree | dc7e60f2487e02cb5c04514796da3a49b160848c /tests | |
parent | 8f33e7d61884012e3c759b563c94f97ecdac4371 (diff) |
Added backend and settings for extended applet security
Diffstat (limited to 'tests')
2 files changed, 346 insertions, 0 deletions
diff --git a/tests/netx/unit/net/sourceforge/jnlp/security/appletextendedsecurity/impl/UnsignedAppletActionStorageImplTest.java b/tests/netx/unit/net/sourceforge/jnlp/security/appletextendedsecurity/impl/UnsignedAppletActionStorageImplTest.java new file mode 100644 index 0000000..cd89b63 --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/security/appletextendedsecurity/impl/UnsignedAppletActionStorageImplTest.java @@ -0,0 +1,114 @@ +/* Copyright (C) 2013 Red Hat, Inc. + +This file is part of IcedTea. + +IcedTea is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 2. + +IcedTea is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with IcedTea; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. + */ + +package net.sourceforge.jnlp.security.appletextendedsecurity.impl; + +import net.sourceforge.jnlp.security.appletextendedsecurity.impl.UnsignedAppletActionStorageImpl; +import net.sourceforge.jnlp.security.appletextendedsecurity.UnsignedAppletActionEntry; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import net.sourceforge.jnlp.ServerAccess; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class UnsignedAppletActionStorageImplTest { + + private static File f1; + private static File f2; + private static File f3; + private static File f4; + + @BeforeClass + public static void preapreTestFiles() throws IOException { + f1 = File.createTempFile("itwMatching", "testFile1"); + f2 = File.createTempFile("itwMatching", "testFile2"); + f3 = File.createTempFile("itwMatching", "testFile3"); + f4 = File.createTempFile("itwMatching", "testFile4"); + ServerAccess.saveFile("A 123456 .* .* jar1,jar2", f1); + ServerAccess.saveFile("A 123456 .* \\Qbla\\E jar1,jar2", f2); + } + + @Test + public void allMatchingDocAndCode() { + UnsignedAppletActionStorageImpl i1 = new UnsignedAppletActionStorageImpl(f1); + UnsignedAppletActionEntry r1 = i1.getMatchingItem("bla", "blaBla", Arrays.asList(new String[]{"jar1", "jar2"})); + Assert.assertNotNull("r1 should be found", r1); + UnsignedAppletActionEntry r3 = i1.getMatchingItem("blah", "blaBla", Arrays.asList(new String[]{"jar2", "jar1"})); + Assert.assertNotNull("r3 should be found", r1); + UnsignedAppletActionEntry r4 = i1.getMatchingItem("blha", "blaBlam", Arrays.asList(new String[]{"jar2", "wrong_jar"})); + Assert.assertNull("r4 should NOT be found", r4); + UnsignedAppletActionEntry r5 = i1.getMatchingItem("blaBla", "blaBlaBla", Arrays.asList(new String[]{"jar2"})); + Assert.assertNull("r5 should NOT be found", r5); + + } + + @Test + public void allMatchingDocAndStrictCode() { + UnsignedAppletActionStorageImpl i1 = new UnsignedAppletActionStorageImpl(f2); + UnsignedAppletActionEntry r1 = i1.getMatchingItem("whatever", "bla", Arrays.asList(new String[]{"jar1", "jar2"})); + Assert.assertNotNull("r1 should be found", r1); + UnsignedAppletActionEntry r3 = i1.getMatchingItem("whatever", null, Arrays.asList(new String[]{"jar2", "jar1"})); + Assert.assertNotNull("r3 should be found", r1); + UnsignedAppletActionEntry r2 = i1.getMatchingItem("bla", "blaBlam", Arrays.asList(new String[]{"jar1", "jar2"})); + Assert.assertNull("r2 should NOT be found", r2); + UnsignedAppletActionEntry r4 = i1.getMatchingItem(null, "blaBlam", null); + Assert.assertNull("r4 should NOT be found", r4); + + } + + @Test + public void allMatchingDocAndCodeWithNulls() { + UnsignedAppletActionStorageImpl i1 = new UnsignedAppletActionStorageImpl(f1); + UnsignedAppletActionEntry r1 = i1.getMatchingItem("bla", "blaBla", null); + Assert.assertNotNull("r1 should be found", r1); + UnsignedAppletActionEntry r3 = i1.getMatchingItem("bla", "whatever", null); + Assert.assertNotNull("r3 should be found", r1); + UnsignedAppletActionEntry r2 = i1.getMatchingItem("bla", "blaBla", Arrays.asList(new String[]{"jar2", "jar1"})); + Assert.assertNotNull("r2 should be found", r2); + UnsignedAppletActionEntry r4 = i1.getMatchingItem("bla", "blaBla", null); + Assert.assertNotNull("r4 should be found", r4); + UnsignedAppletActionEntry r5 = i1.getMatchingItem("", "blaBla", Arrays.asList(new String[]{"jar2", "jar1"})); + Assert.assertNotNull("r5 should be found", r5); + UnsignedAppletActionEntry r6 = i1.getMatchingItem(null, null, Arrays.asList(new String[]{"jar2", "jar1"})); + Assert.assertNotNull("r6 should be found", r6); + UnsignedAppletActionEntry r7 = i1.getMatchingItem(null, null, Arrays.asList(new String[]{"jar2", "jar11"})); + Assert.assertNull("r7 should NOT be found", r7); + + + } +} diff --git a/tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/LockingReaderWriterTest.java b/tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/LockingReaderWriterTest.java new file mode 100644 index 0000000..b834f88 --- /dev/null +++ b/tests/netx/unit/net/sourceforge/jnlp/util/lockingfile/LockingReaderWriterTest.java @@ -0,0 +1,232 @@ +/* +Copyright (C) 2013 Red Hat + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package net.sourceforge.jnlp.util.lockingfile; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +public class LockingReaderWriterTest { + + private static File storagefile; + + private static TestStringReaderWriter newInstance() { + return new TestStringReaderWriter(storagefile); + } + + @Before + public void setUp() throws IOException { + storagefile = File.createTempFile("foo", "bar"); + } + + @Test + public void testSimpleActions() throws IOException { + TestStringReaderWriter storage = newInstance(); + + storage.add("teststring"); + assertTrue(storage.contains("teststring")); + storage.remove("teststring"); + assertFalse(storage.contains("teststring")); + } + + @Test + public void testInterleavedActions() throws IOException { + TestStringReaderWriter storage1 = newInstance(); + TestStringReaderWriter storage2 = newInstance(); + + storage1.add("teststring"); + assertTrue(storage2.contains("teststring")); + storage2.remove("teststring"); + assertFalse(storage1.contains("teststring")); + } + + static class TestThread extends Thread { + String testString; + int iterations; + Throwable error = null; + + TestThread(String testString, int iterations) { + this.testString = testString; + this.iterations = iterations; + } + + @Override + public void run() { + try { + TestStringReaderWriter storage = newInstance(); + for (int i = 0; i < iterations; i++) { + assertTrue(storage.contains(this.testString)); + storage.add(this.testString); + storage.remove(this.testString); + assertTrue(storage.contains(this.testString)); + } + } catch (Throwable error) { + error.printStackTrace(); + this.error = error; + } + } + } + + private void concurrentReadWrites(int threadAmount, int iterations, + String testString) throws InterruptedException { + TestStringReaderWriter storage = newInstance(); + + storage.add(testString); + + List<TestThread> testThreads = new ArrayList<TestThread>(); + + for (int i = 0; i < threadAmount; i++) { + TestThread thread = new TestThread(testString, iterations); + testThreads.add(thread); + thread.start(); + } + + for (int i = 0; i < threadAmount; i++) { + testThreads.get(i).join(); + } + + assertTrue(storage.contains(testString)); + storage.remove(testString); + + // So long as number adds == number writes, we should be left with + // nothing at end. + assertFalse(storage.contains(testString)); + } + + // Long testing string, the contents are not important + private String makeLongTestString() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 1000; i++) { + sb.append(Integer.toString(i)); + } + return sb.toString(); + } + + @Test + public void testManyReadWrite() throws Exception { + int oneThread = 1; + String shortString = "teststring"; + + // This was causing 'too many open files' because FileUtils#getFileLock + // leaks file descriptors. No longer used. + concurrentReadWrites(oneThread, 500 /* iterations */, shortString); + } + + @Test + public void testManyThreads() throws Exception { + int threadAmount = 25; + String shortString = "teststring"; + String longString = makeLongTestString(); + + concurrentReadWrites(threadAmount, 10 /* per-thread iterations */, + shortString); + concurrentReadWrites(threadAmount, 2 /* per-thread iterations */, + longString); + } + + /** + * Concrete implementation to aid in testing LockingReaderWriter + */ + public static class TestStringReaderWriter extends LockingReaderWriter { + + private List<String> cachedContents = new ArrayList<String>(); + + public TestStringReaderWriter(File file) { + super(file); + } + + @Override + public void writeContent(BufferedWriter writer) throws IOException { + for (String string : cachedContents) { + writer.write(string); + writer.newLine(); + } + } + + @Override + protected void readLine(String line) { + this.cachedContents.add(line); + } + + @Override + protected void readContents() throws IOException { + cachedContents.clear(); + super.readContents(); + } + + /* + * Atomic container abstraction methods. + */ + synchronized public void add(final String line) { + doLocked(new Runnable() { + + public void run() { + try { + readContents(); + cachedContents.add(line); + writeContents(); + } catch (IOException ex) { + throw new StorageIoException(ex); + } + } + }); + } + + synchronized public boolean contains(final String line) { + final boolean[] doesContain = { false }; + doLocked(new Runnable() { + + public void run() { + try { + readContents(); + doesContain[0] = cachedContents.contains(line); + } catch (IOException e) { + throw new StorageIoException(e); + } + } + }); + return doesContain[0]; + } + + synchronized public boolean remove(final String line) { + final boolean[] didRemove = { false }; + + doLocked(new Runnable() { + public void run() { + try { + readContents(); + didRemove[0] = cachedContents.remove(line); + writeContents(); + } catch (IOException e) { + throw new StorageIoException(e); + } + } + }); + + return didRemove[0]; + } + } +}
\ No newline at end of file |