Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.runtime;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.math.BigInteger;
import java.security.Principal;
Expand All @@ -29,7 +27,7 @@
import java.util.Set;
import javax.net.ssl.X509TrustManager;
import org.eclipse.core.internal.runtime.CollectionTrustManager;
import org.junit.Test;
import org.junit.jupiter.api.Test;

@SuppressWarnings("restriction")
public class CollectionTrustManagerTest {
Expand All @@ -44,8 +42,8 @@ public void testAcceptedIssuers() throws Exception {

X509Certificate[] allAcceptedIssuers = collectionTrustManager.getAcceptedIssuers();

assertThat(allAcceptedIssuers,
arrayContaining(acceptedIssuers1[0], acceptedIssuers1[1], acceptedIssuers2[0], acceptedIssuers2[1]));
assertThat(allAcceptedIssuers).containsExactly(acceptedIssuers1[0], acceptedIssuers1[1], acceptedIssuers2[0],
acceptedIssuers2[1]);
}

@Test
Expand All @@ -68,8 +66,8 @@ public void testCheckClientTrusted() throws Exception {
CertificateException exception = assertThrows(CertificateException.class, () -> {
collectionTrustManager.checkClientTrusted(chainTrustedByNone, authType);
});
assertThat(exception, sameInstance(manager1.exception)); // first in the list
assertThat(exception.getSuppressed(), arrayContaining(sameInstance(manager2.exception))); // second, suppressed
assertThat(exception).isSameAs(manager1.exception); // first in the list
assertThat(exception.getSuppressed()).containsExactly(manager2.exception); // second, suppressed
}

@Test
Expand All @@ -92,8 +90,8 @@ public void testCheckServerTrusted() throws Exception {
CertificateException exception = assertThrows(CertificateException.class, () -> {
collectionTrustManager.checkServerTrusted(chainTrustedByNone, authType);
});
assertThat(exception, sameInstance(manager1.exception)); // first in the list
assertThat(exception.getSuppressed(), arrayContaining(sameInstance(manager2.exception))); // second, suppressed
assertThat(exception).isSameAs(manager1.exception); // first in the list
assertThat(exception.getSuppressed()).containsExactly(manager2.exception); // second, suppressed
}

private static class StubX509TrustManager implements X509TrustManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.runtime;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesRegex;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -45,6 +35,7 @@
import javax.net.ssl.X509KeyManager;
import javax.net.ssl.X509TrustManager;
import javax.security.auth.x500.X500Principal;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.eclipse.core.internal.runtime.CollectionTrustManager;
import org.eclipse.core.internal.runtime.KeyStoreUtil;
import org.eclipse.core.runtime.Platform;
Expand Down Expand Up @@ -103,53 +94,56 @@ public void loadTrustManagers_Default() throws Exception {

keyStoreUtil.setUpSslContext();

assertThat(SSLContext.getDefault(), is(keyStoreUtil.recordedSslContext));
assertThat(SSLContext.getDefault()).isEqualTo(keyStoreUtil.recordedSslContext);

assertThat(keyStoreUtil.recordedTrustManagers, arrayWithSize(1));
assertThat(keyStoreUtil.recordedTrustManagers[0], instanceOf(CollectionTrustManager.class));
assertThat(((CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0]).getAcceptedIssuers(),
not(emptyArray()));
assertThat(keyStoreUtil.recordedTrustManagers).hasSize(1);
assertThat(keyStoreUtil.recordedTrustManagers[0])
.asInstanceOf(InstanceOfAssertFactories.type(CollectionTrustManager.class))
.satisfies(manager -> assertThat(manager.getAcceptedIssuers()).isNotEmpty());

CollectionTrustManager tm = (CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0];

// jvm
assertThat(tm.getTrustManagers(), not(empty()));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores, not(empty()));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).manager(), is(tm.getTrustManagers().get(0)));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).store(), is(nullValue()));
assertThat(tm.getTrustManagers()).isNotEmpty();
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores).isNotEmpty();
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).manager())
.isEqualTo(tm.getTrustManagers().get(0));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).store()).isNull();
assertThat(
Arrays.stream(tm.getTrustManagers().get(0).getAcceptedIssuers())
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName).toList(),
hasItem(matchesRegex("(?i).*digicert.*root.*")));
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName))
.anySatisfy(name -> assertThat(name).matches("(?i).*digicert.*root.*"));

