From 2e4b82dc05a6abff0180325a4342fd2298aa6b70 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 20 Feb 2024 23:01:01 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/activitypub/inbox/InboxTest.kt | 40 ++++++++++++++++++- .../api/inbox/InboxControllerImplTest.kt | 24 +++++------ .../HttpSignatureHeaderCheckerTest.kt | 2 +- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/intTest/kotlin/activitypub/inbox/InboxTest.kt b/src/intTest/kotlin/activitypub/inbox/InboxTest.kt index 6bd4c2d6..aab8b225 100644 --- a/src/intTest/kotlin/activitypub/inbox/InboxTest.kt +++ b/src/intTest/kotlin/activitypub/inbox/InboxTest.kt @@ -17,11 +17,13 @@ package activitypub.inbox import dev.usbharu.hideout.SpringApplication +import dev.usbharu.hideout.util.Base64Util import org.flywaydb.core.Flyway import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Qualifier import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.TestConfiguration @@ -37,17 +39,25 @@ import org.springframework.transaction.annotation.Transactional import org.springframework.web.context.WebApplicationContext import util.TestTransaction import util.WithMockHttpSignature +import java.security.MessageDigest +import java.time.ZonedDateTime +import java.time.format.DateTimeFormatter @SpringBootTest(classes = [SpringApplication::class]) @AutoConfigureMockMvc @Transactional class InboxTest { + @Autowired + @Qualifier("http") + private lateinit var dateTimeFormatter: DateTimeFormatter + @Autowired private lateinit var context: WebApplicationContext private lateinit var mockMvc: MockMvc + @BeforeEach fun setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(context) @@ -62,6 +72,12 @@ class InboxTest { .post("/inbox") { content = "{}" contentType = MediaType.APPLICATION_JSON + header("Host", "example.com") + header("Date", ZonedDateTime.now().format(dateTimeFormatter)) + header( + "Digest", + "SHA-256=" + Base64Util.encode(MessageDigest.getInstance("SHA-256").digest("{}".toByteArray())) + ) } .asyncDispatch() .andExpect { status { isUnauthorized() } } @@ -74,7 +90,13 @@ class InboxTest { .post("/inbox") { content = "{}" contentType = MediaType.APPLICATION_JSON - header("Signature", "") + header("Signature", "a") + header("Host", "example.com") + header("Date", ZonedDateTime.now().format(dateTimeFormatter)) + header( + "Digest", + "SHA-256=" + Base64Util.encode(MessageDigest.getInstance("SHA-256").digest("{}".toByteArray())) + ) } .asyncDispatch() .andExpect { status { isAccepted() } } @@ -87,8 +109,15 @@ class InboxTest { .post("/users/hoge/inbox") { content = "{}" contentType = MediaType.APPLICATION_JSON + header("Host", "example.com") + header("Date", ZonedDateTime.now().format(dateTimeFormatter)) + header( + "Digest", + "SHA-256=" + Base64Util.encode(MessageDigest.getInstance("SHA-256").digest("{}".toByteArray())) + ) } .asyncDispatch() + .andDo { print() } .andExpect { status { isUnauthorized() } } } @@ -99,9 +128,16 @@ class InboxTest { .post("/users/hoge/inbox") { content = "{}" contentType = MediaType.APPLICATION_JSON - header("Signature", "") + header("Signature", "a") + header("Host", "example.com") + header("Date", ZonedDateTime.now().format(dateTimeFormatter)) + header( + "Digest", + "SHA-256=" + Base64Util.encode(MessageDigest.getInstance("SHA-256").digest("{}".toByteArray())) + ) } .asyncDispatch() + .andDo { print() } .andExpect { status { isAccepted() } } } diff --git a/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt b/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt index 462a5262..5ff88e2f 100644 --- a/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/activitypub/interfaces/api/inbox/InboxControllerImplTest.kt @@ -90,7 +90,7 @@ class InboxControllerImplTest { header("Signature", "a") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=" + digest) }.asyncDispatch().andExpect { status { isAccepted() } } @@ -110,7 +110,7 @@ class InboxControllerImplTest { header("Signature", "a") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") }.asyncDispatch().andExpect { status { isAccepted() } } @@ -135,7 +135,7 @@ class InboxControllerImplTest { header("Signature", "a") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") }.asyncDispatch().andExpect { status { isAccepted() } } @@ -165,7 +165,7 @@ class InboxControllerImplTest { header("Signature", "a") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") }.asyncDispatch().andExpect { status { isAccepted() } } @@ -185,7 +185,7 @@ class InboxControllerImplTest { header("Signature", "a") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") }.asyncDispatch().andExpect { status { isAccepted() } } @@ -210,7 +210,7 @@ class InboxControllerImplTest { header("Signature", "a") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") }.asyncDispatch().andExpect { status { isAccepted() } } @@ -427,7 +427,7 @@ class InboxControllerImplTest { header("Signature", "") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") } .asyncDispatch() .andExpect { @@ -470,7 +470,7 @@ class InboxControllerImplTest { header("Signature", "") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") } .asyncDispatch() .andExpect { @@ -493,7 +493,7 @@ class InboxControllerImplTest { contentType = MediaType.APPLICATION_JSON header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") } .asyncDispatch() .andExpect { @@ -516,7 +516,7 @@ class InboxControllerImplTest { header("Signature", "") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") } .asyncDispatch() .andExpect { @@ -537,7 +537,7 @@ class InboxControllerImplTest { contentType = MediaType.APPLICATION_JSON header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") } .asyncDispatch() .andExpect { @@ -560,7 +560,7 @@ class InboxControllerImplTest { header("Signature", "") header("Host", "example.com") header("Date", ZonedDateTime.now().format(dateTimeFormatter)) - header("Digest", digest) + header("Digest", "SHA-256=$digest") } .asyncDispatch() .andExpect { diff --git a/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureHeaderCheckerTest.kt b/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureHeaderCheckerTest.kt index b9fb9308..4a150e10 100644 --- a/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureHeaderCheckerTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/core/infrastructure/springframework/httpsignature/HttpSignatureHeaderCheckerTest.kt @@ -87,7 +87,7 @@ class HttpSignatureHeaderCheckerTest { val digest = Base64Util.encode(sha256.digest(requestBody.toByteArray())) assertDoesNotThrow { - httpSignatureHeaderChecker.checkDigest(requestBody.toByteArray(), digest) + httpSignatureHeaderChecker.checkDigest(requestBody.toByteArray(), "SHA-256=" + digest) } }