Skip to content

Commit

Permalink
Merge pull request #3256 from armanbilge/fix/js-tls-socket-addresses
Browse files Browse the repository at this point in the history
Fix `TLSSocket` addresses on JS
  • Loading branch information
armanbilge authored Jul 11, 2023
2 parents 240f62a + 201e7db commit 9c52344
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion io/js/src/main/scala/fs2/io/net/tls/TLSSocketPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private[tls] trait TLSSocketCompanionPlatform { self: TLSSocket.type =>
} yield new AsyncTLSSocket(
tlsSock,
readStream,
socket,
sessionRef.discrete.unNone.head.compile.lastOrError,
F.delay[Any](tlsSock.alpnProtocol).flatMap {
case false => "".pure // mimicking JVM
Expand All @@ -81,8 +82,12 @@ private[tls] trait TLSSocketCompanionPlatform { self: TLSSocket.type =>
private[tls] final class AsyncTLSSocket[F[_]: Async](
sock: facade.tls.TLSSocket,
readStream: SuspendedStream[F, Byte],
underlying: Socket[F],
val session: F[SSLSession],
val applicationProtocol: F[String]
) extends Socket.AsyncSocket[F](sock, readStream)
with UnsealedTLSSocket[F]
with UnsealedTLSSocket[F] {
override def localAddress = underlying.localAddress
override def remoteAddress = underlying.remoteAddress
}
}
41 changes: 41 additions & 0 deletions io/js/src/test/scala/fs2/io/net/tls/TLSSocketSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -360,5 +360,46 @@ class TLSSocketSuite extends TLSSuite {
.to(Chunk)
.intercept[SSLException]
}

test("get local and remote address") {
val setup = for {
tlsContext <- Resource.eval(testTlsContext(true))
addressAndConnections <- Network[IO].serverResource(Some(ip"127.0.0.1"))
(serverAddress, server) = addressAndConnections
client = Network[IO]
.client(serverAddress)
.flatMap(
tlsContext
.clientBuilder(_)
.withParameters(
TLSParameters(checkServerIdentity =
Some((sn, _) => Either.cond(sn == "localhost", (), new RuntimeException()))
)
)
.build
)
} yield server.flatMap(s => Stream.resource(tlsContext.server(s))) -> client

Stream
.resource(setup)
.flatMap { case (server, clientSocket) =>
val serverSocketAddresses = server.evalMap { socket =>
socket.localAddress.product(socket.remoteAddress)
}

val clientSocketAddresses =
Stream.resource(clientSocket).evalMap { socket =>
socket.localAddress.product(socket.remoteAddress)
}

serverSocketAddresses.parZip(clientSocketAddresses).map {
case ((serverLocal, serverRemote), (clientLocal, clientRemote)) =>
assertEquals(clientRemote, serverLocal)
assertEquals(clientLocal, serverRemote)
}
}
.compile
.drain
}
}
}

0 comments on commit 9c52344

Please sign in to comment.