-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test for DefaultCountriesDataLoaderImpl.java
Security test
- Loading branch information
1 parent
cd66608
commit 25c2249
Showing
4 changed files
with
70 additions
and
5 deletions.
There are no files selected for viewing
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
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
49 changes: 49 additions & 0 deletions
49
location4j/src/test/java/com/tomaytotomato/loader/DefaultCountriesDataLoaderImplTest.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,49 @@ | ||
package com.tomaytotomato.loader; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DefaultCountriesDataLoaderImplTest { | ||
|
||
/** | ||
* Verifies that the constructor throws a SecurityException if resource paths do not match. | ||
*/ | ||
@DisplayName("Constructor should throw SecurityException for mismatched resource paths") | ||
@Test | ||
void testSecurityExceptionThrownWhenFilePathIsInvalid() { | ||
|
||
// Given | ||
class TestCountriesDataLoader extends DefaultCountriesDataLoaderImpl { | ||
@Override | ||
protected URL getResource(String resource) { | ||
try { | ||
if (resource.equals("/location4j.bin")) { | ||
return new URI("file:///mock-path/location4j.bin").toURL(); | ||
} else if (resource.equals("TestCountriesDataLoader.class")) { | ||
return new URI("file:///hacked/DefaultCountriesDataLoaderImpl.class").toURL(); | ||
} | ||
} catch (MalformedURLException | URISyntaxException e) { | ||
throw new RuntimeException("Failed to create mock URL for resource: " + resource, e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected InputStream getResourceAsStream(String resource) { | ||
return this.getClass().getResourceAsStream("/hacked-location4j.bin"); | ||
} | ||
} | ||
|
||
// When Then | ||
assertThatThrownBy(TestCountriesDataLoader::new) | ||
.isInstanceOf(SecurityException.class) | ||
.hasMessageContaining("/location4j.bin is not in the same artifact as the loader: security issue"); | ||
} | ||
} |
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 @@ | ||
fl;kdsa;'flsdakjfdsal;kfjds;klj2309rjfsdkl;jfsd;lkfjdsa;lkfdsaja;sldkj |