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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions buildSrc/src/main/kotlin/b2sdk.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>("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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
Original file line number Diff line number Diff line change
@@ -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]);
}
}
Original file line number Diff line number Diff line change
@@ -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<B2CancellationToken> 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<B2PartStorer> 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.<B2PartStorer>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.<B2PartStorer>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.<B2PartStorer>asList(
new FakePartStorer(1, 10, release, true),
new FakePartStorer(2, 10, release, false),
new FakePartStorer(3, 10, release, false)));
final CompletableFuture<List<B2Part>> 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<B2PartStorer> outOfOrder = new ArrayList<>(Arrays.<B2PartStorer>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));
}
}
Loading
Loading