|
6 | 6 | import org.hamcrest.Matcher; |
7 | 7 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
8 | 8 | import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Nested; |
9 | 10 | import org.junit.jupiter.api.Test; |
10 | 11 |
|
| 12 | +import java.nio.file.Paths; |
11 | 13 | import java.security.KeyStore; |
12 | 14 | import java.security.KeyStoreException; |
13 | 15 | import java.util.List; |
| 16 | +import java.util.stream.IntStream; |
| 17 | +import java.util.stream.Stream; |
14 | 18 |
|
15 | 19 | import static java.util.Collections.list; |
| 20 | +import static java.util.stream.Collectors.toList; |
16 | 21 | import static no.digipost.signature.client.Certificates.PRODUCTION; |
17 | 22 | import static no.digipost.signature.client.Certificates.TEST; |
| 23 | +import static no.digipost.signature.client.core.internal.security.TrustStoreLoader.generateAlias; |
18 | 24 | import static org.hamcrest.MatcherAssert.assertThat; |
19 | 25 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 26 | +import static org.hamcrest.Matchers.everyItem; |
| 27 | +import static org.hamcrest.Matchers.hasSize; |
20 | 28 | import static org.hamcrest.Matchers.is; |
| 29 | +import static org.hamcrest.Matchers.matchesRegex; |
| 30 | +import static org.hamcrest.Matchers.notNullValue; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertAll; |
21 | 32 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | +import static org.quicktheories.QuickTheory.qt; |
| 34 | +import static org.quicktheories.generators.SourceDSL.strings; |
| 35 | +import static uk.co.probablyfine.matchers.Java8Matchers.where; |
22 | 36 |
|
23 | 37 | class TrustStoreLoaderTest { |
24 | 38 |
|
@@ -78,6 +92,50 @@ void loads_certificates_from_file_location() throws KeyStoreException { |
78 | 92 | assertThat(trustStore, containsExactlyTheAliases("certificatetest:commfides_test_ca.cer")); |
79 | 93 | } |
80 | 94 |
|
| 95 | + @Nested |
| 96 | + class GenerateAlias { |
| 97 | + @Test |
| 98 | + void generateAliasFromUnixPath() { |
| 99 | + assertThat(generateAlias(Paths.get("/blah/blah/funny/env/MyCert.cer")), is("env:MyCert.cer")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void generateAliasFromWindowsPath() { |
| 104 | + assertThat(generateAlias(Paths.get("C:/blah/blah/funny/env/MyCert.cer")), is("env:MyCert.cer")); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void generateAliasFromUnixFileInJarUrlString() { |
| 109 | + assertThat(generateAlias("/blah/fun/prod/WEB-INF/lib/mylib.jar!/certificates/env/MyCert.cer"), is("env:MyCert.cer")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void generateAliasFromWindowsFileInJarUrlString() { |
| 114 | + assertThat(generateAlias("file:/C:/blah/fun/prod/WEB-INF/lib/mylib.jar!/certificates/env/MyCert.cer"), is("env:MyCert.cer")); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void generateUniqueDefaultAliasesForNullsAndEmptyStrings() { |
| 119 | + List<String> defaultAliases = Stream.<String>of(null, "", " ", " \n ").map(s -> TrustStoreLoader.generateAlias(s)).collect(toList()); |
| 120 | + int aliasCount = defaultAliases.size(); |
| 121 | + assertAll("default aliases", |
| 122 | + () -> assertThat(defaultAliases, everyItem(matchesRegex("certificate-alias-\\d+"))), |
| 123 | + () -> assertAll(IntStream.range(0, aliasCount).mapToObj(defaultAliases::get).map(alias -> () -> { |
| 124 | + List<String> otherAliases = defaultAliases.stream().filter(a -> !alias.equals(a)).collect(toList()); |
| 125 | + assertThat("other alises than '" + alias + "'", otherAliases, hasSize(aliasCount - 1)); |
| 126 | + }))); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void alwaysGeneratesAnAlias() { |
| 131 | + qt() |
| 132 | + .forAll(strings().allPossible().ofLengthBetween(0, 100)) |
| 133 | + .checkAssert(s -> assertThat(s, where(TrustStoreLoader::generateAlias, notNullValue()))); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + } |
| 138 | + |
81 | 139 |
|
82 | 140 | private static Matcher<KeyStore> containsExactlyTheAliases(String ... certAliases) { |
83 | 141 | return new TypeSafeDiagnosingMatcher<KeyStore>() { |
|
0 commit comments