if (OS.WINDOWS.equals(OS.current())) {
assertThat(tm.getTrustManagers(), hasSize(2));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores, hasSize(2));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).manager(),
is(tm.getTrustManagers().get(1)));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).store().getType(), is("Windows-ROOT"));
assertThat(tm.getTrustManagers()).hasSize(2);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores).hasSize(2);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).manager())
.isEqualTo(tm.getTrustManagers().get(1));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).store().getType())
.isEqualTo("Windows-ROOT");
assertThat(
Arrays.stream(tm.getTrustManagers().get(1).getAcceptedIssuers())
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName).toList(),
hasItem(matchesRegex("(?i).*digicert.*root.*")));
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName))
.anySatisfy(name -> assertThat(name).matches("(?i).*digicert.*root.*"));
} else if (OS.MAC.equals(OS.current())) {
assertThat(tm.getTrustManagers(), hasSize(2));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores, hasSize(2));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).manager(),
is(tm.getTrustManagers().get(1)));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).store().getType(), is("KeychainStore"));
assertThat(tm.getTrustManagers()).hasSize(2);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores).hasSize(2);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).manager())
.isEqualTo(tm.getTrustManagers().get(1));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(1).store().getType())
.isEqualTo("KeychainStore");
// Apple KeychainStore only includes the 'System' certificates
// (enterprise/admin managed)
// but not the 'System Roots' ones (public CAs).
// There's nothing guaranteed / deterministic in the 'System' on CI machines
// that we could check for here...
} else {
assertThat(tm.getTrustManagers(), hasSize(1));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores, hasSize(1));
assertThat(tm.getTrustManagers()).hasSize(1);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores).hasSize(1);
}

// no private keys
assertThat(keyStoreUtil.recordedKeyManagers, emptyArray());
assertThat(keyStoreUtil.recordedKeyManagers).isEmpty();
}

@Test
Expand All @@ -162,26 +156,25 @@ public void loadTrustManagers_TrustSystemPropertiesPointToCustomTrustStore() thr

keyStoreUtil.setUpSslContext();

assertThat(SSLContext.getDefault(), is(keyStoreUtil.recordedSslContext));
assertThat(SSLContext.getDefault()).isEqualTo(keyStoreUtil.recordedSslContext);

assertThat(keyStoreUtil.recordedTrustManagers, arrayWithSize(1));
assertThat(((CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0]).getAcceptedIssuers(),
not(emptyArray()));
assertThat(keyStoreUtil.recordedTrustManagers).hasSize(1);
assertThat(((CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0]).getAcceptedIssuers()).isNotEmpty();

CollectionTrustManager tm = (CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0];

assertThat(tm.getTrustManagers(), hasSize(1)); // only the properties-based store
assertThat(tm.getTrustManagers()).hasSize(1); // only the properties-based store

assertThat(
Arrays.stream(tm.getTrustManagers().get(0).getAcceptedIssuers())
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName).toList(),
hasItem("CN=Test,C=DE"));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores, hasSize(1));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).manager(), is(tm.getTrustManagers().get(0)));
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName).toList())
.contains("CN=Test,C=DE");
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores).hasSize(1);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).manager()).isEqualTo(tm.getTrustManagers().get(0));
// null caused KeyManagerFactory to load default system properties
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).store(), is(nullValue()));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).store()).isNull();

assertThat(keyStoreUtil.recordedKeyManagers, emptyArray());
assertThat(keyStoreUtil.recordedKeyManagers).isEmpty();
}

@Test
Expand All @@ -200,24 +193,25 @@ public void loadTrustManagers_TrustSystemPropertiesPointToPlatformSpecificKeysto

keyStoreUtil.setUpSslContext();

assertThat(SSLContext.getDefault(), is(keyStoreUtil.recordedSslContext));
assertThat(SSLContext.getDefault()).isEqualTo(keyStoreUtil.recordedSslContext);

assertThat(keyStoreUtil.recordedTrustManagers, arrayWithSize(1));
assertThat(keyStoreUtil.recordedTrustManagers).hasSize(1);

CollectionTrustManager tm = (CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0];

assertThat(tm.getTrustManagers(), hasSize(1)); // only the properties-based store
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores, hasSize(1));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).manager(), is(tm.getTrustManagers().get(0)));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).store(), is(nullValue()));
assertThat(tm.getTrustManagers()).hasSize(1); // only the properties-based store
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores).hasSize(1);
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).manager())
.isEqualTo(tm.getTrustManagers().get(0));
assertThat(keyStoreUtil.createdTrustManagersAndKeyStores.get(0).store()).isNull();

if (OS.WINDOWS.equals(OS.current())) {
assertThat(((CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0]).getAcceptedIssuers(),
not(emptyArray()));
assertThat(((CollectionTrustManager) keyStoreUtil.recordedTrustManagers[0]).getAcceptedIssuers())
.isNotEmpty();
assertThat(
Arrays.stream(tm.getTrustManagers().get(0).getAcceptedIssuers())
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName).toList(),
hasItem(matchesRegex("(?i).*digicert.*root.*")));
.map(X509Certificate::getSubjectX500Principal).map(X500Principal::getName))
.anySatisfy(name -> assertThat(name).matches("(?i).*digicert.*root.*"));
} else if (OS.MAC.equals(OS.current())) {
// Apple KeychainStore only includes the 'System' certificates
// (enterprise/admin managed)
Expand All @@ -234,10 +228,10 @@ public void loadKeyManagers_Default() throws Exception {

keyStoreUtil.setUpSslContext();

assertThat(SSLContext.getDefault(), is(keyStoreUtil.recordedSslContext));
assertThat(SSLContext.getDefault()).isEqualTo(keyStoreUtil.recordedSslContext);

assertThat(keyStoreUtil.recordedKeyManagers, emptyArray());
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores, hasSize(0));
assertThat(keyStoreUtil.recordedKeyManagers).isEmpty();
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores).isEmpty();
}

