From fee88a4b88e1e4e9af97bd263f28811235d16c9d Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 6 Aug 2023 18:02:30 +0200 Subject: [PATCH] Write empty string to identity header field if name is null --- .../k9/message/IdentityHeaderBuilder.java | 4 ++- .../k9/message/IdentityHeaderBuilderTest.kt | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/core/src/main/java/com/fsck/k9/message/IdentityHeaderBuilder.java b/app/core/src/main/java/com/fsck/k9/message/IdentityHeaderBuilder.java index cb6c2c234..33e957c01 100644 --- a/app/core/src/main/java/com/fsck/k9/message/IdentityHeaderBuilder.java +++ b/app/core/src/main/java/com/fsck/k9/message/IdentityHeaderBuilder.java @@ -1,6 +1,8 @@ package com.fsck.k9.message; +import java.util.Objects; + import android.net.Uri; import android.net.Uri.Builder; @@ -87,7 +89,7 @@ public class IdentityHeaderBuilder { } if (identityChanged) { - appendValue(IdentityField.NAME, identity.getName()); + appendValue(IdentityField.NAME, Objects.requireNonNullElse(identity.getName(), "")); appendValue(IdentityField.EMAIL, identity.getEmail()); } diff --git a/app/core/src/test/java/com/fsck/k9/message/IdentityHeaderBuilderTest.kt b/app/core/src/test/java/com/fsck/k9/message/IdentityHeaderBuilderTest.kt index 9334d26e0..80aeb2c2a 100644 --- a/app/core/src/test/java/com/fsck/k9/message/IdentityHeaderBuilderTest.kt +++ b/app/core/src/test/java/com/fsck/k9/message/IdentityHeaderBuilderTest.kt @@ -1,7 +1,10 @@ package com.fsck.k9.message +import android.net.Uri import app.k9mail.core.android.testing.RobolectricTest +import assertk.Assert import assertk.assertThat +import assertk.assertions.contains import assertk.assertions.isGreaterThan import com.fsck.k9.Account.QuoteStyle import com.fsck.k9.Identity @@ -35,6 +38,21 @@ class IdentityHeaderBuilderTest : RobolectricTest() { assertIsValidHeader(identityHeader) } + @Test + fun `identity header without identity name`() { + val identityHeader = IdentityHeaderBuilder() + .setIdentity(createIdentity(email = "test@domain.example", name = null)) + .setIdentityChanged(true) + .setBody(TextBody("irrelevant")) + .setQuoteStyle(QuoteStyle.PREFIX) + .setMessageFormat(SimpleMessageFormat.TEXT) + .setQuoteTextMode(QuotedTextMode.NONE) + .build() + + assertThat(identityHeader).containsParameter(IdentityField.EMAIL, "test@domain.example") + assertThat(identityHeader).containsParameter(IdentityField.NAME, "") + } + private fun assertIsValidHeader(identityHeader: String) { try { MimeHeaderChecker.checkHeader(IDENTITY_HEADER, identityHeader) @@ -55,3 +73,11 @@ class IdentityHeaderBuilderTest : RobolectricTest() { return Identity(description, name, email, signature, signatureUse, replyTo) } } + +private fun Assert.containsParameter(identityField: IdentityField, value: String) = given { actual -> + assertThat("&${unfold(actual)}&").contains("&${identityField.value()}=${Uri.encode(value)}&") +} + +private fun unfold(headerValue: String): String { + return headerValue.replace(Regex("\r?\n "), "") +}