Skip to content

Commit

Permalink
Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakky54 committed Jan 20, 2024
1 parent 9ab92fa commit ddf967d
Showing 1 changed file with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

Expand All @@ -49,7 +48,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
Expand Down Expand Up @@ -191,6 +189,57 @@ void loadWindowsSystemKeyStore() {
}
}

@Test
void loadWindowsSystemKeyStoreWhileExcludingSomeKeystoreTypes() {
System.setProperty("sslcontext-kickstart.excluded-system-keystore-types", "Windows-ROOT-LOCALMACHINE,Windows-ROOT-CURRENTUSER");
LogCaptor logCaptor = LogCaptor.forClass(KeyStoreUtils.class);
logCaptor.setLogLevelToDebug();

System.setProperty("os.name", "windows");
KeyStore windowsRootKeyStore = mock(KeyStore.class);
KeyStore windowsMyKeyStore = mock(KeyStore.class);
KeyStore windowsMyCurrentUserKeyStore = mock(KeyStore.class);
KeyStore windowsMyLocalmachineKeyStore = mock(KeyStore.class);
KeyStore windowsRootCurrentUserKeyStore = mock(KeyStore.class);
KeyStore windowsRootLocalmachineKeyStore = mock(KeyStore.class);

try (MockedStatic<KeyStoreUtils> keyStoreUtilsMock = mockStatic(KeyStoreUtils.class, invocation -> {
Method method = invocation.getMethod();
if ("loadSystemKeyStores".equals(method.getName()) && method.getParameterCount() == 0) {
return invocation.callRealMethod();
} else if ("createKeyStoreIfAvailable".equals(method.getName()) && method.getParameterCount() == 2 && "Windows-ROOT".equals(invocation.getArgument(0))) {
return Optional.of(windowsRootKeyStore);
} else if ("createKeyStoreIfAvailable".equals(method.getName()) && method.getParameterCount() == 2 && "Windows-MY".equals(invocation.getArgument(0))) {
return Optional.of(windowsMyKeyStore);
} else if ("createKeyStoreIfAvailable".equals(method.getName()) && method.getParameterCount() == 2 && "Windows-MY-CURRENTUSER".equals(invocation.getArgument(0))) {
return Optional.of(windowsMyCurrentUserKeyStore);
} else if ("createKeyStoreIfAvailable".equals(method.getName()) && method.getParameterCount() == 2 && "Windows-MY-LOCALMACHINE".equals(invocation.getArgument(0))) {
return Optional.of(windowsMyLocalmachineKeyStore);
} else if ("createKeyStoreIfAvailable".equals(method.getName()) && method.getParameterCount() == 2 && "Windows-ROOT-LOCALMACHINE".equals(invocation.getArgument(0))) {
return Optional.of(windowsRootLocalmachineKeyStore);
} else if ("createKeyStoreIfAvailable".equals(method.getName()) && method.getParameterCount() == 2 && "Windows-ROOT-CURRENTUSER".equals(invocation.getArgument(0))) {
return Optional.of(windowsRootCurrentUserKeyStore);
} else if ("countAmountOfTrustMaterial".equals(method.getName())) {
return 2;
} else if ("getExcludedSystemKeyStoreTypes".equals(method.getName())) {
return invocation.callRealMethod();
} else {
return invocation.getMock();
}
})) {
List<KeyStore> keyStores = KeyStoreUtils.loadSystemKeyStores();
assertThat(keyStores)
.contains(windowsRootKeyStore, windowsMyKeyStore, windowsMyCurrentUserKeyStore, windowsMyLocalmachineKeyStore)
.doesNotContain(windowsRootCurrentUserKeyStore, windowsRootLocalmachineKeyStore);

assertThat(logCaptor.getDebugLogs()).contains("Loaded [8] system trusted certificates");
} finally {
resetOsName();
System.clearProperty("sslcontext-kickstart.excluded-system-keystore-types");
}
}


@Test
void loadAndroidSystemKeyStoreWithAndroidSystemProperty() {
System.setProperty("os.name", "Linux");
Expand Down

0 comments on commit ddf967d

Please sign in to comment.