Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4e7f0813 authored by cketti's avatar cketti
Browse files

Fix extracting alert text from negative IMAP responses

parent 8b99095a
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap;


import static com.fsck.k9.mail.store.imap.ImapResponseParser.equalsIgnoreCase;


class AlertResponse {
    private static final String ALERT_RESPONSE_CODE = "ALERT";


    private AlertResponse() {
    }

    public static String getAlertText(ImapResponse response) {
        if (response.size() < 3 || !response.isList(1)) {
            return null;
        }

        ImapList responseTextCode = response.getList(1);
        if (responseTextCode.size() != 1 || !equalsIgnoreCase(responseTextCode.get(0), ALERT_RESPONSE_CODE)) {
            return null;
        }

        return response.getString(2);
    }
}
+0 −13
Original line number Diff line number Diff line
@@ -55,19 +55,6 @@ class ImapResponse extends ImapList {
        this.callback = callback;
    }

    public String getAlertText() {
        if (size() > 1 && ImapResponseParser.equalsIgnoreCase(get(1), "[ALERT]")) {
            StringBuilder sb = new StringBuilder();
            for (int i = 2, count = size(); i < count; i++) {
                sb.append(get(i).toString());
                sb.append(' ');
            }
            return sb.toString();
        } else {
            return null;
        }
    }

    @Override
    public String toString() {
        return "#" + (commandContinuationRequested ? "+" : tag) + "# " + super.toString();
+3 −2
Original line number Diff line number Diff line
@@ -117,8 +117,9 @@ class ImapResponseParser {
        } while (response == null || response.getTag() == null);

        if (response.size() < 1 || !equalsIgnoreCase(response.get(0), Responses.OK)) {
            throw new NegativeImapResponseException("Command: " + commandToLog + "; response: " + response.toString(),
                    response.getAlertText());
            String message = "Command: " + commandToLog + "; response: " + response.toString();
            String alertText = AlertResponse.getAlertText(response);
            throw new NegativeImapResponseException(message, alertText);
        }

        return responses;
+4 −0
Original line number Diff line number Diff line
@@ -13,4 +13,8 @@ class NegativeImapResponseException extends MessagingException {
        super(message, true);
        this.alertText = alertText;
    }

    public String getAlertText() {
        return alertText;
    }
}
+66 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.imap;


import org.junit.Test;

import static com.fsck.k9.mail.store.imap.ImapResponseHelper.createImapResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;


public class AlertResponseTest {

    @Test
    public void getAlertText_withProperAlertResponse() throws Exception {
        ImapResponse imapResponse = createImapResponse("x NO [ALERT] Please don't do that");

        String result = AlertResponse.getAlertText(imapResponse);

        assertEquals("Please don't do that", result);
    }

    @Test
    public void getAlertText_withoutResponseCodeText_shouldReturnNull() throws Exception {
        ImapResponse imapResponse = createImapResponse("x NO");

        String result = AlertResponse.getAlertText(imapResponse);

        assertNull(result);
    }

    @Test
    public void getAlertText_withoutAlertText_shouldReturnNull() throws Exception {
        ImapResponse imapResponse = createImapResponse("x NO [ALERT]");

        String result = AlertResponse.getAlertText(imapResponse);

        assertNull(result);
    }

    @Test
    public void getAlertText_withoutResponseCodeTextList_shouldReturnNull() throws Exception {
        ImapResponse imapResponse = createImapResponse("x NO ALERT ALARM!");

        String result = AlertResponse.getAlertText(imapResponse);

        assertNull(result);
    }

    @Test
    public void getAlertText_withResponseCodeTextContainingTooManyItems_shouldReturnNull() throws Exception {
        ImapResponse imapResponse = createImapResponse("x NO [ALERT SOMETHING] ALARM!");

        String result = AlertResponse.getAlertText(imapResponse);

        assertNull(result);
    }

    @Test
    public void getAlertText_withWrongResponseCodeText_shouldReturnNull() throws Exception {
        ImapResponse imapResponse = createImapResponse("x NO [ALARM] ALERT!");

        String result = AlertResponse.getAlertText(imapResponse);

        assertNull(result);
    }
}
Loading