Skip to content
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

[release/6.0] Fix XmlSqlBinaryReader and introduce a corpus of SqlXml tests (#81878) #82062

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
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
<Uri>https://github.com/dotnet/runtime-assets</Uri>
<Sha>6c6b7f90677142ad7bd829eb28d3bcf8ad775dd7</Sha>
</Dependency>
<Dependency Name="System.Data.Common.TestData" Version="6.0.0-beta.23113.6">
<Uri>https://github.com/dotnet/runtime-assets</Uri>
<Sha>dc9053bd5cb930dab70bb04745ce69a1a113da78</Sha>
</Dependency>
<Dependency Name="System.Drawing.Common.TestData" Version="6.0.0-beta.21518.1">
<Uri>https://github.com/dotnet/runtime-assets</Uri>
<Sha>6c6b7f90677142ad7bd829eb28d3bcf8ad775dd7</Sha>
Expand Down
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<!-- Runtime-Assets dependencies -->
<SystemRuntimeNumericsTestDataVersion>6.0.0-beta.21518.1</SystemRuntimeNumericsTestDataVersion>
<SystemComponentModelTypeConverterTestDataVersion>6.0.0-beta.21518.1</SystemComponentModelTypeConverterTestDataVersion>
<SystemDataCommonTestDataVersion>6.0.0-beta.23113.6</SystemDataCommonTestDataVersion>
<SystemDrawingCommonTestDataVersion>6.0.0-beta.21518.1</SystemDrawingCommonTestDataVersion>
<SystemIOCompressionTestDataVersion>6.0.0-beta.21518.1</SystemIOCompressionTestDataVersion>
<SystemIOPackagingTestDataVersion>6.0.0-beta.21518.1</SystemIOPackagingTestDataVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
</ItemGroup>
<!-- S.D.SqlClient isn't live built anymore. -->
<ItemGroup>
<PackageReference Include="System.Data.Common.TestData" Version="$(SystemDataCommonTestDataVersion)" />
<PackageReference Include="System.Data.SqlClient" Version="$(SystemDataSqlClientVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

Expand All @@ -32,10 +15,8 @@ namespace System.Data.Tests.SqlTypes
{
public class SqlXmlTest
{
// Test constructor
[Fact] // .ctor (Stream)
//[Category ("NotDotNet")] // Name cannot begin with the '.' character, hexadecimal value 0x00. Line 1, position 2
public void Constructor2_Stream_Unicode()
[Fact]
public void Constructor_Stream_Unicode()
{
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>";
MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(xmlStr));
Expand All @@ -44,8 +25,8 @@ public void Constructor2_Stream_Unicode()
Assert.Equal(xmlStr, xmlSql.Value);
}

[Fact] // .ctor (Stream)
public void Constructor2_Stream_Empty()
[Fact]
public void Constructor_Stream_Empty()
{
MemoryStream ms = new MemoryStream();
SqlXml xmlSql = new SqlXml(ms);
Expand All @@ -54,16 +35,16 @@ public void Constructor2_Stream_Empty()
}

[Fact]
public void Constructor2_Stream_Null()
public void Constructor_Stream_Null()
{
SqlXml xmlSql = new SqlXml((Stream)null);
Assert.True(xmlSql.IsNull);

Assert.Throws<SqlNullValueException>(() => xmlSql.Value);
}

[Fact] // .ctor (XmlReader)
public void Constructor3()
[Fact]
public void Constructor_StringReader()
{
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>";
XmlReader xrdr = new XmlTextReader(new StringReader(xmlStr));
Expand All @@ -72,8 +53,8 @@ public void Constructor3()
Assert.Equal(xmlStr, xmlSql.Value);
}

[Fact] // .ctor (XmlReader)
public void Constructor3_XmlReader_Empty()
[Fact]
public void Constructor_XmlReader_Empty()
{
XmlReaderSettings xs = new XmlReaderSettings();
xs.ConformanceLevel = ConformanceLevel.Fragment;
Expand All @@ -84,7 +65,7 @@ public void Constructor3_XmlReader_Empty()
}

[Fact]
public void Constructor3_XmlReader_Null()
public void Constructor_XmlReader_Null()
{
SqlXml xmlSql = new SqlXml((XmlReader)null);
Assert.True(xmlSql.IsNull);
Expand All @@ -93,7 +74,6 @@ public void Constructor3_XmlReader_Null()
}

[Fact]
//[Category ("NotDotNet")] // Name cannot begin with the '.' character, hexadecimal value 0x00. Line 1, position 2
public void CreateReader_Stream_Unicode()
{
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>";
Expand All @@ -107,7 +87,7 @@ public void CreateReader_Stream_Unicode()
}

[Fact]
public void SqlXml_fromXmlReader_CreateReaderTest()
public void CreateReader_XmlTextReader_CanReadContent()
{
string xmlStr = "<Employee><FirstName>Varadhan</FirstName><LastName>Veerapuram</LastName></Employee>";
XmlReader rdr = new XmlTextReader(new StringReader(xmlStr));
Expand All @@ -119,8 +99,82 @@ public void SqlXml_fromXmlReader_CreateReaderTest()
Assert.Equal(xmlStr, xrdr.ReadOuterXml());
}

public static class CreateReader_TestFiles
{
private static TheoryData<string, string> _filesAndBaselines;

// The test files are made available through the System.Data.Common.TestData package included in dotnet/runtime-assets
private static void EnsureFileList()
{
if (_filesAndBaselines is null)
{
IEnumerable<string> text = Directory.EnumerateFiles(Path.Combine("SqlXml.CreateReader", "Baseline-Text"), "*.xml");
IEnumerable<string> binary = Directory.EnumerateFiles(Path.Combine("SqlXml.CreateReader", "SqlBinaryXml"), "*.bmx");

// Make sure that we found our test files; otherwise the theories would succeed without validating anything
Assert.NotEmpty(text);
Assert.NotEmpty(binary);

TheoryData<string, string> filesAndBaselines = new TheoryData<string, string>();

// Use the Text XML files as their own baselines
filesAndBaselines.Append(text.Select(f => new string[] { TextXmlFileName(f), TextXmlFileName(f) }).ToArray());

// Use the matching Text XML files as the baselines for the SQL Binary XML files
filesAndBaselines.Append(binary
.Select(Path.GetFileNameWithoutExtension)
.Intersect(text.Select(Path.GetFileNameWithoutExtension))
.Select(f => new string[] { SqlBinaryXmlFileName(f), TextXmlFileName(f) }).ToArray());

_filesAndBaselines = filesAndBaselines;

string TextXmlFileName(string name) => Path.Combine("SqlXml.CreateReader", "Baseline-Text", $"{name}.xml");
string SqlBinaryXmlFileName(string name) => Path.Combine("SqlXml.CreateReader", "SqlBinaryXml", $"{name}.bmx");
}
}

public static TheoryData<string, string> FilesAndBaselines
{
get
{
EnsureFileList();
return _filesAndBaselines;
}
}

public static string ReadAllXml(XmlReader reader)
{
using StringWriter writer = new StringWriter();
using XmlWriter xmlWriter = new XmlTextWriter(writer);

while (reader.Read()) xmlWriter.WriteNode(reader, false);

return writer.ToString();
}
}

[Theory]
[MemberData(nameof(CreateReader_TestFiles.FilesAndBaselines), MemberType = typeof(CreateReader_TestFiles))]
public void CreateReader_TestAgainstBaseline(string testFile, string baselineFile)
{
// Get our expected output by using XmlReader directly
using XmlReader baselineReader = XmlReader.Create(baselineFile);
string expected = CreateReader_TestFiles.ReadAllXml(baselineReader);

// Now produce the actual output through SqlXml.CreateReader
using FileStream xmlStream = new FileStream(testFile, FileMode.Open);
SqlXml sqlXml = new SqlXml(xmlStream);

// When the input is text, an XmlTextReader will be returned
// When the input is SQL Binary XML, an XmlSqlBinaryReader will be returned
using XmlReader actualReader = sqlXml.CreateReader();
string actual = CreateReader_TestFiles.ReadAllXml(actualReader);

Assert.Equal(expected, actual);
}

[Fact]
public void SqlXml_fromZeroLengthStream_CreateReaderTest()
public void SqlXml_FromZeroLengthStream_CreateReaderTest()
{
MemoryStream stream = new MemoryStream();
SqlXml xmlSql = new SqlXml(stream);
Expand All @@ -131,7 +185,7 @@ public void SqlXml_fromZeroLengthStream_CreateReaderTest()
}

[Fact]
public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest_withFragment()
public void SqlXml_FromZeroLengthXmlReader_CreateReaderTest_withFragment()
{
XmlReaderSettings xs = new XmlReaderSettings();
xs.ConformanceLevel = ConformanceLevel.Fragment;
Expand All @@ -145,7 +199,7 @@ public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest_withFragment()
}

[Fact]
public void SqlXml_fromZeroLengthXmlReader_CreateReaderTest()
public void SqlXml_FromZeroLengthXmlReader_CreateReaderTest()
{
XmlReader rdr = new XmlTextReader(new StringReader(string.Empty));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3519,7 +3519,9 @@ private XmlNodeType CheckText(bool attr)
Debug.Assert(_checkCharacters, "this.checkCharacters");
// grab local copy (perf)

ReadOnlySpan<byte> data = _data.AsSpan(_tokDataPos, _end - _tokDataPos);
// Get the bytes for the current token. _tokDataPos is the beginning position,
// and _pos has advanced to the next token (1 past the end of this token).
ReadOnlySpan<byte> data = _data.AsSpan(_tokDataPos, _pos - _tokDataPos);
Debug.Assert(data.Length % 2 == 0, "Data size should not be odd");

if (!attr)
Expand Down