Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Net.Http.Internal;
using Newtonsoft.Json.Linq;
namespace System.Net.Http.Formatting
{
internal class JTokenRoundTripComparer
{
public static bool Compare(JToken initValue, JToken newValue)
{
if (initValue == null && newValue == null)
{
return true;
}
if (initValue == null || newValue == null)
{
return false;
}
if (initValue is JValue)
{
string initStr;
if (initValue.Type == JTokenType.String)
{
initStr = initValue.ToString();
}
else
{
initStr = ((JValue)initValue).Value.ToString();
}
string newStr;
if (newValue is JValue)
{
newStr = newValue.ToString();
initStr = UriQueryUtility.UrlDecode(UriQueryUtility.UrlEncode(initStr));
return initStr.Equals(newStr);
}
else if (newValue is JObject && ((JObject)newValue).Count == 1)
{
initStr = String.Format("{0}", initValue.ToString());
return ((IDictionary<string, JToken>)newValue).ContainsKey(initStr);
}
return false;
}
if (((JContainer)initValue).Count != ((JContainer)newValue).Count)
{
return false;
}
if (initValue is JObject && newValue is JObject)
{
foreach (KeyValuePair<string, JToken> item in (JObject)initValue)
{
if (!Compare(item.Value, newValue[item.Key]))
{
return false;
}
}
return true;
}
if (initValue is JArray && newValue is JArray)
{
for (int i = 0; i < ((JArray)initValue).Count; i++)
{
if (!Compare(initValue[i], newValue[i]))
{
return false;
}
}
return true;
}
return false;
}
}
}

View File

