Skip to content

Commit

Permalink
Added more test coverage to existing IMAP unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Dec 30, 2023
1 parent 38f9995 commit 3de2abf
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 19 deletions.
54 changes: 38 additions & 16 deletions UnitTests/Net/Imap/ImapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5496,22 +5496,28 @@ static List<ImapReplayCommand> CreateGMailCommands ()
commands.Add (new ImapReplayCommand ("A00000082 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.set-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000083 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 -X-GM-LABELS.SILENT (\\Important \"Custom Label\" NIL)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000084 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 +X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.add-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000085 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.set-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000086 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) -X-GM-LABELS.SILENT (\\Important \"Custom Label\" NIL)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000087 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) +X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.add-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000088 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 FLAGS (\\Answered \\Seen)\r\n", "gmail.set-flags.txt"));
commands.Add (new ImapReplayCommand ("A00000089 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 -FLAGS.SILENT (\\Answered)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000090 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 +FLAGS.SILENT (\\Deleted)\r\n", "gmail.add-flags.txt"));
commands.Add (new ImapReplayCommand ("A00000091 CHECK\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000092 UNSELECT\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000093 SUBSCRIBE UnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000094 LSUB \"\" \"%\"\r\n", "gmail.lsub-personal.txt"));
commands.Add (new ImapReplayCommand ("A00000095 UNSUBSCRIBE UnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000096 CREATE UnitTests/Dummy\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000097 LIST \"\" UnitTests/Dummy\r\n", "gmail.list-unittests-dummy.txt"));
commands.Add (new ImapReplayCommand ("A00000098 RENAME UnitTests RenamedUnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000099 DELETE RenamedUnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000100 LOGOUT\r\n", "gmail.logout.txt"));
commands.Add (new ImapReplayCommand ("A00000085 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.set-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000086 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) -X-GM-LABELS.SILENT (\\Important \"Custom Label\" NIL)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000087 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) +X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.add-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000088 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.set-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000089 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 -X-GM-LABELS.SILENT (\\Important \"Custom Label\" NIL)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000090 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 +X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.add-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000091 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.set-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000092 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) -X-GM-LABELS.SILENT (\\Important \"Custom Label\" NIL)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000093 STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 (UNCHANGEDSINCE 5) +X-GM-LABELS (\\Important \"Custom Label\" NIL)\r\n", "gmail.add-labels.txt"));
commands.Add (new ImapReplayCommand ("A00000094 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 FLAGS (\\Answered \\Seen)\r\n", "gmail.set-flags.txt"));
commands.Add (new ImapReplayCommand ("A00000095 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 -FLAGS.SILENT (\\Answered)\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000096 UID STORE 1:3,5,7:9,11:14,26:29,31,34,41:43,50 +FLAGS.SILENT (\\Deleted)\r\n", "gmail.add-flags.txt"));
commands.Add (new ImapReplayCommand ("A00000097 CHECK\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000098 UNSELECT\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000099 SUBSCRIBE UnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000100 LSUB \"\" \"%\"\r\n", "gmail.lsub-personal.txt"));
commands.Add (new ImapReplayCommand ("A00000101 UNSUBSCRIBE UnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000102 CREATE UnitTests/Dummy\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000103 LIST \"\" UnitTests/Dummy\r\n", "gmail.list-unittests-dummy.txt"));
commands.Add (new ImapReplayCommand ("A00000104 RENAME UnitTests RenamedUnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000105 DELETE RenamedUnitTests\r\n", ImapReplayCommandResponse.OK));
commands.Add (new ImapReplayCommand ("A00000106 LOGOUT\r\n", "gmail.logout.txt"));

return commands;
}
Expand Down Expand Up @@ -5631,6 +5637,14 @@ public void TestGMail ()
created.RemoveLabels (matches, labels, true);
created.AddLabels (matches, labels, false);

created.SetLabels (matches, 5, labels, false);
created.RemoveLabels (matches, 5, labels, true);
created.AddLabels (matches, 5, labels, false);

created.SetLabels (indexes, labels, false);
created.RemoveLabels (indexes, labels, true);
created.AddLabels (indexes, labels, false);

created.SetLabels (indexes, 5, labels, false);
created.RemoveLabels (indexes, 5, labels, true);
created.AddLabels (indexes, 5, labels, false);
Expand Down Expand Up @@ -5794,6 +5808,14 @@ public async Task TestGMailAsync ()
await created.RemoveLabelsAsync (matches, labels, true);
await created.AddLabelsAsync (matches, labels, false);

await created.SetLabelsAsync (matches, 5, labels, false);
await created.RemoveLabelsAsync (matches, 5, labels, true);
await created.AddLabelsAsync (matches, 5, labels, false);

await created.SetLabelsAsync (indexes, labels, false);
await created.RemoveLabelsAsync (indexes, labels, true);
await created.AddLabelsAsync (indexes, labels, false);

await created.SetLabelsAsync (indexes, 5, labels, false);
await created.RemoveLabelsAsync (indexes, 5, labels, true);
await created.AddLabelsAsync (indexes, 5, labels, false);
Expand Down
93 changes: 90 additions & 3 deletions UnitTests/Net/Imap/ImapFolderFlagsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,22 @@ public void TestNotSupportedExceptions ()
}
}

