From 52100c9123192a8de5867e593e73f0f8de17510c Mon Sep 17 00:00:00 2001 From: scarreras Date: Tue, 17 Nov 2026 17:52:44 -0300 Subject: [PATCH 1/5] Add regression tests for known defects, tagged known-defect Tests only; no production code changes. Adds 21 test classes covering 56 findings. Tests for defects that are still present are tagged @Category(KnownDefect.class). The default `test` task (and so `build` and CI) excludes that tag, so the build stays green. `./gradlew knownDefectTests` runs only the tagged tests and is expected to fail until each defect is fixed; remove the tag when a fix lands so the test joins the normal suite. Tests that pass today (fixed defects, guards, already-correct cases) run in the default suite and protect the current behaviour. Repository-health checks skip instead of failing when the tests are not run from a full checkout. Build change: buildSrc/src/main/kotlin/b2sdk.gradle.kts excludes the category from `test` and registers `knownDefectTests` for modules that contain the marker interface com.backblaze.b2.testing.KnownDefect. Co-Authored-By: Claude Sonnet 5.5 --- buildSrc/src/main/kotlin/b2sdk.gradle.kts | 23 ++ ...AccountAuthorizationCacheFindingsTest.java | 66 ++++++ ...2CancellableContentSourceFindingsTest.java | 47 ++++ .../client/B2LargeFileStorerFindingsTest.java | 210 +++++++++++++++++ .../B2LargeFileUploaderFindingsTest.java | 91 ++++++++ .../client/B2ListIterablesFindingsTest.java | 144 ++++++++++++ .../b2/client/B2RetryAfterFindingsTest.java | 71 ++++++ .../b2/client/B2RetryerFindingsTest.java | 53 +++++ .../B2StorageClientImplFindingsTest.java | 154 +++++++++++++ .../b2/client/B2WebifierUrlFindingsTest.java | 40 ++++ .../B2ContentFileWriterFindingsTest.java | 88 +++++++ .../contentSources/B2HeadersFindingsTest.java | 36 +++ ...2ApplicationKeyCapabilityFindingsTest.java | 22 ++ .../B2EventNotificationRuleFindingsTest.java | 30 +++ .../B2StructuresSecurityFindingsTest.java | 58 +++++ .../backblaze/b2/json/B2JsonFindingsTest.java | 83 +++++++ .../b2/repo/RepoHealthFindingsTest.java | 156 +++++++++++++ .../com/backblaze/b2/testing/KnownDefect.java | 17 ++ ...ncFileOnCloseOutputStreamFindingsTest.java | 53 +++++ .../backblaze/b2/util/B2UtilFindingsTest.java | 85 +++++++ .../B2WebApiHttpClientFindingsTest.java | 215 ++++++++++++++++++ .../HttpClientFactoryFindingsTest.java | 51 +++++ .../com/backblaze/b2/testing/KnownDefect.java | 17 ++ .../b2/sample/SampleFindingsTest.java | 150 ++++++++++++ .../com/backblaze/b2/testing/KnownDefect.java | 17 ++ 25 files changed, 1977 insertions(+) create mode 100644 core/src/test/java/com/backblaze/b2/client/B2AccountAuthorizationCacheFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2CancellableContentSourceFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2LargeFileUploaderFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2RetryAfterFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/contentHandlers/B2ContentFileWriterFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/contentSources/B2HeadersFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/structures/B2ApplicationKeyCapabilityFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/structures/B2EventNotificationRuleFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/client/structures/B2StructuresSecurityFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/repo/RepoHealthFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/testing/KnownDefect.java create mode 100644 core/src/test/java/com/backblaze/b2/util/B2FlushAndSyncFileOnCloseOutputStreamFindingsTest.java create mode 100644 core/src/test/java/com/backblaze/b2/util/B2UtilFindingsTest.java create mode 100644 httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java create mode 100644 httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java create mode 100644 httpclient/src/test/java/com/backblaze/b2/testing/KnownDefect.java create mode 100644 samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java create mode 100644 samples/src/test/java/com/backblaze/b2/testing/KnownDefect.java diff --git a/buildSrc/src/main/kotlin/b2sdk.gradle.kts b/buildSrc/src/main/kotlin/b2sdk.gradle.kts index d791bb93f..f9cf422b7 100644 --- a/buildSrc/src/main/kotlin/b2sdk.gradle.kts +++ b/buildSrc/src/main/kotlin/b2sdk.gradle.kts @@ -39,6 +39,29 @@ dependencies { testImplementation("org.mockito:mockito-core:1.9.5") } +// Tests that document a defect that is still present carry @Category(KnownDefect.class). The default +// `test` task (and so `build` and CI) excludes them; `./gradlew knownDefectTests` runs only them and is +// expected to fail until the defects are fixed. Only modules that have the marker interface take part. +val knownDefectMarker = "com.backblaze.b2.testing.KnownDefect" +if (file("src/test/java/com/backblaze/b2/testing/KnownDefect.java").exists()) { + tasks.test { + useJUnit { + excludeCategories(knownDefectMarker) + } + } + + tasks.register("knownDefectTests") { + group = "verification" + description = "Runs only the tests tagged KnownDefect; they fail until the defect they document is fixed." + testClassesDirs = sourceSets["test"].output.classesDirs + classpath = sourceSets["test"].runtimeClasspath + useJUnit { + includeCategories(knownDefectMarker) + } + shouldRunAfter(tasks.test) + } +} + tasks.jar { manifest { attributes( diff --git a/core/src/test/java/com/backblaze/b2/client/B2AccountAuthorizationCacheFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2AccountAuthorizationCacheFindingsTest.java new file mode 100644 index 000000000..e18aa5ce3 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2AccountAuthorizationCacheFindingsTest.java @@ -0,0 +1,66 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2LocalException; +import com.backblaze.b2.client.structures.B2AccountAuthorization; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import static com.backblaze.b2.client.B2TestHelpers.makeAuthWithAccountId; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class B2AccountAuthorizationCacheFindingsTest extends B2BaseTest { + private final B2StorageClientWebifier webifier = mock(B2StorageClientWebifier.class); + private final B2AccountAuthorizer authorizer = mock(B2AccountAuthorizer.class); + private final B2AccountAuthorizationCache cache = new B2AccountAuthorizationCache(webifier, authorizer); + + private void authorizeThenSwitchToOtherAccount() throws B2Exception { + doReturn(makeAuthWithAccountId("accountId", 1)).when(authorizer).authorize(webifier); + cache.get(); + cache.clear(); + doReturn(makeAuthWithAccountId("anotherAccountId", 2)).when(authorizer).authorize(webifier); + } + + /** Finding C1 (and C2: the existing test never made a second get() call). */ + @Category(KnownDefect.class) + @Test + public void testFindingC1_C2_mismatchedAccountAuthIsNotStoredAndServedOnNextGet() throws B2Exception { + authorizeThenSwitchToOtherAccount(); + try { + cache.get(); + fail("first get() should throw because the account id changed"); + } catch (B2LocalException expected) { + // expected + } + + // the second get() must not hand out the login of the other account. + try { + final B2AccountAuthorization second = cache.get(); + fail("second get() returned the authorization of a different account: " + second.getAccountId()); + } catch (B2LocalException expected) { + // expected + } + } + + /** Finding C1: the error text runs two words together ("...Idbut previously..."). */ + @Category(KnownDefect.class) + @Test + public void testFindingC1_mismatchMessageHasSpaceBeforeBut() throws B2Exception { + authorizeThenSwitchToOtherAccount(); + try { + cache.get(); + fail("should have thrown"); + } catch (B2LocalException e) { + assertTrue(e.getMessage(), e.getMessage().contains("anotherAccountId but previously")); + } + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2CancellableContentSourceFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2CancellableContentSourceFindingsTest.java new file mode 100644 index 000000000..7c29b3973 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2CancellableContentSourceFindingsTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.contentSources.B2ContentSource; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class B2CancellableContentSourceFindingsTest extends B2BaseTest { + /** Finding C35: closing the stream after a cancel may complain, but the wrapped stream must still be closed. */ + @Category(KnownDefect.class) + @Test + public void testFindingC35_wrappedStreamClosedEvenAfterCancel() throws Exception { + final boolean[] closed = {false}; + final InputStream tracked = new ByteArrayInputStream(new byte[10]) { + @Override + public void close() throws IOException { + closed[0] = true; + super.close(); + } + }; + final B2ContentSource wrapped = mock(B2ContentSource.class); + when(wrapped.createInputStream()).thenReturn(tracked); + final B2CancellationToken token = new B2CancellationToken(); + final InputStream in = new B2CancellableContentSource(wrapped, token).createInputStream(); + + token.cancel(); + try { + in.close(); + } catch (IOException ignoredForThisFinding) { + // whether close() reports the cancellation is not what this finding is about. + } + assertTrue("the wrapped stream was left open", closed[0]); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java new file mode 100644 index 000000000..28fa24448 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java @@ -0,0 +1,210 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.contentSources.B2ContentSource; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2InternalErrorException; +import com.backblaze.b2.client.exceptions.B2LocalException; +import com.backblaze.b2.client.structures.B2Part; +import com.backblaze.b2.client.structures.B2StoreLargeFileRequest; +import com.backblaze.b2.client.structures.B2UploadListener; +import com.backblaze.b2.client.structures.B2UploadProgress; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.After; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicReference; + +import static com.backblaze.b2.client.B2TestHelpers.fileId; +import static com.backblaze.b2.client.B2TestHelpers.makeMd5; +import static com.backblaze.b2.client.B2TestHelpers.makeSha1; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +public class B2LargeFileStorerFindingsTest extends B2BaseTest { + private final B2AccountAuthorizationCache authCache = mock(B2AccountAuthorizationCache.class); + private final B2StorageClientWebifier webifier = mock(B2StorageClientWebifier.class); + private final B2Retryer retryer = new B2Retryer(mock(B2Sleeper.class)); + private final ExecutorService pool = Executors.newFixedThreadPool(4); + + @After + public void tearDown() { + Thread.interrupted(); // never leak an interrupt flag into other tests + pool.shutdownNow(); + } + + /** A part storer we fully control. */ + private static class FakePartStorer implements B2PartStorer { + private final int partNumber; + private final long size; + private final CountDownLatch release; + private final boolean failImmediately; + final AtomicReference token = new AtomicReference<>(); + + FakePartStorer(int partNumber, long size, CountDownLatch release, boolean failImmediately) { + this.partNumber = partNumber; + this.size = size; + this.release = release; + this.failImmediately = failImmediately; + } + + @Override + public int getPartNumber() { + return partNumber; + } + + @Override + public long getPartSizeOrThrow() { + return size; + } + + @Override + public B2Part storePart(B2LargeFileStorer storer, B2UploadListener listener, B2CancellationToken cancellationToken) + throws IOException, B2Exception { + token.set(cancellationToken); + if (failImmediately) { + throw new B2InternalErrorException("simulated part failure"); + } + try { + release.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new B2LocalException("interrupted", "interrupted"); + } + return new B2Part(fileId(1), partNumber, size, makeSha1(partNumber), makeMd5(partNumber), 1, null); + } + } + + private B2LargeFileStorer storerFor(List parts) { + return new B2LargeFileStorer( + B2StoreLargeFileRequest.builder(fileId(9)).build(), + parts, + authCache, + webifier, + retryer, + B2DefaultRetryPolicy.supplier(), + pool, + false); + } + + /** Finding C6: an interrupt of the calling thread must be restored (flag set again) when storeFile reports failure. */ + @Category(KnownDefect.class) + @Test + public void testFindingC6_interruptFlagRestoredWhenStoreFileInterrupted() throws Exception { + final CountDownLatch never = new CountDownLatch(1); + final B2LargeFileStorer storer = storerFor(Arrays.asList( + new FakePartStorer(1, 10, never, false), + new FakePartStorer(2, 10, never, false))); + Thread.currentThread().interrupt(); + try { + storer.storeParts(null); + fail("expected a failure because the thread is interrupted"); + } catch (B2Exception expected) { + // expected + } + final boolean flagRestored = Thread.interrupted(); + never.countDown(); + assertTrue("interrupt flag was swallowed by storeParts", flagRestored); + } + + /** Finding C6: after the caller is interrupted, the part uploads in progress must be told to stop. */ + @Category(KnownDefect.class) + @Test + public void testFindingC6_partUploadsAreCancelledWhenCallerInterrupted() throws Exception { + final CountDownLatch never = new CountDownLatch(1); + final B2LargeFileStorer storer2 = storerFor(Arrays.asList( + new FakePartStorer(1, 10, never, false), new FakePartStorer(2, 10, never, false))); + final FakePartStorer watched = (FakePartStorer) storer2.getPartStorers().get(0); + Thread.currentThread().interrupt(); + try { + storer2.storeParts(null); + } catch (B2Exception expected) { + // expected + } + Thread.interrupted(); + final long deadline2 = System.currentTimeMillis() + 2000; + while ((watched.token.get() == null || !watched.token.get().isCancelled()) && System.currentTimeMillis() < deadline2) { + Thread.sleep(5); + } + final boolean cancelled = watched.token.get() != null && watched.token.get().isCancelled(); + never.countDown(); + assertTrue("part uploads were not cancelled after the caller was interrupted", cancelled); + } + + /** Finding C7: the exception raised for an interrupt must carry the InterruptedException as its cause. */ + @Category(KnownDefect.class) + @Test + public void testFindingC7_interruptedExceptionIsTheCause() throws Exception { + final CountDownLatch never = new CountDownLatch(1); + final B2LargeFileStorer storer = storerFor(Arrays.asList(new FakePartStorer(1, 10, never, false))); + Thread.currentThread().interrupt(); + try { + storer.storeParts(null); + fail("expected a failure"); + } catch (B2Exception e) { + never.countDown(); + Thread.interrupted(); + assertTrue("cause was: " + e.getCause(), e.getCause() instanceof InterruptedException); + } + } + + /** Finding C8: when one part fails, the caller must see the error without waiting for the other parts to finish. */ + @Category(KnownDefect.class) + @Test + public void testFindingC8_firstPartFailureFailsFastWithoutWaitingForOtherParts() throws Exception { + final CountDownLatch release = new CountDownLatch(1); + final B2LargeFileStorer storer = storerFor(Arrays.asList( + new FakePartStorer(1, 10, release, true), + new FakePartStorer(2, 10, release, false), + new FakePartStorer(3, 10, release, false))); + final CompletableFuture> future = storer.storePartsAsync(null); + try { + future.get(2, TimeUnit.SECONDS); + fail("expected the failure of part 1"); + } catch (TimeoutException e) { + fail("the failure of part 1 was not reported while parts 2 and 3 were still running"); + } catch (ExecutionException expected) { + assertTrue(expected.getCause() instanceof B2InternalErrorException); + } finally { + release.countDown(); + } + } + + private B2ContentSource sizedSource(long size) throws IOException { + final B2ContentSource s = mock(B2ContentSource.class); + org.mockito.Mockito.when(s.getContentLength()).thenReturn(size); + return s; + } + + /** Finding C22: start bytes must follow part-number order even when the caller lists the parts out of order. */ + @Category(KnownDefect.class) + @Test + public void testFindingC22_startBytesComputedInPartNumberOrder() throws Exception { + final List outOfOrder = new ArrayList<>(Arrays.asList( + new B2UploadingPartStorer(2, sizedSource(500)), + new B2UploadingPartStorer(1, sizedSource(100)))); + final B2LargeFileStorer storer = storerFor(outOfOrder); + assertEquals(0L, storer.getStartByteOrUnknown(1)); + assertEquals(100L, storer.getStartByteOrUnknown(2)); + assertTrue(B2UploadProgress.UNKNOWN_PART_START_BYTE != storer.getStartByteOrUnknown(2)); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2LargeFileUploaderFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2LargeFileUploaderFindingsTest.java new file mode 100644 index 000000000..432eb0c19 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2LargeFileUploaderFindingsTest.java @@ -0,0 +1,91 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.contentSources.B2ContentSource; +import com.backblaze.b2.client.contentSources.B2ContentTypes; +import com.backblaze.b2.client.contentSources.B2Headers; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2InternalErrorException; +import com.backblaze.b2.client.structures.B2AccountAuthorization; +import com.backblaze.b2.client.structures.B2FileVersion; +import com.backblaze.b2.client.structures.B2UploadFileRequest; +import com.backblaze.b2.util.B2BaseTest; +import com.backblaze.b2.util.B2Collections; +import org.junit.After; +import org.junit.Test; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static com.backblaze.b2.client.B2TestHelpers.bucketId; +import static com.backblaze.b2.client.B2TestHelpers.fileId; +import static com.backblaze.b2.client.B2TestHelpers.fileName; +import static com.backblaze.b2.client.B2TestHelpers.makeSha1; +import static com.backblaze.b2.client.B2TestHelpers.makeVersion; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class B2LargeFileUploaderFindingsTest extends B2BaseTest { + private final B2StorageClientWebifier webifier = mock(B2StorageClientWebifier.class); + private final B2AccountAuthorizationCache authCache = mock(B2AccountAuthorizationCache.class); + private final B2AccountAuthorization auth = B2TestHelpers.makeAuth(1); + private final B2ContentSource source = mock(B2ContentSource.class); + private final ExecutorService pool = Executors.newFixedThreadPool(2); + + @After + public void tearDown() { + pool.shutdownNow(); + } + + /** Finding C34: resuming a large file that was started with a known SHA-1 must not be refused as a "fileInfo mismatch". */ + @Category(KnownDefect.class) + @Test + public void testFindingC34_resumeOfFileStartedWithKnownSha1IsAccepted() throws Exception { + final String sha1 = makeSha1(7); + when(source.getContentLength()).thenReturn(2000L); + when(source.getSha1OrNull()).thenReturn(sha1); + final B2UploadFileRequest request = B2UploadFileRequest + .builder(bucketId(1), fileName(2), B2ContentTypes.APPLICATION_OCTET, source) + .build(); + final B2FileVersion started = new B2FileVersion( + fileId(1), fileName(2), 0, B2ContentTypes.APPLICATION_OCTET, null, null, + B2Collections.mapOf(B2Headers.LARGE_FILE_SHA1_INFO_NAME, sha1), + "start", 1234, null, null, null, null); + try { + B2LargeFileUploader.throwIfLargeFileVersionDoesntSeemToMatchRequest(started, 2000L, request); + } catch (B2Exception e) { + fail("resume was refused although the content matches: " + e.getMessage()); + } + } + + /** Finding C9: when a part upload fails after the large file was started, the error must identify the file so it can be resumed or cancelled. */ + @Category(KnownDefect.class) + @Test + public void testFindingC9_failureAfterStartNamesTheLargeFileId() throws Exception { + when(source.getContentLength()).thenReturn(2 * auth.getRecommendedPartSize()); + final B2FileVersion started = makeVersion(1, 2); + when(webifier.startLargeFile(anyObject(), anyObject())).thenReturn(started); + when(webifier.getUploadPartUrl(anyObject(), anyObject())).thenThrow(new B2InternalErrorException("test", "part trouble")); + final B2UploadFileRequest request = B2UploadFileRequest + .builder(bucketId(1), fileName(2), B2ContentTypes.APPLICATION_OCTET, source) + .build(); + final B2LargeFileUploader uploader = new B2LargeFileUploader( + new B2Retryer(mock(B2Sleeper.class)), webifier, authCache, B2DefaultRetryPolicy.supplier(), pool, + B2PartSizes.from(auth), request, 2 * auth.getRecommendedPartSize()); + try { + uploader.uploadLargeFile(); + fail("expected failure"); + } catch (B2Exception e) { + assertTrue("message does not contain the large file id " + started.getFileId() + ": " + e.getMessage(), + e.getMessage().contains(started.getFileId())); + } + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java new file mode 100644 index 000000000..005d2bcef --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java @@ -0,0 +1,144 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2InternalErrorException; +import com.backblaze.b2.client.exceptions.B2RuntimeException; +import com.backblaze.b2.client.structures.B2ApplicationKey; +import com.backblaze.b2.client.structures.B2FileVersion; +import com.backblaze.b2.client.structures.B2ListFileNamesRequest; +import com.backblaze.b2.client.structures.B2ListFileNamesResponse; +import com.backblaze.b2.client.structures.B2ListKeysRequest; +import com.backblaze.b2.client.structures.B2ListKeysResponse; +import com.backblaze.b2.client.structures.B2ListPartsRequest; +import com.backblaze.b2.client.structures.B2ListPartsResponse; +import com.backblaze.b2.client.structures.B2Part; +import com.backblaze.b2.util.B2BaseTest; +import com.backblaze.b2.util.B2Collections; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.TreeSet; + +import static com.backblaze.b2.client.B2TestHelpers.SAMPLE_MD5; +import static com.backblaze.b2.client.B2TestHelpers.SAMPLE_SHA1; +import static com.backblaze.b2.client.B2TestHelpers.bucketId; +import static com.backblaze.b2.client.B2TestHelpers.fileId; +import static com.backblaze.b2.client.B2TestHelpers.makeVersion; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class B2ListIterablesFindingsTest extends B2BaseTest { + private final B2StorageClientImpl client = mock(B2StorageClientImpl.class); + + private static B2ApplicationKey key(String id) { + return new B2ApplicationKey("accountId", id, "name-" + id, new TreeSet<>(), "bucketId", + "namePrefix", 12345678L, Collections.emptySet()); + } + + private static B2Part part(int i) { + return new B2Part(fileId(1), i, i * 1000L, SAMPLE_SHA1, SAMPLE_MD5, i, null); + } + + /** Finding C14 (keys): an empty page that is not the last must not end the listing. */ + @Category(KnownDefect.class) + @Test + public void testFindingC14_keysIteratorContinuesPastEmptyMiddlePage() throws B2Exception { + final B2ListKeysRequest r1 = B2ListKeysRequest.builder().setMaxKeyCount(10).build(); + final B2ListKeysRequest r2 = B2ListKeysRequest.builder().setMaxKeyCount(10).setStartApplicationKeyId("id5").build(); + final B2ListKeysRequest r3 = B2ListKeysRequest.builder().setMaxKeyCount(10).setStartApplicationKeyId("id6").build(); + when(client.listKeys(eq(r1))).thenReturn(new B2ListKeysResponse(B2Collections.listOf(key("id1")), "id5")); + when(client.listKeys(eq(r2))).thenReturn(new B2ListKeysResponse(new ArrayList(), "id6")); + when(client.listKeys(eq(r3))).thenReturn(new B2ListKeysResponse(B2Collections.listOf(key("id6")), null)); + + final List seen = new ArrayList<>(); + final Iterator iter = new B2ListKeysIterator(client, r1); + while (iter.hasNext()) { + seen.add(iter.next().getApplicationKeyId()); + } + assertEquals(B2Collections.listOf("id1", "id6"), seen); + } + + /** Finding C14 (keys): next() on an exhausted iterator must throw NoSuchElementException per the Iterator contract. */ + @Category(KnownDefect.class) + @Test + public void testFindingC14_keysIteratorNextWhenExhaustedThrowsNoSuchElement() throws B2Exception { + final B2ListKeysRequest r1 = B2ListKeysRequest.builder().build(); + when(client.listKeys(eq(r1))).thenReturn(new B2ListKeysResponse(new ArrayList(), null)); + final Iterator iter = new B2ListKeysIterator(client, r1); + try { + iter.next(); + fail("expected an exception"); + } catch (NoSuchElementException expected) { + // expected + } catch (RuntimeException other) { + fail("wrong exception type: " + other); + } + } + + /** Finding C14 (parts): an empty page that is not the last must not end the listing. */ + @Category(KnownDefect.class) + @Test + public void testFindingC14_partsIteratorContinuesPastEmptyMiddlePage() throws B2Exception { + final B2ListPartsRequest request = B2ListPartsRequest.builder(fileId(1)).build(); + when(client.listParts(anyObject())).thenReturn( + new B2ListPartsResponse(B2Collections.listOf(part(1)), 2), + new B2ListPartsResponse(new ArrayList(), 3), + new B2ListPartsResponse(B2Collections.listOf(part(3)), null)); + + final List seen = new ArrayList<>(); + final Iterator iter = new B2ListPartsIterableImpl(client, request).iterator(); + while (iter.hasNext()) { + seen.add(iter.next().getPartNumber()); + } + assertEquals(B2Collections.listOf(1, 3), seen); + } + + /** + * Finding C13: when loading the next page fails, the item already in hand (the last one on the + * current page) must still be returned; the failure should surface on the following hasNext(). + */ + @Category(KnownDefect.class) + @Test + public void testFindingC13_lastItemOfPageIsNotLostWhenNextPageFails() throws B2Exception { + final B2ListFileNamesRequest first = B2ListFileNamesRequest.builder(bucketId(1)).build(); + final List page = B2Collections.listOf(makeVersion(1, 1), makeVersion(2, 1)); + when(client.listFileNames(first)).thenReturn(new B2ListFileNamesResponse(page, "file/next")); + when(client.listFileNames(B2ListFileNamesRequest.builder(bucketId(1)).setStartFileName("file/next").build())) + .thenThrow(new B2InternalErrorException("test", "testing")); + + final Iterator iter = new B2ListFileNamesIterable(client, first).iterator(); + assertTrue(iter.hasNext()); + assertSame(page.get(0), iter.next()); + assertTrue(iter.hasNext()); + final B2FileVersion last; + try { + last = iter.next(); + } catch (B2RuntimeException e) { + fail("the last item of the page was lost: next() threw " + e.getMessage()); + return; + } + assertSame(page.get(1), last); + try { + iter.hasNext(); + fail("the page-load failure should surface from hasNext()"); + } catch (B2RuntimeException expected) { + assertTrue(expected.getMessage().contains("failed to advance iterator")); + } + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2RetryAfterFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2RetryAfterFindingsTest.java new file mode 100644 index 000000000..5e7be7469 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2RetryAfterFindingsTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2ServiceUnavailableException; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.After; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +public class B2RetryAfterFindingsTest extends B2BaseTest { + @After + public void clearFlag() { + Thread.interrupted(); + } + + private String runWithRetryAfter(Integer retryAfter) throws B2Exception { + final AtomicInteger calls = new AtomicInteger(); + return new B2Retryer(new B2Sleeper()).doRetry("op", mock(B2AccountAuthorizationCache.class), (isRetry) -> { + if (calls.getAndIncrement() == 0) { + throw new B2ServiceUnavailableException("busy", retryAfter, "try later"); + } + return "ok"; + }, B2DefaultRetryPolicy.supplier().get()); + } + + /** Finding C11: a negative Retry-After must not escape the retry loop as a raw IllegalArgumentException. */ + @Category(KnownDefect.class) + @Test + public void testFindingC11_negativeRetryAfterDoesNotThrowRawError() throws B2Exception { + try { + assertEquals("ok", runWithRetryAfter(-5)); + } catch (IllegalArgumentException e) { + fail("raw error escaped the retry loop: " + e); + } + } + + /** + * Finding C11: a very large Retry-After (here 2.2 million seconds) overflows the millisecond conversion. + * The thread is pre-interrupted so that a correct implementation returns immediately instead of sleeping. + */ + @Category(KnownDefect.class) + @Test + public void testFindingC11_hugeRetryAfterDoesNotOverflowIntoRawError() { + Thread.currentThread().interrupt(); + try { + new B2Sleeper().sleepSeconds(2_200_000); + } catch (IllegalArgumentException e) { + fail("sleepSeconds overflowed into a raw error: " + e); + } + } + + /** Guard for C12 (judged not a bug): the default policy deliberately obeys the server's Retry-After without capping it. */ + @Test + public void testGuardC12_defaultPolicyObeysServerRetryAfterWithoutCap() { + final B2RetryPolicy policy = B2DefaultRetryPolicy.supplier().get(); + final Integer wait = policy.gotRetryableAfterDelay("op", 1, 0, + new B2ServiceUnavailableException("busy", 86400, "try later")); + assertEquals(Integer.valueOf(86400), wait); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java new file mode 100644 index 000000000..13d734b4e --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2InternalErrorException; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.After; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertTrue; + +public class B2RetryerFindingsTest extends B2BaseTest { + @After + public void clearFlag() { + Thread.interrupted(); + } + + /** + * Finding C10: when the thread's interrupt flag is set (for example after a cancelled upload task), + * the retry loop must not burn through all its attempts with no pause at all: it should stop + * retrying (or still pause). + */ + @Category(KnownDefect.class) + @Test + public void testFindingC10_interruptedThreadDoesNotRetryWithoutPausing() { + final B2Retryer retryer = new B2Retryer(new B2Sleeper()); + final AtomicInteger attempts = new AtomicInteger(); + final long start = System.nanoTime(); + Thread.currentThread().interrupt(); + try { + retryer.doRetry("op", mock(), (isRetry) -> { + attempts.incrementAndGet(); + throw new B2InternalErrorException("always failing"); + }, B2DefaultRetryPolicy.supplier().get()); + } catch (B2Exception expected) { + // expected + } + final long elapsedMillis = (System.nanoTime() - start) / 1_000_000; + assertTrue("made " + attempts.get() + " attempts in " + elapsedMillis + " ms with no pause", + attempts.get() <= 2 || elapsedMillis >= 1000); + } + + private static B2AccountAuthorizationCache mock() { + return org.mockito.Mockito.mock(B2AccountAuthorizationCache.class); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java new file mode 100644 index 000000000..df69283e2 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java @@ -0,0 +1,154 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.contentSources.B2ContentSource; +import com.backblaze.b2.client.exceptions.B2ConnectionBrokenException; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.structures.B2AccountAuthorization; +import com.backblaze.b2.client.structures.B2CopyFileRequest; +import com.backblaze.b2.client.structures.B2CreateKeyRequest; +import com.backblaze.b2.client.structures.B2DeleteKeyRequest; +import com.backblaze.b2.client.structures.B2StartLargeFileRequest; +import com.backblaze.b2.client.structures.B2UploadFileRequest; +import com.backblaze.b2.util.B2BaseTest; +import com.backblaze.b2.util.B2Clock; +import org.junit.Test; + +import java.io.IOException; +import java.util.Collections; + +import static com.backblaze.b2.client.B2TestHelpers.bucketId; +import static com.backblaze.b2.client.B2TestHelpers.fileId; +import static com.backblaze.b2.client.B2TestHelpers.fileName; +import static com.backblaze.b2.util.B2DateTimeUtil.parseDateTime; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class B2StorageClientImplFindingsTest extends B2BaseTest { + private static final B2AccountAuthorization ACCOUNT_AUTH = B2TestHelpers.makeAuth(1); + private final B2StorageClientWebifier webifier = mock(B2StorageClientWebifier.class); + private final B2ClientConfig config = B2ClientConfig.builder(ACCOUNT_AUTH.getAccountId(), "applicationKey", "FindingsTest/0.0.1").build(); + private final B2Sleeper sleeper = mock(B2Sleeper.class); + private final B2StorageClientImpl client = + new B2StorageClientImpl(webifier, config, B2DefaultRetryPolicy.supplier(), new B2Retryer(sleeper)); + + private B2ConnectionBrokenException lostReply() { + return new B2ConnectionBrokenException("broken", null, "reply lost"); + } + + private void authorize() throws B2Exception { + B2Clock.useSimulator(parseDateTime("2018-04-17 01:26:32")); + when(webifier.authorizeAccount(anyObject())).thenReturn(ACCOUNT_AUTH); + } + + /** Finding C3: create key must not be blindly retried after a lost reply (could create a duplicate key). */ + @Category(KnownDefect.class) + @Test + public void testFindingC3_createKeyNotRetried() throws B2Exception { + authorize(); + when(webifier.createKey(anyObject(), anyObject())).thenThrow(lostReply()); + try { + client.createKey(B2CreateKeyRequest.builder(Collections.singleton("listFiles"), "k").build()); + } catch (B2Exception expected) { + // expected + } + verify(webifier, times(1)).createKey(anyObject(), anyObject()); + } + + /** Finding C3: start large file must not be blindly retried. */ + @Category(KnownDefect.class) + @Test + public void testFindingC3_startLargeFileNotRetried() throws B2Exception { + authorize(); + when(webifier.startLargeFile(anyObject(), anyObject())).thenThrow(lostReply()); + try { + client.startLargeFile(B2StartLargeFileRequest.builder(bucketId(1), fileName(1), "text/plain").build()); + } catch (B2Exception expected) { + // expected + } + verify(webifier, times(1)).startLargeFile(anyObject(), anyObject()); + } + + /** Finding C3: create bucket must not be blindly retried. */ + @Category(KnownDefect.class) + @Test + public void testFindingC3_createBucketNotRetried() throws B2Exception { + authorize(); + when(webifier.createBucket(anyObject(), anyObject())).thenThrow(lostReply()); + try { + client.createBucket("bucket1", "allPublic"); + } catch (B2Exception expected) { + // expected + } + verify(webifier, times(1)).createBucket(anyObject(), anyObject()); + } + + /** Finding C3: delete key must not be blindly retried (a retry would report a false "not found"). */ + @Category(KnownDefect.class) + @Test + public void testFindingC3_deleteKeyNotRetried() throws B2Exception { + authorize(); + when(webifier.deleteKey(anyObject(), anyObject())).thenThrow(lostReply()); + try { + client.deleteKey(B2DeleteKeyRequest.builder("keyId").build()); + } catch (B2Exception expected) { + // expected + } + verify(webifier, times(1)).deleteKey(anyObject(), anyObject()); + } + + /** Finding C3: copy file must not be blindly retried (could create a duplicate version). */ + @Category(KnownDefect.class) + @Test + public void testFindingC3_copyFileNotRetried() throws B2Exception { + authorize(); + when(webifier.copyFile(anyObject(), anyObject())).thenThrow(lostReply()); + try { + client.copySmallFile(B2CopyFileRequest.builder(fileId(1), fileName(2)).build()); + } catch (B2Exception expected) { + // expected + } + verify(webifier, times(1)).copyFile(anyObject(), anyObject()); + } + + /** Finding C16: after close(), calls must be refused cleanly without reaching the (closed) HTTP layer. */ + @Category(KnownDefect.class) + @Test + public void testFindingC16_callAfterCloseIsRefusedCleanly() throws B2Exception { + authorize(); + client.close(); + try { + client.listBuckets(com.backblaze.b2.client.structures.B2ListBucketsRequest.builder("accountId").build()); + fail("a call after close() should be refused"); + } catch (B2Exception | IllegalStateException expected) { + // a clean refusal is expected. + } + verify(webifier, never()).listBuckets(anyObject(), anyObject()); + } + + /** Finding C37: a large upload must send the source's last-modified time just like a small upload does. */ + @Category(KnownDefect.class) + @Test + public void testFindingC37_startLargeFileCarriesSrcLastModifiedMillis() throws Exception { + final B2ContentSource source = mock(B2ContentSource.class); + when(source.getContentLength()).thenReturn(10L); + when(source.getSha1OrNull()).thenReturn(null); + when(source.getSrcLastModifiedMillisOrNull()).thenReturn(1234567L); + final B2UploadFileRequest upload = B2UploadFileRequest + .builder(bucketId(1), fileName(1), "text/plain", source) + .build(); + final B2StartLargeFileRequest start = B2StartLargeFileRequest.buildFrom(upload); + assertEquals("1234567", start.getFileInfo().get("src_last_modified_millis")); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java new file mode 100644 index 000000000..c9c6d96e0 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.structures.B2DownloadByIdRequest; +import com.backblaze.b2.client.structures.B2DownloadByNameRequest; +import com.backblaze.b2.client.webApiClients.B2WebApiClient; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; + +public class B2WebifierUrlFindingsTest extends B2BaseTest { + private final B2StorageClientWebifierImpl webifier = new B2StorageClientWebifierImpl( + mock(B2WebApiClient.class), "agent/1.0", "https://api.testb2.com", null); + + /** Finding C24: an "&" or "=" in a file id must be encoded so it cannot inject extra download options. */ + @Category(KnownDefect.class) + @Test + public void testFindingC24_fileIdWithAmpersandIsEncodedInDownloadByIdUrl() throws Exception { + final String url = webifier.getDownloadByIdUrl(B2TestHelpers.makeAuth(1), + B2DownloadByIdRequest.builder("4_zid&b2ContentType=text/html").build()); + assertFalse("raw & survived in: " + url, url.substring(url.indexOf("fileId=")).contains("&")); + } + + /** Finding C24: an "&" in a bucket name must be encoded in the download-by-name URL. */ + @Category(KnownDefect.class) + @Test + public void testFindingC24_bucketNameWithAmpersandIsEncodedInDownloadByNameUrl() throws Exception { + final String url = webifier.getDownloadByNameUrl(B2TestHelpers.makeAuth(1), + B2DownloadByNameRequest.builder("my&bucket", "file.txt").build()); + assertEquals("downloadUrl1/file/my%26bucket/file.txt", url); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/contentHandlers/B2ContentFileWriterFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/contentHandlers/B2ContentFileWriterFindingsTest.java new file mode 100644 index 000000000..9711b1b4c --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/contentHandlers/B2ContentFileWriterFindingsTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.contentHandlers; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.contentSources.B2Headers; +import com.backblaze.b2.client.contentSources.B2HeadersImpl; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.util.B2BaseTest; +import com.backblaze.b2.util.B2Sha1; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class B2ContentFileWriterFindingsTest extends B2BaseTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private static final String OLD = "previous good contents"; + + private static B2Headers headers(int len, String sha1OrNull) { + final B2HeadersImpl.Builder b = B2HeadersImpl.builder().set(B2Headers.CONTENT_LENGTH, "" + len); + if (sha1OrNull != null) { + b.set(B2Headers.CONTENT_SHA1, sha1OrNull); + } + return b.build(); + } + + private File existingFile() throws IOException { + final File f = folder.newFile("target.bin"); + Files.write(f.toPath(), OLD.getBytes(StandardCharsets.UTF_8)); + return f; + } + + /** Finding C4: a download whose checksum is wrong must not destroy the older file at the destination. */ + @Category(KnownDefect.class) + @Test + public void testFindingC4_checksumMismatchLeavesPreviousFileIntact() throws Exception { + final File f = existingFile(); + final byte[] data = "new bytes".getBytes(StandardCharsets.UTF_8); + final String wrongSha1 = B2Sha1.hexSha1OfBytes("something else".getBytes(StandardCharsets.UTF_8)); + + try { + B2ContentFileWriter.builder(f).build().readContent(headers(data.length, wrongSha1), new ByteArrayInputStream(data)); + fail("expected sha1 mismatch"); + } catch (B2Exception expected) { + // expected + } + assertEquals(OLD, new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8)); + } + + /** Finding C4: a dropped connection must not leave a truncated file (or destroy the older one). */ + @Category(KnownDefect.class) + @Test + public void testFindingC4_droppedConnectionLeavesPreviousFileIntact() throws Exception { + final File f = existingFile(); + final InputStream dropping = new InputStream() { + private int n; + @Override + public int read() throws IOException { + if (n++ < 5) { + return 'x'; + } + throw new IOException("connection reset"); + } + }; + try { + B2ContentFileWriter.builder(f).build().readContent(headers(100, null), dropping); + fail("expected failure"); + } catch (B2Exception expected) { + // expected + } + assertEquals(OLD, new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8)); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/contentSources/B2HeadersFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/contentSources/B2HeadersFindingsTest.java new file mode 100644 index 000000000..48ed0534b --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/contentSources/B2HeadersFindingsTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.contentSources; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class B2HeadersFindingsTest extends B2BaseTest { + /** Finding C30: a raw value with a bad percent sequence (e.g. "100%") must not make getB2FileInfo() throw. */ + @Category(KnownDefect.class) + @Test + public void testFindingC30_badPercentSequenceInFileInfoValueDoesNotThrow() { + final B2Headers headers = B2HeadersImpl.builder() + .set(B2Headers.FILE_INFO_PREFIX + "discount", "100%") + .set(B2Headers.FILE_INFO_PREFIX + "color", "gr%C3%BCn") + .build(); + final Map info; + try { + info = headers.getB2FileInfo(); + } catch (RuntimeException e) { + fail("getB2FileInfo threw: " + e); + return; + } + assertEquals("100%", info.get("discount")); + assertEquals("gr\u00fcn", info.get("color")); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/structures/B2ApplicationKeyCapabilityFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/structures/B2ApplicationKeyCapabilityFindingsTest.java new file mode 100644 index 000000000..00bd91695 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/structures/B2ApplicationKeyCapabilityFindingsTest.java @@ -0,0 +1,22 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.structures; + +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class B2ApplicationKeyCapabilityFindingsTest extends B2BaseTest { + /** + * Finding C48: the existing round-trip test would pass for any spelling. Pin the exact wire names + * (the SDK expects these strings from, and sends them to, the B2 API). + */ + @Test + public void testFindingC48_notificationCapabilityNamesAreExact() { + assertEquals("readBucketNotifications", B2Capabilities.READ_BUCKET_NOTIFICATIONS); + assertEquals("writeBucketNotifications", B2Capabilities.WRITE_BUCKET_NOTIFICATIONS); + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/structures/B2EventNotificationRuleFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/structures/B2EventNotificationRuleFindingsTest.java new file mode 100644 index 000000000..d49eed1d7 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/structures/B2EventNotificationRuleFindingsTest.java @@ -0,0 +1,30 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.structures; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import java.util.TreeSet; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +public class B2EventNotificationRuleFindingsTest extends B2BaseTest { + /** Finding C36: a rule built with the 5-argument constructor must answer isSuspended() (false), not throw a NullPointerException. */ + @Category(KnownDefect.class) + @Test + public void testFindingC36_isSuspendedOnFiveArgConstructorReturnsFalse() { + final B2EventNotificationRule rule = new B2EventNotificationRule( + "myRule", new TreeSet(), "prefix/", null, true); + try { + assertFalse(rule.isSuspended()); + } catch (NullPointerException e) { + fail("isSuspended() threw NullPointerException for a rule that was never suspended"); + } + } +} diff --git a/core/src/test/java/com/backblaze/b2/client/structures/B2StructuresSecurityFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/structures/B2StructuresSecurityFindingsTest.java new file mode 100644 index 000000000..f94980ac1 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/client/structures/B2StructuresSecurityFindingsTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.structures; + +import com.backblaze.b2.client.B2TestHelpers; +import com.backblaze.b2.client.exceptions.B2SignatureVerificationException; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +/** Abuse-case tests for C15 and C33. */ +public class B2StructuresSecurityFindingsTest extends B2BaseTest { + private static final String SECRET = "3XDfkdQte2OgA78qCtSD17LAzpj6ay9H"; + private static final String PAYLOAD = "{\"events\": []}"; + + /** Finding C15 (security): toString() of the account authorization must not contain the authorization token. */ + @Test + public void testFindingC15_accountAuthorizationToStringHidesToken() { + final B2AccountAuthorization auth = B2TestHelpers.makeAuth(1); + assertFalse("token printed: " + auth, auth.toString().contains(auth.getAuthorizationToken())); + } + + /** Finding C15 (security): toString() of an SSE-C request must not contain the customer key. */ + @Test + public void testFindingC15_sseForRequestToStringHidesCustomerKey() { + final String key = java.util.Base64.getEncoder().encodeToString("ThisIsA32ByteSecretKeyForTesting!".getBytes(StandardCharsets.UTF_8)); + final B2FileSseForRequest sse = B2FileSseForRequest.createSseCAes256(key); + assertFalse("customer key printed: " + sse, sse.toString().contains(key)); + } + + /** Finding C33 (security): when a signing secret is configured, an event without a signature header must be rejected. */ + @Test + public void testFindingC33_missingSignatureWithSecretIsRejected_string() throws Exception { + try { + final B2EventNotification parsed = B2EventNotification.parse(PAYLOAD, null, SECRET); + fail("unsigned event was accepted: " + parsed); + } catch (B2SignatureVerificationException expected) { + // expected + } + } + + /** Finding C33 (security): same for the byte[] overload. */ + @Test + public void testFindingC33_missingSignatureWithSecretIsRejected_bytes() throws Exception { + try { + final B2EventNotification parsed = B2EventNotification.parse(PAYLOAD.getBytes(StandardCharsets.UTF_8), null, SECRET); + fail("unsigned event was accepted: " + parsed); + } catch (B2SignatureVerificationException expected) { + // expected + } + } +} diff --git a/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java b/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java new file mode 100644 index 000000000..7a94b330c --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.json; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.util.B2BaseTest; +import org.junit.Test; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class B2JsonFindingsTest extends B2BaseTest { + public static class Versioned { + @B2Json.versionRange(firstVersion = 4, lastVersion = 6) + @B2Json.required + public final int x; + + @B2Json.ignored + public final int version; + + @B2Json.constructor(params = "x, v", versionParam = "v") + public Versioned(int x, int v) { + this.x = x; + this.version = v; + } + } + + /** Finding C26: building a versioned type from a URL parameter map must work (argument list is too short today). */ + @Category(KnownDefect.class) + @Test + public void testFindingC26_versionedTypeFromUrlParameterMap() throws Exception { + final B2JsonOptions options = B2JsonOptions.builder().setVersion(5).build(); + final Versioned v; + try { + v = B2Json.get().fromUrlParameterMap(Collections.singletonMap("x", "7"), Versioned.class, options); + } catch (RuntimeException | B2JsonException e) { + fail("could not build a versioned type from url parameters: " + e); + return; + } + assertEquals(7, v.x); + assertEquals(5, v.version); + } + + /** Finding C27: an unknown field must be rejected whatever its position; a later discarded field must not hide it. */ + @Category(KnownDefect.class) + @Test + public void testFindingC27_unknownFieldBeforeDiscardedFieldStillRejected() { + // "tail" is unknown; "breed" is a field Cat is declared to discard. + final String json = "{ \"type\": \"cat\", \"tail\": \"long\", \"breed\": \"siamese\", \"name\": \"Charlie\" }"; + try { + final Object pet = B2Json.get().fromJson(json, B2JsonUnionBaseHandlerTest.Pet.class, B2JsonOptions.DEFAULT); + fail("unknown field 'tail' was silently accepted: " + pet); + } catch (B2JsonException expected) { + // expected + } + } + + /** Finding C19: hostile deeply nested input must fail cleanly (B2JsonException), not blow the stack. */ + @Category(KnownDefect.class) + @Test + public void testFindingC19_deeplyNestedInputFailsCleanly() throws IOException { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 200_000; i++) { + sb.append('['); + } + final B2JsonReader reader = new B2JsonReader(new StringReader(sb.toString())); + try { + reader.skipValue(); + fail("expected an error"); + } catch (B2JsonException expected) { + // expected + } catch (StackOverflowError e) { + fail("StackOverflowError: the JSON skipper has no depth limit"); + } + } +} diff --git a/core/src/test/java/com/backblaze/b2/repo/RepoHealthFindingsTest.java b/core/src/test/java/com/backblaze/b2/repo/RepoHealthFindingsTest.java new file mode 100644 index 000000000..be1557b76 --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/repo/RepoHealthFindingsTest.java @@ -0,0 +1,156 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * + * Repository-health checks for known defects (C42, C51-C56). + * License https://www.backblaze.com/using_b2_code.html + * + * These tests read files of the repository (CHANGELOG.md, README.md, SECURITY.md + * and the sample source), not Java behaviour. Gradle runs tests with the module directory + * as working directory, so the repository root is the parent directory. + * + * Outside a full checkout (no settings.gradle.kts in the parent directory) the tests are skipped. + * + * A test that fails today carries @Category(KnownDefect.class) and runs only in the knownDefectTests task. + * C50 (a claim about missing tests) has no natural check and is covered by the C4, C6 and C7 tests. + */ +package com.backblaze.b2.repo; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +public class RepoHealthFindingsTest { + private static File root() { + final File root = new File("..").getAbsoluteFile(); + // skip (not fail) when the module is not built from a full checkout, for example from a source archive + assumeTrue("not run from a full repository checkout (no settings.gradle.kts at " + root + ")", + new File(root, "settings.gradle.kts").isFile()); + return root; + } + + private static String read(String relativePath) throws IOException { + final File f = new File(root(), relativePath); + assumeTrue("file not present in this checkout: " + relativePath, f.isFile()); + return new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8); + } + + private static List lines(String relativePath) throws IOException { + final List result = new ArrayList<>(); + for (String line : read(relativePath).split("\n", -1)) { + result.add(line); + } + return result; + } + + private static String[] workflows() { + final File dir = new File(root(), ".github/workflows"); + final String[] names = dir.list((d, n) -> n.endsWith(".yml") || n.endsWith(".yaml")); + assumeTrue("no workflow files in this checkout", names != null && names.length > 0); + return names; + } + + /** Finding C42: the --noProgress option sets showProgress, but nothing ever reads it. */ + @Category(KnownDefect.class) + @Test + public void testFindingC42_noProgressOptionIsUsed() throws IOException { + final String source = read("samples/src/main/java/com/backblaze/b2/sample/B2.java"); + final Matcher uses = Pattern.compile("showProgress(?!\\s*=[^=])").matcher(source); + int reads = 0; + while (uses.find()) { + // the declaration "private boolean showProgress = true;" is also an assignment, so it is skipped above + reads++; + } + assertTrue("showProgress is assigned but never read, so --noProgress has no effect", reads > 0); + } + + /** Finding C51: every released version heading in the changelog has a link target, and Unreleased compares from the latest release. */ + @Category(KnownDefect.class) + @Test + public void testFindingC51_changelogHeadingsHaveLinkTargets() throws IOException { + final String text = read("CHANGELOG.md"); + final Matcher heading = Pattern.compile("(?m)^## \\[([^\\]]+)\\]").matcher(text); + final List missing = new ArrayList<>(); + String latestRelease = null; + while (heading.find()) { + final String name = heading.group(1); + if (!Pattern.compile("(?m)^\\[" + Pattern.quote(name) + "\\]:\\s*\\S+").matcher(text).find()) { + missing.add(name); + } + if (latestRelease == null && !name.equalsIgnoreCase("Unreleased")) { + latestRelease = name; + } + } + assertTrue("changelog headings without a link target: " + missing, missing.isEmpty()); + assertTrue("the [Unreleased] link should compare from v" + latestRelease, + Pattern.compile("(?m)^\\[Unreleased\\]:.*v" + Pattern.quote(String.valueOf(latestRelease)) + "\\.\\.\\.").matcher(text).find()); + } + + /** Finding C52: the release workflow must not use the deprecated ::set-output command. */ + @Category(KnownDefect.class) + @Test + public void testFindingC52_releaseWorkflowAvoidsDeprecatedSetOutput() throws IOException { + for (String name : workflows()) { + assertFalse(name + " uses the deprecated ::set-output command; write to $GITHUB_OUTPUT", + read(".github/workflows/" + name).contains("::set-output")); + } + } + + /** Finding C53: a fix must not be listed under an "Added" heading (6.3.0 lists "Fixed ..." under Added). */ + @Category(KnownDefect.class) + @Test + public void testFindingC53_fixesAreNotListedUnderAdded() throws IOException { + String heading = ""; + final List misplaced = new ArrayList<>(); + for (String line : lines("CHANGELOG.md")) { + if (line.startsWith("###")) { + heading = line.trim(); + } else if (line.startsWith("## ")) { + heading = ""; + } else if (heading.equals("### Added") && line.matches("^[*-]\\s+Fixed\\b.*")) { + misplaced.add(line.trim()); + } + } + assertTrue("entries starting with 'Fixed' under '### Added': " + misplaced, misplaced.isEmpty()); + } + + /** Finding C54: the README must not point at a Travis badge, the removed 'compile' Gradle keyword, or the placeholder core jar version. */ + @Category(KnownDefect.class) + @Test + public void testFindingC54_readmeIsNotStale() throws IOException { + final String readme = read("README.md"); + assertFalse("README still shows a Travis build badge although CI is GitHub Actions", readme.contains("travis-ci")); + assertFalse("README still uses the removed 'compile' Gradle configuration", readme.contains("compile 'com.backblaze.b2")); + assertFalse("README classpath example names b2-sdk-core-0.0.1.jar", readme.contains("b2-sdk-core-0.0.1.jar")); + } + + /** Finding C55: the library is Apache HttpComponents HttpClient, not "Apache Commons HttpClient". */ + @Category(KnownDefect.class) + @Test + public void testFindingC55_readmeNamesTheRightHttpLibrary() throws IOException { + assertFalse("README says 'Apache Commons HttpClient'", read("README.md").contains("Apache Commons HttpClient")); + } + + /** Finding C56: the repository has a security policy (SECURITY.md at the root, in .github or in docs). */ + @Category(KnownDefect.class) + @Test + public void testFindingC56_securityPolicyExists() { + final File root = root(); + assertTrue("no SECURITY.md at the root, in .github/ or in docs/ (an organisation-wide default may exist outside this repository)", + new File(root, "SECURITY.md").isFile() + || new File(root, ".github/SECURITY.md").isFile() + || new File(root, "docs/SECURITY.md").isFile()); + } +} diff --git a/core/src/test/java/com/backblaze/b2/testing/KnownDefect.java b/core/src/test/java/com/backblaze/b2/testing/KnownDefect.java new file mode 100644 index 000000000..a9953c67d --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/testing/KnownDefect.java @@ -0,0 +1,17 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.testing; + +/** + * JUnit 4 category for a test that documents a defect that is still present in the code. + * + *

