Skip to content

Commit

Permalink
Added a new test case. Delayed schema verification.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Sep 22, 2018
1 parent 99d3f8d commit 2f1d085
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 75 deletions.
75 changes: 27 additions & 48 deletions Microsoft.Web.Administration/ConfigurationElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,7 @@ public ConfigurationAttribute GetAttribute(string attributeName)
if (!ContainsAttribute(attributeName))
{
throw new COMException(
string.Format(
"Filename: \\\\?\\{0}\r\nLine number: {1}\r\nError: Unrecognized attribute '{2}'\r\n\r\n",
FileContext.FileName,
(Entity as IXmlLineInfo).LineNumber,
attributeName));
$"Filename: \\\\?\\{FileContext.FileName}\r\nLine number: {(Entity as IXmlLineInfo).LineNumber}\r\nError: Unrecognized attribute '{attributeName}'\r\n\r\n");
}

return Attributes[attributeName];
Expand All @@ -233,8 +229,9 @@ public ConfigurationElement GetChildElement(string elementName, Type elementType

public ConfigurationElementCollection GetCollection()
{
var section = this as ConfigurationSection;
return section == null ? (this as ConfigurationElementCollection)?.ForceLoad() : section.Root.ForceLoad();
return this is ConfigurationSection section
? section.Root.ForceLoad()
: (this as ConfigurationElementCollection)?.ForceLoad();
}

public ConfigurationElementCollection GetCollection(string collectionName)
Expand Down Expand Up @@ -266,7 +263,7 @@ public ConfigurationElement GetCollection(string collectionName, Type collection
throw new NotImplementedException();
}

public Object GetMetadata(string metadataType)
public object GetMetadata(string metadataType)
{
throw new NotImplementedException();
}
Expand Down Expand Up @@ -306,47 +303,36 @@ public object this[string attributeName]
set { SetAttributeValue(attributeName, value); }
}

public ConfigurationMethodCollection Methods { get; private set; }
public IDictionary<string, string> RawAttributes => ConfigSource == null ? _rawAttributes : ConfigSource.RawAttributes;
public ConfigurationMethodCollection Methods { get; }
public IDictionary<string, string> RawAttributes
=> ConfigSource == null ? _rawAttributes : ConfigSource.RawAttributes;
public ConfigurationElementSchema Schema { get; }

private List<ConfigurationElementCollection> Collections { get; }

internal ConfigurationElement ParentElement { get; }

private ConfigurationLockCollection _lockAllAttributesExcept;

public ConfigurationLockCollection LockAllAttributesExcept
{
get
{
return _lockAllAttributesExcept ??
(_lockAllAttributesExcept =
new ConfigurationLockCollection(this,
ConfigurationLockType.Attribute | ConfigurationLockType.Exclude));
}
}
=> _lockAllAttributesExcept ??
(_lockAllAttributesExcept =
new ConfigurationLockCollection(this,
ConfigurationLockType.Attribute | ConfigurationLockType.Exclude));

private ConfigurationLockCollection _lockAllElementsExcept;

public ConfigurationLockCollection LockAllElementsExcept
{
get
{
return _lockAllElementsExcept ??
(_lockAllElementsExcept =
new ConfigurationLockCollection(this,
ConfigurationLockType.Element | ConfigurationLockType.Exclude));
}
}
=> _lockAllElementsExcept ??
(_lockAllElementsExcept =
new ConfigurationLockCollection(this,
ConfigurationLockType.Element | ConfigurationLockType.Exclude));

private ConfigurationLockCollection _lockAttributes;

public ConfigurationLockCollection LockAttributes
{
get
{
return _lockAttributes ??
(_lockAttributes = new ConfigurationLockCollection(this, ConfigurationLockType.Attribute));
}
}
=> _lockAttributes ??
(_lockAttributes = new ConfigurationLockCollection(this, ConfigurationLockType.Attribute));

private ConfigurationLockCollection _lockElements;
protected internal XElement InnerEntity;
Expand All @@ -357,20 +343,12 @@ public ConfigurationLockCollection LockAttributes
private readonly IDictionary<string, string> _rawAttributes;

public ConfigurationLockCollection LockElements
{
get
{
return _lockElements ??
(_lockElements = new ConfigurationLockCollection(this, ConfigurationLockType.Element));
}
}
=> _lockElements ??
(_lockElements = new ConfigurationLockCollection(this, ConfigurationLockType.Element));

internal string IsLocked
{
get
{
return _isLocked;
}
get => _isLocked;
set
{
_isLocked = value;
Expand Down Expand Up @@ -487,7 +465,7 @@ private void ParseAttribute(XAttribute attribute, string fileName)
case "configSource":
if (FileContext.AppHost)
{
throw new ArgumentException($"Unrecognized attribute '{name}'");
throw new ArgumentException($"Unrecognized attribute 'configSource'");
}

var directory = Path.GetDirectoryName(fileName);
Expand All @@ -505,8 +483,9 @@ private void ParseAttribute(XAttribute attribute, string fileName)

RawAttributes.Add(name, attribute.Value);
var child = Schema.AttributeSchemas[name];
if (child == null && !Schema.AllowUnrecognizedAttributes)
if (child == null && !Schema.AllowUnrecognizedAttributes && FileContext.Parent != null)
{
// IMPORTANT: ignore missing attributes in machine.config.
throw new ArgumentException($"Unrecognized attribute '{name}'");
}

Expand Down
10 changes: 5 additions & 5 deletions Microsoft.Web.Administration/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class FileContext : IEquatable<FileContext>
{
private readonly ServerManager _server;
private readonly object _locker = new object();
private readonly bool _dontThrow;
private readonly bool _doNotThrow;
internal List<SectionDefinition> DefinitionCache = new List<SectionDefinition>();

internal bool AppHost { get; }
Expand All @@ -28,10 +28,10 @@ internal sealed class FileContext : IEquatable<FileContext>

private ProtectedConfiguration _protectedConfiguration;

internal FileContext(ServerManager server, string fileName, FileContext parent, string location, bool appHost, bool dontThrow, bool readOnly, int lineNumber = 0)
internal FileContext(ServerManager server, string fileName, FileContext parent, string location, bool appHost, bool doNotThrow, bool readOnly, int lineNumber = 0)
{
_server = server;
_dontThrow = dontThrow;
_doNotThrow = doNotThrow;
AppHost = appHost;
ReadOnly = readOnly;
Locations = new List<Location>();
Expand Down Expand Up @@ -236,7 +236,7 @@ public ConfigurationSection GetSection(string sectionPath)
public ConfigurationSection GetSection(string sectionPath, string locationPath)
{
Initialize();
if (!_dontThrow)
if (!_doNotThrow)
{
_server.VerifyLocation(locationPath);
}
Expand Down Expand Up @@ -455,7 +455,7 @@ private bool ParseSections(XElement element, ConfigurationElement parent, Locati
result = result || childAdded;
}

if (!result && !_dontThrow && name != "location")
if (!result && !_doNotThrow && name != "location")
{
string link = null;
string oob = null;
Expand Down
19 changes: 19 additions & 0 deletions Microsoft.Web.Administration/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ internal static class Helper
{
internal static readonly string RootPath = "/";

public static readonly string FileNameMachineConfig = IsRunningOnMono()
? "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/4.5/machine.config"
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),
"Microsoft.NET",
IntPtr.Size == 2 ? "Framework" : "Framework64",
"v4.0.30319",
"CONFIG",
"machine.config");

public static readonly string FileNameWebConfig = IsRunningOnMono()
? "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/4.5/web.config"
: Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
"Microsoft.NET",
IntPtr.Size == 2 ? "Framework" : "Framework64",
"v4.0.30319",
"CONFIG",
"web.config");

public static string ExpandIisExpressEnvironmentVariables(this string path)
{
// TODO: IIS_BIN should check pool bitness.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
<ItemGroup>
<None Include="app.config" />
<None Include="Jexus.snk" />
<None Include="Microsoft.VersionNumber.targets" />
<None Include="Resources\original.config" />
</ItemGroup>
<ItemGroup>
Expand Down
25 changes: 4 additions & 21 deletions Microsoft.Web.Administration/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -93,40 +92,24 @@ private void Initialize()

Initialized = true;
PreInitialize();
var machineConfig = Helper.IsRunningOnMono()
? "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/4.5/machine.config"
: Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
"Microsoft.NET",
IntPtr.Size == 2 ? "Framework" : "Framework64",
"v4.0.30319",
"config",
"machine.config");

var machine =
new Configuration(
new FileContext(
this,
machineConfig,
Helper.FileNameMachineConfig,
null,
null,
false,
true,
true)
{ IgnoreSchemaCheck = true });
var webConfig = Helper.IsRunningOnMono()
? "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/4.5/web.config"
: Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
"Microsoft.NET",
IntPtr.Size == 2 ? "Framework" : "Framework64",
"v4.0.30319",
"config",
"web.config");

_web =
new Configuration(
new FileContext(
this,
webConfig,
Helper.FileNameWebConfig,
machine.FileContext,
null,
false,
Expand Down
20 changes: 20 additions & 0 deletions Tests.IIS/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Collections.Generic;

namespace Microsoft.Web.Administration
Expand Down Expand Up @@ -32,5 +33,24 @@ internal static void GetAllDefinitions(this SectionGroup group, IList<SectionDef
child.GetAllDefinitions(result);
}
}

public static readonly string FileNameMachineConfig = IsRunningOnMono()
? "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/4.5/machine.config"
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),
"Microsoft.NET",
IntPtr.Size == 2 ? "Framework" : "Framework64",
"v4.0.30319",
"CONFIG",
"machine.config");

public static readonly string FileNameWebConfig = IsRunningOnMono()
? "/Library/Frameworks/Mono.framework/Versions/Current/etc/mono/4.5/web.config"
: Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
"Microsoft.NET",
IntPtr.Size == 2 ? "Framework" : "Framework64",
"v4.0.30319",
"CONFIG",
"web.config");
}
}
Loading

0 comments on commit 2f1d085

Please sign in to comment.