@ -0,0 +1,119 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Microsoft.TestCommon;
using Moq;
using Xunit;
using Xunit.Extensions;
namespace System.Net.Http.Formatting
{
public class JsonNetValidationTest
{
public static IEnumerable<object[]> Theories
{
get
{
return new TheoryDataSet<string, Type, int>()
{
// Type coercion
{"null", typeof(int), 1},
{"45", typeof(string), 0},
{"random text", typeof(DateTimeOffset), 1},
{"[1,2,3]", typeof(string[]), 0},
{"\"foo\"", typeof(int), 1},
{"\"foo\"", typeof(DateTime), 1},
{"[\"a\",\"b\",\"45\",34]", typeof(int[]), 2},
{"[\"a\",\"b\",\"45\",34]", typeof(DateTime[]), 4},
// Required members
{"{}", typeof(DataContractWithRequiredMembers), 2},
{"[{},{},{}]", typeof(DataContractWithRequiredMembers[]), 6},
// Throwing setters
{"{\"Throws\":\"foo\"}", typeof(TypeWithThrowingSetter), 1},
{"[{\"Throws\":\"foo\"},{\"Throws\":\"foo\"}]", typeof(TypeWithThrowingSetter[]), 2},
};
}
}
[Theory]
[PropertyData("Theories")]
public void ModelErrorsPopulatedWithValidationErrors(string json, Type type, int expectedErrors)
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
formatter.RequiredMemberSelector = new SimpleRequiredMemberSelector();
Mock<IFormatterLogger> mockLogger = new Mock<IFormatterLogger>() { };
JsonNetSerializationTest.Deserialize(json, type, formatter, mockLogger.Object);
mockLogger.Verify(mock => mock.LogError(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(expectedErrors));
}
[Fact]
public void HittingMaxDepthRaisesOnlyOneValidationError()
{
// Arrange
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
Mock<IFormatterLogger> mockLogger = new Mock<IFormatterLogger>();
StringBuilder sb = new StringBuilder("{'A':null}");
for (int i = 0; i < 5000; i++)
{
sb.Insert(0, "{'A':");
sb.Append('}');
}
string json = sb.ToString();
// Act
JsonNetSerializationTest.Deserialize(json, typeof(Nest), formatter, mockLogger.Object);
// Assert
mockLogger.Verify(mock => mock.LogError(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
}
}
// this IRMS treats all member names that start with "Required" as required
public class SimpleRequiredMemberSelector : IRequiredMemberSelector
{
public bool IsRequiredMember(MemberInfo member)
{
return member.Name.StartsWith("Required");
}
}
public class DataContractWithRequiredMembers
{
public string Required1;
public string Required2;
public string Optional;
}
public class TypeWithThrowingSetter
{
public string Throws
{
get
{
return "foo";
}
set
{
throw new NotImplementedException();
}
}
}
public class Nest
{
public Nest A { get; set; }
}
}

View File

@ -0,0 +1,155 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Json;
using Newtonsoft.Json.Linq;
namespace System.Net.Http.Formatting
{
public class JTokenCreatorSurrogate : InstanceCreatorSurrogate
{
private const int MaxDepth = 4;
public override bool CanCreateInstanceOf(Type type)
{
return (type == typeof(JToken) || type == typeof(JArray) || type == typeof(JObject) || type == typeof(JValue));
}
public override object CreateInstanceOf(Type type, Random rndGen)
{
if (!this.CanCreateInstanceOf(type))
{
return null;
}
if (type == typeof(JToken))
{
return CreateJToken(rndGen, 0);
}
else if (type == typeof(JArray))
{
return CreateJArray(rndGen, 0);
}
else if (type == typeof(JObject))
{
return CreateJObject(rndGen, 0);
}
else
{
return CreateJsonPrimitive(rndGen);
}
}
private static JToken CreateJToken(Random rndGen, int depth)
{
if (rndGen.Next() < CreatorSettings.NullValueProbability)
{
return null;
}
if (depth < MaxDepth)
{
switch (rndGen.Next(10))
{
case 0:
case 1:
case 2:
// 30% chance to create an array
return CreateJArray(rndGen, depth);
case 3:
case 4:
case 5:
// 30% chance to create an object
return CreateJObject(rndGen, depth);
default:
// 40% chance to create a primitive
break;
}
}
return CreateJsonPrimitive(rndGen);
}
static JToken CreateJsonPrimitive(Random rndGen)
{
switch (rndGen.Next(17))
{
case 0:
return PrimitiveCreator.CreateInstanceOfChar(rndGen);
case 1:
return new JValue(PrimitiveCreator.CreateInstanceOfByte(rndGen));
case 2:
return PrimitiveCreator.CreateInstanceOfSByte(rndGen);
case 3:
return PrimitiveCreator.CreateInstanceOfInt16(rndGen);
case 4:
return PrimitiveCreator.CreateInstanceOfUInt16(rndGen);
case 5:
return PrimitiveCreator.CreateInstanceOfInt32(rndGen);
case 6:
return PrimitiveCreator.CreateInstanceOfUInt32(rndGen);
case 7:
return PrimitiveCreator.CreateInstanceOfInt64(rndGen);
case 8:
return PrimitiveCreator.CreateInstanceOfUInt64(rndGen);
case 9:
return PrimitiveCreator.CreateInstanceOfDecimal(rndGen);
case 10:
return PrimitiveCreator.CreateInstanceOfDouble(rndGen);
case 11:
return PrimitiveCreator.CreateInstanceOfSingle(rndGen);
case 12:
return PrimitiveCreator.CreateInstanceOfDateTime(rndGen);
case 13:
return PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen);
case 14:
case 15:
// TODO: 199532 fix uri comparer
return PrimitiveCreator.CreateInstanceOfString(rndGen);
default:
return PrimitiveCreator.CreateInstanceOfBoolean(rndGen);
}
}
static JArray CreateJArray(Random rndGen, int depth)
{
int size = rndGen.Next(CreatorSettings.MaxArrayLength);
if (CreatorSettings.NullValueProbability == 0 && size == 0)
{
size++;
}
JArray result = new JArray();
for (int i = 0; i < size; i++)
{
result.Add(CreateJToken(rndGen, depth + 1));
}
return result;
}
static JObject CreateJObject(Random rndGen, int depth)
{
const string keyChars = "abcdefghijklmnopqrstuvwxyz0123456789";
int size = rndGen.Next(CreatorSettings.MaxArrayLength);
if (CreatorSettings.NullValueProbability == 0 && size == 0)
{
size++;
}
JObject result = new JObject();
for (int i = 0; i < size; i++)
{
string key;
do
{
key = PrimitiveCreator.CreateInstanceOfString(rndGen, 10, keyChars);
} while (result.Count > 0 && ((IDictionary<string, JToken>)result).ContainsKey(key));
result.Add(key, CreateJToken(rndGen, depth + 1));
}
return result;
}
}
}

View File

@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("System.Net.Http.Formatting.Test.Integration")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("System.Net.Http.Formatting.Test.Integration")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c858cf22-d435-4996-ba01-fa63ebe12a47")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory),Runtime.sln))\tools\WebStack.settings.targets" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6C18CC83-1E4C-42D2-B93E-55D6C363850C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>System.Net.Http.Formatting</RootNamespace>
<AssemblyName>System.Net.Http.Formatting.Test.Integration</AssemblyName>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>$(WebStackRootPath)\bin\Debug\Test\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>$(WebStackRootPath)\bin\Release\Test\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'CodeCoverage' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>$(WebStackRootPath)\bin\CodeCoverage\Test\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Moq">
<HintPath>..\..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Newtonsoft.Json.4.5.1\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Net.Http.2.0.20326.1\lib\net40\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Net.Http.2.0.20326.1\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="xunit, Version=1.9.0.1566, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\..\packages\xunit.1.9.0.1566\lib\xunit.dll</HintPath>
</Reference>
<Reference Include="xunit.extensions">
<HintPath>..\..\packages\xunit.extensions.1.9.0.1566\lib\xunit.extensions.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="JsonNetSerializationTest.cs" />
<Compile Include="JsonNetValidationTest.cs" />
<Compile Include="JsonValueCreatorSurrogate.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FormUrlEncodedFromContentTests.cs" />
<Compile Include="FormUrlEncodedFromUriQueryTests.cs" />
<Compile Include="JTokenRoundTripComparer.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\System.Net.Http.Formatting\System.Net.Http.Formatting.csproj">
<Project>{668E9021-CE84-49D9-98FB-DF125A9FCDB0}</Project>
<Name>System.Net.Http.Formatting</Name>
</ProjectReference>
<ProjectReference Include="..\Microsoft.TestCommon\Microsoft.TestCommon.csproj">
<Project>{FCCC4CB7-BAF7-4A57-9F89-E5766FE536C0}</Project>
<Name>Microsoft.TestCommon</Name>
</ProjectReference>
<ProjectReference Include="..\System.Net.Http.Formatting.Test.Unit\System.Net.Http.Formatting.Test.Unit.csproj">
<Project>{7AF77741-9158-4D5F-8782-8F21FADF025F}</Project>
<Name>System.Net.Http.Formatting.Test.Unit</Name>
</ProjectReference>
<ProjectReference Include="..\System.Json.Test.Integration\System.Json.Test.Integration.csproj">
<Project>{A7B1264E-BCE5-42A8-8B5E-001A5360B128}</Project>
<Name>System.Json.Test.Integration</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Net.Http" version="2.0.20326.1" />
<package id="Moq" version="4.0.10827" />
<package id="Newtonsoft.Json" version="4.5.1" />
<package id="xunit" version="1.9.0.1566" />
<package id="xunit.extensions" version="1.9.0.1566" />
</packages>