|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 OneLiteFeather |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.reposilite.console.infrastructure |
| 17 | + |
| 18 | +import com.reposilite.ReposiliteJournalist |
| 19 | +import com.reposilite.auth.specification.AuthenticationSpecification |
| 20 | +import com.reposilite.console.CommandExecutor |
| 21 | +import com.reposilite.console.ConsoleFacade |
| 22 | +import com.reposilite.journalist.backend.InMemoryLogger |
| 23 | +import com.reposilite.shared.ErrorResponse |
| 24 | +import com.reposilite.status.FailureFacade |
| 25 | +import com.reposilite.token.AccessTokenPermission.MANAGER |
| 26 | +import com.reposilite.token.AccessTokenType.TEMPORARY |
| 27 | +import com.reposilite.token.api.AccessTokenDto |
| 28 | +import com.reposilite.token.api.CreateAccessTokenRequest |
| 29 | +import net.onelitefeather.ingot.identity.login.LoginFacade |
| 30 | +import net.onelitefeather.ingot.identity.session.SessionCookies |
| 31 | +import org.assertj.core.api.Assertions.assertThat |
| 32 | +import org.junit.jupiter.api.AfterEach |
| 33 | +import org.junit.jupiter.api.BeforeEach |
| 34 | +import org.junit.jupiter.api.Test |
| 35 | +import panda.std.Result |
| 36 | +import panda.std.asSuccess |
| 37 | +import panda.std.reactive.Reference |
| 38 | +import java.io.ByteArrayInputStream |
| 39 | +import java.time.Clock |
| 40 | +import java.time.Duration |
| 41 | +import java.time.Instant |
| 42 | +import java.time.ZoneOffset |
| 43 | + |
| 44 | +/** |
| 45 | + * The liveness of a version 1 connection (task 4.7): a ping every 20 seconds, and a close at the expiry time of a browser session. The timers run on the fake scheduler, so no test waits. |
| 46 | + */ |
| 47 | +internal class ConsoleWebSocketLivenessTest : AuthenticationSpecification() { |
| 48 | + |
| 49 | + private val journalist = ReposiliteJournalist(InMemoryLogger(), 100, testEnv = true, noColor = true) |
| 50 | + private val scheduler = FakeConsoleScheduler() |
| 51 | + private lateinit var handler: ConsoleWebSocketHandler |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + fun createHandler() { |
| 55 | + accessTokenFacade.createAccessToken( |
| 56 | + CreateAccessTokenRequest(TEMPORARY, "deploy-bot", secret = "s3cret", permissions = setOf(MANAGER)) |
| 57 | + ) |
| 58 | + val executor = CommandExecutor( |
| 59 | + journalist = journalist, |
| 60 | + failureFacade = FailureFacade(InMemoryLogger(), Clock.fixed(Instant.EPOCH, ZoneOffset.UTC)), |
| 61 | + source = ByteArrayInputStream(ByteArray(0)), |
| 62 | + ) |
| 63 | + // A real session needs the login store, so a stand-in accepts the one cookie value the tests use and ends it at a fixed time. |
| 64 | + val authenticator = object : ConsoleAuthenticator( |
| 65 | + accessTokenFacade, |
| 66 | + authenticationFacade, |
| 67 | + LoginFacade(service = null, sessions = null, ownTokens = null), |
| 68 | + ) { |
| 69 | + override fun authenticateSession(sessionJwt: String, ip: String): Result<AccessTokenDto, ErrorResponse> = |
| 70 | + accessTokenFacade.getAccessToken("deploy-bot")!!.asSuccess() |
| 71 | + |
| 72 | + override fun sessionExpiry(sessionJwt: String): Instant? = Instant.EPOCH.plus(SESSION_LIFETIME) |
| 73 | + } |
| 74 | + handler = ConsoleWebSocketHandler( |
| 75 | + journalist = journalist, |
| 76 | + consoleAuthenticator = authenticator, |
| 77 | + consoleFacade = ConsoleFacade(journalist, executor), |
| 78 | + forwardedIp = Reference.reference("X-Forwarded-For"), |
| 79 | + audit = {}, |
| 80 | + scheduler = scheduler, |
| 81 | + nodeId = "node-a", |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + @AfterEach |
| 86 | + fun shutDownJournalist() { |
| 87 | + journalist.shutdown() |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `sends a ping every 20 seconds`() { |
| 92 | + val connection = connected() |
| 93 | + |
| 94 | + scheduler.advance(Duration.ofSeconds(19)) |
| 95 | + assertThat(connection.pings).`as`("pings after 19 seconds").isEqualTo(0) |
| 96 | + |
| 97 | + scheduler.advance(Duration.ofSeconds(1)) |
| 98 | + assertThat(connection.pings).`as`("pings after 20 seconds").isEqualTo(1) |
| 99 | + |
| 100 | + scheduler.advance(Duration.ofSeconds(20)) |
| 101 | + assertThat(connection.pings).`as`("pings after 40 seconds").isEqualTo(2) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `drops a connection whose ping cannot be written and pings it no more`() { |
| 106 | + val connection = FakeConsoleConnection(failPing = true) |
| 107 | + handler.handleMessage(connection, "{\"type\":\"hello\",\"protocols\":[1],\"token\":\"deploy-bot:s3cret\"}") |
| 108 | + |
| 109 | + scheduler.advance(Duration.ofSeconds(20)) |
| 110 | + scheduler.advance(Duration.ofSeconds(20)) |
| 111 | + |
| 112 | + assertThat(connection.disconnected).`as`("disconnected").isTrue() |
| 113 | + assertThat(connection.pings).`as`("ping attempts").isEqualTo(1) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `sends no ping to a connection that was released`() { |
| 118 | + val connection = connected() |
| 119 | + |
| 120 | + handler.release(connection) |
| 121 | + scheduler.advance(Duration.ofMinutes(5)) |
| 122 | + |
| 123 | + assertThat(connection.pings).`as`("pings after the release").isEqualTo(0) |
| 124 | + assertThat(connection.closeCode).`as`("close code after the release").isNull() |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `closes a browser session at its expiry with 1008 and unauthorized`() { |
| 129 | + val connection = FakeConsoleConnection( |
| 130 | + origin = "https://repo.example.org", |
| 131 | + requestHost = "repo.example.org", |
| 132 | + cookies = mapOf(SessionCookies.SESSION to "jwt-of-browser"), |
| 133 | + ) |
| 134 | + handler.handleMessage(connection, "{\"type\":\"hello\",\"protocols\":[1]}") |
| 135 | + connection.json.clear() |
| 136 | + scheduler.advance(Duration.ofSeconds(SESSION_LIFETIME.seconds - 1)) |
| 137 | + assertThat(connection.closeCode).`as`("close code before the expiry").isNull() |
| 138 | + |
| 139 | + scheduler.advance(Duration.ofSeconds(1)) |
| 140 | + |
| 141 | + assertThat((connection.json.last() as ErrorFrame).code).`as`("error code").isEqualTo("unauthorized") |
| 142 | + assertThat(connection.closeCode).`as`("close code at the expiry").isEqualTo(1008) |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun `leaves a token connection open past the time a session would expire`() { |
| 147 | + val connection = connected() |
| 148 | + |
| 149 | + scheduler.advance(SESSION_LIFETIME.plusSeconds(60)) |
| 150 | + |
| 151 | + assertThat(connection.closeCode).`as`("close code").isNull() |
| 152 | + } |
| 153 | + |
| 154 | + private fun connected(): FakeConsoleConnection { |
| 155 | + val connection = FakeConsoleConnection() |
| 156 | + handler.handleMessage(connection, "{\"type\":\"hello\",\"protocols\":[1],\"token\":\"deploy-bot:s3cret\"}") |
| 157 | + assertThat(connection.json.first()).`as`("first frame").isInstanceOf(WelcomeFrame::class.java) |
| 158 | + return connection |
| 159 | + } |
| 160 | + |
| 161 | + private companion object { |
| 162 | + val SESSION_LIFETIME: Duration = Duration.ofMinutes(5) |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments