Add more tests

This commit is contained in:
Paul Schaub 2022-03-21 16:15:02 +01:00
parent d7cddf26bb
commit 14030c103c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 63 additions and 2 deletions

View file

@ -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 + ": ")) {
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);
}

View file

@ -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(
@ -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());
}
}