Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TLSSocket addresses on JS #3256

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +383 to +401
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this test can be written in Resource, down from Stream?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serverSocketAddresses is a Stream because server is a Stream of sockets. So we need a .compile somewhere, although it could be sooner rather than later.

.drain
}
}
}