public void TestArgumentExceptions() { using (var client = new SmtpClient()) { var credentials = new NetworkCredential("username", "password"); var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); var message = CreateSimpleMessage(); var sender = message.From.Mailboxes.FirstOrDefault(); var recipients = message.To.Mailboxes.ToList(); var options = FormatOptions.Default; var empty = new MailboxAddress[0]; // Connect Assert.Throws <ArgumentNullException> (() => client.Connect((Uri)null)); Assert.Throws <ArgumentNullException> (async() => await client.ConnectAsync((Uri)null)); Assert.Throws <ArgumentNullException> (() => client.Connect(null, 25, false)); Assert.Throws <ArgumentNullException> (async() => await client.ConnectAsync(null, 25, false)); Assert.Throws <ArgumentException> (() => client.Connect(string.Empty, 25, false)); Assert.Throws <ArgumentException> (async() => await client.ConnectAsync(string.Empty, 25, false)); Assert.Throws <ArgumentOutOfRangeException> (() => client.Connect("host", -1, false)); Assert.Throws <ArgumentOutOfRangeException> (async() => await client.ConnectAsync("host", -1, false)); Assert.Throws <ArgumentNullException> (() => client.Connect(null, 25, SecureSocketOptions.None)); Assert.Throws <ArgumentNullException> (async() => await client.ConnectAsync(null, 25, SecureSocketOptions.None)); Assert.Throws <ArgumentException> (() => client.Connect(string.Empty, 25, SecureSocketOptions.None)); Assert.Throws <ArgumentException> (async() => await client.ConnectAsync(string.Empty, 25, SecureSocketOptions.None)); Assert.Throws <ArgumentOutOfRangeException> (() => client.Connect("host", -1, SecureSocketOptions.None)); Assert.Throws <ArgumentOutOfRangeException> (async() => await client.ConnectAsync("host", -1, SecureSocketOptions.None)); Assert.Throws <ArgumentNullException> (() => client.Connect(null, "host", 25, SecureSocketOptions.None)); Assert.Throws <ArgumentException> (() => client.Connect(socket, "host", 25, SecureSocketOptions.None)); // Authenticate Assert.Throws <ArgumentNullException> (() => client.Authenticate(null)); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(null)); Assert.Throws <ArgumentNullException> (() => client.Authenticate(null, "password")); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(null, "password")); Assert.Throws <ArgumentNullException> (() => client.Authenticate("username", null)); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync("username", null)); Assert.Throws <ArgumentNullException> (() => client.Authenticate(null, credentials)); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(null, credentials)); Assert.Throws <ArgumentNullException> (() => client.Authenticate(Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(Encoding.UTF8, null)); Assert.Throws <ArgumentNullException> (() => client.Authenticate(null, "username", "password")); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(null, "username", "password")); Assert.Throws <ArgumentNullException> (() => client.Authenticate(Encoding.UTF8, null, "password")); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(Encoding.UTF8, null, "password")); Assert.Throws <ArgumentNullException> (() => client.Authenticate(Encoding.UTF8, "username", null)); Assert.Throws <ArgumentNullException> (async() => await client.AuthenticateAsync(Encoding.UTF8, "username", null)); // Send Assert.Throws <ArgumentNullException> (() => client.Send(null)); Assert.Throws <ArgumentNullException> (() => client.Send(null, message)); Assert.Throws <ArgumentNullException> (() => client.Send(options, null)); Assert.Throws <ArgumentNullException> (() => client.Send(message, null, recipients)); Assert.Throws <ArgumentNullException> (() => client.Send(message, sender, null)); Assert.Throws <InvalidOperationException> (() => client.Send(message, sender, empty)); Assert.Throws <ArgumentNullException> (() => client.Send(null, message, sender, recipients)); Assert.Throws <ArgumentNullException> (() => client.Send(options, message, null, recipients)); Assert.Throws <ArgumentNullException> (() => client.Send(options, message, sender, null)); Assert.Throws <InvalidOperationException> (() => client.Send(options, message, sender, empty)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(null)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(null, message)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(options, null)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(message, null, recipients)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(message, sender, null)); Assert.Throws <InvalidOperationException> (async() => await client.SendAsync(message, sender, empty)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(null, message, sender, recipients)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(options, message, null, recipients)); Assert.Throws <ArgumentNullException> (async() => await client.SendAsync(options, message, sender, null)); Assert.Throws <InvalidOperationException> (async() => await client.SendAsync(options, message, sender, empty)); // Expand Assert.Throws <ArgumentNullException> (() => client.Expand(null)); Assert.Throws <ArgumentException> (() => client.Expand(string.Empty)); Assert.Throws <ArgumentException> (() => client.Expand("line1\r\nline2")); Assert.Throws <ArgumentNullException> (async() => await client.ExpandAsync(null)); Assert.Throws <ArgumentException> (async() => await client.ExpandAsync(string.Empty)); Assert.Throws <ArgumentException> (async() => await client.ExpandAsync("line1\r\nline2")); // Verify Assert.Throws <ArgumentNullException> (() => client.Verify(null)); Assert.Throws <ArgumentException> (() => client.Verify(string.Empty)); Assert.Throws <ArgumentException> (() => client.Verify("line1\r\nline2")); Assert.Throws <ArgumentNullException> (async() => await client.VerifyAsync(null)); Assert.Throws <ArgumentException> (async() => await client.VerifyAsync(string.Empty)); Assert.Throws <ArgumentException> (async() => await client.VerifyAsync("line1\r\nline2")); } }
public async void TestBasicFunctionality() { var commands = new List <SmtpReplayCommand> (); commands.Add(new SmtpReplayCommand("", "comcast-greeting.txt")); commands.Add(new SmtpReplayCommand("EHLO unit-tests.mimekit.org\r\n", "comcast-ehlo.txt")); commands.Add(new SmtpReplayCommand("AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n", "comcast-auth-plain.txt")); commands.Add(new SmtpReplayCommand("EHLO unit-tests.mimekit.org\r\n", "comcast-ehlo.txt")); commands.Add(new SmtpReplayCommand("VRFY Smith\r\n", "rfc0821-vrfy.txt")); commands.Add(new SmtpReplayCommand("EXPN Example-People\r\n", "rfc0821-expn.txt")); commands.Add(new SmtpReplayCommand("NOOP\r\n", "comcast-noop.txt")); commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt")); commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt")); commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt")); commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt")); commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt")); commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt")); commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt")); commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt")); commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt")); commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt")); commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt")); commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt")); commands.Add(new SmtpReplayCommand("MAIL FROM:<*****@*****.**>\r\n", "comcast-mail-from.txt")); commands.Add(new SmtpReplayCommand("RCPT TO:<*****@*****.**>\r\n", "comcast-rcpt-to.txt")); commands.Add(new SmtpReplayCommand("DATA\r\n", "comcast-data.txt")); commands.Add(new SmtpReplayCommand(".\r\n", "comcast-data-done.txt")); commands.Add(new SmtpReplayCommand("QUIT\r\n", "comcast-quit.txt")); using (var client = new SmtpClient()) { client.LocalDomain = "unit-tests.mimekit.org"; try { client.ReplayConnect("localhost", new SmtpReplayStream(commands)); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Connect: {0}", ex); } Assert.IsTrue(client.IsConnected, "Client failed to connect."); Assert.IsFalse(client.IsSecure, "IsSecure should be false."); Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Authentication), "Failed to detect AUTH extension"); Assert.IsTrue(client.AuthenticationMechanisms.Contains("LOGIN"), "Failed to detect the LOGIN auth mechanism"); Assert.IsTrue(client.AuthenticationMechanisms.Contains("PLAIN"), "Failed to detect the PLAIN auth mechanism"); Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EightBitMime), "Failed to detect 8BITMIME extension"); Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.EnhancedStatusCodes), "Failed to detect ENHANCEDSTATUSCODES extension"); Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.Size), "Failed to detect SIZE extension"); Assert.AreEqual(36700160, client.MaxSize, "Failed to parse SIZE correctly"); Assert.IsTrue(client.Capabilities.HasFlag(SmtpCapabilities.StartTLS), "Failed to detect STARTTLS extension"); Assert.Throws <ArgumentException> (() => client.Capabilities |= SmtpCapabilities.UTF8); Assert.AreEqual(100000, client.Timeout, "Timeout"); client.Timeout *= 2; try { await client.AuthenticateAsync("username", "password"); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Authenticate: {0}", ex); } try { await client.VerifyAsync("Smith"); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Verify: {0}", ex); } try { await client.ExpandAsync("Example-People"); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Expand: {0}", ex); } try { await client.NoOpAsync(); } catch (Exception ex) { Assert.Fail("Did not expect an exception in NoOp: {0}", ex); } var message = CreateSimpleMessage(); var options = FormatOptions.Default; try { await client.SendAsync(message); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Send: {0}", ex); } try { await client.SendAsync(message, message.From.Mailboxes.FirstOrDefault(), message.To.Mailboxes); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Send: {0}", ex); } try { await client.SendAsync(options, message); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Send: {0}", ex); } try { await client.SendAsync(options, message, message.From.Mailboxes.FirstOrDefault(), message.To.Mailboxes); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Send: {0}", ex); } try { await client.DisconnectAsync(true); } catch (Exception ex) { Assert.Fail("Did not expect an exception in Disconnect: {0}", ex); } Assert.IsFalse(client.IsConnected, "Failed to disconnect"); } }