From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- .../Tests/CrossDomainPolicyParserTests.csproj | 71 +++++++ .../Tests/FlashPolicyParserSocketTests.cs | 124 ++++++++++++ .../Tests/FlashPolicyParserTests.cs | 209 +++++++++++++++++++++ .../Tests/Properties/AssemblyInfo.cs | 36 ++++ .../CrossDomainPolicyParser/Tests/UriToolsTests.cs | 23 +++ 5 files changed, 463 insertions(+) create mode 100644 Runtime/Managed/CrossDomainPolicyParser/Tests/CrossDomainPolicyParserTests.csproj create mode 100644 Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserSocketTests.cs create mode 100644 Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserTests.cs create mode 100644 Runtime/Managed/CrossDomainPolicyParser/Tests/Properties/AssemblyInfo.cs create mode 100644 Runtime/Managed/CrossDomainPolicyParser/Tests/UriToolsTests.cs (limited to 'Runtime/Managed/CrossDomainPolicyParser/Tests') diff --git a/Runtime/Managed/CrossDomainPolicyParser/Tests/CrossDomainPolicyParserTests.csproj b/Runtime/Managed/CrossDomainPolicyParser/Tests/CrossDomainPolicyParserTests.csproj new file mode 100644 index 0000000..0e27635 --- /dev/null +++ b/Runtime/Managed/CrossDomainPolicyParser/Tests/CrossDomainPolicyParserTests.csproj @@ -0,0 +1,71 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {5C04DB87-3B34-43B4-B164-86CE4361DC7B} + Library + Properties + CrossDomainPolicyParserTests + CrossDomainPolicyParserTests + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + False + ..\..\..\..\Tools\NUnit\bin\nunit.framework.dll + + + + + + + + + + + {31C2F345-D887-49DD-A1F6-741CABD74A42} + CrossDomainPolicyParser + + + + + \ No newline at end of file diff --git a/Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserSocketTests.cs b/Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserSocketTests.cs new file mode 100644 index 0000000..8e0f62b --- /dev/null +++ b/Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserSocketTests.cs @@ -0,0 +1,124 @@ +using System.IO; +using System.Text; +using MonoForks.System.Windows.Browser.Net; +using NUnit.Framework; + +namespace CrossDomainPolicyParserTests +{ + [TestFixture] + public class FlashPolicyParserSocketTests + { + [Test] + public void AllDomains_AllPorts_IsAllowed() + { + string policy = @" + + +"; + Assert.IsTrue(RequestAllowed(policy, 123)); + } + + [Test] + public void AllDomains_AllPorts_Trailing0_IsAllowed() + { + string policy = @" + + +" + "\0"; + Assert.IsTrue(RequestAllowed(policy, 123)); + } + + [Test] + public void AllDomains_UsingSpecificPorts_IsAllowed() + { + string policy = @" + + +"; + Assert.IsTrue(RequestAllowed(policy, 1020)); + } + + [Test] + public void AllDomains_OutsideSpecificPorts_IsDisAllowed() + { + string policy = @" + + +"; + Assert.IsFalse(RequestAllowed(policy, 1020)); + } + + [Test] + public void AllDomains_OutsidePortRange_IsDisAllowed() + { + string policy = @" + + +"; + Assert.IsFalse(RequestAllowed(policy, 1020)); + } + + [Test] + public void AllDomains_InsidePortRange_IsAllowed() + { + string policy = @" + + +"; + Assert.IsTrue(RequestAllowed(policy, 1035)); + } + + [Test] + public void PolicyReceivedFromHigherThan1024_DisallowsAccessToBelow1024Ports() + { + string policy = @" + + +"; + Assert.IsFalse(RequestAllowed(policy, 1010, 1300)); + } + + [Test] + public void PolicyReceivedFromHigherThan1024_AllowsAccessToAbove1024Ports() + { + string policy = @" + + +"; + Assert.IsTrue(RequestAllowed(policy, 1035, 1300)); + } + + [Test] + public void PolicyReceivedFromLowerThan1024_AllowsAccessToBelow1024Ports() + { + string policy = @" + + +"; + Assert.IsTrue(RequestAllowed(policy, 1010, 1000)); + } + [Test] + public void PolicyReceivedFromLowerThan1024_AllowsAccessToAbove1024Ports() + { + string policy = @" + + +"; + Assert.IsTrue(RequestAllowed(policy, 1030, 1000)); + } + + private bool RequestAllowed(string xdomain, int port) + { + return RequestAllowed(xdomain, port, 843); + } + + private bool RequestAllowed(string xdomain, int port, int policyport) + { + var ms = new MemoryStream(Encoding.UTF8.GetBytes(xdomain)); + var policy = FlashCrossDomainPolicy.FromStream(ms); + policy.PolicyPort = policyport; + return policy.IsSocketConnectionAllowed(port); + } + + } +} \ No newline at end of file diff --git a/Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserTests.cs b/Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserTests.cs new file mode 100644 index 0000000..976ba92 --- /dev/null +++ b/Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserTests.cs @@ -0,0 +1,209 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using MonoForks.Mono.Xml; +using MonoForks.System.Net; +using NUnit.Framework; +using MonoForks.System.Windows.Browser.Net; +using UnityEngine; +using Uri = MonoForks.System.Uri; + +namespace CrossDomainPolicyParserTests +{ + [TestFixture] + public class FlashPolicyParserTests + { + static string XDomainGlobal = +@" + + + +"; + + string http_hosted = "http://www.host.com/coolgame.unity3d"; + string https_hosted = "https://secure.host.net/coolgame.unity3d"; + string file_hosted = "file:///coolgame.unity3"; + + [Test] + public void GlobalXDomainAcceptsRequestOnSameDomain() + { + string requesturl = "http://www.mach8.nl/index.html"; + + Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, http_hosted)); + } + [Test] + public void GlobalXDomainAcceptsRequestOnSubDomain() + { + string requesturl = "http://subdomain.mach8.nl/index.html"; + + Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, http_hosted)); + } + + [Test] + public void GlobalXDomainAllowsSecureRequestWhenHostedNonSecure() + { + string requesturl = "https://www.mach8.nl/index.html"; + + Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, http_hosted)); + } + [Test] + public void GlobalXDomainAcceptsSecureRequestWhenHostedSecure() + { + string requesturl = "https://www.mach8.nl/index.html"; + + Assert.IsTrue(RequestAllowed(XDomainGlobal, requesturl, https_hosted)); + } + [Test] + public void GlobalXDomainDeniesNonSecureRequestWhenHostedSecure() + { + string requesturl = "http://www.mach8.nl/index.html"; + Assert.IsFalse(RequestAllowed(XDomainGlobal, requesturl, https_hosted)); + } + + [Test] + public void AllDomain_Secure() + { + string policy = @" + + + +"; + + Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void WhenRequestURLMatchesWildCardAccessIsAllowed() + { + string policy = @" + + +"; + + Assert.IsTrue(RequestAllowed(policy, "http://subdomain.mydomain.nl", http_hosted)); + } + + [Test] + public void WhenRequestURLDoesNotMatchWildCardAccessIsDisallowed() + { + string policy = @" + + +"; + + Assert.IsFalse(RequestAllowed(policy, "http://subdomain.myotherdomain.nl", http_hosted)); + } + + + [Test] + public void AllDomains_NoDTD() + { + string policy = @""; + + Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void AllDomains_NoXmlHeader() + { + string policy = @" + + "; + Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void AllDomains_PermittedCrossDomainPolicies_All() + { + // 'all' is the default value + // http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html#site-control-permitted-cross-domain-policies + string policy = @" + + + + +"; + + Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void AllDomains_PermittedCrossDomainPolicies_MasterOnly() + { + string policy = @" + + + + +"; + + Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void AllDomains_PermittedCrossDomainPolicies_None() + { + string policy = @" + + + + +"; + Assert.IsFalse(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void AllDomains_PermittedCrossDomainPolicies_ByContentType() + { + string policy = @" + + + + +"; + Assert.IsFalse(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + public void AllDomains_PermittedCrossDomainPolicies_ByFtpFilename() + { + string policy = @" + + + + +"; + Assert.IsTrue(RequestAllowed(policy, "http://www.host.com", http_hosted)); + } + + [Test] + [ExpectedException(typeof(MiniParser.XMLError))] + public void IllformedPolicyIsRejected() + { + FlashCrossDomainPolicyFromString("bogus", "http://www.host.com"); + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void EmptyPolicyStringIsRejected() + { + FlashCrossDomainPolicyFromString("", "http://www.host.com"); + } + + private bool RequestAllowed(string xdomain, string requesturl, string hosturl) + { + FlashCrossDomainPolicy policy = FlashCrossDomainPolicyFromString(xdomain, hosturl); + var wr = new WebRequest(new Uri(requesturl), new Dictionary()); + return policy.IsAllowed(wr); + } + + private FlashCrossDomainPolicy FlashCrossDomainPolicyFromString(string xdomain, string hosturl) + { + UnityCrossDomainHelper.SetWebSecurityHostUriDelegate(() => hosturl); + + var ms = new MemoryStream(Encoding.UTF8.GetBytes(xdomain)); + return FlashCrossDomainPolicy.FromStream(ms); + } + } +} diff --git a/Runtime/Managed/CrossDomainPolicyParser/Tests/Properties/AssemblyInfo.cs b/Runtime/Managed/CrossDomainPolicyParser/Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..5fd4c99 --- /dev/null +++ b/Runtime/Managed/CrossDomainPolicyParser/Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +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("CrossDomainPolicyParserTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("CrossDomainPolicyParserTests")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2010")] +[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("c40d4596-1c43-4f37-b7fa-545a543577f3")] + +// 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.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Runtime/Managed/CrossDomainPolicyParser/Tests/UriToolsTests.cs b/Runtime/Managed/CrossDomainPolicyParser/Tests/UriToolsTests.cs new file mode 100644 index 0000000..7a29b00 --- /dev/null +++ b/Runtime/Managed/CrossDomainPolicyParser/Tests/UriToolsTests.cs @@ -0,0 +1,23 @@ +using CrossDomainPolicyParser; +using MonoForks.System; +using NUnit.Framework; + +namespace CrossDomainPolicyParserTests +{ + [TestFixture] + public class UriToolsTests + { + [Test] + public void MakeUriWorksForRelativeUri() + { + Uri uri = UriTools.MakeUri("http://mydomain.com/mygame.unity3d", "test.png"); + Assert.AreEqual("mydomain.com",uri.Host); + } + [Test] + public void MakeUriWorksForAbsoluteUri() + { + Uri uri = UriTools.MakeUri("http://mydomain.com/mygame.unity3d", "http://www.google.com/test.png"); + Assert.AreEqual("www.google.com", uri.Host); + } + } +} -- cgit v1.1-26-g67d0