mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Introduce MailboxId sub-classes for even more type-safety
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public class InvalidMailboxIdException extends Exception {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxAuthToken extends MailboxId {
|
||||
public MailboxAuthToken(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxAuthToken} from the given string.
|
||||
*
|
||||
* @throws InvalidMailboxIdException if token is not valid.
|
||||
*/
|
||||
public static MailboxAuthToken fromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
return new MailboxAuthToken(bytesFromString(token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxFileId extends MailboxId {
|
||||
public MailboxFileId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxFileId} from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
*/
|
||||
public static MailboxFileId fromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
return new MailboxFileId(bytesFromString(token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxFolderId extends MailboxId {
|
||||
public MailboxFolderId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxFolderId} from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
*/
|
||||
public static MailboxFolderId fromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
return new MailboxFolderId(bytesFromString(token));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
@@ -14,20 +15,26 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxId extends UniqueId {
|
||||
|
||||
public MailboxId(byte[] id) {
|
||||
public abstract class MailboxId extends UniqueId {
|
||||
MailboxId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxId} from the given string.
|
||||
* Returns valid {@link MailboxId} bytes from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
* @throws InvalidMailboxIdException if token is not valid.
|
||||
*/
|
||||
public static MailboxId fromString(String token) {
|
||||
if (token.length() != 64) throw new IllegalArgumentException();
|
||||
return new MailboxId(fromHexString(token));
|
||||
static byte[] bytesFromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
if (token == null || token.length() != 64) {
|
||||
throw new InvalidMailboxIdException();
|
||||
}
|
||||
try {
|
||||
return fromHexString(token);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new InvalidMailboxIdException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,9 +46,4 @@ public class MailboxId extends UniqueId {
|
||||
public String toString() {
|
||||
return toHexString(getBytes()).toLowerCase(Locale.US);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof MailboxId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import javax.annotation.concurrent.Immutable;
|
||||
public class MailboxProperties {
|
||||
|
||||
private final String onionAddress;
|
||||
private final MailboxId authToken;
|
||||
private final MailboxAuthToken authToken;
|
||||
private final boolean owner;
|
||||
|
||||
public MailboxProperties(String onionAddress, MailboxId authToken,
|
||||
public MailboxProperties(String onionAddress, MailboxAuthToken authToken,
|
||||
boolean owner) {
|
||||
this.onionAddress = onionAddress;
|
||||
this.authToken = authToken;
|
||||
@@ -23,7 +23,7 @@ public class MailboxProperties {
|
||||
return onionAddress;
|
||||
}
|
||||
|
||||
public MailboxId getAuthToken() {
|
||||
public MailboxAuthToken getAuthToken() {
|
||||
return authToken;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.identity.Identity;
|
||||
import org.briarproject.bramble.api.identity.LocalAuthor;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.sync.ClientId;
|
||||
@@ -215,10 +214,6 @@ public class TestUtils {
|
||||
getAgreementPublicKey(), verified);
|
||||
}
|
||||
|
||||
public static MailboxId getMailboxId() {
|
||||
return new MailboxId(getRandomId());
|
||||
}
|
||||
|
||||
public static void writeBytes(File file, byte[] bytes)
|
||||
throws IOException {
|
||||
FileOutputStream outputStream = new FileOutputStream(file);
|
||||
|
||||
@@ -3,7 +3,9 @@ package org.briarproject.bramble.mailbox;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFileId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
|
||||
import java.io.File;
|
||||
@@ -23,7 +25,7 @@ interface MailboxApi {
|
||||
* @return the owner token
|
||||
* @throws ApiException for 401 response.
|
||||
*/
|
||||
MailboxId setup(MailboxProperties properties)
|
||||
MailboxAuthToken setup(MailboxProperties properties)
|
||||
throws IOException, ApiException;
|
||||
|
||||
/**
|
||||
@@ -68,7 +70,7 @@ interface MailboxApi {
|
||||
* The owner can add files to the contacts' inboxes
|
||||
* and the contacts can add files to their own outbox.
|
||||
*/
|
||||
void addFile(MailboxProperties properties, MailboxId folderId,
|
||||
void addFile(MailboxProperties properties, MailboxFolderId folderId,
|
||||
File file) throws IOException, ApiException;
|
||||
|
||||
/**
|
||||
@@ -76,8 +78,8 @@ interface MailboxApi {
|
||||
* <p>
|
||||
* Returns 200 OK with the list of files in JSON.
|
||||
*/
|
||||
List<MailboxFile> getFiles(MailboxProperties properties, MailboxId folderId)
|
||||
throws IOException, ApiException;
|
||||
List<MailboxFile> getFiles(MailboxProperties properties,
|
||||
MailboxFolderId folderId) throws IOException, ApiException;
|
||||
|
||||
/**
|
||||
* Used by owner and contacts to retrieve a file.
|
||||
@@ -87,8 +89,8 @@ interface MailboxApi {
|
||||
*
|
||||
* @param file the empty file the response bytes will be written into.
|
||||
*/
|
||||
void getFile(MailboxProperties properties, MailboxId folderId,
|
||||
MailboxId fileId, File file) throws IOException, ApiException;
|
||||
void getFile(MailboxProperties properties, MailboxFolderId folderId,
|
||||
MailboxFileId fileId, File file) throws IOException, ApiException;
|
||||
|
||||
/**
|
||||
* Used by owner and contacts to delete files.
|
||||
@@ -98,8 +100,8 @@ interface MailboxApi {
|
||||
* @throws TolerableFailureException on 404 response,
|
||||
* because file was most likely deleted already.
|
||||
*/
|
||||
void deleteFile(MailboxProperties properties, MailboxId folderId,
|
||||
MailboxId fileId)
|
||||
void deleteFile(MailboxProperties properties, MailboxFolderId folderId,
|
||||
MailboxFileId fileId)
|
||||
throws IOException, ApiException, TolerableFailureException;
|
||||
|
||||
/**
|
||||
@@ -107,22 +109,23 @@ interface MailboxApi {
|
||||
* for the owner to download.
|
||||
*
|
||||
* @return a list of folder names
|
||||
* to be used with {@link #getFiles(MailboxProperties, MailboxId)}.
|
||||
* to be used with {@link #getFiles(MailboxProperties, MailboxFolderId)}.
|
||||
* @throws IllegalArgumentException if used by non-owner.
|
||||
*/
|
||||
List<MailboxId> getFolders(MailboxProperties properties)
|
||||
List<MailboxFolderId> getFolders(MailboxProperties properties)
|
||||
throws IOException, ApiException;
|
||||
|
||||
@Immutable
|
||||
@JsonSerialize
|
||||
class MailboxContact {
|
||||
public final int contactId;
|
||||
public final MailboxId token, inboxId, outboxId;
|
||||
public final MailboxAuthToken token;
|
||||
public final MailboxFolderId inboxId, outboxId;
|
||||
|
||||
MailboxContact(ContactId contactId,
|
||||
MailboxId token,
|
||||
MailboxId inboxId,
|
||||
MailboxId outboxId) {
|
||||
MailboxAuthToken token,
|
||||
MailboxFolderId inboxId,
|
||||
MailboxFolderId outboxId) {
|
||||
this.contactId = contactId.getInt();
|
||||
this.token = token;
|
||||
this.inboxId = inboxId;
|
||||
@@ -132,10 +135,10 @@ interface MailboxApi {
|
||||
|
||||
@JsonSerialize
|
||||
class MailboxFile implements Comparable<MailboxFile> {
|
||||
public final MailboxId name;
|
||||
public final MailboxFileId name;
|
||||
public final long time;
|
||||
|
||||
public MailboxFile(MailboxId name, long time) {
|
||||
public MailboxFile(MailboxFileId name, long time) {
|
||||
this.name = name;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.mailbox.InvalidMailboxIdException;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFileId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
@@ -33,7 +37,6 @@ import static com.fasterxml.jackson.databind.MapperFeature.BLOCK_UNSAFE_POLYMORP
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static okhttp3.internal.Util.EMPTY_REQUEST;
|
||||
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
|
||||
@NotNullByDefault
|
||||
class MailboxApiImpl implements MailboxApi {
|
||||
@@ -53,7 +56,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public MailboxId setup(MailboxProperties properties)
|
||||
public MailboxAuthToken setup(MailboxProperties properties)
|
||||
throws IOException, ApiException {
|
||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||
Request request = getRequestBuilder(properties.getAuthToken())
|
||||
@@ -74,28 +77,12 @@ class MailboxApiImpl implements MailboxApi {
|
||||
throw new ApiException();
|
||||
}
|
||||
String ownerToken = tokenNode.textValue();
|
||||
if (ownerToken == null || !isValidToken(ownerToken)) {
|
||||
throw new ApiException();
|
||||
}
|
||||
return MailboxId.fromString(ownerToken);
|
||||
} catch (JacksonException e) {
|
||||
return MailboxAuthToken.fromString(ownerToken);
|
||||
} catch (JacksonException | InvalidMailboxIdException e) {
|
||||
throw new ApiException();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO find a batter way to validate (regex?)
|
||||
// that doesn't do hex string conversion twice
|
||||
private boolean isValidToken(String token) {
|
||||
if (token.length() != 64) return false;
|
||||
try {
|
||||
// try to convert to bytes
|
||||
fromHexString(token);
|
||||
return true;
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkStatus(MailboxProperties properties)
|
||||
throws IOException, ApiException {
|
||||
@@ -109,8 +96,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
|
||||
@Override
|
||||
public void addContact(MailboxProperties properties, MailboxContact contact)
|
||||
throws IOException, ApiException,
|
||||
TolerableFailureException {
|
||||
throws IOException, ApiException, TolerableFailureException {
|
||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||
byte[] bodyBytes = mapper.writeValueAsBytes(contact);
|
||||
RequestBody body = RequestBody.create(JSON, bodyBytes);
|
||||
@@ -163,7 +149,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
/* File Management (owner and contacts) */
|
||||
|
||||
@Override
|
||||
public void addFile(MailboxProperties properties, MailboxId folderId,
|
||||
public void addFile(MailboxProperties properties, MailboxFolderId folderId,
|
||||
File file) throws IOException, ApiException {
|
||||
String path = "/files/" + folderId;
|
||||
RequestBody body = RequestBody.create(FILE, file);
|
||||
@@ -173,7 +159,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
|
||||
@Override
|
||||
public List<MailboxFile> getFiles(MailboxProperties properties,
|
||||
MailboxId folderId) throws IOException, ApiException {
|
||||
MailboxFolderId folderId) throws IOException, ApiException {
|
||||
String path = "/files/" + folderId;
|
||||
Response response = sendGetRequest(properties, path);
|
||||
if (response.code() != 200) throw new ApiException();
|
||||
@@ -197,20 +183,19 @@ class MailboxApiImpl implements MailboxApi {
|
||||
}
|
||||
String name = nameNode.asText();
|
||||
long time = timeNode.asLong();
|
||||
if (!isValidToken(name)) throw new ApiException();
|
||||
if (time < 1) throw new ApiException();
|
||||
list.add(new MailboxFile(MailboxId.fromString(name), time));
|
||||
list.add(new MailboxFile(MailboxFileId.fromString(name), time));
|
||||
}
|
||||
Collections.sort(list);
|
||||
return list;
|
||||
} catch (JacksonException e) {
|
||||
} catch (JacksonException | InvalidMailboxIdException e) {
|
||||
throw new ApiException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getFile(MailboxProperties properties, MailboxId folderId,
|
||||
MailboxId fileId, File file) throws IOException, ApiException {
|
||||
public void getFile(MailboxProperties properties, MailboxFolderId folderId,
|
||||
MailboxFileId fileId, File file) throws IOException, ApiException {
|
||||
String path = "/files/" + folderId + "/" + fileId;
|
||||
Response response = sendGetRequest(properties, path);
|
||||
if (response.code() != 200) throw new ApiException();
|
||||
@@ -222,8 +207,8 @@ class MailboxApiImpl implements MailboxApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(MailboxProperties properties, MailboxId folderId,
|
||||
MailboxId fileId)
|
||||
public void deleteFile(MailboxProperties properties,
|
||||
MailboxFolderId folderId, MailboxFileId fileId)
|
||||
throws IOException, ApiException, TolerableFailureException {
|
||||
String path = "/files/" + folderId + "/" + fileId;
|
||||
Request request = getRequestBuilder(properties.getAuthToken())
|
||||
@@ -237,7 +222,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MailboxId> getFolders(MailboxProperties properties)
|
||||
public List<MailboxFolderId> getFolders(MailboxProperties properties)
|
||||
throws IOException, ApiException {
|
||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||
Response response = sendGetRequest(properties, "/folders");
|
||||
@@ -248,7 +233,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
try {
|
||||
JsonNode node = mapper.readTree(body.string());
|
||||
ArrayNode filesNode = getArray(node, "folders");
|
||||
List<MailboxId> list = new ArrayList<>();
|
||||
List<MailboxFolderId> list = new ArrayList<>();
|
||||
for (JsonNode fileNode : filesNode) {
|
||||
if (!fileNode.isObject()) throw new ApiException();
|
||||
ObjectNode objectNode = (ObjectNode) fileNode;
|
||||
@@ -257,11 +242,10 @@ class MailboxApiImpl implements MailboxApi {
|
||||
throw new ApiException();
|
||||
}
|
||||
String id = idNode.asText();
|
||||
if (!isValidToken(id)) throw new ApiException();
|
||||
list.add(MailboxId.fromString(id));
|
||||
list.add(MailboxFolderId.fromString(id));
|
||||
}
|
||||
return list;
|
||||
} catch (JacksonException e) {
|
||||
} catch (JacksonException | InvalidMailboxIdException e) {
|
||||
throw new ApiException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ package org.briarproject.bramble.mailbox;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.mailbox.InvalidMailboxIdException;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxStatus;
|
||||
@@ -44,8 +45,12 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
|
||||
String onion = s.get(SETTINGS_KEY_ONION);
|
||||
String token = s.get(SETTINGS_KEY_TOKEN);
|
||||
if (isNullOrEmpty(onion) || isNullOrEmpty(token)) return null;
|
||||
MailboxId tokenId = MailboxId.fromString(token);
|
||||
return new MailboxProperties(onion, tokenId, true);
|
||||
try {
|
||||
MailboxAuthToken tokenId = MailboxAuthToken.fromString(token);
|
||||
return new MailboxProperties(onion, tokenId, true);
|
||||
} catch (InvalidMailboxIdException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFileId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.mailbox.MailboxApi.ApiException;
|
||||
@@ -32,8 +35,8 @@ import okio.Buffer;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.readBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.writeBytes;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
@@ -63,12 +66,15 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
};
|
||||
private final MailboxApiImpl api = new MailboxApiImpl(httpClientProvider);
|
||||
|
||||
private final MailboxId token = getMailboxId();
|
||||
private final MailboxId token2 = getMailboxId();
|
||||
private final MailboxAuthToken token = new MailboxAuthToken(getRandomId());
|
||||
private final MailboxAuthToken token2 = new MailboxAuthToken(getRandomId());
|
||||
private final ContactId contactId = getContactId();
|
||||
private final MailboxId contactToken = getMailboxId();
|
||||
private final MailboxId contactInboxId = getMailboxId();
|
||||
private final MailboxId contactOutboxId = getMailboxId();
|
||||
private final MailboxAuthToken contactToken =
|
||||
new MailboxAuthToken(getRandomId());
|
||||
private final MailboxFolderId contactInboxId =
|
||||
new MailboxFolderId(getRandomId());
|
||||
private final MailboxFolderId contactOutboxId =
|
||||
new MailboxFolderId(getRandomId());
|
||||
private final MailboxContact mailboxContact = new MailboxContact(
|
||||
contactId, contactToken, contactInboxId, contactOutboxId);
|
||||
|
||||
@@ -428,9 +434,11 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetFiles() throws Exception {
|
||||
MailboxFile mailboxFile1 = new MailboxFile(getMailboxId(), 1337);
|
||||
MailboxFile mailboxFile1 =
|
||||
new MailboxFile(new MailboxFileId(getRandomId()), 1337);
|
||||
MailboxFile mailboxFile2 =
|
||||
new MailboxFile(getMailboxId(), System.currentTimeMillis());
|
||||
new MailboxFile(new MailboxFileId(getRandomId()),
|
||||
System.currentTimeMillis());
|
||||
String fileResponse1 =
|
||||
new ObjectMapper().writeValueAsString(mailboxFile1);
|
||||
String fileResponse2 =
|
||||
@@ -539,7 +547,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetFile() throws Exception {
|
||||
MailboxId name = getMailboxId();
|
||||
MailboxFileId name = new MailboxFileId(getRandomId());
|
||||
File file1 = folder.newFile();
|
||||
File file2 = folder.newFile();
|
||||
File file3 = folder.newFile();
|
||||
@@ -586,7 +594,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testDeleteFile() throws Exception {
|
||||
MailboxId name = getMailboxId();
|
||||
MailboxFileId name = new MailboxFileId(getRandomId());
|
||||
|
||||
MockWebServer server = new MockWebServer();
|
||||
server.enqueue(new MockResponse());
|
||||
@@ -636,8 +644,8 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testGetFolders() throws Exception {
|
||||
MailboxId id1 = getMailboxId();
|
||||
MailboxId id2 = getMailboxId();
|
||||
MailboxFolderId id1 = new MailboxFolderId(getRandomId());
|
||||
MailboxFolderId id2 = new MailboxFolderId(getRandomId());
|
||||
String validResponse1 = "{\"folders\": [ {\"id\": \"" + id1 + "\"} ] }";
|
||||
String validResponse2 = "{\"folders\": [ {\"id\": \"" + id1 + "\"}, " +
|
||||
"{ \"id\": \"" + id2 + "\"} ] }";
|
||||
|
||||
@@ -2,7 +2,10 @@ package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.WeakSingletonProvider;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.mailbox.InvalidMailboxIdException;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFileId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxFolderId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.mailbox.MailboxApi.ApiException;
|
||||
import org.briarproject.bramble.mailbox.MailboxApi.MailboxContact;
|
||||
@@ -28,8 +31,8 @@ import okhttp3.OkHttpClient;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.isOptionalTestEnabled;
|
||||
import static org.briarproject.bramble.test.TestUtils.readBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.writeBytes;
|
||||
@@ -45,8 +48,16 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
private final static String URL_BASE = "http://127.0.0.1:8000";
|
||||
private final static MailboxId SETUP_TOKEN = MailboxId.fromString(
|
||||
"54686973206973206120736574757020746f6b656e20666f722042726961722e");
|
||||
private final static MailboxAuthToken SETUP_TOKEN;
|
||||
|
||||
static {
|
||||
try {
|
||||
SETUP_TOKEN = MailboxAuthToken.fromString(
|
||||
"54686973206973206120736574757020746f6b656e20666f722042726961722e");
|
||||
} catch (InvalidMailboxIdException e) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
private final OkHttpClient client = new OkHttpClient.Builder()
|
||||
.socketFactory(SocketFactory.getDefault())
|
||||
@@ -77,7 +88,7 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
if (ownerProperties != null) return;
|
||||
MailboxProperties setupProperties =
|
||||
new MailboxProperties(URL_BASE, SETUP_TOKEN, true);
|
||||
MailboxId ownerToken = api.setup(setupProperties);
|
||||
MailboxAuthToken ownerToken = api.setup(setupProperties);
|
||||
ownerProperties = new MailboxProperties(URL_BASE, ownerToken, true);
|
||||
}
|
||||
|
||||
@@ -133,7 +144,7 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
List<MailboxFile> files1 =
|
||||
api.getFiles(contactProperties, contact.inboxId);
|
||||
assertEquals(1, files1.size());
|
||||
MailboxId fileName1 = files1.get(0).name;
|
||||
MailboxFileId fileName1 = files1.get(0).name;
|
||||
|
||||
// owner can't check files
|
||||
assertThrows(ApiException.class, () ->
|
||||
@@ -171,15 +182,15 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
api.addFile(contactProperties, contact.outboxId, file3);
|
||||
|
||||
// owner checks folders with available files
|
||||
List<MailboxId> folders = api.getFolders(ownerProperties);
|
||||
List<MailboxFolderId> folders = api.getFolders(ownerProperties);
|
||||
assertEquals(singletonList(contact.outboxId), folders);
|
||||
|
||||
// owner lists files in contact's outbox
|
||||
List<MailboxFile> files2 =
|
||||
api.getFiles(ownerProperties, contact.outboxId);
|
||||
assertEquals(2, files2.size());
|
||||
MailboxId file2name = files2.get(0).name;
|
||||
MailboxId file3name = files2.get(1).name;
|
||||
MailboxFileId file2name = files2.get(0).name;
|
||||
MailboxFileId file3name = files2.get(1).name;
|
||||
|
||||
// contact can't list files in contact's outbox
|
||||
assertThrows(ApiException.class, () ->
|
||||
@@ -232,8 +243,10 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
private MailboxContact getMailboxContact(ContactId contactId) {
|
||||
return new MailboxContact(contactId, getMailboxId(), getMailboxId(),
|
||||
getMailboxId());
|
||||
MailboxAuthToken authToken = new MailboxAuthToken(getRandomId());
|
||||
MailboxFolderId inboxId = new MailboxFolderId(getRandomId());
|
||||
MailboxFolderId outboxId = new MailboxFolderId(getRandomId());
|
||||
return new MailboxContact(contactId, authToken, inboxId, outboxId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.briarproject.bramble.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxId;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxAuthToken;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxSettingsManager;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxStatus;
|
||||
@@ -21,7 +21,7 @@ import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTIN
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_KEY_TOKEN;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_NAMESPACE;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_UPLOADS_NAMESPACE;
|
||||
import static org.briarproject.bramble.test.TestUtils.getMailboxId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -37,7 +37,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
new MailboxSettingsManagerImpl(settingsManager);
|
||||
private final Random random = new Random();
|
||||
private final String onion = getRandomString(64);
|
||||
private final MailboxId token = getMailboxId();
|
||||
private final MailboxAuthToken token = new MailboxAuthToken(getRandomId());
|
||||
private final ContactId contactId1 = new ContactId(random.nextInt());
|
||||
private final ContactId contactId2 = new ContactId(random.nextInt());
|
||||
private final ContactId contactId3 = new ContactId(random.nextInt());
|
||||
@@ -195,7 +195,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(onion, filename1);
|
||||
String filename2 = manager.getPendingUpload(txn, contactId2);
|
||||
assertNotNull(filename2);
|
||||
assertEquals(token, MailboxId.fromString(filename2));
|
||||
assertEquals(token.toString(), filename2);
|
||||
String filename3 = manager.getPendingUpload(txn, contactId3);
|
||||
assertNull(filename3);
|
||||
String filename4 =
|
||||
|
||||
Reference in New Issue
Block a user