Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
briar
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Julian Dehm
briar
Commits
6925dfcb
Commit
6925dfcb
authored
Sep 18, 2018
by
akwizgran
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1240-message-refactoring' into 'master'
Remove raw representation from Message class See merge request
!915
parents
817df9c7
ba592821
Changes
32
Show whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
176 additions
and
130 deletions
+176
-130
bramble-api/src/main/java/org/briarproject/bramble/api/client/BdfMessageValidator.java
.../briarproject/bramble/api/client/BdfMessageValidator.java
+4
-6
bramble-api/src/main/java/org/briarproject/bramble/api/sync/Message.java
.../main/java/org/briarproject/bramble/api/sync/Message.java
+9
-11
bramble-api/src/main/java/org/briarproject/bramble/api/sync/MessageFactory.java
...ava/org/briarproject/bramble/api/sync/MessageFactory.java
+1
-1
bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java
...rc/test/java/org/briarproject/bramble/test/TestUtils.java
+4
-5
bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java
...ava/org/briarproject/bramble/client/ClientHelperImpl.java
+2
-7
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java
...main/java/org/briarproject/bramble/db/DatabaseModule.java
+4
-2
bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
...src/main/java/org/briarproject/bramble/db/H2Database.java
+4
-2
bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
...in/java/org/briarproject/bramble/db/HyperSqlDatabase.java
+4
-2
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
...c/main/java/org/briarproject/bramble/db/JdbcDatabase.java
+12
-4
bramble-core/src/main/java/org/briarproject/bramble/sync/MessageFactoryImpl.java
...ava/org/briarproject/bramble/sync/MessageFactoryImpl.java
+9
-15
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterFactoryImpl.java
...riarproject/bramble/sync/SyncRecordWriterFactoryImpl.java
+6
-2
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java
...a/org/briarproject/bramble/sync/SyncRecordWriterImpl.java
+6
-2
bramble-core/src/test/java/org/briarproject/bramble/client/BdfMessageValidatorTest.java
.../briarproject/bramble/client/BdfMessageValidatorTest.java
+6
-19
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseMigrationTest.java
...va/org/briarproject/bramble/db/DatabaseMigrationTest.java
+3
-0
bramble-core/src/test/java/org/briarproject/bramble/db/DatabasePerformanceComparisonTest.java
...project/bramble/db/DatabasePerformanceComparisonTest.java
+6
-2
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseTraceTest.java
...t/java/org/briarproject/bramble/db/DatabaseTraceTest.java
+5
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabasePerformanceTest.java
...rg/briarproject/bramble/db/H2DatabasePerformanceTest.java
+4
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java
...test/java/org/briarproject/bramble/db/H2DatabaseTest.java
+4
-6
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTraceTest.java
...java/org/briarproject/bramble/db/H2DatabaseTraceTest.java
+3
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2HyperSqlDatabasePerformanceComparisonTest.java
...amble/db/H2HyperSqlDatabasePerformanceComparisonTest.java
+6
-3
bramble-core/src/test/java/org/briarproject/bramble/db/H2MigrationTest.java
...est/java/org/briarproject/bramble/db/H2MigrationTest.java
+1
-1
bramble-core/src/test/java/org/briarproject/bramble/db/H2SelfDatabasePerformanceComparisonTest.java
...t/bramble/db/H2SelfDatabasePerformanceComparisonTest.java
+4
-2
bramble-core/src/test/java/org/briarproject/bramble/db/H2SleepDatabasePerformanceComparisonTest.java
.../bramble/db/H2SleepDatabasePerformanceComparisonTest.java
+5
-3
bramble-core/src/test/java/org/briarproject/bramble/db/HyperSqlDatabasePerformanceTest.java
...arproject/bramble/db/HyperSqlDatabasePerformanceTest.java
+4
-2
bramble-core/src/test/java/org/briarproject/bramble/db/HyperSqlDatabaseTest.java
...ava/org/briarproject/bramble/db/HyperSqlDatabaseTest.java
+4
-6
bramble-core/src/test/java/org/briarproject/bramble/db/HyperSqlMigrationTest.java
...va/org/briarproject/bramble/db/HyperSqlMigrationTest.java
+3
-3
bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java
...st/java/org/briarproject/bramble/db/JdbcDatabaseTest.java
+14
-10
bramble-core/src/test/java/org/briarproject/bramble/db/SingleDatabasePerformanceTest.java
...riarproject/bramble/db/SingleDatabasePerformanceTest.java
+5
-2
bramble-core/src/test/java/org/briarproject/bramble/sync/SyncIntegrationTest.java
...va/org/briarproject/bramble/sync/SyncIntegrationTest.java
+1
-1
bramble-core/src/test/java/org/briarproject/bramble/test/TestMessageFactory.java
...ava/org/briarproject/bramble/test/TestMessageFactory.java
+30
-0
bramble-core/src/test/java/org/briarproject/bramble/test/ValidatorTestCase.java
...java/org/briarproject/bramble/test/ValidatorTestCase.java
+0
-1
briar-core/src/test/java/org/briarproject/briar/introduction/MessageEncoderTest.java
...g/briarproject/briar/introduction/MessageEncoderTest.java
+3
-4
No files found.
bramble-api/src/main/java/org/briarproject/bramble/api/client/BdfMessageValidator.java
View file @
6925dfcb
...
...
@@ -16,7 +16,6 @@ import java.util.logging.Logger;
import
javax.annotation.concurrent.Immutable
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_CLOCK_DIFFERENCE
;
@Immutable
...
...
@@ -49,14 +48,13 @@ public abstract class BdfMessageValidator implements MessageValidator {
throw
new
InvalidMessageException
(
"Timestamp is too far in the future"
);
}
byte
[]
raw
=
m
.
getRaw
();
if
(
raw
.
length
<=
MESSAGE_HEADER_LENGTH
)
{
byte
[]
body
=
m
.
getBody
();
if
(
body
.
length
==
0
)
{
throw
new
InvalidMessageException
(
"Message is too short"
);
}
try
{
BdfList
body
=
clientHelper
.
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
BdfMessageContext
result
=
validateMessage
(
m
,
g
,
body
);
BdfList
bodyList
=
clientHelper
.
toList
(
body
);
BdfMessageContext
result
=
validateMessage
(
m
,
g
,
bodyList
);
Metadata
meta
=
metadataEncoder
.
encode
(
result
.
getDictionary
());
return
new
MessageContext
(
meta
,
result
.
getDependencies
());
}
catch
(
FormatException
e
)
{
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/sync/Message.java
View file @
6925dfcb
package
org.briarproject.bramble.api.sync
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_
BODY_
LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
public
class
Message
{
...
...
@@ -13,17 +13,15 @@ public class Message {
private
final
MessageId
id
;
private
final
GroupId
groupId
;
private
final
long
timestamp
;
private
final
byte
[]
raw
;
private
final
byte
[]
body
;
public
Message
(
MessageId
id
,
GroupId
groupId
,
long
timestamp
,
byte
[]
raw
)
{
if
(
raw
.
length
<=
MESSAGE_HEADER_LENGTH
)
throw
new
IllegalArgumentException
();
if
(
raw
.
length
>
MAX_MESSAGE_LENGTH
)
public
Message
(
MessageId
id
,
GroupId
groupId
,
long
timestamp
,
byte
[]
body
)
{
if
(
body
.
length
>
MAX_MESSAGE_BODY_LENGTH
)
throw
new
IllegalArgumentException
();
this
.
id
=
id
;
this
.
groupId
=
groupId
;
this
.
timestamp
=
timestamp
;
this
.
raw
=
raw
;
this
.
body
=
body
;
}
/**
...
...
@@ -51,14 +49,14 @@ public class Message {
* Returns the length of the raw message in bytes.
*/
public
int
getRawLength
()
{
return
raw
.
length
;
return
MESSAGE_HEADER_LENGTH
+
body
.
length
;
}
/**
* Returns the
raw message
.
* Returns the
message body
.
*/
public
byte
[]
get
Raw
()
{
return
raw
;
public
byte
[]
get
Body
()
{
return
body
;
}
@Override
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/sync/MessageFactory.java
View file @
6925dfcb
...
...
@@ -9,5 +9,5 @@ public interface MessageFactory {
Message
createMessage
(
byte
[]
raw
);
Message
createMessage
(
MessageId
m
,
byte
[]
raw
);
byte
[]
getRawMessage
(
Message
m
);
}
bramble-api/src/test/java/org/briarproject/bramble/test/TestUtils.java
View file @
6925dfcb
...
...
@@ -33,7 +33,6 @@ import static org.briarproject.bramble.api.properties.TransportPropertyConstants
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
ClientId
.
MAX_CLIENT_ID_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_GROUP_DESCRIPTOR_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MAX_MESSAGE_BODY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
public
class
TestUtils
{
...
...
@@ -132,13 +131,13 @@ public class TestUtils {
public
static
Message
getMessage
(
GroupId
groupId
)
{
int
bodyLength
=
1
+
random
.
nextInt
(
MAX_MESSAGE_BODY_LENGTH
);
return
getMessage
(
groupId
,
MESSAGE_HEADER_LENGTH
+
bodyLength
);
return
getMessage
(
groupId
,
bodyLength
);
}
public
static
Message
getMessage
(
GroupId
groupId
,
int
raw
Length
)
{
public
static
Message
getMessage
(
GroupId
groupId
,
int
body
Length
)
{
MessageId
id
=
new
MessageId
(
getRandomId
());
byte
[]
raw
=
getRandomBytes
(
raw
Length
);
return
new
Message
(
id
,
groupId
,
timestamp
,
raw
);
byte
[]
body
=
getRandomBytes
(
body
Length
);
return
new
Message
(
id
,
groupId
,
timestamp
,
body
);
}
public
static
double
getMedian
(
Collection
<?
extends
Number
>
samples
)
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/client/ClientHelperImpl.java
View file @
6925dfcb
...
...
@@ -41,7 +41,6 @@ import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_N
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_PUBLIC_KEY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
properties
.
TransportPropertyConstants
.
MAX_PROPERTIES_PER_TRANSPORT
;
import
static
org
.
briarproject
.
bramble
.
api
.
properties
.
TransportPropertyConstants
.
MAX_PROPERTY_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
ValidationUtils
.
checkLength
;
import
static
org
.
briarproject
.
bramble
.
util
.
ValidationUtils
.
checkSize
;
...
...
@@ -147,9 +146,7 @@ class ClientHelperImpl implements ClientHelper {
@Override
public
BdfList
getMessageAsList
(
Transaction
txn
,
MessageId
m
)
throws
DbException
,
FormatException
{
byte
[]
raw
=
db
.
getMessage
(
txn
,
m
).
getRaw
();
return
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
return
toList
(
db
.
getMessage
(
txn
,
m
).
getBody
());
}
@Override
...
...
@@ -361,9 +358,7 @@ class ClientHelperImpl implements ClientHelper {
@Override
public
BdfList
toList
(
Message
m
)
throws
FormatException
{
byte
[]
raw
=
m
.
getRaw
();
return
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
return
toList
(
m
.
getBody
());
}
@Override
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseModule.java
View file @
6925dfcb
...
...
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.db.DatabaseComponent;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.event.EventBus
;
import
org.briarproject.bramble.api.lifecycle.ShutdownManager
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
java.sql.Connection
;
...
...
@@ -18,8 +19,9 @@ public class DatabaseModule {
@Provides
@Singleton
Database
<
Connection
>
provideDatabase
(
DatabaseConfig
config
,
Clock
clock
)
{
return
new
H2Database
(
config
,
clock
);
Database
<
Connection
>
provideDatabase
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
return
new
H2Database
(
config
,
messageFactory
,
clock
);
}
@Provides
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
View file @
6925dfcb
...
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.db.MigrationListener
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.util.StringUtils
;
...
...
@@ -36,9 +37,10 @@ class H2Database extends JdbcDatabase {
private
volatile
SecretKey
key
=
null
;
@Inject
H2Database
(
DatabaseConfig
config
,
Clock
clock
)
{
H2Database
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
clock
);
messageFactory
,
clock
);
this
.
config
=
config
;
File
dir
=
config
.
getDatabaseDirectory
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
View file @
6925dfcb
...
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.db.MigrationListener
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.util.StringUtils
;
...
...
@@ -37,9 +38,10 @@ class HyperSqlDatabase extends JdbcDatabase {
private
volatile
SecretKey
key
=
null
;
@Inject
HyperSqlDatabase
(
DatabaseConfig
config
,
Clock
clock
)
{
HyperSqlDatabase
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
super
(
HASH_TYPE
,
SECRET_TYPE
,
BINARY_TYPE
,
COUNTER_TYPE
,
STRING_TYPE
,
clock
);
messageFactory
,
clock
);
this
.
config
=
config
;
File
dir
=
config
.
getDatabaseDirectory
();
String
path
=
new
File
(
dir
,
"db"
).
getAbsolutePath
();
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
View file @
6925dfcb
...
...
@@ -21,6 +21,7 @@ import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Group.Visibility;
import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.sync.MessageFactory;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.MessageStatus;
import org.briarproject.bramble.api.sync.ValidationManager.State;
...
...
@@ -61,6 +62,7 @@ import static org.briarproject.bramble.api.db.Metadata.REMOVE;
import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE;
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE;
import static org.briarproject.bramble.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.bramble.api.sync.ValidationManager.State.DELIVERED;
import static org.briarproject.bramble.api.sync.ValidationManager.State.PENDING;
import static org.briarproject.bramble.api.sync.ValidationManager.State.UNKNOWN;
...
...
@@ -305,6 +307,7 @@ abstract class JdbcDatabase implements Database<Connection> {
// Different database libraries use different names for certain types
private final String hashType, secretType, binaryType;
private final String counterType, stringType;
private final MessageFactory messageFactory;
private final Clock clock;
// Locking: connectionsLock
...
...
@@ -320,12 +323,14 @@ abstract class JdbcDatabase implements Database<Connection> {
private final Condition connectionsChanged = connectionsLock.newCondition();
JdbcDatabase(String hashType, String secretType, String binaryType,
String
counterType
,
String
stringType
,
Clock
clock
)
{
String counterType, String stringType,
MessageFactory messageFactory, Clock clock) {
this.hashType = hashType;
this.secretType = secretType;
this.binaryType = binaryType;
this.counterType = counterType;
this.stringType = stringType;
this.messageFactory = messageFactory;
this.clock = clock;
}
...
...
@@ -727,7 +732,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ps.setLong(3, m.getTimestamp());
ps.setInt(4, state.getValue());
ps.setBoolean(5, messageShared);
byte
[]
raw
=
m
.
getRaw
(
);
byte[] raw = m
essageFactory.getRawMessage(m
);
ps.setInt(6, raw.length);
ps.setBytes(7, raw);
int affected = ps.executeUpdate();
...
...
@@ -741,7 +746,7 @@ abstract class JdbcDatabase implements Database<Connection> {
boolean offered = removeOfferedMessage(txn, c, m.getId());
boolean seen = offered || (sender != null && c.equals(sender));
addStatus(txn, m.getId(), c, m.getGroupId(), m.getTimestamp(),
m
.
getRawLength
()
,
state
,
e
.
getValue
(),
messageShared
,
raw.length
, state, e.getValue(), messageShared,
false, seen);
}
// Update denormalised column in messageDependencies if dependency
...
...
@@ -1501,7 +1506,10 @@ abstract class JdbcDatabase implements Database<Connection> {
rs.close();
ps.close();
if (raw == null) throw new MessageDeletedException();
return
new
Message
(
m
,
g
,
timestamp
,
raw
);
if (raw.length < MESSAGE_HEADER_LENGTH) throw new AssertionError();
byte[] body = new byte[raw.length - MESSAGE_HEADER_LENGTH];
System.arraycopy(raw, MESSAGE_HEADER_LENGTH, body, 0, body.length);
return new Message(m, g, timestamp, body);
} catch (SQLException e) {
tryToClose(rs);
tryToClose(ps);
...
...
bramble-core/src/main/java/org/briarproject/bramble/sync/MessageFactoryImpl.java
View file @
6925dfcb
...
...
@@ -39,11 +39,7 @@ class MessageFactoryImpl implements MessageFactory {
if
(
body
.
length
>
MAX_MESSAGE_BODY_LENGTH
)
throw
new
IllegalArgumentException
();
MessageId
id
=
getMessageId
(
g
,
timestamp
,
body
);
byte
[]
raw
=
new
byte
[
MESSAGE_HEADER_LENGTH
+
body
.
length
];
System
.
arraycopy
(
g
.
getBytes
(),
0
,
raw
,
0
,
UniqueId
.
LENGTH
);
ByteUtils
.
writeUint64
(
timestamp
,
raw
,
UniqueId
.
LENGTH
);
System
.
arraycopy
(
body
,
0
,
raw
,
MESSAGE_HEADER_LENGTH
,
body
.
length
);
return
new
Message
(
id
,
g
,
timestamp
,
raw
);
return
new
Message
(
id
,
g
,
timestamp
,
body
);
}
private
MessageId
getMessageId
(
GroupId
g
,
long
timestamp
,
byte
[]
body
)
{
...
...
@@ -69,18 +65,16 @@ class MessageFactoryImpl implements MessageFactory {
byte
[]
body
=
new
byte
[
raw
.
length
-
MESSAGE_HEADER_LENGTH
];
System
.
arraycopy
(
raw
,
MESSAGE_HEADER_LENGTH
,
body
,
0
,
body
.
length
);
MessageId
id
=
getMessageId
(
g
,
timestamp
,
body
);
return
new
Message
(
id
,
g
,
timestamp
,
raw
);
return
new
Message
(
id
,
g
,
timestamp
,
body
);
}
@Override
public
Message
createMessage
(
MessageId
m
,
byte
[]
raw
)
{
if
(
raw
.
length
<
MESSAGE_HEADER_LENGTH
)
throw
new
IllegalArgumentException
();
if
(
raw
.
length
>
MAX_MESSAGE_LENGTH
)
throw
new
IllegalArgumentException
();
byte
[]
groupId
=
new
byte
[
UniqueId
.
LENGTH
];
System
.
arraycopy
(
raw
,
0
,
groupId
,
0
,
UniqueId
.
LENGTH
);
long
timestamp
=
ByteUtils
.
readUint64
(
raw
,
UniqueId
.
LENGTH
);
return
new
Message
(
m
,
new
GroupId
(
groupId
),
timestamp
,
raw
);
public
byte
[]
getRawMessage
(
Message
m
)
{
byte
[]
body
=
m
.
getBody
();
byte
[]
raw
=
new
byte
[
MESSAGE_HEADER_LENGTH
+
body
.
length
];
System
.
arraycopy
(
m
.
getGroupId
().
getBytes
(),
0
,
raw
,
0
,
UniqueId
.
LENGTH
);
ByteUtils
.
writeUint64
(
m
.
getTimestamp
(),
raw
,
UniqueId
.
LENGTH
);
System
.
arraycopy
(
body
,
0
,
raw
,
MESSAGE_HEADER_LENGTH
,
body
.
length
);
return
raw
;
}
}
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterFactoryImpl.java
View file @
6925dfcb
...
...
@@ -3,6 +3,7 @@ package org.briarproject.bramble.sync;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.record.RecordWriter
;
import
org.briarproject.bramble.api.record.RecordWriterFactory
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.sync.SyncRecordWriter
;
import
org.briarproject.bramble.api.sync.SyncRecordWriterFactory
;
...
...
@@ -13,16 +14,19 @@ import javax.inject.Inject;
@NotNullByDefault
class
SyncRecordWriterFactoryImpl
implements
SyncRecordWriterFactory
{
private
final
MessageFactory
messageFactory
;
private
final
RecordWriterFactory
recordWriterFactory
;
@Inject
SyncRecordWriterFactoryImpl
(
RecordWriterFactory
recordWriterFactory
)
{
SyncRecordWriterFactoryImpl
(
MessageFactory
messageFactory
,
RecordWriterFactory
recordWriterFactory
)
{
this
.
messageFactory
=
messageFactory
;
this
.
recordWriterFactory
=
recordWriterFactory
;
}
@Override
public
SyncRecordWriter
createRecordWriter
(
OutputStream
out
)
{
RecordWriter
writer
=
recordWriterFactory
.
createRecordWriter
(
out
);
return
new
SyncRecordWriterImpl
(
writer
);
return
new
SyncRecordWriterImpl
(
messageFactory
,
writer
);
}
}
bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java
View file @
6925dfcb
...
...
@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.record.Record;
import
org.briarproject.bramble.api.record.RecordWriter
;
import
org.briarproject.bramble.api.sync.Ack
;
import
org.briarproject.bramble.api.sync.Message
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.sync.MessageId
;
import
org.briarproject.bramble.api.sync.Offer
;
import
org.briarproject.bramble.api.sync.Request
;
...
...
@@ -25,10 +26,12 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION;
@NotNullByDefault
class
SyncRecordWriterImpl
implements
SyncRecordWriter
{
private
final
MessageFactory
messageFactory
;
private
final
RecordWriter
writer
;
private
final
ByteArrayOutputStream
payload
=
new
ByteArrayOutputStream
();
SyncRecordWriterImpl
(
RecordWriter
writer
)
{
SyncRecordWriterImpl
(
MessageFactory
messageFactory
,
RecordWriter
writer
)
{
this
.
messageFactory
=
messageFactory
;
this
.
writer
=
writer
;
}
...
...
@@ -46,7 +49,8 @@ class SyncRecordWriterImpl implements SyncRecordWriter {
@Override
public
void
writeMessage
(
Message
m
)
throws
IOException
{
writer
.
writeRecord
(
new
Record
(
PROTOCOL_VERSION
,
MESSAGE
,
m
.
getRaw
()));
byte
[]
raw
=
messageFactory
.
getRawMessage
(
m
);
writer
.
writeRecord
(
new
Record
(
PROTOCOL_VERSION
,
MESSAGE
,
raw
));
}
@Override
...
...
bramble-core/src/test/java/org/briarproject/bramble/client/BdfMessageValidatorTest.java
View file @
6925dfcb
...
...
@@ -16,10 +16,8 @@ import org.jmock.Expectations;
import
org.jmock.lib.legacy.ClassImposteriser
;
import
org.junit.Test
;
import
static
org
.
briarproject
.
bramble
.
api
.
sync
.
SyncConstants
.
MESSAGE_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_CLOCK_DIFFERENCE
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getMessage
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomBytes
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertSame
;
...
...
@@ -58,8 +56,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
context
.
checking
(
new
Expectations
()
{{
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
-
MAX_CLOCK_DIFFERENCE
));
oneOf
(
clientHelper
).
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
oneOf
(
clientHelper
).
toList
(
message
.
getBody
());
will
(
returnValue
(
body
));
oneOf
(
metadataEncoder
).
encode
(
dictionary
);
will
(
returnValue
(
meta
));
...
...
@@ -84,18 +81,11 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
@Test
(
expected
=
InvalidMessageException
.
class
)
public
void
testRejectsTooShortMessage
()
throws
Exception
{
byte
[]
invalidRaw
=
getRandomBytes
(
MESSAGE_HEADER_LENGTH
);
// Use a mock message so the length of the raw message can be invalid
Message
invalidMessage
=
context
.
mock
(
Message
.
class
);
Message
invalidMessage
=
getMessage
(
groupId
,
0
);
context
.
checking
(
new
Expectations
()
{{
//noinspection ResultOfMethodCallIgnored
oneOf
(
invalidMessage
).
getTimestamp
();
will
(
returnValue
(
timestamp
));
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
));
oneOf
(
invalidMessage
).
getRaw
();
will
(
returnValue
(
invalidRaw
));
}});
failIfSubclassIsCalled
.
validateMessage
(
invalidMessage
,
group
);
...
...
@@ -103,13 +93,12 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
@Test
public
void
testAcceptsMinLengthMessage
()
throws
Exception
{
Message
shortMessage
=
getMessage
(
groupId
,
MESSAGE_HEADER_LENGTH
+
1
);
Message
shortMessage
=
getMessage
(
groupId
,
1
);
context
.
checking
(
new
Expectations
()
{{
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
));
oneOf
(
clientHelper
).
toList
(
shortMessage
.
getRaw
(),
MESSAGE_HEADER_LENGTH
,
1
);
oneOf
(
clientHelper
).
toList
(
shortMessage
.
getBody
());
will
(
returnValue
(
body
));
oneOf
(
metadataEncoder
).
encode
(
dictionary
);
will
(
returnValue
(
meta
));
...
...
@@ -137,8 +126,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
context
.
checking
(
new
Expectations
()
{{
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
));
oneOf
(
clientHelper
).
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
oneOf
(
clientHelper
).
toList
(
message
.
getBody
());
will
(
throwException
(
new
FormatException
()));
}});
...
...
@@ -150,8 +138,7 @@ public class BdfMessageValidatorTest extends ValidatorTestCase {
context
.
checking
(
new
Expectations
()
{{
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
));
oneOf
(
clientHelper
).
toList
(
raw
,
MESSAGE_HEADER_LENGTH
,
raw
.
length
-
MESSAGE_HEADER_LENGTH
);
oneOf
(
clientHelper
).
toList
(
message
.
getBody
());
will
(
returnValue
(
body
));
}});
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseMigrationTest.java
View file @
6925dfcb
...
...
@@ -7,10 +7,12 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
org.briarproject.bramble.api.settings.Settings
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.system.SystemClock
;
import
org.briarproject.bramble.test.BrambleMockTestCase
;
import
org.briarproject.bramble.test.TestDatabaseConfig
;
import
org.briarproject.bramble.test.TestMessageFactory
;
import
org.briarproject.bramble.test.TestUtils
;
import
org.jmock.Expectations
;
import
org.junit.After
;
...
...
@@ -45,6 +47,7 @@ public abstract class DatabaseMigrationTest extends BrambleMockTestCase {
protected
final
DatabaseConfig
config
=
new
TestDatabaseConfig
(
testDir
,
1024
*
1024
);
protected
final
MessageFactory
messageFactory
=
new
TestMessageFactory
();
protected
final
SecretKey
key
=
getSecretKey
();
protected
final
Clock
clock
=
new
SystemClock
();
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/DatabasePerformanceComparisonTest.java
View file @
6925dfcb
...
...
@@ -3,9 +3,11 @@ package org.briarproject.bramble.db;
import
org.briarproject.bramble.api.crypto.SecretKey
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.system.SystemClock
;
import
org.briarproject.bramble.test.TestDatabaseConfig
;
import
org.briarproject.bramble.test.TestMessageFactory
;
import
org.briarproject.bramble.test.UTest
;
import
java.io.IOException
;
...
...
@@ -30,7 +32,8 @@ public abstract class DatabasePerformanceComparisonTest
private
SecretKey
databaseKey
=
getSecretKey
();
abstract
Database
<
Connection
>
createDatabase
(
boolean
conditionA
,
DatabaseConfig
databaseConfig
,
Clock
clock
);
DatabaseConfig
databaseConfig
,
MessageFactory
messageFactory
,
Clock
clock
);
@Override
protected
void
benchmark
(
String
name
,
...
...
@@ -73,7 +76,8 @@ public abstract class DatabasePerformanceComparisonTest
private
Database
<
Connection
>
openDatabase
(
boolean
conditionA
)
throws
DbException
{
Database
<
Connection
>
db
=
createDatabase
(
conditionA
,
new
TestDatabaseConfig
(
testDir
,
MAX_SIZE
),
new
SystemClock
());
new
TestDatabaseConfig
(
testDir
,
MAX_SIZE
),
new
TestMessageFactory
(),
new
SystemClock
());
db
.
open
(
databaseKey
,
null
);
return
db
;
}
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseTraceTest.java
View file @
6925dfcb
...
...
@@ -3,9 +3,11 @@ package org.briarproject.bramble.db;
import
org.briarproject.bramble.api.crypto.SecretKey
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.db.DbException
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.system.SystemClock
;
import
org.briarproject.bramble.test.TestDatabaseConfig
;
import
org.briarproject.bramble.test.TestMessageFactory
;
import
org.briarproject.bramble.util.IoUtils
;
import
java.io.File
;
...
...
@@ -24,7 +26,7 @@ public abstract class DatabaseTraceTest extends DatabasePerformanceTest {
private
SecretKey
databaseKey
=
getSecretKey
();
abstract
Database
<
Connection
>
createDatabase
(
DatabaseConfig
databaseConfig
,
Clock
clock
);
MessageFactory
messageFactory
,
Clock
clock
);
@Nullable
protected
abstract
File
getTraceFile
();
...
...
@@ -46,7 +48,8 @@ public abstract class DatabaseTraceTest extends DatabasePerformanceTest {
private
Database
<
Connection
>
openDatabase
()
throws
DbException
{
Database
<
Connection
>
db
=
createDatabase
(
new
TestDatabaseConfig
(
testDir
,
MAX_SIZE
),
new
SystemClock
());
new
TestDatabaseConfig
(
testDir
,
MAX_SIZE
),
new
TestMessageFactory
(),
new
SystemClock
());
db
.
open
(
databaseKey
,
null
);
return
db
;
}
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabasePerformanceTest.java
View file @
6925dfcb
package
org.briarproject.bramble.db
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.junit.Ignore
;
...
...
@@ -13,7 +14,8 @@ public class H2DatabasePerformanceTest extends SingleDatabasePerformanceTest {
}
@Override
protected
JdbcDatabase
createDatabase
(
DatabaseConfig
config
,
Clock
clock
)
{
return
new
H2Database
(
config
,
clock
);
protected
JdbcDatabase
createDatabase
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
return
new
H2Database
(
config
,
messageFactory
,
clock
);
}
}
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTest.java
View file @
6925dfcb
package
org.briarproject.bramble.db
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
public
class
H2DatabaseTest
extends
JdbcDatabaseTest
{
public
H2DatabaseTest
()
throws
Exception
{
super
();
}
@Override
protected
JdbcDatabase
createDatabase
(
DatabaseConfig
config
,
Clock
clock
)
{
return
new
H2Database
(
config
,
clock
);
protected
JdbcDatabase
createDatabase
(
DatabaseConfig
config
,
MessageFactory
messageFactory
,
Clock
clock
)
{
return
new
H2Database
(
config
,
messageFactory
,
clock
);
}
}
bramble-core/src/test/java/org/briarproject/bramble/db/H2DatabaseTraceTest.java
View file @
6925dfcb
package
org.briarproject.bramble.db
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.sync.MessageFactory
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.junit.Ignore
;
...
...
@@ -14,8 +15,8 @@ public class H2DatabaseTraceTest extends DatabaseTraceTest {
@Override
Database
<
Connection
>
createDatabase
(
DatabaseConfig
databaseConfig
,
Clock
clock
)
{
return
new
H2Database
(
databaseConfig
,
clock
)
{
MessageFactory
messageFactory
,
Clock
clock
)
{
return
new
H2Database
(
databaseConfig
,
messageFactory
,
clock
)
{
@Override
@Nonnull
String
getUrl
()
{
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/H2HyperSqlDatabasePerformanceComparisonTest.java
View file @
6925dfcb
package
org.briarproject.bramble.db
;
import
org.briarproject.bramble.api.db.DatabaseConfig
;
import
org.briarproject.bramble.api.sync.MessageFactory
;