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/FlashPolicyParserTests.cs | 209 +++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserTests.cs (limited to 'Runtime/Managed/CrossDomainPolicyParser/Tests/FlashPolicyParserTests.cs') 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); + } + } +} -- cgit v1.1-26-g67d0