|
9 | 9 | // (C) 2006 John Luke
|
10 | 10 | //
|
11 | 11 |
|
| 12 | +using System.Collections.Generic; |
| 13 | +using System.Globalization; |
12 | 14 | using System.IO;
|
13 | 15 | using System.Net.NetworkInformation;
|
14 | 16 | using System.Net.Sockets;
|
| 17 | +using System.Reflection; |
15 | 18 | using System.Threading;
|
16 | 19 | using System.Threading.Tasks;
|
| 20 | +using Microsoft.DotNet.RemoteExecutor; |
17 | 21 | using Systen.Net.Mail.Tests;
|
18 | 22 | using Xunit;
|
19 | 23 |
|
@@ -523,5 +527,60 @@ public async Task SendMail_SendQUITOnDispose(bool asyncSend)
|
523 | 527 | quitReceived.Wait(TimeSpan.FromSeconds(30));
|
524 | 528 | Assert.True(quitMessageReceived, "QUIT message not received");
|
525 | 529 | }
|
| 530 | + |
| 531 | + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] |
| 532 | + [InlineData("foo@[\r\n bar]")] |
| 533 | + [InlineData("foo@[bar\r\n ]")] |
| 534 | + [InlineData("foo@[bar\r\n baz]")] |
| 535 | + public void MultiLineDomainLiterals_Enabled_Success(string input) |
| 536 | + { |
| 537 | + RemoteExecutor.Invoke(static (string @input) => |
| 538 | + { |
| 539 | + AppContext.SetSwitch("System.Net.AllowFullDomainLiterals", true); |
| 540 | + |
| 541 | + var address = new MailAddress(@input); |
| 542 | + |
| 543 | + // Using address with new line breaks the protocol so we cannot easily use LoopbackSmtpServer |
| 544 | + // Instead we call internal method that does the extra validation. |
| 545 | + string? host = (string?)typeof(MailAddress).InvokeMember("GetAddress", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, address, new object[] { true }); |
| 546 | + Assert.Equal(input, host); |
| 547 | + }, input).Dispose(); |
| 548 | + } |
| 549 | + |
| 550 | + [Theory] |
| 551 | + [MemberData(nameof(SendMail_MultiLineDomainLiterals_Data))] |
| 552 | + public async Task SendMail_MultiLineDomainLiterals_Disabled_Throws(string from, string to, bool asyncSend) |
| 553 | + { |
| 554 | + using var server = new LoopbackSmtpServer(); |
| 555 | + |
| 556 | + using SmtpClient client = server.CreateClient(); |
| 557 | + client.Credentials = new NetworkCredential("Foo", "Bar"); |
| 558 | + |
| 559 | + using var msg = new MailMessage(@from, @to, "subject", "body"); |
| 560 | + |
| 561 | + await Assert.ThrowsAsync<SmtpException>(async () => |
| 562 | + { |
| 563 | + if (asyncSend) |
| 564 | + { |
| 565 | + await client.SendMailAsync(msg).WaitAsync(TimeSpan.FromSeconds(30)); |
| 566 | + } |
| 567 | + else |
| 568 | + { |
| 569 | + client.Send(msg); |
| 570 | + } |
| 571 | + }); |
| 572 | + } |
| 573 | + |
| 574 | + public static IEnumerable<object[]> SendMail_MultiLineDomainLiterals_Data() |
| 575 | + { |
| 576 | + foreach (bool async in new[] { true, false }) |
| 577 | + { |
| 578 | + foreach (string address in new[] { "foo@[\r\n bar]", "foo@[bar\r\n ]", "foo@[bar\r\n baz]" }) |
| 579 | + { |
| 580 | + yield return new object[] { address, "[email protected]", async }; |
| 581 | + yield return new object[] { "[email protected]", address, async }; |
| 582 | + } |
| 583 | + } |
| 584 | + } |
526 | 585 | }
|
527 | 586 | }
|
0 commit comments