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..f4b5dc212 --- /dev/null +++ b/httpclient/src/test/java/com/backblaze/b2/client/webApiHttpClient/HttpClientFactoryFindingsTest.java @@ -0,0 +1,67 @@ +/* + * 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). + * 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(); + } + } +} 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 { +}