@Test
Expand All @@ -251,18 +245,18 @@ public void loadKeyManagers_KeySystemPropertiesPointToCustomKeyStore() throws Ex

keyStoreUtil.setUpSslContext();

assertThat(SSLContext.getDefault(), is(keyStoreUtil.recordedSslContext));
assertThat(SSLContext.getDefault()).isEqualTo(keyStoreUtil.recordedSslContext);

assertThat(keyStoreUtil.recordedKeyManagers, arrayWithSize(1));
assertThat(keyStoreUtil.recordedKeyManagers[0], instanceOf(X509KeyManager.class));
assertThat(keyStoreUtil.recordedKeyManagers).hasSize(1);
assertThat(keyStoreUtil.recordedKeyManagers[0]).isInstanceOf(X509KeyManager.class);

X509KeyManager km = (X509KeyManager) keyStoreUtil.recordedKeyManagers[0];

assertThat(keyStoreUtil.createdKeyManagersAndKeyStores, hasSize(1));
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores.get(0).manager(), is(km));
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores.get(0).store().getType(), is("PKCS12"));
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores).hasSize(1);
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores.get(0).manager()).isEqualTo(km);
assertThat(keyStoreUtil.createdKeyManagersAndKeyStores.get(0).store().getType()).isEqualTo("PKCS12");

assertThat(km.getPrivateKey("test.key"), not(nullValue()));
assertThat(km.getPrivateKey("test.key")).isNotNull();
}

@Test
Expand All @@ -273,7 +267,7 @@ public void optInSystemPropertyNotSet() throws Exception {

keyStoreUtil.setUpSslContext();

assertThat(SSLContext.getDefault(), is(previousSslContext));
assertThat(SSLContext.getDefault()).isEqualTo(previousSslContext);
}

private String copyResourceToTempDirAndGetPath(String resourceName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package org.eclipse.core.tests.internal.runtime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.File;
import java.io.PrintWriter;
Expand All @@ -41,11 +41,11 @@ static class TestException extends Exception {

private void assertStatusEqual(String msg, IStatus[] expected, IStatus[] actual) {
if (expected == null) {
assertNull(msg + " expected null but got: " + Arrays.toString(actual), actual);
assertNull(actual, msg + " expected null but got: " + Arrays.toString(actual));
return;
}
if (actual == null) {
assertNull(msg + " expected " + Arrays.toString(expected) + " but got null", expected);
assertNull(expected, msg + " expected " + Arrays.toString(expected) + " but got null");
}
assertThat(actual).as(msg + " number of statuses").hasSameSizeAs(expected);
for (int i = 0, imax = expected.length; i < imax; i++) {
Expand All @@ -54,24 +54,24 @@ private void assertStatusEqual(String msg, IStatus[] expected, IStatus[] actual)
}

private void assertStatusEquals(String msg, IStatus expected, IStatus actual) {
assertEquals(msg + " severity", expected.getSeverity(), actual.getSeverity());
assertEquals(msg + " plugin-id", expected.getPlugin(), actual.getPlugin());
assertEquals(msg + " code", expected.getCode(), actual.getCode());
assertEquals(msg + " message", expected.getMessage(), actual.getMessage());
assertEquals(expected.getSeverity(), actual.getSeverity(), msg + " severity");
assertEquals(expected.getPlugin(), actual.getPlugin(), msg + " plugin-id");
assertEquals(expected.getCode(), actual.getCode(), msg + " code");
assertEquals(expected.getMessage(), actual.getMessage(), msg + " message");
assertExceptionEquals(msg + " exception", expected.getException(), actual.getException());
assertStatusEqual(msg + " children", expected.getChildren(), actual.getChildren());
}

private void assertExceptionEquals(String msg, Throwable expected, Throwable actual) {
if (expected == null) {
assertNull(msg + " expected null but got: " + actual, actual);
assertNull(actual, msg + " expected null but got: " + actual);
return;
}
if (actual == null) {
assertNull(msg + " expected " + expected + " but got null", expected);
assertNull(expected, msg + " expected " + expected + " but got null");
}
assertEquals(msg + " stack trace", encodeStackTrace(expected), encodeStackTrace(actual));
assertEquals(msg + " message", expected.getMessage(), actual.getMessage());
assertEquals(encodeStackTrace(expected), encodeStackTrace(actual), msg + " stack trace");
assertEquals(expected.getMessage(), actual.getMessage(), msg + " message");
}

protected String encodeStackTrace(Throwable t) {
Expand Down
Loading
Loading