Fix roundtripping of binary data, add unit test

This commit is contained in:
Frederik Carlier
2018-06-20 12:59:57 +02:00
parent e8112c76b1
commit 8fdd1388fe
6 changed files with 296 additions and 42 deletions

View File

@@ -0,0 +1,25 @@
using Claunia.PropertyList;
using System.IO;
using Xunit;
namespace plistcil.test
{
public class BinaryPropertyListWriterTests
{
[Fact]
public void RoundtripTest()
{
byte[] data = File.ReadAllBytes("test-files/plist.bin");
NSObject root = PropertyListParser.Parse(data);
using (MemoryStream actualOutput = new MemoryStream())
using (Stream expectedOutput = File.OpenRead("test-files/plist.bin"))
using (ValidatingStream validatingStream = new ValidatingStream(actualOutput, expectedOutput))
{
BinaryPropertyListWriter writer = new BinaryPropertyListWriter(validatingStream);
writer.ReuseObjectIds = false;
writer.Write(root);
}
}
}
}

View File

@@ -0,0 +1,134 @@
// <copyright file="ValidatingCompositeStream.cs" company="Quamotion">
// Copyright (c) Quamotion. All rights reserved.
// </copyright>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace plistcil.test
{
/// <summary>
/// A <see cref="Stream"/> which writes its output to a <see cref="Stream"/> and validates that the data which
/// is being written to the output stream matches the data in a reference stream.
/// </summary>
internal class ValidatingStream : Stream
{
private Stream output;
private Stream expectedOutput;
/// <summary>
/// Initializes a new instance of the <see cref="ValidatingCompositeStream"/> class.
/// </summary>
/// <param name="output">
/// The <see cref="Stream"/> to which to write data.
/// </param>
/// <param name="expectedOutput">
/// The reference stream for <paramref name="output"/>.
/// </param>
public ValidatingStream(Stream output, Stream expectedOutput)
{
this.output = output ?? throw new ArgumentNullException(nameof(output));
this.expectedOutput = expectedOutput ?? throw new ArgumentNullException(nameof(expectedOutput));
}
/// <inheritdoc/>
public override bool CanRead
{
get { return false; }
}
/// <inheritdoc/>
public override bool CanSeek
{
get { return false; }
}
/// <inheritdoc/>
public override bool CanWrite
{
get { return true; }
}
/// <inheritdoc/>
public override long Length
{
get { return this.output.Length; }
}
/// <inheritdoc/>
public override long Position
{
get { return this.output.Position; }
set { throw new NotImplementedException(); }
}
/// <inheritdoc/>
public override void Flush()
{
this.output.Flush();
}
/// <inheritdoc/>
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
/// <inheritdoc/>
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public override void Write(byte[] buffer, int offset, int count)
{
byte[] expected = new byte[buffer.Length];
this.expectedOutput.Read(expected, offset, count);
byte[] bufferChunk = buffer.Skip(offset).Take(count).ToArray();
byte[] expectedChunk = expected.Skip(offset).Take(count).ToArray();
// Make sure the data being writen matches the data which was written to the expected stream.
// This will detect any errors as the invalid data is being written out - as opposed to post-
// test binary validation.
Assert.Equal(expectedChunk, bufferChunk);
this.output.Write(buffer, offset, count);
}
/// <inheritdoc/>
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
byte[] expected = new byte[buffer.Length];
await this.expectedOutput.ReadAsync(expected, offset, count, cancellationToken).ConfigureAwait(false);
byte[] bufferChunk = buffer.Skip(offset).Take(count).ToArray();
byte[] expectedChunk = expected.Skip(offset).Take(count).ToArray();
// Make sure the data being writen matches the data which was written to the expected stream.
// This will detect any errors as the invalid data is being written out - as opposed to post-
// test binary validation.
Assert.Equal(expectedChunk, bufferChunk);
await this.output.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
}
}
}

View File

@@ -19,6 +19,9 @@
</ItemGroup>
<ItemGroup>
<None Include="..\plist-cil.benchmark\plist.bin" Link="test-files\plist.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\test-files\test1-ascii-gnustep.plist">
<Link>test-files\test1-ascii-gnustep.plist</Link>
<Gettext-ScanForTranslations>False</Gettext-ScanForTranslations>