Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<Compile Remove="commands\utility\FormatAndOutput\OutGridView\TableView.cs" />
<Compile Remove="commands\utility\FormatAndOutput\out-printer\PrinterLineOutput.cs" />
<Compile Remove="commands\utility\FormatAndOutput\out-printer\out-printer.cs" />
<Compile Remove="commands\utility\Send-MailMessage.cs" />
<Compile Remove="commands\utility\ShowCommand\ShowCommand.cs" />
<Compile Remove="commands\utility\ShowCommand\ShowCommandCommandInfo.cs" />
<Compile Remove="commands\utility\ShowCommand\ShowCommandModuleInfo.cs" />
Expand All @@ -60,17 +59,15 @@
<Compile Remove="gen\UtilityMshSnapinResources.cs" />
<Compile Remove="gen\OutPrinterDisplayStrings.cs" />
<Compile Remove="gen\UpdateListStrings.cs" />
<Compile Remove="gen\SendMailMessageStrings.cs" />
<Compile Remove="gen\ConvertFromStringResources.cs" />
<Compile Remove="gen\ConvertStringResources.cs" />
<Compile Remove="gen\FlashExtractStrings.cs" />
<Compile Remove="gen\ImmutableStrings.cs" />

<EmbeddedResource Remove="resources\FormatAndOut_out_gridview.resx" />
<EmbeddedResource Remove="resources\UtilityMshSnapinResources.resx" />
<EmbeddedResource Remove="resources\OutPrinterDisplayStrings.resx" />
<EmbeddedResource Remove="resources\UpdateListStrings.resx" />
<EmbeddedResource Remove="resources\SendMailMessageStrings.resx" />
<EmbeddedResource Remove="resources\ConvertFromStringResources.resx" />
<EmbeddedResource Remove="resources\ConvertStringResources.resx" />
<EmbeddedResource Remove="resources\FlashExtractStrings.resx" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Clear-Variable", "Export-Clixml", "Import-Clixml", "Import-PowerShellDataFile", "ConvertTo-Xml", "Select-Xml", "Write-Debug",
"Write-Verbose", "Write-Warning", "Write-Error", "Write-Information", "Write-Output", "Set-PSBreakpoint",
"Get-PSBreakpoint", "Remove-PSBreakpoint", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash",
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash",
"Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "New-TemporaryFile", "Get-Verb", "Format-Hex"
FunctionsToExport= "Import-PowerShellDataFile"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CmdletsToExport= "Format-List", "Format-Custom", "Format-Table", "Format-Wide",
"Clear-Variable", "Export-Clixml", "Import-Clixml", "Import-PowerShellDataFile","ConvertTo-Xml", "Select-Xml", "Write-Debug",
"Write-Verbose", "Write-Warning", "Write-Error", "Write-Information", "Write-Output", "Set-PSBreakpoint",
"Get-PSBreakpoint", "Remove-PSBreakpoint", "New-TemporaryFile", "Enable-PSBreakpoint", "Disable-PSBreakpoint", "Get-PSCallStack",
"Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash",
"Send-MailMessage", "Get-TraceSource", "Set-TraceSource", "Trace-Command", "Get-FileHash",
"Unblock-File", "Get-Runspace", "Debug-Runspace", "Enable-RunspaceDebug", "Disable-RunspaceDebug",
"Get-RunspaceDebug", "Wait-Debugger" , "Get-Uptime", "Get-Verb", "Format-Hex"
FunctionsToExport= "ConvertFrom-SddlString"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
Describe "Basic Send-MailMessage tests" -Tags CI {
Copy link
Member

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?

Copy link
Contributor Author

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-MailMessage and have this PR be taken as-is, but I don't know if that works with testing policies.

Copy link
Member

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this, you set $alreadyHasMail to $true above and then it's always set to $false here

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 block, the $alreadyHasMail variable is checked before deleting the mailbox. The early return at line 104 keeps that set to $true if the mailbox is not empty.

}
AfterAll {
if (-not $alreadyHasMail)
{
Set-Content -Value "" -Path $mailBox -Force -ErrorAction SilentlyContinue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this just be delete the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we're running as root or as a member of the mail group, we can write to the file but we cannot delete it.

}
}

It @PesterArgs {
$body = "Greetings from me."
$subject = "Test message"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't you also validate the subject?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
}
2 changes: 1 addition & 1 deletion test/powershell/engine/DefaultCommands.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Select-Object", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Select-String", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Select-Xml", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Send-MailMessage", , $($FullCLR )
"Cmdlet", "Send-MailMessage", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Set-Acl", , $($FullCLR -or $CoreWindows )
"Cmdlet", "Set-Alias", , $($FullCLR -or $CoreWindows -or $CoreUnix)
"Cmdlet", "Set-AuthenticodeSignature", , $($FullCLR -or $CoreWindows )
Expand Down