[Test]
public void TestChangingFlagsOnEmptyListOfMessages ()
static IList<ImapReplayCommand> CreateChangingFlagsOnEmptyListOfMessagesCommands ()
{
var commands = new List<ImapReplayCommand> {
return new List<ImapReplayCommand> {
new ImapReplayCommand ("", "dovecot.greeting.txt"),
new ImapReplayCommand ("A00000000 LOGIN username password\r\n", "dovecot.authenticate+gmail-capabilities.txt"),
new ImapReplayCommand ("A00000001 NAMESPACE\r\n", "dovecot.namespace.txt"),
new ImapReplayCommand ("A00000002 LIST \"\" \"INBOX\" RETURN (SUBSCRIBED CHILDREN)\r\n", "dovecot.list-inbox.txt"),
new ImapReplayCommand ("A00000003 LIST (SPECIAL-USE) \"\" \"*\" RETURN (SUBSCRIBED CHILDREN)\r\n", "dovecot.list-special-use.txt"),
new ImapReplayCommand ("A00000004 SELECT INBOX (CONDSTORE)\r\n", "common.select-inbox.txt")
};
}

[Test]
public void TestChangingFlagsOnEmptyListOfMessages ()
{
var commands = CreateChangingFlagsOnEmptyListOfMessagesCommands ();

using (var client = new ImapClient () { TagPrefix = 'A' }) {
var credentials = new NetworkCredential ("username", "password");
Expand Down Expand Up @@ -518,5 +523,87 @@ public void TestChangingFlagsOnEmptyListOfMessages ()
client.Disconnect (false);
}
}

[Test]
public async Task TestChangingFlagsOnEmptyListOfMessagesAsync ()
{
var commands = CreateChangingFlagsOnEmptyListOfMessagesCommands ();

using (var client = new ImapClient () { TagPrefix = 'A' }) {
var credentials = new NetworkCredential ("username", "password");

try {
await client.ConnectAsync (new ImapReplayStream (commands, true), "localhost", 143, SecureSocketOptions.None);
} catch (Exception ex) {
Assert.Fail ($"Did not expect an exception in Connect: {ex}");
}

// Note: we do not want to use SASL at all...
client.AuthenticationMechanisms.Clear ();

try {
await client.AuthenticateAsync (credentials);
} catch (Exception ex) {
Assert.Fail ($"Did not expect an exception in Authenticate: {ex}");
}

Assert.IsInstanceOf<ImapEngine> (client.Inbox.SyncRoot, "SyncRoot");

var inbox = (ImapFolder) client.Inbox;
await inbox.OpenAsync (FolderAccess.ReadWrite);

ulong modseq = 409601020304;
var uids = Array.Empty<UniqueId> ();
var indexes = Array.Empty<int> ();
IList<UniqueId> unmodifiedUids;
IList<int> unmodifiedIndexes;

// AddFlags
unmodifiedIndexes = await inbox.AddFlagsAsync (indexes, modseq, MessageFlags.Seen, true);
Assert.That (unmodifiedIndexes.Count, Is.EqualTo (0));

unmodifiedUids = await inbox.AddFlagsAsync (uids, modseq, MessageFlags.Seen, true);
Assert.That (unmodifiedUids.Count, Is.EqualTo (0));

// RemoveFlags
unmodifiedIndexes = await inbox.RemoveFlagsAsync (indexes, modseq, MessageFlags.Seen, true);
Assert.That (unmodifiedIndexes.Count, Is.EqualTo (0));

unmodifiedUids = await inbox.RemoveFlagsAsync (uids, modseq, MessageFlags.Seen, true);
Assert.That (unmodifiedUids.Count, Is.EqualTo (0));

// SetFlags
unmodifiedIndexes = await inbox.SetFlagsAsync (indexes, modseq, MessageFlags.Seen, true);
Assert.That (unmodifiedIndexes.Count, Is.EqualTo (0));

unmodifiedUids = await inbox.SetFlagsAsync (uids, modseq, MessageFlags.Seen, true);
Assert.That (unmodifiedUids.Count, Is.EqualTo (0));

var labels = new string[] { "Label1", "Label2" };

// AddLabels
unmodifiedIndexes = await inbox.AddLabelsAsync (indexes, modseq, labels, true);
Assert.That (unmodifiedIndexes.Count, Is.EqualTo (0));

unmodifiedUids = await inbox.AddLabelsAsync (uids, modseq, labels, true);
Assert.That (unmodifiedUids.Count, Is.EqualTo (0));

// RemoveLabels
unmodifiedIndexes = await inbox.RemoveLabelsAsync (indexes, modseq, labels, true);
Assert.That (unmodifiedIndexes.Count, Is.EqualTo (0));

unmodifiedUids = await inbox.RemoveLabelsAsync (uids, modseq, labels, true);
Assert.That (unmodifiedUids.Count, Is.EqualTo (0));

// SetLabels
unmodifiedIndexes = await inbox.SetLabelsAsync (indexes, modseq, labels, true);
Assert.That (unmodifiedIndexes.Count, Is.EqualTo (0));

unmodifiedUids = await inbox.SetLabelsAsync (uids, modseq, labels, true);
Assert.That (unmodifiedUids.Count, Is.EqualTo (0));

await client.DisconnectAsync (false);
}
}
}
}
2 changes: 2 additions & 0 deletions UnitTests/Net/Imap/ImapUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public void TestFormattingSimpleIndexRange ()
Assert.Throws<ArgumentNullException> (() => ImapUtils.FormatIndexSet (engine, null));
Assert.Throws<ArgumentException> (() => ImapUtils.FormatIndexSet (engine, new int[0]));

Assert.Throws<ArgumentNullException> (() => ImapUtils.FormatIndexSet (engine, null, new int[1]));

actual = ImapUtils.FormatIndexSet (engine, indexes);
Assert.That (actual, Is.EqualTo (expect), "Formatting a simple range of indexes failed.");
}
Expand Down

0 comments on commit 3de2abf

Please sign in to comment.