-
Notifications
You must be signed in to change notification settings - Fork 487
fix: Image type recognition failure in FileTypeUtils #812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
delei
merged 8 commits into
apache:main
from
bengbengbalabalabeng:fix-image-type-parser
Feb 27, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1032c40
fix: Image type recognition failure in FileTypeUtils
bengbengbalabalabeng a5f85ab
Merge branch 'main' into fix-image-type-parser
bengbengbalabalabeng 7c7efa8
Merge branch 'main' into fix-image-type-parser
bengbengbalabalabeng f81f086
fix: use longest-prefix-first matching for magic numbers
bengbengbalabalabeng 3d13ba8
Merge branch 'main' into fix-image-type-parser
alaahong 24a230d
Merge branch 'main' into fix-image-type-parser
bengbengbalabalabeng 18d6ff3
Merge branch 'main' into fix-image-type-parser
delei a558c94
test: add sample image files for FileTypeUtils tests
bengbengbalabalabeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
150 changes: 150 additions & 0 deletions
150
fesod-sheet/src/test/java/org/apache/fesod/sheet/util/FileTypeUtilsTest.java
This file contains hidden or 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,150 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fesod.sheet.util; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import org.apache.fesod.sheet.metadata.data.ImageData; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
|
|
||
| /** | ||
| * Tests {@link FileTypeUtils} | ||
| */ | ||
| class FileTypeUtilsTest { | ||
|
|
||
| private byte[] realJpeg; | ||
| private byte[] realPng; | ||
| private byte[] realSvg; | ||
|
|
||
| @BeforeEach | ||
| void setup() throws Exception { | ||
| realJpeg = loadImage("fesod-logo-jpeg.jpeg"); | ||
| realPng = loadImage("fesod-logo-png.png"); | ||
| realSvg = loadImage("fesod-logo-svg.svg"); | ||
| } | ||
|
|
||
| private byte[] loadImage(String filename) throws IOException { | ||
| try (InputStream is = getClass().getClassLoader().getResourceAsStream("images" + File.separator + filename); ) { | ||
| Assertions.assertNotNull(is); | ||
|
|
||
| ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
| byte[] data = new byte[4096]; | ||
| int n; | ||
| while ((n = is.read(data, 0, data.length)) != -1) { | ||
| buffer.write(data, 0, n); | ||
| } | ||
| return buffer.toByteArray(); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @NullAndEmptySource | ||
| void test_getImageType_NullOrEmpty(byte[] input) { | ||
| Assertions.assertNull(FileTypeUtils.getImageType(input)); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageType_tooShort() { | ||
| byte[] input = new byte[] {(byte) 0x00, (byte) 0x01}; | ||
| Assertions.assertNull(FileTypeUtils.getImageType(input)); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageType_safeCopy() { | ||
| // JPEG | ||
| byte[] input = new byte[10]; | ||
| input[0] = (byte) 0xFF; | ||
| input[1] = (byte) 0xD8; | ||
| input[2] = (byte) 0xFF; | ||
| input[3] = (byte) 0xE0; | ||
|
|
||
| Assertions.assertDoesNotThrow(() -> { | ||
| ImageData.ImageType type = FileTypeUtils.getImageType(input); | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_JPEG, type); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageType_JPEG() { | ||
| // JPEG: ffd8ff | ||
| byte[] jpeg = new byte[30]; | ||
| jpeg[0] = (byte) 0xFF; | ||
| jpeg[1] = (byte) 0xD8; | ||
| jpeg[2] = (byte) 0xFF; | ||
|
|
||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_JPEG, FileTypeUtils.getImageType(jpeg)); | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_JPEG, FileTypeUtils.getImageType(realJpeg)); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageType_PNG() { | ||
| // PNG: 89504e47 | ||
| byte[] png = new byte[30]; | ||
| png[0] = (byte) 0x89; | ||
| png[1] = (byte) 0x50; | ||
| png[2] = (byte) 0x4E; | ||
| png[3] = (byte) 0x47; | ||
|
|
||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_PNG, FileTypeUtils.getImageType(png)); | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_PNG, FileTypeUtils.getImageType(realPng)); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageType_unknown() { | ||
| byte[] unknown = new byte[30]; | ||
| Assertions.assertNull(FileTypeUtils.getImageType(unknown)); | ||
| Assertions.assertNull(FileTypeUtils.getImageType(realSvg)); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageTypeFormat_success() { | ||
| byte[] jpeg = new byte[30]; | ||
| jpeg[0] = (byte) 0xFF; | ||
| jpeg[1] = (byte) 0xD8; | ||
| jpeg[2] = (byte) 0xFF; | ||
|
|
||
| int typeOfJpeg = FileTypeUtils.getImageTypeFormat(jpeg); | ||
| int typeOfRealJpeg = FileTypeUtils.getImageTypeFormat(realJpeg); | ||
| int typeOfPng = FileTypeUtils.getImageTypeFormat(realPng); | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_JPEG.getValue(), typeOfJpeg); | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_JPEG.getValue(), typeOfRealJpeg); | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_PNG.getValue(), typeOfPng); | ||
| } | ||
|
|
||
| @Test | ||
| void test_getImageTypeFormat_default() { | ||
| byte[] unknown = new byte[30]; | ||
|
|
||
| int result = FileTypeUtils.getImageTypeFormat(unknown); | ||
|
|
||
| Assertions.assertEquals(FileTypeUtils.defaultImageType.getValue(), result); | ||
| } | ||
|
|
||
| @Test | ||
| void test_DefaultConfig() { | ||
| Assertions.assertEquals(ImageData.ImageType.PICTURE_TYPE_PNG, FileTypeUtils.defaultImageType); | ||
| } | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @bengbengbalabalabeng
For unit test classes, would it be preferable to provide some small image file examples?
FYI, these small image files are recommended to be stored in the resources directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion.
I’ve added small sample image files for the unit tests.