• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pgpainless / sop-java / #54

25 Sep 2025 09:07PM UTC coverage: 58.308% (-0.1%) from 58.411%
#54

push

other

vanitasvitae
Remove unused import

2088 of 3581 relevant lines covered (58.31%)

0.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.03
/sop-java-testfixtures/src/main/java/sop/testsuite/operation/InlineSignInlineVerifyTest.java
1
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package sop.testsuite.operation;
6

7
import org.junit.jupiter.api.Assertions;
8
import org.junit.jupiter.api.condition.EnabledIf;
9
import org.junit.jupiter.params.ParameterizedTest;
10
import org.junit.jupiter.params.provider.Arguments;
11
import org.junit.jupiter.params.provider.MethodSource;
12
import sop.ByteArrayAndResult;
13
import sop.SOP;
14
import sop.Verification;
15
import sop.enums.InlineSignAs;
16
import sop.enums.SignatureMode;
17
import sop.exception.SOPGPException;
18
import sop.testsuite.JUtils;
19
import sop.testsuite.TestData;
20
import sop.testsuite.assertions.VerificationListAssert;
21

22
import java.io.IOException;
23
import java.nio.charset.StandardCharsets;
24
import java.util.Date;
25
import java.util.List;
26
import java.util.stream.Stream;
27

28
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
29
import static org.junit.jupiter.api.Assertions.assertThrows;
30

