| 1 |
/* AppletTestTests.java |
| 2 |
Copyright (C) 2011 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, version 2. |
| 9 |
|
| 10 |
IcedTea is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with IcedTea; see the file COPYING. If not, write to |
| 17 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
02110-1301 USA. |
| 19 |
|
| 20 |
Linking this library statically or dynamically with other modules is |
| 21 |
making a combined work based on this library. Thus, the terms and |
| 22 |
conditions of the GNU General Public License cover the whole |
| 23 |
combination. |
| 24 |
|
| 25 |
As a special exception, the copyright holders of this library give you |
| 26 |
permission to link this library with independent modules to produce an |
| 27 |
executable, regardless of the license terms of these independent |
| 28 |
modules, and to copy and distribute the resulting executable under |
| 29 |
terms of your choice, provided that you also meet, for each linked |
| 30 |
independent module, the terms and conditions of the license of that |
| 31 |
module. An independent module is a module which is not derived from |
| 32 |
or based on this library. If you modify this library, you may extend |
| 33 |
this exception to your version of the library, but you are not |
| 34 |
obligated to do so. If you do not wish to do so, delete this |
| 35 |
exception statement from your version. |
| 36 |
*/ |
| 37 |
|
| 38 |
import java.awt.image.BufferedImage; |
| 39 |
import java.io.File; |
| 40 |
import java.io.IOException; |
| 41 |
import java.util.Arrays; |
| 42 |
import java.util.List; |
| 43 |
import javax.imageio.ImageIO; |
| 44 |
import net.sourceforge.jnlp.ClosingListener; |
| 45 |
import net.sourceforge.jnlp.OptionsDefinitions; |
| 46 |
import net.sourceforge.jnlp.ProcessResult; |
| 47 |
import net.sourceforge.jnlp.ServerAccess; |
| 48 |
import net.sourceforge.jnlp.annotations.KnownToFail; |
| 49 |
import net.sourceforge.jnlp.annotations.NeedsDisplay; |
| 50 |
import net.sourceforge.jnlp.annotations.TestInBrowsers; |
| 51 |
import net.sourceforge.jnlp.browsertesting.BrowserTest; |
| 52 |
import net.sourceforge.jnlp.browsertesting.Browsers; |
| 53 |
import net.sourceforge.jnlp.closinglisteners.RulesFolowingClosingListener; |
| 54 |
import net.sourceforge.jnlp.runtime.JNLPRuntime; |
| 55 |
import net.sourceforge.jnlp.util.InvalidJarHeaderException; |
| 56 |
import net.sourceforge.jnlp.util.JarFile; |
| 57 |
import org.junit.Assert; |
| 58 |
import org.junit.Test; |
| 59 |
|
| 60 |
public class GifarTestcases extends BrowserTest { |
| 61 |
|
| 62 |
List<String> trustIgnore = Arrays.asList(new String[]{ServerAccess.HEADLES_OPTION, OptionsDefinitions.OPTIONS.TRUSTALL.option, OptionsDefinitions.OPTIONS.NOHEADERS.option}); |
| 63 |
List<String> trust = Arrays.asList(new String[]{ServerAccess.HEADLES_OPTION, OptionsDefinitions.OPTIONS.TRUSTALL.option}); |
| 64 |
RulesFolowingClosingListener.ContainsRule exceptionRule = new RulesFolowingClosingListener.ContainsRule(InvalidJarHeaderException.class.getName()); |
| 65 |
RulesFolowingClosingListener.ContainsRule okRule = new RulesFolowingClosingListener.ContainsRule("Image loaded"); |
| 66 |
RulesFolowingClosingListener.ContainsRule sucideRule = new RulesFolowingClosingListener.ContainsRule("gifar killing himself"); |
| 67 |
|
| 68 |
private ClosingListener getExceptionClosingListener() { |
| 69 |
return new RulesFolowingClosingListener(exceptionRule); |
| 70 |
} |
| 71 |
|
| 72 |
private ClosingListener getOkClosingListener() { |
| 73 |
return new RulesFolowingClosingListener(okRule, sucideRule); |
| 74 |
} |
| 75 |
File okJar = new File(server.getDir(), "GifarBase.jar"); |
| 76 |
File hackedJar = new File(server.getDir(), "Gifar.jar"); |
| 77 |
File okImage = new File(server.getDir(), "happyNonAnimated.gif"); |
| 78 |
File hackedImage = new File(server.getDir(), "Gifar.gif"); |
| 79 |
|
| 80 |
@Test |
| 81 |
public void unittest_verify_okJar() throws IOException { |
| 82 |
JNLPRuntime.setIgnoreHeaders(false); |
| 83 |
JarFile j1 = new JarFile(okJar); |
| 84 |
Assert.assertNotNull(j1); |
| 85 |
JNLPRuntime.setIgnoreHeaders(true); |
| 86 |
JarFile j2 = new JarFile(okJar); |
| 87 |
Assert.assertNotNull(j2); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void unittest_verify_badJar() throws IOException { |
| 93 |
JNLPRuntime.setIgnoreHeaders(false); |
| 94 |
Exception ex=null; |
| 95 |
JarFile j1=null; |
| 96 |
try{ |
| 97 |
j1 = new JarFile(hackedJar); |
| 98 |
}catch(InvalidJarHeaderException e){ |
| 99 |
ex=e; |
| 100 |
} |
| 101 |
Assert.assertNull(j1); |
| 102 |
Assert.assertNotNull(ex); |
| 103 |
Assert.assertEquals(InvalidJarHeaderException.class, ex.getClass()); |
| 104 |
JNLPRuntime.setIgnoreHeaders(true); |
| 105 |
JarFile j2 = new JarFile(hackedJar); |
| 106 |
Assert.assertNotNull(j2); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
@Test |
| 112 |
public void unittest_verify_badImageAsJar() throws IOException { |
| 113 |
JNLPRuntime.setIgnoreHeaders(false); |
| 114 |
Exception ex=null; |
| 115 |
JarFile j1=null; |
| 116 |
try{ |
| 117 |
j1 = new JarFile(hackedImage); |
| 118 |
}catch(InvalidJarHeaderException e){ |
| 119 |
ex=e; |
| 120 |
} |
| 121 |
Assert.assertNull(j1); |
| 122 |
Assert.assertNotNull(ex); |
| 123 |
Assert.assertEquals(InvalidJarHeaderException.class, ex.getClass()); |
| 124 |
JNLPRuntime.setIgnoreHeaders(true); |
| 125 |
JarFile j2 = new JarFile(hackedImage); |
| 126 |
Assert.assertNotNull(j2); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void unittest_verify_okImage() throws IOException { |
| 132 |
JNLPRuntime.setIgnoreHeaders(false); |
| 133 |
BufferedImage j1 = ImageIO.read(okImage); |
| 134 |
Assert.assertNotNull(j1); |
| 135 |
JNLPRuntime.setIgnoreHeaders(true); |
| 136 |
BufferedImage j2 = ImageIO.read(okImage); |
| 137 |
Assert.assertNotNull(j2); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void unittest_verify_badImaqe() throws IOException { |
| 143 |
JNLPRuntime.setIgnoreHeaders(false); |
| 144 |
BufferedImage j1 = ImageIO.read(hackedImage); |
| 145 |
Assert.assertNotNull(j1); |
| 146 |
JNLPRuntime.setIgnoreHeaders(true); |
| 147 |
BufferedImage j2 = ImageIO.read(hackedImage); |
| 148 |
Assert.assertNotNull(j2); |
| 149 |
|
| 150 |
} |
| 151 |
@Test |
| 152 |
@NeedsDisplay |
| 153 |
public void GifarViaJnlp_application() throws Exception { |
| 154 |
ProcessResult pr = server.executeJavaws(trust, "gifar_application.jnlp"); |
| 155 |
Assert.assertFalse("stdout " + okRule.toFailingString() + " but did", okRule.evaluate(pr.stdout)); |
| 156 |
Assert.assertTrue("stderr " + exceptionRule.toPassingString() + " but did'nt", exceptionRule.evaluate(pr.stderr)); |
| 157 |
Assert.assertEquals((Integer) 1, pr.returnValue); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
@NeedsDisplay |
| 162 |
//this test is known to fail on jdk8+ which do nto alow some "gif" instances on classpath |
| 163 |
@KnownToFail |
| 164 |
public void GifarViaJnlp_application_ignoreHeaders() throws Exception { |
| 165 |
ProcessResult pr = server.executeJavaws(trustIgnore, "gifar_application.jnlp"); |
| 166 |
Assert.assertTrue("stdout " + okRule.toPassingString() + " but didn't", okRule.evaluate(pr.stdout)); |
| 167 |
Assert.assertFalse("stderr " + exceptionRule.toFailingString() + " but did", exceptionRule.evaluate(pr.stderr)); |
| 168 |
Assert.assertEquals((Integer) 0, pr.returnValue); |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
@NeedsDisplay |
| 173 |
public void GifarViaJnlp_applet() throws Exception { |
| 174 |
ProcessResult pr = server.executeJavaws(trust, "gifar_applet.jnlp"); |
| 175 |
Assert.assertFalse("stdout " + okRule.toFailingString() + " but did", okRule.evaluate(pr.stdout)); |
| 176 |
Assert.assertTrue("stderr " + exceptionRule.toPassingString() + " but didn't", exceptionRule.evaluate(pr.stderr)); |
| 177 |
Assert.assertEquals((Integer) 1, pr.returnValue); |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
@NeedsDisplay |
| 182 |
//this test is known to fail on jdk8+ which do nto alow some "gif" instances on classpath |
| 183 |
@KnownToFail |
| 184 |
public void GifarViaJnlp_applet_ignoreHeaders() throws Exception { |
| 185 |
ProcessResult pr = server.executeJavaws(trustIgnore, "gifar_applet.jnlp"); |
| 186 |
Assert.assertTrue("stdout " + okRule.toPassingString() + " but didn't", okRule.evaluate(pr.stdout)); |
| 187 |
Assert.assertFalse("stderr " + exceptionRule.toFailingString() + " but did", exceptionRule.evaluate(pr.stderr)); |
| 188 |
Assert.assertEquals((Integer) 0, pr.returnValue); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
@TestInBrowsers(testIn = {Browsers.all}) |
| 193 |
@NeedsDisplay |
| 194 |
public void GifarViaBrowser_hacked() throws Exception { |
| 195 |
ProcessResult pr = server.executeBrowser("gifarView_hacked.html", getOkClosingListener(), getExceptionClosingListener()); |
| 196 |
Assert.assertFalse("stdout " + okRule.toFailingString() + " but did", okRule.evaluate(pr.stdout)); |
| 197 |
Assert.assertTrue("stderr " + exceptionRule.toPassingString() + " but didn't", exceptionRule.evaluate(pr.stderr)); |
| 198 |
|
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
@TestInBrowsers(testIn = {Browsers.one}) |
| 204 |
@NeedsDisplay |
| 205 |
public void GifarViaBrowser_ok() throws Exception { |
| 206 |
ProcessResult pr = server.executeBrowser("gifarView_ok.html", getOkClosingListener(), getExceptionClosingListener()); |
| 207 |
Assert.assertTrue("stdout " + okRule.toPassingString() + " but didn't", okRule.evaluate(pr.stdout)); |
| 208 |
Assert.assertFalse("stderr " + exceptionRule.toFailingString() + " but did", exceptionRule.evaluate(pr.stderr)); |
| 209 |
|
| 210 |
|
| 211 |
} |
| 212 |
} |