2016-08-29 19:01:03 +02:00
|
|
|
Describe "Serialization Tests" -tags "CI" {
|
|
|
|
BeforeAll {
|
|
|
|
$testfileName="SerializationTest.txt"
|
|
|
|
$testPath = Join-Path $TestDrive $testfileName
|
|
|
|
$testfile = New-Item $testPath -Force
|
|
|
|
function SerializeAndDeserialize([PSObject]$inputObject)
|
|
|
|
{
|
|
|
|
$xmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($inputObject.GetType())
|
|
|
|
$stream = [System.IO.StreamWriter]$testPath
|
|
|
|
$xmlWriter = [System.Xml.XmlWriter]::Create($stream)
|
|
|
|
$xmlSerializer.Serialize($xmlWriter,$inputObject)
|
|
|
|
$stream.Close()
|
|
|
|
|
|
|
|
$stream = [System.IO.StreamReader]$testPath
|
|
|
|
$xmlReader = [System.Xml.XmlReader]::Create($stream)
|
|
|
|
$outputObject = $xmlSerializer.Deserialize($xmlReader)
|
|
|
|
$stream.Close()
|
|
|
|
$xmlReader.Dispose()
|
|
|
|
return $outputObject;
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
enum MyColorFlag
|
|
|
|
{
|
|
|
|
RED
|
|
|
|
BLUE
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
AfterAll {
|
2016-08-29 19:01:03 +02:00
|
|
|
Remove-Item $testPath -Force -ErrorAction SilentlyContinue
|
2017-01-16 22:31:14 +01:00
|
|
|
}
|
2016-08-29 19:01:03 +02:00
|
|
|
|
|
|
|
It 'Test DateTimeUtc serialize and deserialize work as expected.' {
|
|
|
|
$inputObject = [System.DateTime]::UtcNow;
|
|
|
|
SerializeAndDeserialize($inputObject) | Should be $inputObject
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
It 'Test DateTime stamps serialize and deserialize work as expected.' {
|
|
|
|
$objs = [System.DateTime]::MaxValue, [System.DateTime]::MinValue, [System.DateTime]::Today, (new-object System.DateTime), (new-object System.DateTime 123456789)
|
|
|
|
foreach($inputObject in $objs)
|
|
|
|
{
|
|
|
|
SerializeAndDeserialize($inputObject) | Should be $inputObject
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
#pending because of "System.Uri cannot be serialized because it does not have a parameterless constructor."
|
|
|
|
It 'Test system Uri objects serialize and deserialize work as expected.' -Pending:$true {
|
|
|
|
$uristrings = "http://www.microsoft.com","http://www.microsoft.com:8000","http://www.microsoft.com/index.html","http://www.microsoft.com/default.asp","http://www.microsoft.com/Hello%20World.htm"
|
|
|
|
foreach($uristring in $uristrings)
|
|
|
|
{
|
|
|
|
$inputObject = new-object System.Uri $uristring
|
|
|
|
SerializeAndDeserialize($inputObject) | Should be $inputObject
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
It 'Test a byte array serialize and deserialize work as expected.' {
|
|
|
|
$objs1 = [byte]0, [byte]1, [byte]2, [byte]3, [byte]255
|
|
|
|
$objs2 = @()
|
|
|
|
$objs3 = [byte]128
|
|
|
|
$objs4 = @()
|
|
|
|
for($i=0;$i -lt 256;$i++)
|
|
|
|
{
|
|
|
|
$objs4 += [byte]$i
|
|
|
|
}
|
|
|
|
$objsArray = New-Object System.Collections.ArrayList
|
|
|
|
$objsArray.Add($objs1)
|
|
|
|
$objsArray.Add($objs2)
|
|
|
|
$objsArray.Add($objs3)
|
|
|
|
$objsArray.Add($objs4)
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
foreach($inputObject in $objsArray )
|
|
|
|
{
|
|
|
|
$outputs = SerializeAndDeserialize($inputObject);
|
|
|
|
for($i=0;$i -lt $inputObject.Length;$i++)
|
|
|
|
{
|
|
|
|
$outputs[$i] | Should be $inputObject[$i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
It 'Test Enum serialize and deserialize work as expected.' {
|
|
|
|
$inputObject = [MyColorFlag]::RED
|
|
|
|
SerializeAndDeserialize($inputObject).ToString() | Should be $inputObject.ToString()
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
It 'Test SecureString serialize and deserialize work as expected.' {
|
2017-09-27 20:54:22 +02:00
|
|
|
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
|
2016-08-29 19:01:03 +02:00
|
|
|
$inputObject = Convertto-Securestring -String "PowerShellRocks!" -AsPlainText -Force
|
|
|
|
SerializeAndDeserialize($inputObject).Length | Should be $inputObject.Length
|
|
|
|
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
|
2016-08-29 19:01:03 +02:00
|
|
|
It 'Test ScriptProperty object serialize and deserialize work as expected.' {
|
|
|
|
$versionObject = New-Object PSObject
|
|
|
|
$versionObject | Add-Member -MemberType NoteProperty -Name TestNote -Value "TestNote"
|
2017-01-16 22:31:14 +01:00
|
|
|
$versionObject | Add-Member -MemberType ScriptProperty -Name TestScriptProperty -Value { ($this.TestNote) }
|
2016-08-29 19:01:03 +02:00
|
|
|
|
|
|
|
SerializeAndDeserialize($versionObject).TestScriptProperty | Should be $versionObject.TestScriptProperty
|
|
|
|
}
|
2017-01-16 22:31:14 +01:00
|
|
|
}
|
2016-08-29 19:01:03 +02:00
|
|
|
|