diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt index 691b6e6c..39a7dbde 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt @@ -171,9 +171,8 @@ interface SelfSignatureSubpackets : BaseSignatureSubpackets { fun setFeatures(features: Features?): SelfSignatureSubpackets - fun getFeatures(): List = getFeaturesPacket() - ?.let { Feature.fromBitmask(it.features.toInt()) } - .orEmpty() + fun getFeatures(): List = + getFeaturesPacket()?.let { Feature.fromBitmask(it.features.toInt()) }.orEmpty() fun getFeaturesPacket(): Features? diff --git a/pgpainless-core/src/test/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpacketCallbackTest.kt b/pgpainless-core/src/test/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpacketCallbackTest.kt new file mode 100644 index 00000000..74daf729 --- /dev/null +++ b/pgpainless-core/src/test/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpacketCallbackTest.kt @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: 2024 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.signature.subpackets + +import java.util.* +import openpgp.toSecondsPrecision +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.pgpainless.algorithm.Feature +import org.pgpainless.algorithm.KeyFlag + +class SelfSignatureSubpacketCallbackTest { + + @Test + fun testSetHashedSignatureCreationTimeViaCallback() { + val subpackets: SelfSignatureSubpackets = SignatureSubpackets.createEmptySubpackets() + val date = Date().toSecondsPrecision() + val callback = SelfSignatureSubpackets.applyHashed { setSignatureCreationTime(date) } + + callback.modifyHashedSubpackets(subpackets) + + assertEquals(subpackets.getSignatureCreationTime(), date) + } + + @Test + fun testSetUnhashedSignatureCreationTimeViaCallback() { + val subpackets: SelfSignatureSubpackets = SignatureSubpackets.createEmptySubpackets() + val date = Date().toSecondsPrecision() + val callback = SelfSignatureSubpackets.applyUnhashed { setSignatureCreationTime(date) } + + callback.modifyUnhashedSubpackets(subpackets) + + assertEquals(subpackets.getSignatureCreationTime(), date) + } + + @Test + fun `test that mismatching applyHashed() with modifyUnhashedSubpackets() will not modify the subpackets`() { + val subpackets: SelfSignatureSubpackets = SignatureSubpackets.createEmptySubpackets() + val callback = + SelfSignatureSubpackets.applyHashed { + setKeyFlags(KeyFlag.CERTIFY_OTHER) + setFeatures(Feature.MODIFICATION_DETECTION) + } + + callback.modifyUnhashedSubpackets(subpackets) + + assertNull(subpackets.getKeyFlagsPacket()) + assertNull(subpackets.getFeaturesPacket()) + } + + @Test + fun testThen() { + val subpackets: SelfSignatureSubpackets = SignatureSubpackets.createEmptySubpackets() + val firstCallback = + SelfSignatureSubpackets.applyHashed { + addNotationData(false, "test@pgpainless.org", "foo-bar") + setFeatures(Feature.MODIFICATION_DETECTION) + } + val secondCallback = + SelfSignatureSubpackets.applyHashed { + clearNotationData() + setKeyFlags(KeyFlag.CERTIFY_OTHER) + } + + // removes the notation, but preserves the features and adds key-flags + firstCallback.then(secondCallback).modifyHashedSubpackets(subpackets) + + assertTrue(subpackets.getNotationDataPackets().isEmpty()) + assertEquals(listOf(Feature.MODIFICATION_DETECTION), subpackets.getFeatures()) + assertEquals(listOf(KeyFlag.CERTIFY_OTHER), subpackets.getKeyFlags()) + } +}