From 3de2abf028a06072e9c5254523e995cc4f18abda Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Sat, 30 Dec 2023 10:39:48 -0500 Subject: [PATCH] Added more test coverage to existing IMAP unit tests --- UnitTests/Net/Imap/ImapClientTests.cs | 54 +++++++++---- UnitTests/Net/Imap/ImapFolderFlagsTests.cs | 93 +++++++++++++++++++++- UnitTests/Net/Imap/ImapUtilsTests.cs | 2 + 3 files changed, 130 insertions(+), 19 deletions(-) diff --git a/UnitTests/Net/Imap/ImapClientTests.cs b/UnitTests/Net/Imap/ImapClientTests.cs index a66d33b013..d1f300fe62 100644 --- a/UnitTests/Net/Imap/ImapClientTests.cs +++ b/UnitTests/Net/Imap/ImapClientTests.cs @@ -5496,22 +5496,28 @@ static List 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; } @@ -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); @@ -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); diff --git a/UnitTests/Net/Imap/ImapFolderFlagsTests.cs b/UnitTests/Net/Imap/ImapFolderFlagsTests.cs index 37d4c5ee29..f7ec7fbc12 100644 --- a/UnitTests/Net/Imap/ImapFolderFlagsTests.cs +++ b/UnitTests/Net/Imap/ImapFolderFlagsTests.cs @@ -430,10 +430,9 @@ public void TestNotSupportedExceptions () } } - [Test] - public void TestChangingFlagsOnEmptyListOfMessages () + static IList CreateChangingFlagsOnEmptyListOfMessagesCommands () { - var commands = new List { + return new List { 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"), @@ -441,6 +440,12 @@ public void TestChangingFlagsOnEmptyListOfMessages () 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"); @@ -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 (client.Inbox.SyncRoot, "SyncRoot"); + + var inbox = (ImapFolder) client.Inbox; + await inbox.OpenAsync (FolderAccess.ReadWrite); + + ulong modseq = 409601020304; + var uids = Array.Empty (); + var indexes = Array.Empty (); + IList unmodifiedUids; + IList 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); + } + } } } diff --git a/UnitTests/Net/Imap/ImapUtilsTests.cs b/UnitTests/Net/Imap/ImapUtilsTests.cs index e008109cb1..57e7404421 100644 --- a/UnitTests/Net/Imap/ImapUtilsTests.cs +++ b/UnitTests/Net/Imap/ImapUtilsTests.cs @@ -59,6 +59,8 @@ public void TestFormattingSimpleIndexRange () Assert.Throws (() => ImapUtils.FormatIndexSet (engine, null)); Assert.Throws (() => ImapUtils.FormatIndexSet (engine, new int[0])); + Assert.Throws (() => 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."); }