Tests carrying @Category(KnownDefect.class) are excluded from the default test + * task, so they do not turn the build red, and run with ./gradlew knownDefectTests. + * A test is expected to FAIL there until the defect is fixed. When it is fixed, remove the + * annotation so the test joins the normal suite and protects the fix. The test method name carries + * the code-review finding id (for example testFindingC21_...).

+ */ +public interface KnownDefect { +} diff --git a/core/src/test/java/com/backblaze/b2/util/B2FlushAndSyncFileOnCloseOutputStreamFindingsTest.java b/core/src/test/java/com/backblaze/b2/util/B2FlushAndSyncFileOnCloseOutputStreamFindingsTest.java new file mode 100644 index 000000000..23e42048f --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/util/B2FlushAndSyncFileOnCloseOutputStreamFindingsTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.util; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class B2FlushAndSyncFileOnCloseOutputStreamFindingsTest extends B2BaseTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + /** Finding C5: if flush fails during close(), the underlying handle must still be closed. */ + @Category(KnownDefect.class) + @Test + public void testFindingC5_failedFlushStillClosesHandle() throws Exception { + final File f = folder.newFile("x"); + final boolean[] closed = {false}; + final FileOutputStream failingFlush = new FileOutputStream(f) { + @Override + public void flush() throws IOException { + throw new IOException("simulated disk error"); + } + + @Override + public void close() throws IOException { + closed[0] = true; + super.close(); + } + }; + final B2FlushAndSyncFileOnCloseOutputStream out = B2FlushAndSyncFileOnCloseOutputStream.create(failingFlush); + try { + out.close(); + fail("close should propagate the flush failure"); + } catch (IOException expected) { + // expected + } + final boolean wasClosed = closed[0]; + failingFlush.close(); // test cleanup only, after the observation + assertTrue("file handle was never closed after the flush failed", wasClosed); + } +} diff --git a/core/src/test/java/com/backblaze/b2/util/B2UtilFindingsTest.java b/core/src/test/java/com/backblaze/b2/util/B2UtilFindingsTest.java new file mode 100644 index 000000000..5d4abf8af --- /dev/null +++ b/core/src/test/java/com/backblaze/b2/util/B2UtilFindingsTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.util; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +public class B2UtilFindingsTest extends B2BaseTest { + private static byte[] seq(int n) { + final byte[] b = new byte[n]; + for (int i = 0; i < n; i++) { + b[i] = (byte) i; + } + return b; + } + + private static byte[] readAll(InputStream in) throws IOException { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + int c; + while ((c = in.read()) != -1) { + out.write(c); + } + return out.toByteArray(); + } + + /** Finding C21: after skip(n) the excerpt must still return all of its remaining bytes. */ + @Category(KnownDefect.class) + @Test + public void testFindingC21_skipDoesNotShortenExcerpt() throws IOException { + final B2InputStreamExcerpt excerpt = new B2InputStreamExcerpt(new ByteArrayInputStream(seq(10)), 0, 10); + assertEquals(3, excerpt.skip(3)); + final byte[] rest = readAll(excerpt); + assertEquals("bytes 3..9 expected after skipping 3 of 10", 7, rest.length); + assertEquals(3, rest[0]); + assertEquals(9, rest[6]); + } + + /** Finding C20: an excerpt longer than 2 GiB must still be readable (remaining-bytes count must not overflow). */ + @Category(KnownDefect.class) + @Test + public void testFindingC20_excerptLongerThan2GiBCanBeRead() throws IOException { + final long threeGiB = 3L * 1024 * 1024 * 1024; + final B2InputStreamExcerpt excerpt = new B2InputStreamExcerpt(new ByteArrayInputStream(seq(100)), 0, threeGiB); + final byte[] buf = new byte[16]; + final int n = excerpt.read(buf, 0, buf.length); + assertEquals(16, n); + assertEquals(15, buf[15]); + } + + /** Finding C29: a range number too large for a long must give null (as documented), not throw. */ + @Category(KnownDefect.class) + @Test + public void testFindingC29_parseOverflowingNumberReturnsNull() { + try { + assertNull(B2ByteRange.parse("bytes=99999999999999999999-")); + assertNull(B2ByteRange.parse("bytes=0-99999999999999999999")); + } catch (NumberFormatException e) { + fail("parse() threw instead of returning null: " + e); + } + } + + /** + * Findings C23 and C49: a space in a file name must be encoded so that it cannot be mistaken for a literal + * plus in a URL path (RFC 3986 path encoding is %20). NOTE: whether the B2 server itself accepts "+" as a + * space needs a live request; this test asserts the URL-standard behaviour. + */ + @Category(KnownDefect.class) + @Test + public void testFindingC23_C49_spaceInFileNameIsPercentTwentyNotPlus() { + final String encoded = B2StringUtil.percentEncode("a b+c"); + assertEquals("a%20b%2Bc", encoded); + } +} diff --git a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java new file mode 100644 index 000000000..40ec49edd --- /dev/null +++ b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java @@ -0,0 +1,215 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.webApiHttpClient; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.backblaze.b2.client.contentHandlers.B2ContentMemoryWriter; +import com.backblaze.b2.client.contentSources.B2Headers; +import com.backblaze.b2.client.contentSources.B2HeadersImpl; +import com.backblaze.b2.client.exceptions.B2Exception; +import com.backblaze.b2.client.exceptions.B2NetworkTimeoutException; +import com.backblaze.b2.client.webApiClients.B2WebApiClient; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class B2WebApiHttpClientFindingsTest { + private HttpServer server; + private String baseUrl; + private volatile Handler handler; + + private interface Handler { + void handle(HttpExchange exchange) throws IOException; + } + + @Before + public void startServer() throws Exception { + server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 10); + server.createContext("/", exchange -> { + try { + handler.handle(exchange); + } finally { + exchange.close(); + } + }); + server.start(); + baseUrl = "http://127.0.0.1:" + server.getAddress().getPort() + "/x"; + } + + @After + public void stopServer() { + server.stop(0); + } + + private static B2WebApiClient clientWithSocketTimeoutSeconds(int secs) { + return B2WebApiHttpClientImpl.builder() + .setHttpClientFactory(HttpClientFactoryImpl.builder() + .setSupportInsecureHttp(true) + .setSocketTimeoutSeconds(secs) + .build()) + .build(); + } + + private static B2WebApiClient defaultClient() { + return clientWithSocketTimeoutSeconds(20); + } + + private static void reply(HttpExchange ex, int status, String body, String... headerNameValuePairs) throws IOException { + for (int i = 0; i < headerNameValuePairs.length; i += 2) { + ex.getResponseHeaders().add(headerNameValuePairs[i], headerNameValuePairs[i + 1]); + } + final byte[] bytes = body.getBytes(StandardCharsets.UTF_8); + if ("HEAD".equals(ex.getRequestMethod())) { + ex.sendResponseHeaders(status, -1); + } else { + ex.sendResponseHeaders(status, bytes.length == 0 ? -1 : bytes.length); + if (bytes.length > 0) { + ex.getResponseBody().write(bytes); + } + } + } + + /** Finding C25: downloads must honour the read timeout configured on the HTTP factory. */ + @Category(KnownDefect.class) + @Test + public void testFindingC25_downloadHonoursConfiguredSocketTimeout() throws Exception { + handler = ex -> { + try { + Thread.sleep(3500); // the server stalls before answering + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + reply(ex, 200, "late"); + }; + final B2WebApiClient client = clientWithSocketTimeoutSeconds(1); + final long start = System.nanoTime(); + try { + client.getContent(baseUrl, null, B2ContentMemoryWriter.build()); + fail("getContent waited " + (System.nanoTime() - start) / 1_000_000 + " ms although the socket timeout was 1 s"); + } catch (B2NetworkTimeoutException expected) { + // expected + } finally { + client.close(); + } + } + + /** Finding C31: a failed HEAD must report the server's Retry-After and a usable status. */ + @Category(KnownDefect.class) + @Test + public void testFindingC31_headFailureKeepsRetryAfter() throws Exception { + handler = ex -> reply(ex, 503, "", "Retry-After", "7"); + final B2WebApiClient client = defaultClient(); + try { + client.head(baseUrl, null); + fail("expected an exception"); + } catch (B2Exception e) { + assertEquals(503, e.getStatus()); + assertEquals("Retry-After was dropped", Integer.valueOf(7), e.getRetryAfterSecondsOrNull()); + assertNotNull("exception has no error code", e.getCode()); + } finally { + client.close(); + } + } + + /** Finding C32: an error reply with an extra, unknown JSON field must still yield the real error code. */ + @Category(KnownDefect.class) + @Test + public void testFindingC32_errorBodyWithExtraFieldKeepsCode() throws Exception { + handler = ex -> reply(ex, 500, "{\"status\":500,\"code\":\"bad\",\"message\":\"went wrong\",\"newField\":\"x\"}", + "Content-Type", "application/json"); + final B2WebApiClient client = defaultClient(); + try { + client.getContent(baseUrl, null, B2ContentMemoryWriter.build()); + fail("expected an exception"); + } catch (B2Exception e) { + assertEquals("bad", e.getCode()); + assertEquals("went wrong", e.getMessage()); + } finally { + client.close(); + } + } + + /** Finding C11 (parse side): a negative Retry-After from the server must not be passed on as a wait time. */ + @Category(KnownDefect.class) + @Test + public void testFindingC11_negativeRetryAfterHeaderIsNotTrusted() throws Exception { + handler = ex -> reply(ex, 503, "{\"status\":503,\"code\":\"service_unavailable\",\"message\":\"m\"}", + "Retry-After", "-5"); + final B2WebApiClient client = defaultClient(); + try { + client.getContent(baseUrl, null, B2ContentMemoryWriter.build()); + fail("expected an exception"); + } catch (B2Exception e) { + final Integer retryAfter = e.getRetryAfterSecondsOrNull(); + assertTrue("negative wait passed on: " + retryAfter, retryAfter == null || retryAfter >= 0); + } finally { + client.close(); + } + } + + /** + * Finding C46 (stronger replacement for the two download-error tests that lack fail()): + * a 500 reply must make getContent throw; passing silently is a failure. + */ + @Test + public void testFindingC46_errorResponseMustThrow() throws Exception { + handler = ex -> reply(ex, 500, "something went wrong", "Content-Type", "text/plain"); + final B2WebApiClient client = defaultClient(); + try { + client.getContent(baseUrl, null, B2ContentMemoryWriter.build()); + fail("getContent did not throw on a 500 reply"); + } catch (B2Exception e) { + assertEquals(500, e.getStatus()); + assertEquals("unknown", e.getCode()); + assertEquals("something went wrong", e.getMessage()); + } finally { + client.close(); + } + } + + /** + * Finding C47: the existing "compressed" test sets no Content-Encoding header, so it cannot notice if + * the client starts decompressing. Here the reply is marked Content-Encoding: gzip and the client + * must hand over the stored (compressed) bytes untouched. + */ + @Test + public void testFindingC47_contentEncodedDownloadIsNotDecompressed() throws Exception { + final byte[] gzipped = gzip("hello hello hello hello hello".getBytes(StandardCharsets.UTF_8)); + handler = ex -> { + ex.getResponseHeaders().add("Content-Encoding", "gzip"); + ex.sendResponseHeaders(200, gzipped.length); + ex.getResponseBody().write(gzipped); + }; + final B2WebApiClient client = defaultClient(); + try { + final B2ContentMemoryWriter sink = B2ContentMemoryWriter.build(); + client.getContent(baseUrl, B2HeadersImpl.builder().build(), sink); + org.junit.Assert.assertArrayEquals(gzipped, sink.getBytes()); + } finally { + client.close(); + } + } + + private static byte[] gzip(byte[] in) throws IOException { + final java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); + try (java.util.zip.GZIPOutputStream gz = new java.util.zip.GZIPOutputStream(bos)) { + gz.write(in); + } + return bos.toByteArray(); + } +} diff --git a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java new file mode 100644 index 000000000..b5178184e --- /dev/null +++ b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.client.webApiHttpClient; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class HttpClientFactoryFindingsTest { + /** Finding C17: the idle-connection cleaner thread must be a daemon so it cannot keep the JVM alive. */ + @Category(KnownDefect.class) + @Test + public void testFindingC17_idleConnectionMonitorThreadIsDaemon() { + final HttpClientFactoryImpl factory = HttpClientFactoryImpl.build(); + try { + Thread monitor = null; + for (Thread t : Thread.getAllStackTraces().keySet()) { + if (t.getClass().getName().contains("IdleConnectionMonitorThread")) { + monitor = t; + } + } + assertNotNull("cleaner thread not found", monitor); + assertTrue("cleaner thread is not a daemon", monitor.isDaemon()); + } finally { + factory.close(); + } + } + + /** + * Guard for C18 (judged not a bug): close() must return promptly (it should not sit out the 5 s wait), + * even when called right after build(). Note: the defect, if present, is a race, so this guard can pass + * while the race exists; it cannot produce a false failure. + */ + @Test + public void testGuardC18_closeReturnsPromptly() { + for (int i = 0; i < 5; i++) { + final HttpClientFactoryImpl factory = HttpClientFactoryImpl.build(); + final long start = System.nanoTime(); + factory.close(); + final long ms = (System.nanoTime() - start) / 1_000_000; + assertTrue("close() took " + ms + " ms", ms < 4000); + } + } +} diff --git a/httpclient/src/test/java/com/backblaze/b2/testing/KnownDefect.java b/httpclient/src/test/java/com/backblaze/b2/testing/KnownDefect.java new file mode 100644 index 000000000..a9953c67d --- /dev/null +++ b/httpclient/src/test/java/com/backblaze/b2/testing/KnownDefect.java @@ -0,0 +1,17 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.testing; + +/** + * JUnit 4 category for a test that documents a defect that is still present in the code. + * + *

Tests carrying @Category(KnownDefect.class) are excluded from the default test + * task, so they do not turn the build red, and run with ./gradlew knownDefectTests. + * A test is expected to FAIL there until the defect is fixed. When it is fixed, remove the + * annotation so the test joins the normal suite and protects the fix. The test method name carries + * the code-review finding id (for example testFindingC21_...).

+ */ +public interface KnownDefect { +} diff --git a/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java b/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java new file mode 100644 index 000000000..772801f7f --- /dev/null +++ b/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java @@ -0,0 +1,150 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + * + * The samples module had no tests. Tests named "...Static" read the sample's source text, because the + * defects are in the body of main()-style methods that cannot be called without a real B2 account + * (or that call System.exit). They are source-pattern checks, not behaviour tests. + */ +package com.backblaze.b2.sample; + +import com.backblaze.b2.testing.KnownDefect; +import org.junit.experimental.categories.Category; +import com.sun.net.httpserver.HttpServer; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; + +public class SampleFindingsTest { + private static String source(String name) throws IOException { + final File f = new File("src/main/java/com/backblaze/b2/sample/" + name); + // skip (not fail) when the sample source is not available, for example outside a full checkout + assumeTrue("sample source not found: " + f.getAbsolutePath(), f.isFile()); + return new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8); + } + + /** Finding C28: a server that ignores the Range header (answers 200, whole body) must be detected, not read as the part. */ + @Category(KnownDefect.class) + @Test + public void testFindingC28_serverIgnoringRangeIsRejected() throws Exception { + final byte[] whole = new byte[1000]; + final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 5); + server.createContext("/", ex -> { + ex.sendResponseHeaders(200, whole.length); // ignores Range, no 206 + ex.getResponseBody().write(whole); + ex.close(); + }); + server.start(); + try { + final String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/f"; + final UrlContentSource part = new UrlContentSource.Builder(url, 100).setStart(200).build(); + try (InputStream in = part.createInputStream()) { + int total = 0; + while (in.read() != -1) { + total++; + } + fail("a 200 reply to a range request was accepted and returned " + total + " bytes for a 100-byte part"); + } catch (IOException expected) { + // expected: the sample should refuse a non-206 answer. + } + } finally { + server.stop(0); + } + } + + /** Finding C43 (static): compressed bytes must not be turned into a String and back. */ + @Category(KnownDefect.class) + @Test + public void testFindingC43_compressedBytesNotRoundTrippedThroughStringStatic() throws IOException { + assertFalse("B2Sample converts deflated bytes with new String(...), which corrupts them", + source("B2Sample.java").contains("new String(buffer, 0, compressedLength)")); + } + + /** + * Finding C44: the huge-file helper must work with the file name the sample passes (it contains '/'). + * Note: this reflects JDK 11/17 behaviour (createTempFile accepts the name) and creates a sparse file of about + * 2 GiB, which a file system without sparse-file support may fully allocate; the file is deleted afterwards. + */ + @Test + public void testFindingC44_makeHugeFileAcceptsTheSamplesFileName() throws Exception { + final Method m = B2Sample.class.getDeclaredMethod("makeHugeFile", String.class); + m.setAccessible(true); + File created = null; + try { + created = (File) m.invoke(null, "demo/large/huge2GBfile.txt"); + assertTrue(created.exists()); + } catch (InvocationTargetException e) { + fail("makeHugeFile threw for the sample's own file name: " + e.getCause()); + } finally { + if (created != null) { + //noinspection ResultOfMethodCallIgnored + created.delete(); + } + } + } + + /** Finding C45 (static): the loop over unfinished large files must list the parts of that loop's file. */ + @Category(KnownDefect.class) + @Test + public void testFindingC45_listsPartsOfLoopVariableStatic() throws IOException { + final String src = source("B2Sample.java"); + final int loop = src.indexOf("for (B2FileVersion largeFileVersion : client.unfinishedLargeFiles(bucketId))"); + assertTrue("loop not found", loop >= 0); + final String body = src.substring(loop, Math.min(src.length(), loop + 400)); + assertTrue("parts are listed for another file:\n" + body, body.contains("client.parts(largeFileVersion.getFileId())")); + } + + /** Finding C38 (static): resuming must not use getContentSha1() ("none" for unfinished large files) as the source SHA-1. */ + @Category(KnownDefect.class) + @Test + public void testFindingC38_finishUploadingUsesLargeFileSha1Static() throws IOException { + assertFalse("B2.java still passes getContentSha1() of the unfinished large file as the source SHA-1", + source("B2.java").contains(".setSha1(largeFileVersion.getContentSha1())")); + } + + /** Finding C39 (static): usage text of get_download_authorization must mention the duration the command requires. */ + @Category(KnownDefect.class) + @Test + public void testFindingC39_downloadAuthorizationUsageMentionsDurationStatic() throws IOException { + final String src = source("B2.java"); + final Matcher m = Pattern.compile("\"\\s*b2 get_download_authorization([^\"]*)\\\\n\"").matcher(src); + assertTrue("usage line not found", m.find()); + assertTrue("usage says: " + m.group(1), m.group(1).contains("validDurationInSecs") || m.group(1).toLowerCase().contains("duration")); + } + + /** Finding C40 (static): finish_uploading_large_file must accept as many arguments as its usage text lists (6). */ + @Category(KnownDefect.class) + @Test + public void testFindingC40_finishUploadingAcceptsAllUsageArgumentsStatic() throws IOException { + final String src = source("B2.java"); + final int at = src.indexOf("private void finish_uploading_large_file"); + final Matcher m = Pattern.compile("checkArgCount\\(args, 3, (\\d+)\\)").matcher(src.substring(at, at + 400)); + assertTrue(m.find()); + // usage: [--noProgress] [--threads N] => up to 6 words + assertEquals(6, Integer.parseInt(m.group(1))); + } + + /** Finding C41 (static): --info must accept values containing '=' (split must be limited to 2 parts). */ + @Category(KnownDefect.class) + @Test + public void testFindingC41_infoOptionAllowsEqualsInValueStatic() throws IOException { + assertFalse("B2.java still calls pair.split(\"=\") without a limit, so a value containing '=' is cut", + source("B2.java").contains("pair.split(\"=\")")); + } +} diff --git a/samples/src/test/java/com/backblaze/b2/testing/KnownDefect.java b/samples/src/test/java/com/backblaze/b2/testing/KnownDefect.java new file mode 100644 index 000000000..a9953c67d --- /dev/null +++ b/samples/src/test/java/com/backblaze/b2/testing/KnownDefect.java @@ -0,0 +1,17 @@ +/* + * Copyright 2026, Backblaze Inc. All Rights Reserved. + * License https://www.backblaze.com/using_b2_code.html + */ +package com.backblaze.b2.testing; + +/** + * JUnit 4 category for a test that documents a defect that is still present in the code. + * + *

Tests carrying @Category(KnownDefect.class) are excluded from the default test + * task, so they do not turn the build red, and run with ./gradlew knownDefectTests. + * A test is expected to FAIL there until the defect is fixed. When it is fixed, remove the + * annotation so the test joins the normal suite and protects the fix. The test method name carries + * the code-review finding id (for example testFindingC21_...).

+ */ +public interface KnownDefect { +} From 26b400e5fc80dea2c41a63971287918b58b4c280 Mon Sep 17 00:00:00 2001 From: scarreras Date: Wed, 18 Nov 2026 10:21:06 -0300 Subject: [PATCH 2/5] Make the close() guard test deterministic testGuardC18_closeReturnsPromptly failed intermittently (about 2 of 12 isolated runs): close() right after build() can land before the cleaner thread reaches its first wait. The guard now waits (bounded) for the thread to be parked first, so it only measures close() latency. Co-Authored-By: Claude Sonnet 5.5 --- .../HttpClientFactoryFindingsTest.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java index b5178184e..f4b5dc212 100644 --- a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java +++ b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java @@ -34,18 +34,34 @@ public void testFindingC17_idleConnectionMonitorThreadIsDaemon() { } /** - * Guard for C18 (judged not a bug): close() must return promptly (it should not sit out the 5 s wait), - * even when called right after build(). Note: the defect, if present, is a race, so this guard can pass - * while the race exists; it cannot produce a false failure. + * Guard for C18 (judged not a bug): close() must return promptly (it should not sit out the 5 s wait). + * The cleaner thread is first allowed to reach its wait, so the guard is deterministic: calling close() + * in the instant between thread start and its first wait() is a separate startup race, which would make + * this guard fail intermittently if it were not excluded here. */ @Test public void testGuardC18_closeReturnsPromptly() { for (int i = 0; i < 5; i++) { final HttpClientFactoryImpl factory = HttpClientFactoryImpl.build(); + awaitCleanerThreadWaiting(); final long start = System.nanoTime(); factory.close(); final long ms = (System.nanoTime() - start) / 1_000_000; assertTrue("close() took " + ms + " ms", ms < 4000); } } + + /** Waits (bounded) until a cleaner thread is parked in wait(), so close() cannot race its startup. */ + private static void awaitCleanerThreadWaiting() { + final long deadline = System.nanoTime() + 2_000_000_000L; + while (System.nanoTime() < deadline) { + for (Thread t : Thread.getAllStackTraces().keySet()) { + if (t.getClass().getName().contains("IdleConnectionMonitorThread") + && t.getState() == Thread.State.TIMED_WAITING) { + return; + } + } + Thread.yield(); + } + } } From 83c0bfb9f7f08a4b28e11d9c3e596147c8e38723 Mon Sep 17 00:00:00 2001 From: scarreras Date: Wed, 18 Nov 2026 16:31:23 -0300 Subject: [PATCH 3/5] Tighten known-defect tests after review: no false passes, no timing or order dependence - bind HTTP fixtures to 127.0.0.1 to match client URLs (works with IPv6 preferred) - C28, C11: handlers signal a latch; assert the request was seen, the Range header, a 200-specific IOException, and status 503 - C6 cancellation: caller on its own thread, wait for uploads to start, then for each to stop (bounded) - C18/C17: use this factory's own cleaner thread; close() runs on a daemon thread with a bounded wait - C14 parts: stub pages per request cursor and verify the requests - C10: controlled sleeper instead of wall-clock time - C3: tests fail if the call returns normally and check the lost-reply failure is propagated - samples C38-C41: drive the B2 command with a mocked client instead of reading source text (C43/C45 stay source checks) - document the fix-PR step of removing @Category(KnownDefect) in b2sdk.gradle.kts Co-Authored-By: Claude Sonnet 5.5 --- buildSrc/src/main/kotlin/b2sdk.gradle.kts | 6 + .../client/B2LargeFileStorerFindingsTest.java | 86 +++++-- .../client/B2ListIterablesFindingsTest.java | 18 +- .../b2/client/B2RetryerFindingsTest.java | 32 ++- .../B2StorageClientImplFindingsTest.java | 26 +- .../B2WebApiHttpClientFindingsTest.java | 16 +- .../HttpClientFactoryFindingsTest.java | 84 ++++-- .../b2/sample/SampleFindingsTest.java | 242 ++++++++++++++++-- 8 files changed, 413 insertions(+), 97 deletions(-) diff --git a/buildSrc/src/main/kotlin/b2sdk.gradle.kts b/buildSrc/src/main/kotlin/b2sdk.gradle.kts index f9cf422b7..abe3303ef 100644 --- a/buildSrc/src/main/kotlin/b2sdk.gradle.kts +++ b/buildSrc/src/main/kotlin/b2sdk.gradle.kts @@ -42,6 +42,12 @@ dependencies { // Tests that document a defect that is still present carry @Category(KnownDefect.class). The default // `test` task (and so `build` and CI) excludes them; `./gradlew knownDefectTests` runs only them and is // expected to fail until the defects are fixed. Only modules that have the marker interface take part. +// +// A marked test is excluded from `test`, so it must not stay marked once its defect is fixed: the PR that +// fixes a defect removes @Category(KnownDefect.class) from that defect's test(s) and confirms they pass under +// the ordinary `./gradlew test`. A marked test that starts passing in `knownDefectTests` is the signal that +// this step is due. Each marked test must fail only for the defect it documents (it asserts that specific +// behaviour; a connection error, timeout or setup problem must not satisfy it). val knownDefectMarker = "com.backblaze.b2.testing.KnownDefect" if (file("src/test/java/com/backblaze/b2/testing/KnownDefect.java").exists()) { tasks.test { diff --git a/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java index 28fa24448..5b3fe6d12 100644 --- a/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java +++ b/core/src/test/java/com/backblaze/b2/client/B2LargeFileStorerFindingsTest.java @@ -59,6 +59,10 @@ private static class FakePartStorer implements B2PartStorer { private final CountDownLatch release; private final boolean failImmediately; final AtomicReference token = new AtomicReference<>(); + /** counted down when storePart() is entered. */ + final CountDownLatch started = new CountDownLatch(1); + /** counted down when storePart() is left, however it is left. */ + final CountDownLatch stopped = new CountDownLatch(1); FakePartStorer(int partNumber, long size, CountDownLatch release, boolean failImmediately) { this.partNumber = partNumber; @@ -81,16 +85,30 @@ public long getPartSizeOrThrow() { public B2Part storePart(B2LargeFileStorer storer, B2UploadListener listener, B2CancellationToken cancellationToken) throws IOException, B2Exception { token.set(cancellationToken); - if (failImmediately) { - throw new B2InternalErrorException("simulated part failure"); - } + started.countDown(); try { - release.await(10, TimeUnit.SECONDS); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new B2LocalException("interrupted", "interrupted"); + if (failImmediately) { + throw new B2InternalErrorException("simulated part failure"); + } + // a worker that behaves like a real upload: it stops when interrupted or when it is told to cancel + final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + try { + while (!release.await(20, TimeUnit.MILLISECONDS)) { + if (cancellationToken != null && cancellationToken.isCancelled()) { + throw new B2LocalException("cancelled", "cancelled"); + } + if (System.nanoTime() > deadline) { + break; + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new B2LocalException("interrupted", "interrupted"); + } + return new B2Part(fileId(1), partNumber, size, makeSha1(partNumber), makeMd5(partNumber), 1, null); + } finally { + stopped.countDown(); } - return new B2Part(fileId(1), partNumber, size, makeSha1(partNumber), makeMd5(partNumber), 1, null); } } @@ -126,28 +144,48 @@ public void testFindingC6_interruptFlagRestoredWhenStoreFileInterrupted() throws assertTrue("interrupt flag was swallowed by storeParts", flagRestored); } - /** Finding C6: after the caller is interrupted, the part uploads in progress must be told to stop. */ + /** + * Finding C6: after the caller is interrupted, the part uploads that were in progress must stop. + * The caller runs on its own thread; the test waits until both uploads have really started before + * interrupting it, then waits (bounded) for each started upload to stop, by whatever means (cancellation + * token or interrupt). + */ @Category(KnownDefect.class) @Test public void testFindingC6_partUploadsAreCancelledWhenCallerInterrupted() throws Exception { final CountDownLatch never = new CountDownLatch(1); - final B2LargeFileStorer storer2 = storerFor(Arrays.asList( - new FakePartStorer(1, 10, never, false), new FakePartStorer(2, 10, never, false))); - final FakePartStorer watched = (FakePartStorer) storer2.getPartStorers().get(0); - Thread.currentThread().interrupt(); + final FakePartStorer part1 = new FakePartStorer(1, 10, never, false); + final FakePartStorer part2 = new FakePartStorer(2, 10, never, false); + final B2LargeFileStorer storer = storerFor(Arrays.asList(part1, part2)); + + final AtomicReference callerOutcome = new AtomicReference<>(); + final Thread caller = new Thread(() -> { + try { + storer.storeParts(null); + } catch (Throwable t) { + callerOutcome.set(t); + } + }, "C6-caller"); + caller.setDaemon(true); try { - storer2.storeParts(null); - } catch (B2Exception expected) { - // expected - } - Thread.interrupted(); - final long deadline2 = System.currentTimeMillis() + 2000; - while ((watched.token.get() == null || !watched.token.get().isCancelled()) && System.currentTimeMillis() < deadline2) { - Thread.sleep(5); + caller.start(); + assertTrue("part 1 never started", part1.started.await(5, TimeUnit.SECONDS)); + assertTrue("part 2 never started", part2.started.await(5, TimeUnit.SECONDS)); + + caller.interrupt(); + + caller.join(5000); + assertTrue("storeParts did not return after its caller was interrupted", !caller.isAlive()); + assertTrue("part 1 upload was still running after the caller was interrupted", + part1.stopped.await(3, TimeUnit.SECONDS)); + assertTrue("part 2 upload was still running after the caller was interrupted", + part2.stopped.await(3, TimeUnit.SECONDS)); + } finally { + never.countDown(); + caller.interrupt(); + pool.shutdownNow(); + pool.awaitTermination(5, TimeUnit.SECONDS); } - final boolean cancelled = watched.token.get() != null && watched.token.get().isCancelled(); - never.countDown(); - assertTrue("part uploads were not cancelled after the caller was interrupted", cancelled); } /** Finding C7: the exception raised for an interrupt must carry the InterruptedException as its cause. */ diff --git a/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java index 005d2bcef..88aa5f042 100644 --- a/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java +++ b/core/src/test/java/com/backblaze/b2/client/B2ListIterablesFindingsTest.java @@ -41,6 +41,8 @@ import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class B2ListIterablesFindingsTest extends B2BaseTest { @@ -96,10 +98,13 @@ public void testFindingC14_keysIteratorNextWhenExhaustedThrowsNoSuchElement() th @Test public void testFindingC14_partsIteratorContinuesPastEmptyMiddlePage() throws B2Exception { final B2ListPartsRequest request = B2ListPartsRequest.builder(fileId(1)).build(); - when(client.listParts(anyObject())).thenReturn( - new B2ListPartsResponse(B2Collections.listOf(part(1)), 2), - new B2ListPartsResponse(new ArrayList(), 3), - new B2ListPartsResponse(B2Collections.listOf(part(3)), null)); + final B2ListPartsRequest second = B2ListPartsRequest.builder(fileId(1)).setStartPartNumber(2).build(); + final B2ListPartsRequest third = B2ListPartsRequest.builder(fileId(1)).setStartPartNumber(3).build(); + // each page is only returned for the request that really asks for it, so a listing that does not + // advance its continuation cursor cannot get the later pages. + when(client.listParts(request)).thenReturn(new B2ListPartsResponse(B2Collections.listOf(part(1)), 2)); + when(client.listParts(second)).thenReturn(new B2ListPartsResponse(new ArrayList(), 3)); + when(client.listParts(third)).thenReturn(new B2ListPartsResponse(B2Collections.listOf(part(3)), null)); final List seen = new ArrayList<>(); final Iterator iter = new B2ListPartsIterableImpl(client, request).iterator(); @@ -107,6 +112,11 @@ public void testFindingC14_partsIteratorContinuesPastEmptyMiddlePage() throws B2 seen.add(iter.next().getPartNumber()); } assertEquals(B2Collections.listOf(1, 3), seen); + // and the requests were exactly: first page, then start part 2, then start part 3 + verify(client).listParts(request); + verify(client).listParts(second); + verify(client).listParts(third); + verifyNoMoreInteractions(client); } /** diff --git a/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java index 13d734b4e..ccae22f19 100644 --- a/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java +++ b/core/src/test/java/com/backblaze/b2/client/B2RetryerFindingsTest.java @@ -12,9 +12,12 @@ import org.junit.After; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class B2RetryerFindingsTest extends B2BaseTest { @After @@ -22,29 +25,46 @@ public void clearFlag() { Thread.interrupted(); } + /** + * A sleeper that never really sleeps: it records each request and behaves like Thread.sleep() called on a + * thread whose interrupt flag is set (the interrupted sleep returns at once, re-interrupts, reports false). + */ + private static class InterruptedSleeper extends B2Sleeper { + final List requestedSeconds = new ArrayList<>(); + + @Override + boolean sleepSeconds(int seconds) { + requestedSeconds.add(seconds); + Thread.currentThread().interrupt(); + return false; + } + } + /** * Finding C10: when the thread's interrupt flag is set (for example after a cancelled upload task), * the retry loop must not burn through all its attempts with no pause at all: it should stop - * retrying (or still pause). + * retrying. The sleeper is controlled by the test, so the result does not depend on wall-clock time. */ @Category(KnownDefect.class) @Test public void testFindingC10_interruptedThreadDoesNotRetryWithoutPausing() { - final B2Retryer retryer = new B2Retryer(new B2Sleeper()); + final InterruptedSleeper sleeper = new InterruptedSleeper(); + final B2Retryer retryer = new B2Retryer(sleeper); final AtomicInteger attempts = new AtomicInteger(); - final long start = System.nanoTime(); Thread.currentThread().interrupt(); try { retryer.doRetry("op", mock(), (isRetry) -> { attempts.incrementAndGet(); throw new B2InternalErrorException("always failing"); }, B2DefaultRetryPolicy.supplier().get()); + fail("the operation always fails, so doRetry must throw"); } catch (B2Exception expected) { // expected } - final long elapsedMillis = (System.nanoTime() - start) / 1_000_000; - assertTrue("made " + attempts.get() + " attempts in " + elapsedMillis + " ms with no pause", - attempts.get() <= 2 || elapsedMillis >= 1000); + assertTrue("the operation was never attempted", attempts.get() >= 1); + assertTrue("made " + attempts.get() + " attempts although every pause (" + sleeper.requestedSeconds + + " s requested) was cut short by the interrupt", + attempts.get() <= 2); } private static B2AccountAuthorizationCache mock() { diff --git a/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java index df69283e2..c30c9f59e 100644 --- a/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java +++ b/core/src/test/java/com/backblaze/b2/client/B2StorageClientImplFindingsTest.java @@ -47,6 +47,17 @@ private B2ConnectionBrokenException lostReply() { return new B2ConnectionBrokenException("broken", null, "reply lost"); } + /** The failure the client reports must still be the lost reply (the exception itself, or one that names it). */ + private static void assertLostReplyPropagated(B2Exception thrown) { + for (Throwable t = thrown; t != null; t = t.getCause()) { + if (t instanceof B2ConnectionBrokenException + && t.getMessage() != null && t.getMessage().contains("reply lost")) { + return; + } + } + fail("the lost-reply failure was not propagated; the client threw: " + thrown); + } + private void authorize() throws B2Exception { B2Clock.useSimulator(parseDateTime("2018-04-17 01:26:32")); when(webifier.authorizeAccount(anyObject())).thenReturn(ACCOUNT_AUTH); @@ -60,8 +71,9 @@ public void testFindingC3_createKeyNotRetried() throws B2Exception { when(webifier.createKey(anyObject(), anyObject())).thenThrow(lostReply()); try { client.createKey(B2CreateKeyRequest.builder(Collections.singleton("listFiles"), "k").build()); + fail("client.createKey returned normally although the reply was lost"); } catch (B2Exception expected) { - // expected + assertLostReplyPropagated(expected); } verify(webifier, times(1)).createKey(anyObject(), anyObject()); } @@ -74,8 +86,9 @@ public void testFindingC3_startLargeFileNotRetried() throws B2Exception { when(webifier.startLargeFile(anyObject(), anyObject())).thenThrow(lostReply()); try { client.startLargeFile(B2StartLargeFileRequest.builder(bucketId(1), fileName(1), "text/plain").build()); + fail("client.startLargeFile returned normally although the reply was lost"); } catch (B2Exception expected) { - // expected + assertLostReplyPropagated(expected); } verify(webifier, times(1)).startLargeFile(anyObject(), anyObject()); } @@ -88,8 +101,9 @@ public void testFindingC3_createBucketNotRetried() throws B2Exception { when(webifier.createBucket(anyObject(), anyObject())).thenThrow(lostReply()); try { client.createBucket("bucket1", "allPublic"); + fail("client.createBucket returned normally although the reply was lost"); } catch (B2Exception expected) { - // expected + assertLostReplyPropagated(expected); } verify(webifier, times(1)).createBucket(anyObject(), anyObject()); } @@ -102,8 +116,9 @@ public void testFindingC3_deleteKeyNotRetried() throws B2Exception { when(webifier.deleteKey(anyObject(), anyObject())).thenThrow(lostReply()); try { client.deleteKey(B2DeleteKeyRequest.builder("keyId").build()); + fail("client.deleteKey returned normally although the reply was lost"); } catch (B2Exception expected) { - // expected + assertLostReplyPropagated(expected); } verify(webifier, times(1)).deleteKey(anyObject(), anyObject()); } @@ -116,8 +131,9 @@ public void testFindingC3_copyFileNotRetried() throws B2Exception { when(webifier.copyFile(anyObject(), anyObject())).thenThrow(lostReply()); try { client.copySmallFile(B2CopyFileRequest.builder(fileId(1), fileName(2)).build()); + fail("client.copySmallFile returned normally although the reply was lost"); } catch (B2Exception expected) { - // expected + assertLostReplyPropagated(expected); } verify(webifier, times(1)).copyFile(anyObject(), anyObject()); } diff --git a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java index 40ec49edd..41df71ba2 100644 --- a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java +++ b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/B2WebApiHttpClientFindingsTest.java @@ -22,6 +22,8 @@ import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -39,7 +41,7 @@ private interface Handler { @Before public void startServer() throws Exception { - server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 10); + server = HttpServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), 10); server.createContext("/", exchange -> { try { handler.handle(exchange); @@ -148,13 +150,21 @@ public void testFindingC32_errorBodyWithExtraFieldKeepsCode() throws Exception { @Category(KnownDefect.class) @Test public void testFindingC11_negativeRetryAfterHeaderIsNotTrusted() throws Exception { - handler = ex -> reply(ex, 503, "{\"status\":503,\"code\":\"service_unavailable\",\"message\":\"m\"}", - "Retry-After", "-5"); + final CountDownLatch requestSeen = new CountDownLatch(1); + handler = ex -> { + requestSeen.countDown(); + reply(ex, 503, "{\"status\":503,\"code\":\"service_unavailable\",\"message\":\"m\"}", + "Retry-After", "-5"); + }; final B2WebApiClient client = defaultClient(); try { client.getContent(baseUrl, null, B2ContentMemoryWriter.build()); fail("expected an exception"); } catch (B2Exception e) { + // a connection failure is not the response under test + assertTrue("the server never received the request; the failure was: " + e, + requestSeen.await(5, TimeUnit.SECONDS)); + assertEquals("the 503 reply was not processed: " + e, 503, e.getStatus()); final Integer retryAfter = e.getRetryAfterSecondsOrNull(); assertTrue("negative wait passed on: " + retryAfter, retryAfter == null || retryAfter >= 0); } finally { diff --git a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java index f4b5dc212..cb0f8e347 100644 --- a/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java +++ b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java @@ -8,24 +8,32 @@ import org.junit.experimental.categories.Category; import org.junit.Test; -import java.util.Map; +import java.lang.reflect.Field; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import static org.junit.Assert.assertTrue; public class HttpClientFactoryFindingsTest { + /** The cleaner thread that belongs to this very factory (not some other factory's). */ + private static Thread cleanerThreadOf(HttpClientFactoryImpl factory) throws Exception { + final Field f = HttpClientFactoryImpl.class.getDeclaredField("connectionJanitor"); + f.setAccessible(true); + return (Thread) f.get(factory); + } + /** Finding C17: the idle-connection cleaner thread must be a daemon so it cannot keep the JVM alive. */ @Category(KnownDefect.class) @Test - public void testFindingC17_idleConnectionMonitorThreadIsDaemon() { + public void testFindingC17_idleConnectionMonitorThreadIsDaemon() throws Exception { final HttpClientFactoryImpl factory = HttpClientFactoryImpl.build(); try { - Thread monitor = null; - for (Thread t : Thread.getAllStackTraces().keySet()) { - if (t.getClass().getName().contains("IdleConnectionMonitorThread")) { - monitor = t; - } - } + final Thread monitor = cleanerThreadOf(factory); assertNotNull("cleaner thread not found", monitor); assertTrue("cleaner thread is not a daemon", monitor.isDaemon()); } finally { @@ -35,33 +43,55 @@ public void testFindingC17_idleConnectionMonitorThreadIsDaemon() { /** * Guard for C18 (judged not a bug): close() must return promptly (it should not sit out the 5 s wait). - * The cleaner thread is first allowed to reach its wait, so the guard is deterministic: calling close() - * in the instant between thread start and its first wait() is a separate startup race, which would make - * this guard fail intermittently if it were not excluded here. + * This factory's own cleaner thread is first required to reach its wait, so the guard is deterministic: + * calling close() in the instant between thread start and its first wait() is a separate startup race. + * close() runs on a daemon thread and is given a bounded time, so a close() that hangs is reported as a + * timeout instead of blocking the test run. */ @Test - public void testGuardC18_closeReturnsPromptly() { - for (int i = 0; i < 5; i++) { - final HttpClientFactoryImpl factory = HttpClientFactoryImpl.build(); - awaitCleanerThreadWaiting(); - final long start = System.nanoTime(); - factory.close(); - final long ms = (System.nanoTime() - start) / 1_000_000; - assertTrue("close() took " + ms + " ms", ms < 4000); + public void testGuardC18_closeReturnsPromptly() throws Exception { + final ExecutorService closer = Executors.newSingleThreadExecutor(r -> { + final Thread t = new Thread(r, "C18-closer"); + t.setDaemon(true); + return t; + }); + try { + for (int i = 0; i < 5; i++) { + final HttpClientFactoryImpl factory = HttpClientFactoryImpl.build(); + boolean closeSubmitted = false; + try { + awaitWaiting(cleanerThreadOf(factory)); + final long start = System.nanoTime(); + final Future closing = closer.submit(factory::close); + closeSubmitted = true; + try { + closing.get(4, TimeUnit.SECONDS); + } catch (TimeoutException e) { + closing.cancel(true); + fail("close() did not return within 4 s"); + } + final long ms = (System.nanoTime() - start) / 1_000_000; + assertTrue("close() took " + ms + " ms", ms < 4000); + } finally { + if (!closeSubmitted) { + factory.close(); + } + } + } + } finally { + closer.shutdownNow(); } } - /** Waits (bounded) until a cleaner thread is parked in wait(), so close() cannot race its startup. */ - private static void awaitCleanerThreadWaiting() { - final long deadline = System.nanoTime() + 2_000_000_000L; + /** Waits (bounded) until the given cleaner thread is parked in wait(); fails if it never gets there. */ + private static void awaitWaiting(Thread cleaner) { + final long deadline = System.nanoTime() + 5_000_000_000L; while (System.nanoTime() < deadline) { - for (Thread t : Thread.getAllStackTraces().keySet()) { - if (t.getClass().getName().contains("IdleConnectionMonitorThread") - && t.getState() == Thread.State.TIMED_WAITING) { - return; - } + if (cleaner.getState() == Thread.State.TIMED_WAITING || cleaner.getState() == Thread.State.WAITING) { + return; } Thread.yield(); } + fail("the factory's cleaner thread did not reach its wait; state=" + cleaner.getState()); } } diff --git a/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java b/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java index 772801f7f..1b787271b 100644 --- a/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java +++ b/samples/src/test/java/com/backblaze/b2/sample/SampleFindingsTest.java @@ -2,9 +2,11 @@ * Copyright 2026, Backblaze Inc. All Rights Reserved. * License https://www.backblaze.com/using_b2_code.html * - * The samples module had no tests. Tests named "...Static" read the sample's source text, because the - * defects are in the body of main()-style methods that cannot be called without a real B2 account - * (or that call System.exit). They are source-pattern checks, not behaviour tests. + * The samples module had no tests. The B2 command-line sample is driven here through its private methods + * (reflection), with a mocked B2StorageClient and with System.exit turned into an exception, so the checks + * observe behaviour. The two B2Sample checks named "...Static" still read the sample's source text, because the + * defects are inside a long main()-style method that cannot be run without a real B2 account; they are + * source-pattern checks, not behaviour tests. */ package com.backblaze.b2.sample; @@ -13,11 +15,27 @@ import com.sun.net.httpserver.HttpServer; import org.junit.Test; +import com.backblaze.b2.client.B2ListFilesIterable; +import com.backblaze.b2.client.B2StorageClient; +import com.backblaze.b2.client.structures.B2Bucket; +import com.backblaze.b2.client.structures.B2FileVersion; +import com.backblaze.b2.client.structures.B2UploadFileRequest; +import org.mockito.ArgumentCaptor; + +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.PrintStream; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.security.Permission; +import java.util.Collections; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; @@ -27,6 +45,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; @@ -44,8 +68,12 @@ private static String source(String name) throws IOException { @Test public void testFindingC28_serverIgnoringRangeIsRejected() throws Exception { final byte[] whole = new byte[1000]; - final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 5); + final CountDownLatch requestSeen = new CountDownLatch(1); + final AtomicReference rangeHeader = new AtomicReference<>(); + final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), 5); server.createContext("/", ex -> { + rangeHeader.set(ex.getRequestHeaders().getFirst("Range")); + requestSeen.countDown(); ex.sendResponseHeaders(200, whole.length); // ignores Range, no 206 ex.getResponseBody().write(whole); ex.close(); @@ -54,15 +82,22 @@ public void testFindingC28_serverIgnoringRangeIsRejected() throws Exception { try { final String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/f"; final UrlContentSource part = new UrlContentSource.Builder(url, 100).setStart(200).build(); + IOException rejection = null; try (InputStream in = part.createInputStream()) { int total = 0; while (in.read() != -1) { total++; } fail("a 200 reply to a range request was accepted and returned " + total + " bytes for a 100-byte part"); - } catch (IOException expected) { - // expected: the sample should refuse a non-206 answer. + } catch (IOException e) { + rejection = e; } + // the exception only counts if the server really saw the range request (not a connection failure) + assertTrue("the server never received the request; the failure was: " + rejection, + requestSeen.await(5, TimeUnit.SECONDS)); + assertEquals("bytes=200-299", rangeHeader.get()); + assertTrue("expected the IOException to identify the unexpected 200 reply, but it was: " + rejection, + rejection.getMessage() != null && rejection.getMessage().contains("200")); } finally { server.stop(0); } @@ -110,41 +145,192 @@ public void testFindingC45_listsPartsOfLoopVariableStatic() throws IOException { assertTrue("parts are listed for another file:\n" + body, body.contains("client.parts(largeFileVersion.getFileId())")); } - /** Finding C38 (static): resuming must not use getContentSha1() ("none" for unfinished large files) as the source SHA-1. */ + /** Finding C38: resuming must not use getContentSha1() ("none" for unfinished large files) as the source SHA-1. */ @Category(KnownDefect.class) @Test - public void testFindingC38_finishUploadingUsesLargeFileSha1Static() throws IOException { - assertFalse("B2.java still passes getContentSha1() of the unfinished large file as the source SHA-1", - source("B2.java").contains(".setSha1(largeFileVersion.getContentSha1())")); + public void testFindingC38_finishUploadingDoesNotUseNoneAsSourceSha1() throws Exception { + final File local = File.createTempFile("c38", ".bin"); + try { + final B2FileVersion unfinished = new B2FileVersion("fileId1", "big.bin", 0L, "b2/x-auto", "none", null, + Collections.emptyMap(), "start", 1L, null, null, null, null); + final CapturedCommand cmd = runFinishUploading(unfinished, + new String[] {"bucket1", "fileId1", local.getPath()}); + assertNull("command exited: " + cmd.exitStatus, cmd.exitStatus); + assertEquals("the unfinished large file was not passed on", unfinished, cmd.finishedVersion); + final String sha1 = cmd.request.getContentSource().getSha1OrNull(); + assertFalse("the source SHA-1 given for the local file is the unfinished file's \"none\"", "none".equals(sha1)); + } finally { + //noinspection ResultOfMethodCallIgnored + local.delete(); + } } - /** Finding C39 (static): usage text of get_download_authorization must mention the duration the command requires. */ + /** Finding C39: usage text of get_download_authorization must mention the duration the command requires. */ @Category(KnownDefect.class) @Test - public void testFindingC39_downloadAuthorizationUsageMentionsDurationStatic() throws IOException { - final String src = source("B2.java"); - final Matcher m = Pattern.compile("\"\\s*b2 get_download_authorization([^\"]*)\\\\n\"").matcher(src); - assertTrue("usage line not found", m.find()); - assertTrue("usage says: " + m.group(1), m.group(1).contains("validDurationInSecs") || m.group(1).toLowerCase().contains("duration")); + public void testFindingC39_downloadAuthorizationUsageMentionsDuration() throws Exception { + final ByteArrayOutputStream err = new ByteArrayOutputStream(); + final PrintStream savedErr = System.err; + Integer exit = null; + final SecurityManager savedManager = System.getSecurityManager(); + System.setSecurityManager(new NoExitSecurityManager()); + System.setErr(new PrintStream(err, true, "UTF-8")); + try { + B2.main(new String[0]); // no command: prints the usage text and exits + } catch (ExitException e) { + exit = e.status; + } finally { + System.setErr(savedErr); + System.setSecurityManager(savedManager); + } + assertEquals("main() with no arguments should exit with status 1", Integer.valueOf(1), exit); + final Matcher m = Pattern.compile("b2 get_download_authorization([^\n]*)\n").matcher(err.toString("UTF-8")); + assertTrue("usage line for get_download_authorization not found in:\n" + err, m.find()); + assertTrue("usage says: " + m.group(1), m.group(1).toLowerCase().contains("duration")); } - /** Finding C40 (static): finish_uploading_large_file must accept as many arguments as its usage text lists (6). */ + /** Finding C40: finish_uploading_large_file must accept as many arguments as its usage text lists (6). */ @Category(KnownDefect.class) @Test - public void testFindingC40_finishUploadingAcceptsAllUsageArgumentsStatic() throws IOException { - final String src = source("B2.java"); - final int at = src.indexOf("private void finish_uploading_large_file"); - final Matcher m = Pattern.compile("checkArgCount\\(args, 3, (\\d+)\\)").matcher(src.substring(at, at + 400)); - assertTrue(m.find()); - // usage: [--noProgress] [--threads N] => up to 6 words - assertEquals(6, Integer.parseInt(m.group(1))); + public void testFindingC40_finishUploadingAcceptsAllUsageArguments() throws Exception { + final File local = File.createTempFile("c40", ".bin"); + try { + final B2FileVersion unfinished = new B2FileVersion("fileId1", "big.bin", 0L, "b2/x-auto", "none", null, + Collections.emptyMap(), "start", 1L, null, null, null, null); + // usage: [--noProgress] [--threads N] => 6 words + final CapturedCommand cmd = runFinishUploading(unfinished, + new String[] {"--noProgress", "--threads", "2", "bucket1", "fileId1", local.getPath()}); + assertNull("the command rejected the full list of arguments from its own usage text: " + cmd.exitStatus + + " / " + cmd.stderr, cmd.exitStatus); + assertNotNull("finishUploadingLargeFile was not called", cmd.request); + } finally { + //noinspection ResultOfMethodCallIgnored + local.delete(); + } } - /** Finding C41 (static): --info must accept values containing '=' (split must be limited to 2 parts). */ + /** Finding C41: --info must accept values containing '=' (the value is everything after the first '='). */ @Category(KnownDefect.class) @Test - public void testFindingC41_infoOptionAllowsEqualsInValueStatic() throws IOException { - assertFalse("B2.java still calls pair.split(\"=\") without a limit, so a value containing '=' is cut", - source("B2.java").contains("pair.split(\"=\")")); + public void testFindingC41_infoOptionAllowsEqualsInValue() throws Exception { + final File local = File.createTempFile("c41", ".bin"); + try { + final B2Bucket bucket = mock(B2Bucket.class); + when(bucket.getBucketName()).thenReturn("bucket1"); + when(bucket.getBucketId()).thenReturn("bucketId1"); + final B2StorageClient client = mock(B2StorageClient.class); + when(client.buckets()).thenReturn(Collections.singletonList(bucket)); + + final Object[] result = new Object[1]; + final CapturedCommand cmd = runCommand(client, "makeUploadRequestFromArgs", + new String[] {"--info", "key=a=b", "bucket1", local.getPath(), "b2name"}, result); + assertNull("the command exited instead of building the request: " + cmd.exitStatus + " / " + cmd.stderr, + cmd.exitStatus); + final B2UploadFileRequest request = (B2UploadFileRequest) result[0]; + assertNotNull(request); + assertEquals("a=b", request.getFileInfo().get("key")); + } finally { + //noinspection ResultOfMethodCallIgnored + local.delete(); + } + } + + // ---- helpers: drive the private B2 command methods without a real account and without exiting the JVM ---- + + private static final class ExitException extends SecurityException { + final int status; + + ExitException(int status) { + super("System.exit(" + status + ")"); + this.status = status; + } + } + + private static final class NoExitSecurityManager extends SecurityManager { + @Override + public void checkExit(int status) { + throw new ExitException(status); + } + + @Override + public void checkPermission(Permission perm) { + // allow everything else + } + } + + private static final class CapturedCommand { + Integer exitStatus; // null when the command did not call System.exit + String stderr = ""; + B2FileVersion finishedVersion; + B2UploadFileRequest request; + } + + private static CapturedCommand runFinishUploading(B2FileVersion unfinished, String[] args) throws Exception { + final B2Bucket bucket = mock(B2Bucket.class); + when(bucket.getBucketName()).thenReturn("bucket1"); + when(bucket.getBucketId()).thenReturn("bucketId1"); + final B2StorageClient client = mock(B2StorageClient.class); + when(client.buckets()).thenReturn(Collections.singletonList(bucket)); + final B2ListFilesIterable files = mock(B2ListFilesIterable.class); + when(files.iterator()).thenAnswer(inv -> Collections.singletonList(unfinished).iterator()); + when(client.unfinishedLargeFiles("bucketId1")).thenReturn(files); + + final CapturedCommand cmd = runCommand(client, "finish_uploading_large_file", args, null); + final ArgumentCaptor version = ArgumentCaptor.forClass(B2FileVersion.class); + final ArgumentCaptor request = ArgumentCaptor.forClass(B2UploadFileRequest.class); + if (cmd.exitStatus == null) { + verify(client).finishUploadingLargeFile(version.capture(), request.capture(), any(ExecutorService.class)); + cmd.finishedVersion = version.getValue(); + cmd.request = request.getValue(); + } + return cmd; + } + + /** Runs the named private method of a B2 instance that talks to the given client; System.exit becomes ExitException. */ + private static CapturedCommand runCommand(B2StorageClient client, + String methodName, + String[] args, + Object[] resultOut) throws Exception { + // B2's constructor builds a real client from B2_* environment variables, so create the instance + // without running it and set the fields the commands use. + final Field unsafeField = Class.forName("sun.misc.Unsafe").getDeclaredField("theUnsafe"); + unsafeField.setAccessible(true); + final Object unsafe = unsafeField.get(null); + final Method allocateInstance = unsafe.getClass().getMethod("allocateInstance", Class.class); + final Field clientField = B2.class.getDeclaredField("client"); + clientField.setAccessible(true); + final Field outField = B2.class.getDeclaredField("out"); + outField.setAccessible(true); + final Field threadsField = B2.class.getDeclaredField("numThreads"); + threadsField.setAccessible(true); + final Method method = B2.class.getDeclaredMethod(methodName, String[].class); + method.setAccessible(true); + + final CapturedCommand cmd = new CapturedCommand(); + final ByteArrayOutputStream err = new ByteArrayOutputStream(); + final PrintStream savedErr = System.err; + final SecurityManager savedManager = System.getSecurityManager(); + System.setSecurityManager(new NoExitSecurityManager()); + System.setErr(new PrintStream(err, true, "UTF-8")); + try (B2 b2 = (B2) allocateInstance.invoke(unsafe, B2.class)) { + clientField.set(b2, client); + outField.set(b2, new PrintStream(new ByteArrayOutputStream(), true, "UTF-8")); + threadsField.setInt(b2, 2); + final Object result = method.invoke(b2, (Object) args); + if (resultOut != null) { + resultOut[0] = result; + } + } catch (InvocationTargetException e) { + if (e.getCause() instanceof ExitException) { + cmd.exitStatus = ((ExitException) e.getCause()).status; + } else { + throw new AssertionError("B2 command " + methodName + " failed unexpectedly: " + e.getCause(), e.getCause()); + } + } finally { + System.setErr(savedErr); + System.setSecurityManager(savedManager); + } + cmd.stderr = err.toString("UTF-8"); + return cmd; } } From 78e3ac7e7f252aee27318920a2373e3348b8bf4a Mon Sep 17 00:00:00 2001 From: Felipe Fumero Date: Thu, 19 Nov 2026 08:53:47 -0600 Subject: [PATCH 4/5] test: cover equals in download file ID query values Add an independent equals-only file ID and assert the exact percent-encoded query. Preserve the existing ampersand cases and known-defect staging. --- .../backblaze/b2/client/B2WebifierUrlFindingsTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java b/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java index c9c6d96e0..95f50f95e 100644 --- a/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java +++ b/core/src/test/java/com/backblaze/b2/client/B2WebifierUrlFindingsTest.java @@ -29,6 +29,15 @@ public void testFindingC24_fileIdWithAmpersandIsEncodedInDownloadByIdUrl() throw assertFalse("raw & survived in: " + url, url.substring(url.indexOf("fileId=")).contains("&")); } + /** Finding C24: an "=" in a file id must be encoded as part of the query value. */ + @Category(KnownDefect.class) + @Test + public void testFindingC24_fileIdWithEqualsIsEncodedInDownloadByIdUrl() throws Exception { + final String url = webifier.getDownloadByIdUrl(B2TestHelpers.makeAuth(1), + B2DownloadByIdRequest.builder("4_zid=only").build()); + assertEquals("fileId=4_zid%3Donly", url.substring(url.indexOf('?') + 1)); + } + /** Finding C24: an "&" in a bucket name must be encoded in the download-by-name URL. */ @Category(KnownDefect.class) @Test From 08429dadc7d1ebbb870beb999544564fbbe4b581 Mon Sep 17 00:00:00 2001 From: scarreras Date: Thu, 19 Nov 2026 22:04:43 -0300 Subject: [PATCH 5/5] Narrow C26 known-defect test to the expected exception type Co-Authored-By: Claude Sonnet 5.5 --- .../src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java b/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java index 7a94b330c..43f94a598 100644 --- a/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java +++ b/core/src/test/java/com/backblaze/b2/json/B2JsonFindingsTest.java @@ -40,7 +40,7 @@ public void testFindingC26_versionedTypeFromUrlParameterMap() throws Exception { final Versioned v; try { v = B2Json.get().fromUrlParameterMap(Collections.singletonMap("x", "7"), Versioned.class, options); - } catch (RuntimeException | B2JsonException e) { + } catch (IllegalArgumentException | B2JsonException e) { fail("could not build a versioned type from url parameters: " + e); return; }