Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
briar
briar
Commits
8fc622f8
Verified
Commit
8fc622f8
authored
Oct 24, 2018
by
Torsten Grote
Browse files
[bramble] Add support for contact aliases
Foundation for
#41
parent
22eed910
Changes
23
Hide whitespace changes
Inline
Side-by-side
bramble-api/src/main/java/org/briarproject/bramble/api/contact/Contact.java
View file @
8fc622f8
...
...
@@ -4,8 +4,12 @@ import org.briarproject.bramble.api.identity.Author;
import
org.briarproject.bramble.api.identity.AuthorId
;
import
org.briarproject.bramble.api.nullsafety.NotNullByDefault
;
import
javax.annotation.Nullable
;
import
javax.annotation.concurrent.Immutable
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
toUtf8
;
@Immutable
@NotNullByDefault
public
class
Contact
{
...
...
@@ -13,13 +17,21 @@ public class Contact {
private
final
ContactId
id
;
private
final
Author
author
;
private
final
AuthorId
localAuthorId
;
@Nullable
private
final
String
alias
;
private
final
boolean
verified
,
active
;
public
Contact
(
ContactId
id
,
Author
author
,
AuthorId
localAuthorId
,
boolean
verified
,
boolean
active
)
{
@Nullable
String
alias
,
boolean
verified
,
boolean
active
)
{
if
(
alias
!=
null
)
{
int
aliasLength
=
toUtf8
(
alias
).
length
;
if
(
aliasLength
==
0
||
aliasLength
>
MAX_AUTHOR_NAME_LENGTH
)
throw
new
IllegalArgumentException
();
}
this
.
id
=
id
;
this
.
author
=
author
;
this
.
localAuthorId
=
localAuthorId
;
this
.
alias
=
alias
;
this
.
verified
=
verified
;
this
.
active
=
active
;
}
...
...
@@ -36,6 +48,11 @@ public class Contact {
return
localAuthorId
;
}
@Nullable
public
String
getAlias
()
{
return
alias
;
}
public
boolean
isVerified
()
{
return
verified
;
}
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/contact/ContactManager.java
View file @
8fc622f8
...
...
@@ -10,6 +10,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import
java.util.Collection
;
import
javax.annotation.Nullable
;
@NotNullByDefault
public
interface
ContactManager
{
...
...
@@ -93,6 +95,12 @@ public interface ContactManager {
void
setContactActive
(
Transaction
txn
,
ContactId
c
,
boolean
active
)
throws
DbException
;
/**
* Sets an alias name for the contact or unsets it if alias is null.
*/
void
setContactAlias
(
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
;
/**
* Return true if a contact with this name and public key already exists
*/
...
...
bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java
View file @
8fc622f8
...
...
@@ -515,6 +515,12 @@ public interface DatabaseComponent {
void
setContactActive
(
Transaction
txn
,
ContactId
c
,
boolean
active
)
throws
DbException
;
/**
* Sets an alias name for the contact or unsets it if alias is null.
*/
void
setContactAlias
(
Transaction
txn
,
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
;
/**
* Sets the given group's visibility to the given contact.
*/
...
...
bramble-core/src/main/java/org/briarproject/bramble/contact/ContactManagerImpl.java
View file @
8fc622f8
...
...
@@ -18,9 +18,13 @@ import java.util.Collection;
import
java.util.List
;
import
java.util.concurrent.CopyOnWriteArrayList
;
import
javax.annotation.Nullable
;
import
javax.annotation.concurrent.ThreadSafe
;
import
javax.inject.Inject
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
toUtf8
;
@ThreadSafe
@NotNullByDefault
class
ContactManagerImpl
implements
ContactManager
{
...
...
@@ -148,6 +152,17 @@ class ContactManagerImpl implements ContactManager {
db
.
setContactActive
(
txn
,
c
,
active
);
}
@Override
public
void
setContactAlias
(
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
{
if
(
alias
!=
null
)
{
int
aliasLength
=
toUtf8
(
alias
).
length
;
if
(
aliasLength
==
0
||
aliasLength
>
MAX_AUTHOR_NAME_LENGTH
)
throw
new
IllegalArgumentException
();
}
db
.
transaction
(
false
,
txn
->
db
.
setContactAlias
(
txn
,
c
,
alias
));
}
@Override
public
boolean
contactExists
(
Transaction
txn
,
AuthorId
remoteAuthorId
,
AuthorId
localAuthorId
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/Database.java
View file @
8fc622f8
...
...
@@ -616,6 +616,12 @@ interface Database<T> {
void
setContactActive
(
T
txn
,
ContactId
c
,
boolean
active
)
throws
DbException
;
/**
* Sets an alias name for a contact.
*/
void
setContactAlias
(
T
txn
,
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
;
/**
* Sets the given group's visibility to the given contact to either
* {@link Visibility VISIBLE} or {@link Visibility SHARED}.
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java
View file @
8fc622f8
...
...
@@ -859,6 +859,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
transaction
.
attach
(
new
ContactStatusChangedEvent
(
c
,
active
));
}
@Override
public
void
setContactAlias
(
Transaction
transaction
,
ContactId
c
,
String
alias
)
throws
DbException
{
if
(
transaction
.
isReadOnly
())
throw
new
IllegalArgumentException
();
T
txn
=
unbox
(
transaction
);
if
(!
db
.
containsContact
(
txn
,
c
))
throw
new
NoSuchContactException
();
db
.
setContactAlias
(
txn
,
c
,
alias
);
}
@Override
public
void
setGroupVisibility
(
Transaction
transaction
,
ContactId
c
,
GroupId
g
,
Visibility
v
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
View file @
8fc622f8
...
...
@@ -56,6 +56,7 @@ import java.util.logging.Logger;
import
javax.annotation.Nullable
;
import
static
java
.
sql
.
Types
.
INTEGER
;
import
static
java
.
sql
.
Types
.
VARCHAR
;
import
static
java
.
util
.
logging
.
Level
.
INFO
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
org
.
briarproject
.
bramble
.
api
.
db
.
Metadata
.
REMOVE
;
...
...
@@ -83,7 +84,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
abstract
class
JdbcDatabase
implements
Database
<
Connection
>
{
// Package access for testing
static
final
int
CODE_SCHEMA_VERSION
=
4
0
;
static
final
int
CODE_SCHEMA_VERSION
=
4
1
;
// Rotation period offsets for incoming transport keys
private
static
final
int
OFFSET_PREV
=
-
1
;
...
...
@@ -113,6 +114,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+
" authorId _HASH NOT NULL,"
+
" formatVersion INT NOT NULL,"
+
" name _STRING NOT NULL,"
+
" alias _STRING,"
// Null if no alias exists
+
" publicKey _BINARY NOT NULL,"
+
" localAuthorId _HASH NOT NULL,"
+
" verified BOOLEAN NOT NULL,"
...
...
@@ -427,7 +429,11 @@ abstract class JdbcDatabase implements Database<Connection> {
// Package access for testing
List
<
Migration
<
Connection
>>
getMigrations
()
{
return
Arrays
.
asList
(
new
Migration38_39
(),
new
Migration39_40
());
return
Arrays
.
asList
(
new
Migration38_39
(),
new
Migration39_40
(),
new
Migration40_41
()
);
}
private
boolean
isCompactionDue
(
Settings
s
)
{
...
...
@@ -1258,8 +1264,8 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement
ps
=
null
;
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT authorId, formatVersion, name,
publicKey
,"
+
" localAuthorId, verified, active"
String
sql
=
"SELECT authorId, formatVersion, name,
alias
,"
+
"
publicKey,
localAuthorId, verified, active"
+
" FROM contacts"
+
" WHERE contactId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
...
...
@@ -1269,15 +1275,17 @@ abstract class JdbcDatabase implements Database<Connection> {
AuthorId
authorId
=
new
AuthorId
(
rs
.
getBytes
(
1
));
int
formatVersion
=
rs
.
getInt
(
2
);
String
name
=
rs
.
getString
(
3
);
byte
[]
publicKey
=
rs
.
getBytes
(
4
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
5
));
boolean
verified
=
rs
.
getBoolean
(
6
);
boolean
active
=
rs
.
getBoolean
(
7
);
String
alias
=
rs
.
getString
(
4
);
byte
[]
publicKey
=
rs
.
getBytes
(
5
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
6
));
boolean
verified
=
rs
.
getBoolean
(
7
);
boolean
active
=
rs
.
getBoolean
(
8
);
rs
.
close
();
ps
.
close
();
Author
author
=
new
Author
(
authorId
,
formatVersion
,
name
,
publicKey
);
return
new
Contact
(
c
,
author
,
localAuthorId
,
verified
,
active
);
return
new
Contact
(
c
,
author
,
localAuthorId
,
alias
,
verified
,
active
);
}
catch
(
SQLException
e
)
{
tryToClose
(
rs
);
tryToClose
(
ps
);
...
...
@@ -1292,7 +1300,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT contactId, authorId, formatVersion, name,"
+
" publicKey, localAuthorId, verified, active"
+
"
alias,
publicKey, localAuthorId, verified, active"
+
" FROM contacts"
;
ps
=
txn
.
prepareStatement
(
sql
);
rs
=
ps
.
executeQuery
();
...
...
@@ -1302,14 +1310,15 @@ abstract class JdbcDatabase implements Database<Connection> {
AuthorId
authorId
=
new
AuthorId
(
rs
.
getBytes
(
2
));
int
formatVersion
=
rs
.
getInt
(
3
);
String
name
=
rs
.
getString
(
4
);
byte
[]
publicKey
=
rs
.
getBytes
(
5
);
String
alias
=
rs
.
getString
(
5
);
byte
[]
publicKey
=
rs
.
getBytes
(
6
);
Author
author
=
new
Author
(
authorId
,
formatVersion
,
name
,
publicKey
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
6
));
boolean
verified
=
rs
.
getBoolean
(
7
);
boolean
active
=
rs
.
getBoolean
(
8
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
7
));
boolean
verified
=
rs
.
getBoolean
(
8
);
boolean
active
=
rs
.
getBoolean
(
9
);
contacts
.
add
(
new
Contact
(
contactId
,
author
,
localAuthorId
,
verified
,
active
));
alias
,
verified
,
active
));
}
rs
.
close
();
ps
.
close
();
...
...
@@ -1350,8 +1359,8 @@ abstract class JdbcDatabase implements Database<Connection> {
PreparedStatement
ps
=
null
;
ResultSet
rs
=
null
;
try
{
String
sql
=
"SELECT contactId, formatVersion, name,
publicKey
,"
+
" localAuthorId, verified, active"
String
sql
=
"SELECT contactId, formatVersion, name,
alias
,"
+
"
publicKey,
localAuthorId, verified, active"
+
" FROM contacts"
+
" WHERE authorId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
...
...
@@ -1362,14 +1371,15 @@ abstract class JdbcDatabase implements Database<Connection> {
ContactId
c
=
new
ContactId
(
rs
.
getInt
(
1
));
int
formatVersion
=
rs
.
getInt
(
2
);
String
name
=
rs
.
getString
(
3
);
byte
[]
publicKey
=
rs
.
getBytes
(
4
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
5
));
boolean
verified
=
rs
.
getBoolean
(
6
);
boolean
active
=
rs
.
getBoolean
(
7
);
String
alias
=
rs
.
getString
(
4
);
byte
[]
publicKey
=
rs
.
getBytes
(
5
);
AuthorId
localAuthorId
=
new
AuthorId
(
rs
.
getBytes
(
6
));
boolean
verified
=
rs
.
getBoolean
(
7
);
boolean
active
=
rs
.
getBoolean
(
8
);
Author
author
=
new
Author
(
remote
,
formatVersion
,
name
,
publicKey
);
contacts
.
add
(
new
Contact
(
c
,
author
,
localAuthorId
,
verified
,
active
));
contacts
.
add
(
new
Contact
(
c
,
author
,
localAuthorId
,
alias
,
verified
,
active
));
}
rs
.
close
();
ps
.
close
();
...
...
@@ -2794,6 +2804,25 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
@Override
public
void
setContactAlias
(
Connection
txn
,
ContactId
c
,
@Nullable
String
alias
)
throws
DbException
{
PreparedStatement
ps
=
null
;
try
{
String
sql
=
"UPDATE contacts SET alias = ? WHERE contactId = ?"
;
ps
=
txn
.
prepareStatement
(
sql
);
if
(
alias
==
null
)
ps
.
setNull
(
1
,
VARCHAR
);
else
ps
.
setString
(
1
,
alias
);
ps
.
setInt
(
2
,
c
.
getInt
());
int
affected
=
ps
.
executeUpdate
();
if
(
affected
<
0
||
affected
>
1
)
throw
new
DbStateException
();
ps
.
close
();
}
catch
(
SQLException
e
)
{
tryToClose
(
ps
);
throw
new
DbException
(
e
);
}
}
@Override
public
void
setGroupVisibility
(
Connection
txn
,
ContactId
c
,
GroupId
g
,
boolean
shared
)
throws
DbException
{
...
...
bramble-core/src/main/java/org/briarproject/bramble/db/Migration40_41.java
0 → 100644
View file @
8fc622f8
package
org.briarproject.bramble.db
;
import
org.briarproject.bramble.api.db.DbException
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.logging.Logger
;
import
javax.annotation.Nullable
;
import
static
java
.
util
.
logging
.
Level
.
WARNING
;
import
static
java
.
util
.
logging
.
Logger
.
getLogger
;
import
static
org
.
briarproject
.
bramble
.
util
.
LogUtils
.
logException
;
class
Migration40_41
implements
Migration
<
Connection
>
{
private
static
final
Logger
LOG
=
getLogger
(
Migration40_41
.
class
.
getName
());
@Override
public
int
getStartVersion
()
{
return
40
;
}
@Override
public
int
getEndVersion
()
{
return
41
;
}
@Override
public
void
migrate
(
Connection
txn
)
throws
DbException
{
Statement
s
=
null
;
try
{
s
=
txn
.
createStatement
();
s
.
execute
(
"ALTER TABLE contacts"
// TODO how to insertTypeNames _STRING ?
+
" ADD alias VARCHAR"
);
}
catch
(
SQLException
e
)
{
tryToClose
(
s
);
throw
new
DbException
(
e
);
}
}
private
void
tryToClose
(
@Nullable
Statement
s
)
{
try
{
if
(
s
!=
null
)
s
.
close
();
}
catch
(
SQLException
e
)
{
logException
(
LOG
,
WARNING
,
e
);
}
}
}
bramble-core/src/test/java/org/briarproject/bramble/contact/ContactManagerImplTest.java
View file @
8fc622f8
...
...
@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.identity.Author;
import
org.briarproject.bramble.api.identity.AuthorId
;
import
org.briarproject.bramble.api.transport.KeyManager
;
import
org.briarproject.bramble.test.BrambleMockTestCase
;
import
org.briarproject.bramble.test.DbExpectations
;
import
org.jmock.Expectations
;
import
org.jmock.Mockery
;
import
org.junit.Test
;
...
...
@@ -20,9 +21,11 @@ import java.util.Collection;
import
java.util.Collections
;
import
java.util.Random
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getAuthor
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomId
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getSecretKey
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
...
@@ -35,9 +38,10 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
private
final
ContactId
contactId
=
new
ContactId
(
42
);
private
final
Author
remote
=
getAuthor
();
private
final
AuthorId
local
=
new
AuthorId
(
getRandomId
());
private
final
String
alias
=
getRandomString
(
MAX_AUTHOR_NAME_LENGTH
);
private
final
boolean
verified
=
false
,
active
=
true
;
private
final
Contact
contact
=
new
Contact
(
contactId
,
remote
,
local
,
verified
,
active
);
new
Contact
(
contactId
,
remote
,
local
,
alias
,
verified
,
active
);
public
ContactManagerImplTest
()
{
contactManager
=
new
ContactManagerImpl
(
db
,
keyManager
);
...
...
@@ -131,7 +135,8 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
public
void
testActiveContacts
()
throws
Exception
{
Collection
<
Contact
>
activeContacts
=
Collections
.
singletonList
(
contact
);
Collection
<
Contact
>
contacts
=
new
ArrayList
<>(
activeContacts
);
contacts
.
add
(
new
Contact
(
new
ContactId
(
3
),
remote
,
local
,
true
,
false
));
contacts
.
add
(
new
Contact
(
new
ContactId
(
3
),
remote
,
local
,
alias
,
true
,
false
));
Transaction
txn
=
new
Transaction
(
null
,
true
);
context
.
checking
(
new
Expectations
()
{{
oneOf
(
db
).
startTransaction
(
true
);
...
...
@@ -171,6 +176,23 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
contactManager
.
setContactActive
(
txn
,
contactId
,
active
);
}
@Test
public
void
testSetContactAlias
()
throws
Exception
{
Transaction
txn
=
new
Transaction
(
null
,
false
);
context
.
checking
(
new
DbExpectations
()
{{
oneOf
(
db
).
transaction
(
with
(
equal
(
false
)),
withDbRunnable
(
txn
));
oneOf
(
db
).
setContactAlias
(
txn
,
contactId
,
alias
);
}});
contactManager
.
setContactAlias
(
contactId
,
alias
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testSetContactAliasTooLong
()
throws
Exception
{
contactManager
.
setContactAlias
(
contactId
,
getRandomString
(
MAX_AUTHOR_NAME_LENGTH
+
1
));
}
@Test
public
void
testContactExists
()
throws
Exception
{
Transaction
txn
=
new
Transaction
(
null
,
true
);
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java
View file @
8fc622f8
...
...
@@ -77,6 +77,7 @@ import static org.briarproject.bramble.test.TestUtils.getMessage;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomId
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getSecretKey
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getTransportId
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
...
...
@@ -99,6 +100,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
private
final
Group
group
;
private
final
Author
author
;
private
final
LocalAuthor
localAuthor
;
private
final
String
alias
;
private
final
Message
message
,
message1
;
private
final
MessageId
messageId
,
messageId1
;
private
final
Metadata
metadata
;
...
...
@@ -115,6 +117,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
groupId
=
group
.
getId
();
author
=
getAuthor
();
localAuthor
=
getLocalAuthor
();
alias
=
getRandomString
(
5
);
message
=
getMessage
(
groupId
);
message1
=
getMessage
(
groupId
);
messageId
=
message
.
getId
();
...
...
@@ -124,7 +127,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
transportId
=
getTransportId
();
maxLatency
=
Integer
.
MAX_VALUE
;
contactId
=
new
ContactId
(
234
);
contact
=
new
Contact
(
contactId
,
author
,
localAuthor
.
getId
(),
contact
=
new
Contact
(
contactId
,
author
,
localAuthor
.
getId
(),
alias
,
true
,
true
);
keySetId
=
new
KeySetId
(
345
);
}
...
...
@@ -288,11 +291,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
// Check whether the contact is in the DB (which it's not)
exactly
(
1
6
).
of
(
database
).
startTransaction
();
exactly
(
1
7
).
of
(
database
).
startTransaction
();
will
(
returnValue
(
txn
));
exactly
(
1
6
).
of
(
database
).
containsContact
(
txn
,
contactId
);
exactly
(
1
7
).
of
(
database
).
containsContact
(
txn
,
contactId
);
will
(
returnValue
(
false
));
exactly
(
1
6
).
of
(
database
).
abortTransaction
(
txn
);
exactly
(
1
7
).
of
(
database
).
abortTransaction
(
txn
);
}});
DatabaseComponent
db
=
createDatabaseComponent
(
database
,
eventBus
,
shutdown
);
...
...
@@ -450,6 +453,16 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
db
.
endTransaction
(
transaction
);
}
transaction
=
db
.
startTransaction
(
false
);
try
{
db
.
setContactAlias
(
transaction
,
contactId
,
alias
);
fail
();
}
catch
(
NoSuchContactException
expected
)
{
// Expected
}
finally
{
db
.
endTransaction
(
transaction
);
}
transaction
=
db
.
startTransaction
(
false
);
try
{
db
.
setGroupVisibility
(
transaction
,
contactId
,
groupId
,
SHARED
);
...
...
bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java
View file @
8fc622f8
...
...
@@ -53,6 +53,7 @@ import static java.util.Collections.singletonList;
import
static
java
.
util
.
Collections
.
singletonMap
;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
SECONDS
;
import
static
org
.
briarproject
.
bramble
.
api
.
db
.
Metadata
.
REMOVE
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
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
;
...
...
@@ -74,6 +75,7 @@ import static org.briarproject.bramble.test.TestUtils.getRandomId;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getSecretKey
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getTestDirectory
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getTransportId
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
...
...
@@ -1713,6 +1715,39 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
db
.
close
();
}
@Test
public
void
testSetContactAlias
()
throws
Exception
{
Database
<
Connection
>
db
=
open
(
false
);
Connection
txn
=
db
.
startTransaction
();
// Add a contact
db
.
addLocalAuthor
(
txn
,
localAuthor
);
assertEquals
(
contactId
,
db
.
addContact
(
txn
,
author
,
localAuthor
.
getId
(),
true
,
true
));
// The contact should have no alias
Contact
contact
=
db
.
getContact
(
txn
,
contactId
);
assertNull
(
contact
.
getAlias
());
// Set a contact alias
String
alias
=
getRandomString
(
MAX_AUTHOR_NAME_LENGTH
);
db
.
setContactAlias
(
txn
,
contactId
,
alias
);
// The contact should have an alias
contact
=
db
.
getContact
(
txn
,
contactId
);
assertEquals
(
alias
,
contact
.
getAlias
());
// Set the contact alias null
db
.
setContactAlias
(
txn
,
contactId
,
null
);
// The contact should have no alias
contact
=
db
.
getContact
(
txn
,
contactId
);
assertNull
(
contact
.
getAlias
());
db
.
commitTransaction
(
txn
);
db
.
close
();
}
@Test
public
void
testSetMessageState
()
throws
Exception
{
Database
<
Connection
>
db
=
open
(
false
);
...
...
bramble-core/src/test/java/org/briarproject/bramble/identity/IdentityManagerImplTest.java
View file @
8fc622f8
...
...
@@ -29,6 +29,7 @@ import static org.briarproject.bramble.api.identity.Author.Status.UNVERIFIED;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
Author
.
Status
.
VERIFIED
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getAuthor
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getLocalAuthor
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
public
class
IdentityManagerImplTest
extends
BrambleMockTestCase
{
...
...
@@ -126,7 +127,7 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
// add one unverified contact
Contact
contact
=
new
Contact
(
new
ContactId
(
1
),
author
,
localAuthor
.
getId
(),
false
,
true
);
localAuthor
.
getId
(),
getRandomString
(
5
),
false
,
true
);
contacts
.
add
(
contact
);
checkAuthorStatusContext
(
authorId
,
contacts
);
...
...
@@ -134,7 +135,7 @@ public class IdentityManagerImplTest extends BrambleMockTestCase {
// add one verified contact
Contact
contact2
=
new
Contact
(
new
ContactId
(
1
),
author
,
localAuthor
.
getId
(),
true
,
true
);
localAuthor
.
getId
(),
getRandomString
(
5
),
true
,
true
);
contacts
.
add
(
contact2
);
checkAuthorStatusContext
(
authorId
,
contacts
);
...
...
bramble-core/src/test/java/org/briarproject/bramble/properties/TransportPropertyManagerImplTest.java