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
680
Issues
680
List
Boards
Labels
Service Desk
Milestones
Merge Requests
11
Merge Requests
11
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
briar
briar
Commits
cd24be7e
Verified
Commit
cd24be7e
authored
Apr 17, 2019
by
akwizgran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests for pending contact factory.
parent
fa562b40
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
1 deletion
+122
-1
bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactoryImpl.java
...iarproject/bramble/contact/PendingContactFactoryImpl.java
+1
-1
bramble-core/src/test/java/org/briarproject/bramble/contact/PendingContactFactoryImplTest.java
...roject/bramble/contact/PendingContactFactoryImplTest.java
+121
-0
No files found.
bramble-core/src/main/java/org/briarproject/bramble/contact/PendingContactFactoryImpl.java
View file @
cd24be7e
...
...
@@ -44,7 +44,7 @@ class PendingContactFactoryImpl implements PendingContactFactory {
private
PublicKey
parseHandshakeLink
(
String
link
)
throws
FormatException
{
Matcher
matcher
=
LINK_REGEX
.
matcher
(
link
);
if
(!
matcher
.
matches
())
throw
new
FormatException
();
if
(!
matcher
.
find
())
throw
new
FormatException
();
link
=
matcher
.
group
();
// Discard anything before or after the link
if
(
link
.
startsWith
(
"briar://"
))
link
=
link
.
substring
(
8
);
byte
[]
base32
=
Base32
.
decode
(
link
,
false
);
...
...
bramble-core/src/test/java/org/briarproject/bramble/contact/PendingContactFactoryImplTest.java
0 → 100644
View file @
cd24be7e
package
org.briarproject.bramble.contact
;
import
org.briarproject.bramble.api.FormatException
;
import
org.briarproject.bramble.api.contact.PendingContact
;
import
org.briarproject.bramble.api.crypto.CryptoComponent
;
import
org.briarproject.bramble.api.crypto.KeyParser
;
import
org.briarproject.bramble.api.crypto.PublicKey
;
import
org.briarproject.bramble.api.system.Clock
;
import
org.briarproject.bramble.test.BrambleMockTestCase
;
import
org.briarproject.bramble.util.Base32
;
import
org.jmock.Expectations
;
import
org.junit.Test
;
import
java.security.GeneralSecurityException
;
import
static
java
.
lang
.
System
.
arraycopy
;
import
static
org
.
briarproject
.
bramble
.
api
.
contact
.
HandshakeLinkConstants
.
BASE32_LINK_BYTES
;
import
static
org
.
briarproject
.
bramble
.
api
.
contact
.
HandshakeLinkConstants
.
FORMAT_VERSION
;
import
static
org
.
briarproject
.
bramble
.
api
.
contact
.
HandshakeLinkConstants
.
ID_LABEL
;
import
static
org
.
briarproject
.
bramble
.
api
.
contact
.
HandshakeLinkConstants
.
RAW_LINK_BYTES
;
import
static
org
.
briarproject
.
bramble
.
api
.
contact
.
PendingContactState
.
WAITING_FOR_CONNECTION
;
import
static
org
.
briarproject
.
bramble
.
api
.
identity
.
AuthorConstants
.
MAX_AUTHOR_NAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomBytes
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomId
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
public
class
PendingContactFactoryImplTest
extends
BrambleMockTestCase
{
private
final
CryptoComponent
crypto
=
context
.
mock
(
CryptoComponent
.
class
);
private
final
Clock
clock
=
context
.
mock
(
Clock
.
class
);
private
final
KeyParser
keyParser
=
context
.
mock
(
KeyParser
.
class
);
private
final
PublicKey
publicKey
=
context
.
mock
(
PublicKey
.
class
);
private
final
PendingContactFactory
pendingContactFactory
=
new
PendingContactFactoryImpl
(
crypto
,
clock
);
private
final
String
alias
=
getRandomString
(
MAX_AUTHOR_NAME_LENGTH
);
private
final
byte
[]
publicKeyBytes
=
getRandomBytes
(
RAW_LINK_BYTES
-
1
);
private
final
byte
[]
idBytes
=
getRandomId
();
private
final
long
timestamp
=
System
.
currentTimeMillis
();
@Test
(
expected
=
FormatException
.
class
)
public
void
testRejectsSyntacticallyInvalidLink
()
throws
Exception
{
pendingContactFactory
.
createPendingContact
(
"briar://potato"
,
alias
);
}
@Test
(
expected
=
FormatException
.
class
)
public
void
testRejectsLinkWithUnknownFormatVersion
()
throws
Exception
{
String
link
=
encodeLink
(
FORMAT_VERSION
+
1
);
pendingContactFactory
.
createPendingContact
(
link
,
alias
);
}
@Test
(
expected
=
FormatException
.
class
)
public
void
testRejectsLinkWithInvalidPublicKey
()
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
crypto
).
getAgreementKeyParser
();
will
(
returnValue
(
keyParser
));
oneOf
(
keyParser
).
parsePublicKey
(
with
(
equal
(
publicKeyBytes
)));
will
(
throwException
(
new
GeneralSecurityException
()));
}});
pendingContactFactory
.
createPendingContact
(
encodeLink
(),
alias
);
}
@Test
public
void
testAcceptsValidLinkWithoutPrefix
()
throws
Exception
{
testAcceptsValidLink
(
encodeLink
());
}
@Test
public
void
testAcceptsValidLinkWithPrefix
()
throws
Exception
{
testAcceptsValidLink
(
"briar://"
+
encodeLink
());
}
@Test
public
void
testAcceptsValidLinkWithRubbish
()
throws
Exception
{
testAcceptsValidLink
(
"before "
+
encodeLink
()
+
" after"
);
}
@Test
public
void
testAcceptsValidLinkWithPrefixAndRubbish
()
throws
Exception
{
testAcceptsValidLink
(
"before briar://"
+
encodeLink
()
+
" after"
);
}
private
void
testAcceptsValidLink
(
String
link
)
throws
Exception
{
context
.
checking
(
new
Expectations
()
{{
oneOf
(
crypto
).
getAgreementKeyParser
();
will
(
returnValue
(
keyParser
));
oneOf
(
keyParser
).
parsePublicKey
(
with
(
equal
(
publicKeyBytes
)));
will
(
returnValue
(
publicKey
));
allowing
(
publicKey
).
getEncoded
();
will
(
returnValue
(
publicKeyBytes
));
oneOf
(
crypto
).
hash
(
ID_LABEL
,
publicKeyBytes
);
will
(
returnValue
(
idBytes
));
oneOf
(
clock
).
currentTimeMillis
();
will
(
returnValue
(
timestamp
));
}});
PendingContact
p
=
pendingContactFactory
.
createPendingContact
(
link
,
alias
);
assertArrayEquals
(
idBytes
,
p
.
getId
().
getBytes
());
assertArrayEquals
(
publicKeyBytes
,
p
.
getPublicKey
());
assertEquals
(
alias
,
p
.
getAlias
());
assertEquals
(
WAITING_FOR_CONNECTION
,
p
.
getState
());
assertEquals
(
timestamp
,
p
.
getTimestamp
());
}
private
String
encodeLink
()
{
return
encodeLink
(
FORMAT_VERSION
);
}
private
String
encodeLink
(
int
formatVersion
)
{
byte
[]
rawLink
=
new
byte
[
RAW_LINK_BYTES
];
rawLink
[
0
]
=
(
byte
)
formatVersion
;
arraycopy
(
publicKeyBytes
,
0
,
rawLink
,
1
,
publicKeyBytes
.
length
);
String
base32
=
Base32
.
encode
(
rawLink
).
toLowerCase
();
assertEquals
(
BASE32_LINK_BYTES
,
base32
.
length
());
return
base32
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment