mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Compare commits
3 Commits
feature-fl
...
print-db-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6fa7520e9 | ||
|
|
d3bffaadf3 | ||
|
|
12b887881d |
@@ -1,8 +1,14 @@
|
||||
package org.briarproject.bramble.api;
|
||||
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public abstract class StringMap extends Hashtable<String, String> {
|
||||
|
||||
protected StringMap(Map<String, String> m) {
|
||||
@@ -52,4 +58,31 @@ public abstract class StringMap extends Hashtable<String, String> {
|
||||
public void putLong(String key, long value) {
|
||||
put(key, String.valueOf(value));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public int[] getIntArray(String key) {
|
||||
String s = get(key);
|
||||
if (s == null) return null;
|
||||
// Handle empty string because "".split(",") returns {""}
|
||||
if (s.length() == 0) return new int[0];
|
||||
String[] intStrings = s.split(",");
|
||||
int[] ints = new int[intStrings.length];
|
||||
try {
|
||||
for (int i = 0; i < ints.length; i++) {
|
||||
ints[i] = Integer.parseInt(intStrings[i]);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
return ints;
|
||||
}
|
||||
|
||||
public void putIntArray(String key, int[] value) {
|
||||
List<String> intStrings = new ArrayList<>();
|
||||
for (int integer : value) {
|
||||
intStrings.add(String.valueOf(integer));
|
||||
}
|
||||
// Puts empty string if input array value is empty
|
||||
put(key, StringUtils.join(intStrings, ","));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -746,4 +746,6 @@ public interface DatabaseComponent extends TransactionManager {
|
||||
*/
|
||||
void updateTransportKeys(Transaction txn, Collection<TransportKeySet> keys)
|
||||
throws DbException;
|
||||
|
||||
void printStats(Transaction txn) throws DbException;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@@ -11,12 +13,14 @@ public class MailboxProperties {
|
||||
private final String baseUrl;
|
||||
private final MailboxAuthToken authToken;
|
||||
private final boolean owner;
|
||||
private final List<MailboxVersion> serverSupports;
|
||||
|
||||
public MailboxProperties(String baseUrl, MailboxAuthToken authToken,
|
||||
boolean owner) {
|
||||
boolean owner, List<MailboxVersion> serverSupports) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.authToken = authToken;
|
||||
this.owner = owner;
|
||||
this.serverSupports = serverSupports;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
@@ -35,4 +39,8 @@ public class MailboxProperties {
|
||||
public boolean isOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public List<MailboxVersion> getServerSupports() {
|
||||
return serverSupports;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class MailboxVersion implements Comparable<MailboxVersion> {
|
||||
|
||||
private final int major;
|
||||
private final int minor;
|
||||
|
||||
public MailboxVersion(int major, int minor) {
|
||||
this.major = major;
|
||||
this.minor = minor;
|
||||
}
|
||||
|
||||
public int getMajor() {
|
||||
return major;
|
||||
}
|
||||
|
||||
public int getMinor() {
|
||||
return minor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof MailboxVersion) {
|
||||
MailboxVersion v = (MailboxVersion) o;
|
||||
return major == v.major && minor == v.minor;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(MailboxVersion v) {
|
||||
int c = major - v.major;
|
||||
if (c != 0) {
|
||||
return c;
|
||||
}
|
||||
return minor - v.minor;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ 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.MailboxProperties;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxPropertiesUpdate;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
@@ -291,6 +292,18 @@ public class TestUtils {
|
||||
a.getOutboxId().equals(b.getOutboxId());
|
||||
}
|
||||
|
||||
public static boolean mailboxPropertiesEqual(
|
||||
@Nullable MailboxProperties a,
|
||||
@Nullable MailboxProperties b) {
|
||||
if (a == null || b == null) {
|
||||
return a == b;
|
||||
}
|
||||
return a.getOnion().equals(b.getOnion()) &&
|
||||
a.getAuthToken().equals(b.getAuthToken()) &&
|
||||
a.isOwner() == b.isOwner() &&
|
||||
a.getServerSupports().equals(b.getServerSupports());
|
||||
}
|
||||
|
||||
public static boolean hasEvent(Transaction txn,
|
||||
Class<? extends Event> eventClass) {
|
||||
for (CommitAction action : txn.getActions()) {
|
||||
|
||||
@@ -862,4 +862,6 @@ interface Database<T> {
|
||||
* Stores the given transport keys, deleting any keys they have replaced.
|
||||
*/
|
||||
void updateTransportKeys(T txn, TransportKeySet ks) throws DbException;
|
||||
|
||||
void printStats(T txn) throws DbException;
|
||||
}
|
||||
|
||||
@@ -1256,6 +1256,12 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStats(Transaction transaction) throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
db.printStats(txn);
|
||||
}
|
||||
|
||||
private class CommitActionVisitor implements Visitor {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,8 +13,12 @@ import org.briarproject.bramble.util.StringUtils;
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -26,6 +30,7 @@ import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.IoUtils.isNonEmptyDirectory;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
||||
|
||||
/**
|
||||
@@ -101,6 +106,73 @@ class H2Database extends JdbcDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStats(Connection txn) throws DbException {
|
||||
List<String> names = printNames(txn);
|
||||
for (String table : names) {
|
||||
tryPrintStats(txn, table);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> printNames(Connection txn) throws DbException {
|
||||
List<String> names = new ArrayList<>();
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql =
|
||||
"SELECT table_catalog, table_schema, table_name FROM INFORMATION_SCHEMA.TABLES";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
String catalog = rs.getString(1);
|
||||
String schema = rs.getString(2);
|
||||
String name = rs.getString(3);
|
||||
LOG.info(
|
||||
String.format("Table %s %s %s", catalog, schema, name));
|
||||
names.add(schema + "." + name);
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
tryToClose(ps, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
private void tryPrintStats(Connection txn, String table) {
|
||||
try {
|
||||
printStats(txn, table);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void printStats(Connection txn, String table) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "CALL DISK_SPACE_USED(?)";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setString(1, table);
|
||||
rs = ps.executeQuery();
|
||||
if (!rs.next()) {
|
||||
rs.close();
|
||||
ps.close();
|
||||
}
|
||||
long size = rs.getLong(1);
|
||||
if (rs.next()) throw new DbStateException();
|
||||
rs.close();
|
||||
ps.close();
|
||||
LOG.info(String.format("Size of table %s: %d", table, size));
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs, LOG, WARNING);
|
||||
tryToClose(ps, LOG, WARNING);
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection createConnection() throws DbException, SQLException {
|
||||
SecretKey key = this.key;
|
||||
|
||||
@@ -93,6 +93,11 @@ class HyperSqlDatabase extends JdbcDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStats(Connection txn) throws DbException {
|
||||
// Not implemented
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Connection createConnection() throws DbException, SQLException {
|
||||
SecretKey key = this.key;
|
||||
|
||||
@@ -25,7 +25,7 @@ interface MailboxApi {
|
||||
* @return the owner token
|
||||
* @throws ApiException for 401 response.
|
||||
*/
|
||||
MailboxAuthToken setup(MailboxProperties properties)
|
||||
MailboxProperties setup(MailboxProperties properties)
|
||||
throws IOException, ApiException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@ 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.mailbox.MailboxVersion;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
@@ -56,7 +57,7 @@ class MailboxApiImpl implements MailboxApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public MailboxAuthToken setup(MailboxProperties properties)
|
||||
public MailboxProperties setup(MailboxProperties properties)
|
||||
throws IOException, ApiException {
|
||||
if (!properties.isOwner()) throw new IllegalArgumentException();
|
||||
Request request = getRequestBuilder(properties.getAuthToken())
|
||||
@@ -75,8 +76,27 @@ class MailboxApiImpl implements MailboxApi {
|
||||
if (tokenNode == null) {
|
||||
throw new ApiException();
|
||||
}
|
||||
String ownerToken = tokenNode.textValue();
|
||||
return MailboxAuthToken.fromString(ownerToken);
|
||||
List<MailboxVersion> serverSupports = new ArrayList<>();
|
||||
ArrayNode serverSupportsNode = getArray(node, "serverSupports");
|
||||
for (JsonNode versionNode : serverSupportsNode) {
|
||||
if (!versionNode.isObject()) throw new ApiException();
|
||||
ObjectNode objectNode = (ObjectNode) versionNode;
|
||||
JsonNode majorNode = objectNode.get("major");
|
||||
JsonNode minorNode = objectNode.get("minor");
|
||||
if (majorNode == null || !majorNode.isNumber()) {
|
||||
throw new ApiException();
|
||||
}
|
||||
if (minorNode == null || !minorNode.isNumber()) {
|
||||
throw new ApiException();
|
||||
}
|
||||
int major = majorNode.asInt();
|
||||
int minor = minorNode.asInt();
|
||||
if (major < 0 || minor < 0) throw new ApiException();
|
||||
serverSupports.add(new MailboxVersion(major, minor));
|
||||
}
|
||||
return new MailboxProperties(properties.getBaseUrl(),
|
||||
MailboxAuthToken.fromString(tokenNode.textValue()),
|
||||
true, serverSupports);
|
||||
} catch (JacksonException | InvalidMailboxIdException e) {
|
||||
throw new ApiException();
|
||||
}
|
||||
|
||||
@@ -115,9 +115,7 @@ class MailboxPairingTaskImpl implements MailboxPairingTask {
|
||||
private void pairMailbox() throws IOException, ApiException, DbException {
|
||||
MailboxProperties mailboxProperties = decodeQrCodePayload(payload);
|
||||
setState(new MailboxPairingState.Pairing());
|
||||
MailboxAuthToken ownerToken = api.setup(mailboxProperties);
|
||||
MailboxProperties ownerProperties = new MailboxProperties(
|
||||
mailboxProperties.getBaseUrl(), ownerToken, true);
|
||||
MailboxProperties ownerProperties = api.setup(mailboxProperties);
|
||||
long time = clock.currentTimeMillis();
|
||||
db.transaction(false, txn -> {
|
||||
mailboxSettingsManager
|
||||
@@ -182,7 +180,8 @@ class MailboxPairingTaskImpl implements MailboxPairingTask {
|
||||
String baseUrl = "http://" + onion + ".onion";
|
||||
byte[] tokenBytes = Arrays.copyOfRange(bytes, 33, 65);
|
||||
MailboxAuthToken setupToken = new MailboxAuthToken(tokenBytes);
|
||||
return new MailboxProperties(baseUrl, setupToken, true);
|
||||
return new MailboxProperties(baseUrl, setupToken, true,
|
||||
new ArrayList<>());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@ 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;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxVersion;
|
||||
import org.briarproject.bramble.api.mailbox.OwnMailboxConnectionStatusEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
@@ -30,6 +32,7 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
|
||||
static final String SETTINGS_NAMESPACE = "mailbox";
|
||||
static final String SETTINGS_KEY_ONION = "onion";
|
||||
static final String SETTINGS_KEY_TOKEN = "token";
|
||||
static final String SETTINGS_KEY_SERVER_SUPPORTS = "serverSupports";
|
||||
static final String SETTINGS_KEY_LAST_ATTEMPT = "lastAttempt";
|
||||
static final String SETTINGS_KEY_LAST_SUCCESS = "lastSuccess";
|
||||
static final String SETTINGS_KEY_ATTEMPTS = "attempts";
|
||||
@@ -55,9 +58,19 @@ 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;
|
||||
int[] ints = s.getIntArray(SETTINGS_KEY_SERVER_SUPPORTS);
|
||||
// We know we were paired, so we must have proper serverSupports
|
||||
// TODO is throwing sensible? But it's done like that below on "parse error"
|
||||
if (ints == null || ints.length == 0 || ints.length % 2 != 0) {
|
||||
throw new DbException();
|
||||
}
|
||||
List<MailboxVersion> serverSupports = new ArrayList<>();
|
||||
for (int i = 0; i < ints.length - 1; i += 2) {
|
||||
serverSupports.add(new MailboxVersion(ints[i], ints[i + 1]));
|
||||
}
|
||||
try {
|
||||
MailboxAuthToken tokenId = MailboxAuthToken.fromString(token);
|
||||
return new MailboxProperties(onion, tokenId, true);
|
||||
return new MailboxProperties(onion, tokenId, true, serverSupports);
|
||||
} catch (InvalidMailboxIdException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
@@ -69,6 +82,14 @@ class MailboxSettingsManagerImpl implements MailboxSettingsManager {
|
||||
Settings s = new Settings();
|
||||
s.put(SETTINGS_KEY_ONION, p.getBaseUrl());
|
||||
s.put(SETTINGS_KEY_TOKEN, p.getAuthToken().toString());
|
||||
List<MailboxVersion> serverSupports = p.getServerSupports();
|
||||
int[] ints = new int[serverSupports.size() * 2];
|
||||
int i = 0;
|
||||
for (MailboxVersion v : serverSupports) {
|
||||
ints[i++] = v.getMajor();
|
||||
ints[i++] = v.getMinor();
|
||||
}
|
||||
s.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS, ints);
|
||||
settingsManager.mergeSettings(txn, s, SETTINGS_NAMESPACE);
|
||||
for (MailboxHook hook : hooks) {
|
||||
hook.mailboxPaired(txn, p.getOnion());
|
||||
|
||||
@@ -37,6 +37,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.mailboxPropertiesEqual;
|
||||
import static org.briarproject.bramble.test.TestUtils.readBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.writeBytes;
|
||||
import static org.briarproject.bramble.util.StringUtils.getRandomString;
|
||||
@@ -80,11 +81,20 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
|
||||
@Test
|
||||
public void testSetup() throws Exception {
|
||||
String validResponse = "{\"token\":\"" + token2 + "\"}";
|
||||
String validVersions = "[ {\"major\":1,\"minor\":0} ]";
|
||||
String validResponse = makeSetupResponse(
|
||||
"\"" + token2 + "\"", validVersions);
|
||||
String invalidResponse = "{\"foo\":\"bar\"}";
|
||||
String invalidTokenResponse = "{\"token\":{\"foo\":\"bar\"}}";
|
||||
String invalidTokenResponse2 =
|
||||
"{\"token\":\"" + getRandomString(64) + "\"}";
|
||||
String invalidTokenResponse = makeSetupResponse(
|
||||
"{\"foo\":\"bar\"}", validVersions);
|
||||
String invalidTokenResponse2 = makeSetupResponse(
|
||||
"\"" + getRandomString(64) + "\"", validVersions);
|
||||
String invalidVersionsResponse = makeSetupResponse(
|
||||
"\"" + token2 + "\"", "42");
|
||||
String invalidVersionsResponse2 = makeSetupResponse(
|
||||
"\"" + token2 + "\"", "[ 1,0 ]");
|
||||
String invalidVersionsResponse3 = makeSetupResponse(
|
||||
"\"" + token2 + "\"", "[ {\"major\":1, \"minor\":-1} ]");
|
||||
|
||||
MockWebServer server = new MockWebServer();
|
||||
server.enqueue(new MockResponse().setBody(validResponse));
|
||||
@@ -94,15 +104,18 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.enqueue(new MockResponse().setResponseCode(500));
|
||||
server.enqueue(new MockResponse().setBody(invalidTokenResponse));
|
||||
server.enqueue(new MockResponse().setBody(invalidTokenResponse2));
|
||||
server.enqueue(new MockResponse().setBody(invalidVersionsResponse));
|
||||
server.enqueue(new MockResponse().setBody(invalidVersionsResponse2));
|
||||
server.enqueue(new MockResponse().setBody(invalidVersionsResponse3));
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true);
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
|
||||
// valid response with valid token
|
||||
assertEquals(token2, api.setup(properties));
|
||||
mailboxPropertiesEqual(properties2, api.setup(properties));
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
assertEquals("/setup", request1.getPath());
|
||||
assertEquals("PUT", request1.getMethod());
|
||||
@@ -149,12 +162,38 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
assertEquals("/setup", request7.getPath());
|
||||
assertEquals("PUT", request7.getMethod());
|
||||
assertToken(request7, token);
|
||||
|
||||
// invalid non-array serverSupports response
|
||||
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||
RecordedRequest request8 = server.takeRequest();
|
||||
assertEquals("/setup", request8.getPath());
|
||||
assertEquals("PUT", request8.getMethod());
|
||||
assertToken(request8, token);
|
||||
|
||||
// invalid non-object in serverSupports array response
|
||||
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||
RecordedRequest request9 = server.takeRequest();
|
||||
assertEquals("/setup", request9.getPath());
|
||||
assertEquals("PUT", request9.getMethod());
|
||||
assertToken(request9, token);
|
||||
|
||||
// invalid negative minor version in serverSupports response
|
||||
assertThrows(ApiException.class, () -> api.setup(properties));
|
||||
RecordedRequest request10 = server.takeRequest();
|
||||
assertEquals("/setup", request10.getPath());
|
||||
assertEquals("PUT", request10.getMethod());
|
||||
assertToken(request10, token);
|
||||
}
|
||||
|
||||
private String makeSetupResponse(String token, String versions) {
|
||||
return "{\"token\":" + token + "," +
|
||||
"\"serverSupports\":" + versions + "}";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetupOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> api.setup(properties)
|
||||
@@ -170,9 +209,9 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true);
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
|
||||
assertTrue(api.checkStatus(properties));
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
@@ -193,7 +232,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testStatusOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> api.checkStatus(properties)
|
||||
@@ -210,9 +249,9 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
MailboxProperties properties2 =
|
||||
new MailboxProperties(baseUrl, token2, true);
|
||||
new MailboxProperties(baseUrl, token2, true, new ArrayList<>());
|
||||
|
||||
api.wipeMailbox(properties);
|
||||
RecordedRequest request1 = server.takeRequest();
|
||||
@@ -242,7 +281,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testWipeOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.wipeMailbox(properties));
|
||||
}
|
||||
@@ -256,7 +295,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// contact gets added as expected
|
||||
api.addContact(properties, mailboxContact);
|
||||
@@ -288,7 +327,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testAddContactOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.addContact(properties, mailboxContact));
|
||||
}
|
||||
@@ -303,7 +342,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// contact gets deleted as expected
|
||||
api.deleteContact(properties, contactId);
|
||||
@@ -340,7 +379,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testDeleteContactOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.deleteContact(properties, contactId));
|
||||
}
|
||||
@@ -367,7 +406,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// valid response with two contacts
|
||||
assertEquals(singletonList(contactId), api.getContacts(properties));
|
||||
@@ -432,7 +471,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testGetContactsOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> api.getContacts(properties)
|
||||
@@ -452,7 +491,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// file gets uploaded as expected
|
||||
api.addFile(properties, contactInboxId, file);
|
||||
@@ -511,7 +550,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// valid response with one file
|
||||
List<MailboxFile> received1 = api.getFiles(properties, contactInboxId);
|
||||
@@ -607,7 +646,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// file gets downloaded as expected
|
||||
api.getFile(properties, contactOutboxId, name, file1);
|
||||
@@ -651,7 +690,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// file gets deleted as expected
|
||||
api.deleteFile(properties, contactInboxId, name);
|
||||
@@ -715,7 +754,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
server.start();
|
||||
String baseUrl = getBaseUrl(server);
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(baseUrl, token, true);
|
||||
new MailboxProperties(baseUrl, token, true, new ArrayList<>());
|
||||
|
||||
// valid response with one folders
|
||||
assertEquals(singletonList(id1), api.getFolders(properties));
|
||||
@@ -784,7 +823,7 @@ public class MailboxApiTest extends BrambleTestCase {
|
||||
@Test
|
||||
public void testGetFoldersOnlyForOwner() {
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties("", token, false);
|
||||
new MailboxProperties("", token, false, new ArrayList<>());
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
api.getFolders(properties));
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -89,17 +90,16 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
assumeTrue(isOptionalTestEnabled(MailboxIntegrationTest.class));
|
||||
|
||||
if (ownerProperties != null) return;
|
||||
MailboxProperties setupProperties =
|
||||
new MailboxProperties(URL_BASE, SETUP_TOKEN, true);
|
||||
MailboxAuthToken ownerToken = api.setup(setupProperties);
|
||||
ownerProperties = new MailboxProperties(URL_BASE, ownerToken, true);
|
||||
MailboxProperties setupProperties = new MailboxProperties(
|
||||
URL_BASE, SETUP_TOKEN, true, new ArrayList<>());
|
||||
ownerProperties = api.setup(setupProperties);
|
||||
}
|
||||
|
||||
@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
|
||||
@@ -107,8 +107,8 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
api.checkStatus(ownerProperties));
|
||||
|
||||
// new setup doesn't work as mailbox is stopping
|
||||
MailboxProperties setupProperties =
|
||||
new MailboxProperties(URL_BASE, SETUP_TOKEN, true);
|
||||
MailboxProperties setupProperties = new MailboxProperties(
|
||||
URL_BASE, SETUP_TOKEN, true, new ArrayList<>());
|
||||
assertThrows(ApiException.class, () -> api.setup(setupProperties));
|
||||
}
|
||||
|
||||
@@ -151,7 +151,8 @@ public class MailboxIntegrationTest extends BrambleTestCase {
|
||||
ContactId contactId = new ContactId(1);
|
||||
MailboxContact contact = getMailboxContact(contactId);
|
||||
MailboxProperties contactProperties = new MailboxProperties(
|
||||
ownerProperties.getBaseUrl(), contact.token, false);
|
||||
ownerProperties.getBaseUrl(), contact.token, false,
|
||||
new ArrayList<>());
|
||||
api.addContact(ownerProperties, contact);
|
||||
|
||||
// upload a file for our contact
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -61,10 +62,10 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
|
||||
new MailboxAuthToken(getRandomId());
|
||||
private final String validPayload = getValidPayload();
|
||||
private final long time = System.currentTimeMillis();
|
||||
private final MailboxProperties setupProperties =
|
||||
new MailboxProperties(onionAddress, setupToken, true);
|
||||
private final MailboxProperties ownerProperties =
|
||||
new MailboxProperties(onionAddress, ownerToken, true);
|
||||
private final MailboxProperties setupProperties = new MailboxProperties(
|
||||
onionAddress, setupToken, true, new ArrayList<>());
|
||||
private final MailboxProperties ownerProperties = new MailboxProperties(
|
||||
onionAddress, ownerToken, true, new ArrayList<>());
|
||||
|
||||
@Test
|
||||
public void testInitialQrCodeReceivedState() {
|
||||
@@ -98,7 +99,7 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
|
||||
oneOf(crypto).encodeOnion(onionBytes);
|
||||
will(returnValue(onion));
|
||||
oneOf(api).setup(with(matches(setupProperties)));
|
||||
will(returnValue(ownerToken));
|
||||
will(returnValue(ownerProperties));
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(time));
|
||||
}});
|
||||
@@ -174,7 +175,7 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
|
||||
oneOf(crypto).encodeOnion(onionBytes);
|
||||
will(returnValue(onion));
|
||||
oneOf(api).setup(with(matches(setupProperties)));
|
||||
will(returnValue(ownerToken));
|
||||
will(returnValue(ownerProperties));
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(time));
|
||||
}});
|
||||
@@ -206,7 +207,8 @@ public class MailboxPairingTaskImplTest extends BrambleMockTestCase {
|
||||
return new PredicateMatcher<>(MailboxProperties.class, p1 ->
|
||||
p1.getAuthToken().equals(p2.getAuthToken()) &&
|
||||
p1.getBaseUrl().equals(p2.getBaseUrl()) &&
|
||||
p1.isOwner() == p2.isOwner());
|
||||
p1.isOwner() == p2.isOwner() &&
|
||||
p1.getServerSupports().equals(p2.getServerSupports()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -76,7 +77,7 @@ public class MailboxPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
public MailboxPropertyManagerImplTest() {
|
||||
ownProps = new MailboxProperties("http://bar.onion",
|
||||
new MailboxAuthToken(getRandomId()), true);
|
||||
new MailboxAuthToken(getRandomId()), true, new ArrayList<>());
|
||||
props = new MailboxPropertiesUpdate(ownProps.getOnion(),
|
||||
new MailboxAuthToken(getRandomId()),
|
||||
new MailboxFolderId(getRandomId()),
|
||||
|
||||
@@ -6,6 +6,7 @@ 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;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxVersion;
|
||||
import org.briarproject.bramble.api.mailbox.OwnMailboxConnectionStatusEvent;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
@@ -13,12 +14,15 @@ import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_KEY_ATTEMPTS;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_KEY_LAST_ATTEMPT;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_KEY_LAST_SUCCESS;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_KEY_ONION;
|
||||
import static org.briarproject.bramble.mailbox.MailboxSettingsManagerImpl.SETTINGS_KEY_SERVER_SUPPORTS;
|
||||
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;
|
||||
@@ -40,6 +44,9 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
private final Random random = new Random();
|
||||
private final String onion = getRandomString(64);
|
||||
private final MailboxAuthToken token = new MailboxAuthToken(getRandomId());
|
||||
private final List<MailboxVersion> serverSupports =
|
||||
asList(new MailboxVersion(1, 0), new MailboxVersion(1, 1));
|
||||
private final int[] serverSupportsInts = {1, 0, 1, 1};
|
||||
private final ContactId contactId1 = new ContactId(random.nextInt());
|
||||
private final ContactId contactId2 = new ContactId(random.nextInt());
|
||||
private final ContactId contactId3 = new ContactId(random.nextInt());
|
||||
@@ -67,6 +74,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
Settings settings = new Settings();
|
||||
settings.put(SETTINGS_KEY_ONION, onion);
|
||||
settings.put(SETTINGS_KEY_TOKEN, token.toString());
|
||||
settings.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS, serverSupportsInts);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(settingsManager).getSettings(txn, SETTINGS_NAMESPACE);
|
||||
@@ -77,6 +85,7 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
assertNotNull(properties);
|
||||
assertEquals(onion, properties.getBaseUrl());
|
||||
assertEquals(token, properties.getAuthToken());
|
||||
assertEquals(serverSupports, properties.getServerSupports());
|
||||
assertTrue(properties.isOwner());
|
||||
}
|
||||
|
||||
@@ -86,8 +95,10 @@ public class MailboxSettingsManagerImplTest extends BrambleMockTestCase {
|
||||
Settings expectedSettings = new Settings();
|
||||
expectedSettings.put(SETTINGS_KEY_ONION, onion);
|
||||
expectedSettings.put(SETTINGS_KEY_TOKEN, token.toString());
|
||||
MailboxProperties properties =
|
||||
new MailboxProperties(onion, token, true);
|
||||
expectedSettings.putIntArray(SETTINGS_KEY_SERVER_SUPPORTS,
|
||||
serverSupportsInts);
|
||||
MailboxProperties properties = new MailboxProperties(onion, token, true,
|
||||
serverSupports);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(settingsManager).mergeSettings(txn, expectedSettings,
|
||||
|
||||
@@ -215,6 +215,7 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
if (IS_DEBUG_BUILD || shouldWarnOldAndroidExpiry()) {
|
||||
navDrawerViewModel.checkExpiryWarning();
|
||||
}
|
||||
navDrawerViewModel.printStats();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,9 +2,9 @@ package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
@@ -41,6 +41,7 @@ public class NavDrawerViewModel extends DbViewModel {
|
||||
private static final String SHOW_TRANSPORTS_ONBOARDING =
|
||||
"showTransportsOnboarding";
|
||||
|
||||
private final DatabaseComponent db;
|
||||
private final SettingsManager settingsManager;
|
||||
|
||||
private final MutableLiveData<Boolean> showExpiryWarning =
|
||||
@@ -54,10 +55,11 @@ public class NavDrawerViewModel extends DbViewModel {
|
||||
NavDrawerViewModel(Application app,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
TransactionManager db,
|
||||
DatabaseComponent db,
|
||||
AndroidExecutor androidExecutor,
|
||||
SettingsManager settingsManager) {
|
||||
super(app, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.db = db;
|
||||
this.settingsManager = settingsManager;
|
||||
}
|
||||
|
||||
@@ -174,4 +176,16 @@ public class NavDrawerViewModel extends DbViewModel {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void printStats() {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
db.transaction(false, txn -> {
|
||||
db.printStats(txn);
|
||||
});
|
||||
} catch (DbException e) {
|
||||
handleException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user