Skip to content

Commit

Permalink
fix(PublicThirdPartyEndpoint): Import certificate as ByteArray (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Mar 15, 2021
1 parent 6523014 commit 16d1df1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,26 @@ public class PublicThirdPartyEndpoint internal constructor(
* Import the public endpoint at [publicAddress].
*
* @param publicAddress The public address of the endpoint (e.g., `ping.awala.services`).
* @param identityCertificate The identity certificate of the endpoint.
* @param identityCertificateSerialized The DER serialization of identity certificate of the
* endpoint.
*/
@Throws(
PersistenceException::class,
InvalidThirdPartyEndpoint::class
)
public suspend fun import(
publicAddress: String,
identityCertificate: Certificate
identityCertificateSerialized: ByteArray
): PublicThirdPartyEndpoint {
val identityCertificate = try {
Certificate.deserialize(identityCertificateSerialized)
} catch (exc: CertificateException) {
throw InvalidThirdPartyEndpoint("Malformed identity certificate", exc)
}
try {
identityCertificate.validate()
} catch (exc: CertificateException) {
throw InvalidThirdPartyEndpoint("Invalid identity certificate")
throw InvalidThirdPartyEndpoint("Invalid identity certificate", exc)
}
val thirdPartyAddress = identityCertificate.subjectPrivateAddress
Storage.publicThirdParty.set(
Expand All @@ -175,6 +181,7 @@ public class PublicThirdPartyEndpoint internal constructor(

public class UnknownThirdPartyEndpointException(message: String) : AwaladroidException(message)
public class UnknownFirstPartyEndpointException(message: String) : AwaladroidException(message)
public class InvalidThirdPartyEndpoint(message: String) : AwaladroidException(message)
public class InvalidThirdPartyEndpoint(message: String, cause: Throwable? = null) :
AwaladroidException(message, cause)
public class InvalidAuthorizationException(message: String, cause: Throwable) :
AwaladroidException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class PublicThirdPartyEndpointTest {
@Test
fun import_successful() = runBlockingTest {
val publicAddress = "example.org"
with(PublicThirdPartyEndpoint.import(publicAddress, PDACertPath.PDA)) {
with(PublicThirdPartyEndpoint.import(publicAddress, PDACertPath.PDA.serialize())) {
assertEquals(publicAddress, this.publicAddress)
assertEquals(PDACertPath.PDA, identityCertificate)
assertEquals("https://$publicAddress", this.address)
Expand All @@ -71,6 +71,14 @@ internal class PublicThirdPartyEndpointTest {
)
}

@Test
fun import_malformedCertificate() = runBlockingTest {
expectedException.expect(InvalidThirdPartyEndpoint::class.java)
expectedException.expectMessage("Malformed identity certificate")

PublicThirdPartyEndpoint.import("example.org", "malformed".toByteArray())
}

@Test
fun import_invalidCertificate() = runBlockingTest {
val cert = issueEndpointCertificate(
Expand All @@ -83,7 +91,7 @@ internal class PublicThirdPartyEndpointTest {
expectedException.expect(InvalidThirdPartyEndpoint::class.java)
expectedException.expectMessage("Invalid identity certificate")

PublicThirdPartyEndpoint.import("example.org", cert)
PublicThirdPartyEndpoint.import("example.org", cert.serialize())
}

@Test
Expand Down

0 comments on commit 16d1df1

Please sign in to comment.