-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to check workaround will terminate properly on bad images.
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/test/java/net/coobird/thumbnailator/tasks/io/InputStreamImageSourceMalformedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Thumbnailator - a thumbnail generation library | ||
* | ||
* Copyright (c) 2008-2021 Chris Kroells | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package net.coobird.thumbnailator.tasks.io; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@RunWith(Parameterized.class) | ||
public class InputStreamImageSourceMalformedTest { | ||
|
||
@Parameterized.Parameters(name = "type={0}, length={1}") | ||
public static Collection<Object> testCases() { | ||
List<Object[]> cases = new ArrayList<Object[]>(); | ||
for (String type : Arrays.asList("jpg", "png", "bmp")) { | ||
for (int i = 1; i <= 40; i++) { | ||
cases.add(new Object[] { type, i }); | ||
} | ||
} | ||
return Arrays.asList(cases.toArray()); | ||
} | ||
|
||
@Parameterized.Parameter | ||
public String type; | ||
|
||
@Parameterized.Parameter(value = 1) | ||
public Integer length; | ||
|
||
@Before @After | ||
public void cleanup() { | ||
System.clearProperty("thumbnailator.disableExifWorkaround"); | ||
} | ||
|
||
@Test | ||
public void terminatesProperlyWithWorkaround() { | ||
runTest(); | ||
} | ||
|
||
@Test | ||
public void terminatesProperlyWithoutWorkaround() { | ||
System.setProperty("thumbnailator.disableExifWorkaround", "true"); | ||
runTest(); | ||
} | ||
|
||
/** | ||
* Test to check that reading an abnormal file won't cause image reading | ||
* to end up in a bad state like in an infinite loop. | ||
*/ | ||
private void runTest() { | ||
try { | ||
byte[] bytes = new byte[length]; | ||
InputStream sourceIs = ClassLoader.getSystemResourceAsStream(String.format("Thumbnailator/grid.%s", type)); | ||
sourceIs.read(bytes); | ||
sourceIs.close(); | ||
|
||
ByteArrayInputStream is = new ByteArrayInputStream(bytes); | ||
InputStreamImageSource source = new InputStreamImageSource(is); | ||
|
||
source.read(); | ||
|
||
} catch (Exception e) { | ||
// terminates properly, even if an exception is thrown. | ||
} | ||
} | ||
} |