mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Add method for wiping the mailbox
This commit is contained in:
@@ -35,6 +35,14 @@ interface MailboxApi {
|
|||||||
boolean checkStatus(MailboxProperties properties)
|
boolean checkStatus(MailboxProperties properties)
|
||||||
throws IOException, ApiException;
|
throws IOException, ApiException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unpairs Briar and the mailbox (owner only).
|
||||||
|
* Resets mailbox state to that after first install
|
||||||
|
* (e.g. removes all stored files as well).
|
||||||
|
*/
|
||||||
|
void wipeMailbox(MailboxProperties properties)
|
||||||
|
throws IOException, ApiException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a new contact to the mailbox.
|
* Adds a new contact to the mailbox.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -105,6 +105,19 @@ class MailboxApiImpl implements MailboxApi {
|
|||||||
return response.isSuccessful();
|
return response.isSuccessful();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void wipeMailbox(MailboxProperties properties)
|
||||||
|
throws IOException, ApiException {
|
||||||
|
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||||
|
Request request = getRequestBuilder(properties.getAuthToken())
|
||||||
|
.url(properties.getOnionAddress() + "/")
|
||||||
|
.delete()
|
||||||
|
.build();
|
||||||
|
OkHttpClient client = httpClientProvider.get();
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.code() != 204) throw new ApiException();
|
||||||
|
}
|
||||||
|
|
||||||
/* Contact Management API (owner only) */
|
/* Contact Management API (owner only) */
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -194,6 +194,53 @@ public class MailboxApiTest extends BrambleTestCase {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWipe() throws Exception {
|
||||||
|
MockWebServer server = new MockWebServer();
|
||||||
|
server.enqueue(new MockResponse().setResponseCode(204));
|
||||||
|
server.enqueue(new MockResponse().setResponseCode(200));
|
||||||
|
server.enqueue(new MockResponse().setResponseCode(401));
|
||||||
|
server.enqueue(new MockResponse().setResponseCode(500));
|
||||||
|
server.start();
|
||||||
|
String baseUrl = getBaseUrl(server);
|
||||||
|
MailboxProperties properties =
|
||||||
|
new MailboxProperties(baseUrl, token, true);
|
||||||
|
MailboxProperties properties2 =
|
||||||
|
new MailboxProperties(baseUrl, token2, true);
|
||||||
|
|
||||||
|
api.wipeMailbox(properties);
|
||||||
|
RecordedRequest request1 = server.takeRequest();
|
||||||
|
assertEquals("/", request1.getPath());
|
||||||
|
assertEquals("DELETE", request1.getMethod());
|
||||||
|
assertToken(request1, token);
|
||||||
|
|
||||||
|
assertThrows(ApiException.class, () -> api.wipeMailbox(properties2));
|
||||||
|
RecordedRequest request2 = server.takeRequest();
|
||||||
|
assertEquals("/", request2.getPath());
|
||||||
|
assertEquals("DELETE", request2.getMethod());
|
||||||
|
assertToken(request2, token2);
|
||||||
|
|
||||||
|
assertThrows(ApiException.class, () -> api.wipeMailbox(properties));
|
||||||
|
RecordedRequest request3 = server.takeRequest();
|
||||||
|
assertEquals("/", request3.getPath());
|
||||||
|
assertEquals("DELETE", request3.getMethod());
|
||||||
|
assertToken(request3, token);
|
||||||
|
|
||||||
|
assertThrows(ApiException.class, () -> api.wipeMailbox(properties));
|
||||||
|
RecordedRequest request4 = server.takeRequest();
|
||||||
|
assertEquals("/", request4.getPath());
|
||||||
|
assertEquals("DELETE", request4.getMethod());
|
||||||
|
assertToken(request4, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWipeOnlyForOwner() {
|
||||||
|
MailboxProperties properties =
|
||||||
|
new MailboxProperties("", token, false);
|
||||||
|
assertThrows(IllegalArgumentException.class, () ->
|
||||||
|
api.wipeMailbox(properties));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddContact() throws Exception {
|
public void testAddContact() throws Exception {
|
||||||
MockWebServer server = new MockWebServer();
|
MockWebServer server = new MockWebServer();
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.mailbox.MailboxApi.MailboxContact;
|
|||||||
import org.briarproject.bramble.mailbox.MailboxApi.MailboxFile;
|
import org.briarproject.bramble.mailbox.MailboxApi.MailboxFile;
|
||||||
import org.briarproject.bramble.mailbox.MailboxApi.TolerableFailureException;
|
import org.briarproject.bramble.mailbox.MailboxApi.TolerableFailureException;
|
||||||
import org.briarproject.bramble.test.BrambleTestCase;
|
import org.briarproject.bramble.test.BrambleTestCase;
|
||||||
|
import org.junit.AfterClass;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
@@ -48,11 +49,12 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
|||||||
private final static MailboxId SETUP_TOKEN = MailboxId.fromString(
|
private final static MailboxId SETUP_TOKEN = MailboxId.fromString(
|
||||||
"54686973206973206120736574757020746f6b656e20666f722042726961722e");
|
"54686973206973206120736574757020746f6b656e20666f722042726961722e");
|
||||||
|
|
||||||
private final OkHttpClient client = new OkHttpClient.Builder()
|
private static final OkHttpClient client = new OkHttpClient.Builder()
|
||||||
.socketFactory(SocketFactory.getDefault())
|
.socketFactory(SocketFactory.getDefault())
|
||||||
.connectTimeout(60_000, MILLISECONDS)
|
.connectTimeout(60_000, MILLISECONDS)
|
||||||
.build();
|
.build();
|
||||||
private final WeakSingletonProvider<OkHttpClient> httpClientProvider =
|
private static final WeakSingletonProvider<OkHttpClient>
|
||||||
|
httpClientProvider =
|
||||||
new WeakSingletonProvider<OkHttpClient>() {
|
new WeakSingletonProvider<OkHttpClient>() {
|
||||||
@Override
|
@Override
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@@ -60,7 +62,8 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private final MailboxApiImpl api = new MailboxApiImpl(httpClientProvider);
|
private final static MailboxApiImpl api =
|
||||||
|
new MailboxApiImpl(httpClientProvider);
|
||||||
// needs to be static to keep values across different tests
|
// needs to be static to keep values across different tests
|
||||||
private static MailboxProperties ownerProperties;
|
private static MailboxProperties ownerProperties;
|
||||||
|
|
||||||
@@ -81,6 +84,23 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
|||||||
ownerProperties = new MailboxProperties(URL_BASE, ownerToken, true);
|
ownerProperties = new MailboxProperties(URL_BASE, ownerToken, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
// we can't test wiping as a regular test as it stops the mailbox
|
||||||
|
public static void wipe() throws IOException, ApiException {
|
||||||
|
if (!isOptionalTestEnabled(MailboxIntegrationTest.class)) return;
|
||||||
|
|
||||||
|
api.wipeMailbox(ownerProperties);
|
||||||
|
|
||||||
|
// check doesn't work anymore
|
||||||
|
assertThrows(ApiException.class, () ->
|
||||||
|
api.checkStatus(ownerProperties));
|
||||||
|
|
||||||
|
// new setup doesn't work as mailbox is stopping
|
||||||
|
MailboxProperties setupProperties =
|
||||||
|
new MailboxProperties(URL_BASE, SETUP_TOKEN, true);
|
||||||
|
assertThrows(ApiException.class, () -> api.setup(setupProperties));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStatus() throws Exception {
|
public void testStatus() throws Exception {
|
||||||
assertTrue(api.checkStatus(ownerProperties));
|
assertTrue(api.checkStatus(ownerProperties));
|
||||||
|
|||||||
Reference in New Issue
Block a user