-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Enable Send-MailMessage on CoreCLR #3869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| Describe "Basic Send-MailMessage tests" -Tags CI { | ||
| BeforeAll { | ||
| function test-smtpserver | ||
| { | ||
| $rv = $false | ||
|
|
||
| try | ||
| { | ||
| $tc = New-Object -TypeName System.Net.Sockets.TcpClient -ArgumentList "localhost", 25 | ||
| $rv = $tc.Connected | ||
| $tc.Close() | ||
| } | ||
| catch | ||
| { | ||
| $rv = false | ||
| } | ||
|
|
||
| return $rv | ||
| } | ||
|
|
||
| function read-mail | ||
| { | ||
| Param( | ||
| [parameter(Mandatory=$true)] | ||
| [String] | ||
| $mailBox | ||
| ) | ||
|
|
||
| $state = "init" | ||
| $mail = Get-Content $mailBox | ||
| $rv = @{} | ||
| foreach ($line in $mail) | ||
| { | ||
| switch ($state) | ||
| { | ||
| "init" | ||
| { | ||
| if ($line.Length -gt 0) | ||
| { | ||
| $state = "headers" | ||
| } | ||
| } | ||
| "headers" | ||
| { | ||
| if ($line.StartsWith("From: ")) | ||
| { | ||
| $rv.From = $line.Substring(6) | ||
| } | ||
| elseif ($line.StartsWith("To: ")) | ||
| { | ||
| if ($rv.To -eq $null) | ||
| { | ||
| $rv.To = @() | ||
| } | ||
|
|
||
| $rv.To += $line.Substring(4) | ||
| } | ||
| elseif ($line.StartsWith("Subject: ")) | ||
| { | ||
| $rv.Subject = $line.Substring(9); | ||
| } | ||
| elseif ($line.Length -eq 0) | ||
| { | ||
| $state = "body" | ||
| } | ||
| } | ||
| "body" | ||
| { | ||
| if ($line.Length -eq 0) | ||
| { | ||
| $state = "done" | ||
| continue | ||
| } | ||
|
|
||
| if ($rv.Body -eq $null) | ||
| { | ||
| $rv.Body = @() | ||
| } | ||
|
|
||
| $rv.Body += $line | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $rv | ||
| } | ||
|
|
||
| $PesterArgs = @{ Name = "Can send mail message from user to self"} | ||
| $alreadyHasMail = $true | ||
|
|
||
| if (-not $IsLinux) | ||
| { | ||
| $PesterArgs["Skip"] = $true | ||
| $PesterArgs["Name"] += " (skipped: not Linux)" | ||
| return | ||
| } | ||
|
|
||
| $domain = [Environment]::MachineName | ||
| if (-not (test-smtpserver)) | ||
| { | ||
| $PesterArgs["Pending"] = $true | ||
| $PesterArgs["Name"] += " (pending: no mail server detected)" | ||
| return | ||
| } | ||
|
|
||
| $user = [Environment]::UserName | ||
| $inPassword = Select-String "^${user}:" /etc/passwd -ErrorAction SilentlyContinue | ||
| if (-not $inPassword) | ||
| { | ||
| $PesterArgs["Pending"] = $true | ||
| $PesterArgs["Name"] += " (pending: user not in /etc/passwd)" | ||
| return | ||
| } | ||
|
|
||
| $address = "$user@$domain" | ||
| $mailStore = "/var/mail" | ||
| $mailBox = Join-Path $mailStore $user | ||
| $mailBoxFile = Get-Item $mailBox -ErrorAction SilentlyContinue | ||
| if ($mailBoxFile -ne $null -and $mailBoxFile.Length -gt 2) | ||
| { | ||
| $PesterArgs["Pending"] = $true | ||
| $PesterArgs["Name"] += " (pending: mailbox not empty)" | ||
| return | ||
| } | ||
| $alreadyHasMail = $false | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this, you set
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one of those steps to avoid deleting an existing mailbox. In the |
||
| } | ||
| AfterAll { | ||
| if (-not $alreadyHasMail) | ||
| { | ||
| Set-Content -Value "" -Path $mailBox -Force -ErrorAction SilentlyContinue | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this just be delete the file?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless we're running as root or as a member of the |
||
| } | ||
| } | ||
|
|
||
| It @PesterArgs { | ||
| $body = "Greetings from me." | ||
| $subject = "Test message" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't you also validate the subject?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| Send-MailMessage -To $address -From $address -Subject $subject -Body $body -SmtpServer 127.0.0.1 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one more check needed above to be sure that there's an smtp server running on localhost
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| Test-Path -Path $mailBox | Should Be $true | ||
| $mail = read-mail $mailBox | ||
| $mail.From | Should BeExactly $address | ||
| $mail.To.Count | Should BeExactly 1 | ||
| $mail.To[0] | Should BeExactly $address | ||
| $mail.Subject | Should BeExactly $subject | ||
| $mail.Body.Count | Should BeExactly 1 | ||
| $mail.Body[0] | Should BeExactly $body | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this test require Administrator on Windows or root on all non-Windows platforms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this test runs only on Linux. So long as the sender/receiver of the email is the currently logged-in user, running as root is not required.
I've been looking into ways to make tests more cross-platform, using a suggestion from @iSazonov, but it's going to take some effort.
My personal feeling is that I'd like to create a separate issue for testing
Send-MailMessageand have this PR be taken as-is, but I don't know if that works with testing policies.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeffbi create a separate issue for that and link it to this