diff options
author | Darius Jokilehto <dariusjokilehto+os@gmail.com> | 2022-02-04 16:13:27 +0000 |
---|---|---|
committer | Darius Jokilehto <dariusjokilehto+os@gmail.com> | 2022-02-08 09:52:03 +0000 |
commit | 78c9b9260a5287d09c87b407e396021590714513 (patch) | |
tree | fc6a40d2ac048c356795cba27adb77effa2fd260 /org.eclipse.jgit.test | |
parent | 2cc0009737182822a82731f523c0f6ded7601ddb (diff) | |
download | jgit-78c9b9260a5287d09c87b407e396021590714513.tar.gz jgit-78c9b9260a5287d09c87b407e396021590714513.zip |
Stop initCause throwing in readAdvertisedRefs
BasePackConnection::readAdvertisedRefsImpl was creating an exception by
calling `noRepository`, and then blindly calling `initCause` on it. As
`noRepository` can be overridden, it's not guaranteed to be missing a
cause.
BasePackPushConnection overrides `noRepository` and initiates a fetch,
which may throw a `NoRemoteRepositoryException` with a cause.
In this case calling `initCause` threw an `IllegalStateException`.
In order to throw the correct exception, we now return the
BasePackPushConnection exception and suppress the one thrown by
BasePackConnection
Bug: 578511
Change-Id: Ic1018b214be1e83d895979ee6c7cbce3f6765f6f
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java | 24 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackPushConnectionTest.java | 96 |
2 files changed, 119 insertions, 1 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java index 7d438c1dd8..505f334905 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java @@ -15,11 +15,15 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import java.io.ByteArrayInputStream; +import java.io.EOFException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import org.eclipse.jgit.errors.NoRemoteRepositoryException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectIdRef; @@ -30,6 +34,16 @@ import org.junit.Test; public class BasePackConnectionTest { @Test + public void testReadAdvertisedRefsShouldThrowExceptionWithOriginalCause() { + try (FailingBasePackConnection basePackConnection = + new FailingBasePackConnection()) { + Exception result = assertThrows(NoRemoteRepositoryException.class, + basePackConnection::readAdvertisedRefs); + assertEquals(EOFException.class, result.getCause().getClass()); + } + } + + @Test public void testUpdateWithSymRefsAdds() { final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/main", ObjectId.fromString( @@ -244,4 +258,12 @@ public class BasePackConnectionTest { assertEquals(oidName, headRef.getObjectId().name()); assertEquals(oidName, mainRef.getObjectId().name()); } -}
\ No newline at end of file + + private static class FailingBasePackConnection extends BasePackConnection { + FailingBasePackConnection() { + super(new TransportLocal(new URIish(), + new java.io.File(""))); + pckIn = new PacketLineIn(new ByteArrayInputStream(new byte[0])); + } + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackPushConnectionTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackPushConnectionTest.java new file mode 100644 index 0000000000..cf8c5ffe79 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackPushConnectionTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2022 Darius Jokilehto <darius.jokilehto+os@gmail.com> + * Copyright (c) 2022 Antonio Barone <syntonyze@gmail.com> + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.transport; + +import org.eclipse.jgit.errors.NoRemoteRepositoryException; +import org.eclipse.jgit.errors.TransportException; +import org.eclipse.jgit.internal.JGitText; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.IOException; +import java.util.Arrays; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class BasePackPushConnectionTest { + @Test + public void testNoRemoteRepository() { + NoRemoteRepositoryException openFetchException = + new NoRemoteRepositoryException( new URIish(), "not found"); + IOException ioException = new IOException("not read"); + + try (FailingBasePackPushConnection fbppc = + new FailingBasePackPushConnection(openFetchException)) { + TransportException result = fbppc.noRepository(ioException); + + assertEquals(openFetchException, result); + assertThat(Arrays.asList(result.getSuppressed()), + hasItem(ioException)); + } + } + + @Test + public void testPushNotPermitted() { + URIish uri = new URIish(); + TransportException openFetchException = new TransportException(uri, + "a transport exception"); + IOException ioException = new IOException("not read"); + + try (FailingBasePackPushConnection fbppc = + new FailingBasePackPushConnection(openFetchException)) { + TransportException result = fbppc.noRepository(ioException); + + assertEquals(TransportException.class, result.getClass()); + assertThat(result.getMessage(), + endsWith(JGitText.get().pushNotPermitted)); + assertEquals(openFetchException, result.getCause()); + assertThat(Arrays.asList(result.getSuppressed()), + hasItem(ioException)); + } + } + + @Test + public void testReadAdvertisedRefPropagatesCauseAndSuppressedExceptions() { + IOException ioException = new IOException("not read"); + try (FailingBasePackPushConnection basePackConnection = + new FailingBasePackPushConnection( + new NoRemoteRepositoryException( + new URIish(), "not found", ioException))) { + Exception result = assertThrows(NoRemoteRepositoryException.class, + basePackConnection::readAdvertisedRefs); + assertEquals(ioException, result.getCause()); + assertThat(Arrays.asList(result.getSuppressed()), + hasItem(instanceOf(EOFException.class))); + } + } + + private static class FailingBasePackPushConnection + extends BasePackPushConnection { + FailingBasePackPushConnection(TransportException openFetchException) { + super(new TransportLocal(new URIish(), + new java.io.File("")) { + @Override public FetchConnection openFetch() + throws TransportException { + throw openFetchException; + } + }); + pckIn = new PacketLineIn(new ByteArrayInputStream(new byte[0])); + } + } +} |