31
@EnabledIf("sop.testsuite.operation.AbstractSOPTest#hasBackends")
32
public class InlineSignInlineVerifyTest extends AbstractSOPTest {
1✔
33

34
    static Stream<Arguments> provideInstances() {
35
        return provideBackends();
1✔
36
    }
37

38
    @ParameterizedTest
39
    @MethodSource("provideInstances")
40
    public void inlineSignVerifyAlice(SOP sop) throws IOException {
41
        byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
1✔
42

43
        byte[] inlineSigned = assumeSupported(sop::inlineSign)
1✔
44
                .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
1✔
45
                .data(message)
1✔
46
                .getBytes();
×
47

48
        JUtils.assertArrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE);
×
49

50
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
×
51
                .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
×
52
                .data(inlineSigned)
×
53
                .toByteArrayAndResult();
×
54

55
        assertArrayEquals(message, bytesAndResult.getBytes());
×
56

57
        List<Verification> verificationList = bytesAndResult.getResult();
×
58
        VerificationListAssert.assertThatVerificationList(verificationList)
×
59
                .isNotEmpty()
×
60
                .hasSingleItem()
×
61
                .issuedBy(TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
×
62
    }
×
63

64
    @ParameterizedTest
65
    @MethodSource("provideInstances")
66
    public void inlineSignVerifyAliceNoArmor(SOP sop) throws IOException {
67
        byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
1✔
68

69
        byte[] inlineSigned = assumeSupported(sop::inlineSign)
1✔
70
                .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
1✔
71
                .noArmor()
1✔
72
                .data(message)
1✔
73
                .getBytes();
×
74

75
        Assertions.assertFalse(JUtils.arrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE));
×
76

77
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
×
78
                .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
×
79
                .data(inlineSigned)
×
80
                .toByteArrayAndResult();
×
81

82
        assertArrayEquals(message, bytesAndResult.getBytes());
×
83

84
        List<Verification> verificationList = bytesAndResult.getResult();
×
85
        VerificationListAssert.assertThatVerificationList(verificationList)
×
86
                .isNotEmpty()
×
87
                .hasSingleItem()
×
88
                .issuedBy(TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
×
89
    }
×
90

91
    @ParameterizedTest
92
    @MethodSource("provideInstances")
93
    public void clearsignVerifyAlice(SOP sop) throws IOException {
94
        byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
1✔
95

96
        byte[] clearsigned = assumeSupported(sop::inlineSign)
1✔
97
                .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
1✔
98
                .mode(InlineSignAs.clearsigned)
1✔
99
                .data(message)
1✔
100
                .getBytes();
×
101

102
        JUtils.assertArrayStartsWith(clearsigned, TestData.BEGIN_PGP_SIGNED_MESSAGE);
×
103

104
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
×
105
                .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
×
106
                .data(clearsigned)
×
107
                .toByteArrayAndResult();
×
108

109
        assertArrayEquals(message, bytesAndResult.getBytes(),
×
110
                "ASCII armored message does not appear to start with the 'BEGIN PGP SIGNED MESSAGE' header.");
111

112
        List<Verification> verificationList = bytesAndResult.getResult();
×
113
        VerificationListAssert.assertThatVerificationList(verificationList)
×
114
                .hasSingleItem()
×
115
                .issuedBy(TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT)
×
116
                .hasModeOrNull(SignatureMode.text);
×
117
    }
×
118

119
    @ParameterizedTest
120
    @MethodSource("provideInstances")
121
    public void inlineVerifyCompareSignatureDate(SOP sop) throws IOException {
122
        byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
1✔
123
        Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
1✔
124

125
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
1✔
126
                .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
1✔
127
                .data(message)
1✔
128
                .toByteArrayAndResult();
×
129

130
        List<Verification> verificationList = bytesAndResult.getResult();
×
131
        VerificationListAssert.assertThatVerificationList(verificationList)
×
132
                .isNotEmpty()
×
133
                .hasSingleItem()
×
134
                .isCreatedAt(signatureDate)
×
135
                .issuedBy(TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
×
136
    }
×
137

138
    @ParameterizedTest
139
    @MethodSource("provideInstances")
140
    public void assertNotBeforeThrowsNoSignature(SOP sop) {
141
        byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
1✔
142
        Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
1✔
143
        Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec before sig
1✔
144

145
        assertThrows(SOPGPException.NoSignature.class, () -> assumeSupported(sop::inlineVerify)
1✔
146
                .notBefore(afterSignature)
1✔
147
                .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
1✔
148
                .data(message)
1✔
149
                .toByteArrayAndResult());
×
150
    }
×
151

152
    @ParameterizedTest
153
    @MethodSource("provideInstances")
154
    public void assertNotAfterThrowsNoSignature(SOP sop) {
155
        byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
1✔
156
        Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
1✔
157
        Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig
1✔
158

159
        assertThrows(SOPGPException.NoSignature.class, () -> assumeSupported(sop::inlineVerify)
1✔
160
                .notAfter(beforeSignature)
1✔
161
                .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
1✔
162
                .data(message)
1✔
163
                .toByteArrayAndResult());
×
164
    }
×
165

166
    @ParameterizedTest
167
    @MethodSource("provideInstances")
168
    public void inlineSignVerifyBob(SOP sop) throws IOException {
169
        byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
1✔
170

171
        byte[] inlineSigned = assumeSupported(sop::inlineSign)
1✔
172
                .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
1✔
173
                .data(message)
1✔
174
                .getBytes();
×
175

176
        JUtils.assertArrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE);
×
177

178
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
×
179
                .cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
×
180
                .data(inlineSigned)
×
181
                .toByteArrayAndResult();
×
182

183
        assertArrayEquals(message, bytesAndResult.getBytes());
×
184

185
        List<Verification> verificationList = bytesAndResult.getResult();
×
186
        VerificationListAssert.assertThatVerificationList(verificationList)
×
187
                .isNotEmpty()
×
188
                .hasSingleItem()
×
189
                .issuedBy(TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
×
190
    }
×
191

192
    @ParameterizedTest
193
    @MethodSource("provideInstances")
194
    public void inlineSignVerifyCarol(SOP sop) throws IOException {
195
        byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
1✔
196

197
        byte[] inlineSigned = assumeSupported(sop::inlineSign)
1✔
198
                .key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
1✔
199
                .data(message)
1✔
200
                .getBytes();
×
201

202
        JUtils.assertArrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE);
×
203

204
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
×
205
                .cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
×
206
                .data(inlineSigned)
×
207
                .toByteArrayAndResult();
×
208

209
        assertArrayEquals(message, bytesAndResult.getBytes());
×
210

211
        List<Verification> verificationList = bytesAndResult.getResult();
×
212
        VerificationListAssert.assertThatVerificationList(verificationList)
×
213
                .isNotEmpty()
×
214
                .hasSingleItem()
×
215
                .issuedBy(TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
×
216
    }
×
217

218
    @ParameterizedTest
219
    @MethodSource("provideInstances")
220
    public void inlineSignVerifyProtectedKey(SOP sop) throws IOException {
221
        byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
1✔
222

223
        byte[] inlineSigned = assumeSupported(sop::inlineSign)
1✔
224
                .withKeyPassword(TestData.PASSWORD)
1✔
225
                .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
1✔
226
                .mode(InlineSignAs.binary)
1✔
227
                .data(message)
1✔
228
                .getBytes();
×
229

230
        ByteArrayAndResult<List<Verification>> bytesAndResult = assumeSupported(sop::inlineVerify)
×
231
                .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
×
232
                .data(inlineSigned)
×
233
                .toByteArrayAndResult();
×
234

235
        List<Verification> verificationList = bytesAndResult.getResult();
×
236
        VerificationListAssert.assertThatVerificationList(verificationList)
×
237
                .hasSingleItem()
×
238
                .issuedBy(TestData.PASSWORD_PROTECTED_SIGNING_FINGERPRINT, TestData.PASSWORD_PROTECTED_PRIMARY_FINGERPRINT);
×
239
    }
×
240

241
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc