mirror of
https://codeberg.org/PGPainless/wkd-java.git
synced 2024-11-21 14:52:12 +01:00
Add more tests
This commit is contained in:
parent
d7cddf26bb
commit
14030c103c
2 changed files with 63 additions and 2 deletions
|
@ -32,6 +32,15 @@ public final class WKDPolicy {
|
|||
this.submissionAddress = submissionAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a {@link WKDPolicy} object by reading from the given {@link InputStream}.
|
||||
* The stream will be closed by this method.
|
||||
*
|
||||
* @param inputStream InputStream
|
||||
* @return parsed WKDPolicy object
|
||||
*
|
||||
* @throws IOException in case of an error
|
||||
*/
|
||||
public static WKDPolicy fromInputStream(InputStream inputStream) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
|
||||
|
@ -57,7 +66,11 @@ public final class WKDPolicy {
|
|||
continue;
|
||||
}
|
||||
if (prepared.startsWith(KEYWORD_PROTOCOL_VERSION + ": ")) {
|
||||
protocolVersion = Integer.parseInt(prepared.substring(KEYWORD_PROTOCOL_VERSION.length() + 2));
|
||||
try {
|
||||
protocolVersion = Integer.parseInt(prepared.substring(KEYWORD_PROTOCOL_VERSION.length() + 2));
|
||||
} catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (prepared.startsWith(KEYWORD_SUBMISSION_ADDRESS + ": ")) {
|
||||
|
@ -65,6 +78,8 @@ public final class WKDPolicy {
|
|||
}
|
||||
}
|
||||
|
||||
inputStream.close();
|
||||
|
||||
return new WKDPolicy(mailboxOnly, daneOnly, authSubmit, protocolVersion, submissionAddress);
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,23 @@ public class WKDPolicyTest {
|
|||
assertNull(policy.getSubmissionAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWhitespaceOnlyPolicy() throws IOException {
|
||||
ByteArrayInputStream whitespaceOnly = new ByteArrayInputStream(
|
||||
("\n" +
|
||||
"\r\n" +
|
||||
" \n" +
|
||||
"\t\n" +
|
||||
"\n").getBytes(StandardCharsets.UTF_8));
|
||||
WKDPolicy policy = WKDPolicy.fromInputStream(whitespaceOnly);
|
||||
|
||||
assertFalse(policy.isMailboxOnly());
|
||||
assertFalse(policy.isDaneOnly());
|
||||
assertFalse(policy.isAuthSubmit());
|
||||
assertNull(policy.getProtocolVersion());
|
||||
assertNull(policy.getSubmissionAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseSparsePolicy() throws IOException {
|
||||
ByteArrayInputStream sparse = new ByteArrayInputStream(
|
||||
|
@ -51,7 +68,7 @@ public class WKDPolicyTest {
|
|||
"auth-submit\n" +
|
||||
"protocol-version: 12\n" +
|
||||
"submission-address: key-submission-example.org@directory.example.org")
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
WKDPolicy policy = WKDPolicy.fromInputStream(full);
|
||||
|
||||
assertTrue(policy.isMailboxOnly());
|
||||
|
@ -61,4 +78,33 @@ public class WKDPolicyTest {
|
|||
assertEquals(12, policy.getProtocolVersion());
|
||||
assertEquals("key-submission-example.org@directory.example.org", policy.getSubmissionAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedInput_MissingSpaces() throws IOException {
|
||||
ByteArrayInputStream malformed = new ByteArrayInputStream(
|
||||
("submission-address submit-keys-here@directory.example.org\n" +
|
||||
"protocol-version:12").getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
WKDPolicy policy = WKDPolicy.fromInputStream(malformed);
|
||||
assertNull(policy.getProtocolVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedInput_missingColon() throws IOException {
|
||||
ByteArrayInputStream malformed = new ByteArrayInputStream(
|
||||
("submission-address submit-keys-here@directory.example.org\n" +
|
||||
"protocol-version 13").getBytes(StandardCharsets.UTF_8));
|
||||
WKDPolicy policy = WKDPolicy.fromInputStream(malformed);
|
||||
assertNull(policy.getSubmissionAddress());
|
||||
assertNull(policy.getProtocolVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedInput_NotANumber() throws IOException {
|
||||
ByteArrayInputStream malformed = new ByteArrayInputStream(
|
||||
"protocol-version: ABC".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
WKDPolicy policy = WKDPolicy.fromInputStream(malformed);
|
||||
assertNull(policy.getProtocolVersion());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue