public void RepeatedElementDetectionIsCaseInsensitive() { var xml = @"<settings> <DefaultConnection> <ConnectionString>TestConnectionString1</ConnectionString> <Provider>SqlClient1</Provider> </DefaultConnection> <defaultconnection> <ConnectionString>TestConnectionString2</ConnectionString> <Provider>SqlClient2</Provider> </defaultconnection> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("TestConnectionString1", xmlConfigSrc.Get("DefaultConnection:0:ConnectionString")); Assert.Equal("SqlClient1", xmlConfigSrc.Get("DefaultConnection:0:Provider")); Assert.Equal("TestConnectionString2", xmlConfigSrc.Get("DefaultConnection:1:ConnectionString")); Assert.Equal("SqlClient2", xmlConfigSrc.Get("DefaultConnection:1:Provider")); }
public void RepeatedElementsUnderNameContributeToPrefix() { var xml = @"<settings Name='Data'> <DefaultConnection> <ConnectionString>TestConnectionString1</ConnectionString> <Provider>SqlClient1</Provider> </DefaultConnection> <DefaultConnection> <ConnectionString>TestConnectionString2</ConnectionString> <Provider>SqlClient2</Provider> </DefaultConnection> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("TestConnectionString1", xmlConfigSrc.Get("Data:DefaultConnection:0:ConnectionString")); Assert.Equal("SqlClient1", xmlConfigSrc.Get("Data:DefaultConnection:0:Provider")); Assert.Equal("TestConnectionString2", xmlConfigSrc.Get("Data:DefaultConnection:1:ConnectionString")); Assert.Equal("SqlClient2", xmlConfigSrc.Get("Data:DefaultConnection:1:Provider")); }
public void LoadValidXmlFromStreamProvider() { var xml = @" <settings> <Data.Setting> <DefaultConnection> <Connection.String>Test.Connection.String</Connection.String> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Inventory> </Data.Setting> </settings>"; var config = new ConfigurationBuilder().AddXmlStream(TestStreamHelpers.StringToStream(xml)).Build(); Assert.Equal("Test.Connection.String", config["DATA.SETTING:DEFAULTCONNECTION:CONNECTION.STRING"]); Assert.Equal("SqlClient", config["DATA.SETTING:DefaultConnection:Provider"]); Assert.Equal("AnotherTestConnectionString", config["data.setting:inventory:connectionstring"]); Assert.Equal("MySql", config["Data.setting:Inventory:Provider"]); }
public void NameAttributeCanBeUsedToSimulateArrays() { var xml = @"<settings> <DefaultConnection Name='0'> <ConnectionString>TestConnectionString1</ConnectionString> <Provider>SqlClient1</Provider> </DefaultConnection> <DefaultConnection Name='1'> <ConnectionString>TestConnectionString2</ConnectionString> <Provider>SqlClient2</Provider> </DefaultConnection> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("TestConnectionString1", xmlConfigSrc.Get("DefaultConnection:0:ConnectionString")); Assert.Equal("SqlClient1", xmlConfigSrc.Get("DefaultConnection:0:Provider")); Assert.Equal("TestConnectionString2", xmlConfigSrc.Get("DefaultConnection:1:ConnectionString")); Assert.Equal("SqlClient2", xmlConfigSrc.Get("DefaultConnection:1:Provider")); }
public void ThrowExceptionWhenFindNamespace() { var xml = @"<settings xmlns:MyNameSpace='http://microsoft.com/wwa/mynamespace'> <MyNameSpace:Data> <DefaultConnection> <ConnectionString>TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Inventory> </MyNameSpace:Data> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); var expectedMsg = SR.Format(SR.Error_NamespaceIsNotSupported, SR.Format(SR.Msg_LineInfo, 1, 11)); var exception = Assert.Throws <FormatException>(() => xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml))); Assert.Equal(expectedMsg, exception.Message); }
public void ThrowExceptionWhenKeyIsDuplicatedWithDifferentCasing() { var xml = @"<settings> <Data> <DefaultConnection> <ConnectionString>TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </DefaultConnection> </Data> <data name='defaultconnection' connectionstring='NewConnectionString'> <provider>NewProvider</provider> </data> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); var expectedMsg = SR.Format(SR.Error_KeyIsDuplicated, "data:defaultconnection:connectionstring", SR.Format(SR.Msg_LineInfo, 8, 52)); var exception = Assert.Throws <FormatException>(() => xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml))); Assert.Equal(expectedMsg, exception.Message); }
public void SupportMixingNameAttributesAndCommonAttributes() { var xml = @"<settings> <Data Name='DefaultConnection' ConnectionString='TestConnectionString' Provider='SqlClient' /> <Data Name='Inventory' ConnectionString='AnotherTestConnectionString'> <Provider>MySql</Provider> </Data> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("DefaultConnection", xmlConfigSrc.Get("Data:DefaultConnection:Name")); Assert.Equal("TestConnectionString", xmlConfigSrc.Get("Data:DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", xmlConfigSrc.Get("Data:DefaultConnection:Provider")); Assert.Equal("Inventory", xmlConfigSrc.Get("Data:Inventory:Name")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", xmlConfigSrc.Get("Data:Inventory:Provider")); }
public void NestedRepeatedElementsContributeToPrefix() { var xml = @"<settings> <DefaultConnection> <ConnectionString>TestConnectionString1</ConnectionString> <ConnectionString>TestConnectionString2</ConnectionString> </DefaultConnection> <DefaultConnection> <ConnectionString>TestConnectionString3</ConnectionString> <ConnectionString>TestConnectionString4</ConnectionString> </DefaultConnection> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("TestConnectionString1", xmlConfigSrc.Get("DefaultConnection:0:ConnectionString:0")); Assert.Equal("TestConnectionString2", xmlConfigSrc.Get("DefaultConnection:0:ConnectionString:1")); Assert.Equal("TestConnectionString3", xmlConfigSrc.Get("DefaultConnection:1:ConnectionString:0")); Assert.Equal("TestConnectionString4", xmlConfigSrc.Get("DefaultConnection:1:ConnectionString:1")); }
public void SupportAndIgnoreComments() { var ini = @" ; Comments [DefaultConnection] # Comments ConnectionString=TestConnectionString / Comments Provider=SqlClient [Data:Inventory] ConnectionString=AnotherTestConnectionString Provider=MySql "; var iniConfigSrc = new IniConfigurationProvider(new IniConfigurationSource()); iniConfigSrc.Load(TestStreamHelpers.StringToStream(ini)); Assert.Equal("TestConnectionString", iniConfigSrc.Get("DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", iniConfigSrc.Get("DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", iniConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", iniConfigSrc.Get("Data:Inventory:Provider")); }
public void ThrowExceptionWhenKeyIsDuplicated() { var xml = @"<settings> <Data> <DefaultConnection> <ConnectionString>TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </DefaultConnection> </Data> <Data Name='DefaultConnection' ConnectionString='NewConnectionString'> <Provider>NewProvider</Provider> </Data> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(ArbitraryFilePath); var expectedMsg = Resources.FormatError_KeyIsDuplicated("Data:DefaultConnection:ConnectionString", Resources.FormatMsg_LineInfo(8, 52)); var exception = Assert.Throws <FormatException>(() => xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml))); Assert.Equal(expectedMsg, exception.Message); }
public void NameAttributeInRootElementContributesToPrefix() { var xml = @"<settings Name='Data'> <DefaultConnection> <ConnectionString>TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Inventory> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(ArbitraryFilePath); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("TestConnectionString", xmlConfigSrc.Get("Data:DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", xmlConfigSrc.Get("Data:DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", xmlConfigSrc.Get("Data:Inventory:Provider")); }
public void SupportMixingChildElementsAndAttributes() { var xml = @"<settings Port='8008'> <Data> <DefaultConnection Provider='SqlClient'> <ConnectionString>TestConnectionString</ConnectionString> </DefaultConnection> <Inventory ConnectionString='AnotherTestConnectionString'> <Provider>MySql</Provider> </Inventory> </Data> </settings>"; var xmlConfigSrc = new XmlConfigurationSource(ArbitraryFilePath); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("8008", xmlConfigSrc.Get("Port")); Assert.Equal("TestConnectionString", xmlConfigSrc.Get("Data:DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", xmlConfigSrc.Get("Data:DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", xmlConfigSrc.Get("Data:Inventory:Provider")); }
public void CommonAttributesContributeToKeyValuePairs() { var xml = @"<settings Port=""8008""> <Data> <DefaultConnection ConnectionString=""TestConnectionString"" Provider=""SqlClient""/> <Inventory ConnectionString=""AnotherTestConnectionString"" Provider=""MySql""/> </Data> </settings>"; var xmlConfigSrc = new XmlConfigurationSource(ArbitraryFilePath); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("8008", xmlConfigSrc.Get("Port")); Assert.Equal("TestConnectionString", xmlConfigSrc.Get("Data:DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", xmlConfigSrc.Get("Data:DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", xmlConfigSrc.Get("Data:Inventory:Provider")); }
public void ArrayMerge() { var json1 = @"{ 'ip': [ '1.2.3.4', '7.8.9.10', '11.12.13.14' ] }"; var json2 = @"{ 'ip': { '3': '15.16.17.18' } }"; var jsonConfigSource1 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1)); var jsonConfigSource2 = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath); jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2)); var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.Add(jsonConfigSource1, load: false); configurationBuilder.Add(jsonConfigSource2, load: false); var config = configurationBuilder.Build(); Assert.Equal(4, config.GetSection("ip").GetChildren().Count()); Assert.Equal("1.2.3.4", config["ip:0"]); Assert.Equal("7.8.9.10", config["ip:1"]); Assert.Equal("11.12.13.14", config["ip:2"]); Assert.Equal("15.16.17.18", config["ip:3"]); }
public void SupportAndIgnoreComments() { var xml = @"<!-- Comments --> <settings> <Data> <!-- Comments --> <DefaultConnection> <ConnectionString><!-- Comments -->TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Inventory> </Data> </settings><!-- Comments -->"; var xmlConfigSrc = new XmlConfigurationSource(ArbitraryFilePath); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("TestConnectionString", xmlConfigSrc.Get("Data:DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", xmlConfigSrc.Get("Data:DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", xmlConfigSrc.Get("Data:Inventory:Provider")); }
public void NestedArrays() { var json = @"{ 'ip': [ [ '1.2.3.4', '5.6.7.8' ], [ '9.10.11.12', '13.14.15.16' ], ] }"; var jsonConfigSource = new NewtonsoftJsonConfigurationProvider(new NewtonsoftJsonConfigurationSource()); jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0")); Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1")); Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0")); Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1")); }
public void LoadKeyValuePairsFromValidXml() { var xml = @" <settings> <Data.Setting> <DefaultConnection> <Connection.String>Test.Connection.String</Connection.String> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Inventory> </Data.Setting> </settings>"; var xmlConfigSrc = new XmlConfigurationSource(ArbitraryFilePath); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("Test.Connection.String", xmlConfigSrc.Get("DATA.SETTING:DEFAULTCONNECTION:CONNECTION.STRING")); Assert.Equal("SqlClient", xmlConfigSrc.Get("DATA.SETTING:DefaultConnection:Provider")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("data.setting:inventory:connectionstring")); Assert.Equal("MySql", xmlConfigSrc.Get("Data.setting:Inventory:Provider")); }
public void ArrayOfObjects() { var json = @"{ 'ip': [ { 'address': '1.2.3.4', 'hidden': false }, { 'address': '5.6.7.8', 'hidden': true } ] }"; var jsonConfigSource = new NewtonsoftJsonConfigurationProvider(new NewtonsoftJsonConfigurationSource()); jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address")); Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden")); Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address")); Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden")); }
public void NameAttributeContributesToPrefix() { var xml = @"<settings> <Data Name='DefaultConnection'> <ConnectionString>TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </Data> <Data Name='Inventory'> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Data> </settings>"; var xmlConfigSrc = new XmlConfigurationProvider(new XmlConfigurationSource()); xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml)); Assert.Equal("DefaultConnection", xmlConfigSrc.Get("Data:DefaultConnection:Name")); Assert.Equal("TestConnectionString", xmlConfigSrc.Get("Data:DefaultConnection:ConnectionString")); Assert.Equal("SqlClient", xmlConfigSrc.Get("Data:DefaultConnection:Provider")); Assert.Equal("Inventory", xmlConfigSrc.Get("Data:Inventory:Name")); Assert.Equal("AnotherTestConnectionString", xmlConfigSrc.Get("Data:Inventory:ConnectionString")); Assert.Equal("MySql", xmlConfigSrc.Get("Data:Inventory:Provider")); }
public void NestedArrays() { var json = @"{ ""ip"": [ [ ""1.2.3.4"", ""5.6.7.8"" ], [ ""9.10.11.12"", ""13.14.15.16"" ] ] }"; var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource()); jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0")); Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1")); Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0")); Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1")); }
public void ArrayOfObjects() { var json = @"{ ""ip"": [ { ""address"": ""1.2.3.4"", ""hidden"": false }, { ""address"": ""5.6.7.8"", ""hidden"": true } ] }"; var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource()); jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address")); Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden")); Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address")); Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden")); }
public void ThrowExceptionWhenFindDTD() { var xml = @"<!DOCTYPE DefaultConnection[ <!ELEMENT DefaultConnection (ConnectionString,Provider)> <!ELEMENT ConnectionString (#PCDATA)> <!ELEMENT Provider (#PCDATA)> ]> <settings> <Data> <DefaultConnection> <ConnectionString>TestConnectionString</ConnectionString> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <ConnectionString>AnotherTestConnectionString</ConnectionString> <Provider>MySql</Provider> </Inventory> </Data> </settings>"; var xmlConfigSrc = new XmlConfigurationSource(ArbitraryFilePath); var isMono = Type.GetType("Mono.Runtime") != null; var expectedMsg = isMono ? "Document Type Declaration (DTD) is prohibited in this XML. Line 1, position 10." : "For security reasons DTD is prohibited in this XML document. " + "To enable DTD processing set the DtdProcessing property on XmlReaderSettings " + "to Parse and pass the settings into XmlReader.Create method."; var exception = Assert.Throws <System.Xml.XmlException>(() => xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml))); Assert.Equal(expectedMsg, exception.Message); }
public void LoadKeyValuePairsFromValidEncryptedXml_ThrowsPlatformNotSupported() { var xml = @" <settings> <Data.Setting> <DefaultConnection> <Connection.String>Test.Connection.String</Connection.String> <Provider>SqlClient</Provider> </DefaultConnection> <Inventory> <EncryptedData Type=""http://www.w3.org/2001/04/xmlenc#Element"" xmlns=""http://www.w3.org/2001/04/xmlenc#""> <EncryptionMethod Algorithm=""http://www.w3.org/2001/04/xmlenc#aes256-cbc"" /> <KeyInfo xmlns=""http://www.w3.org/2000/09/xmldsig#""> <EncryptedKey xmlns=""http://www.w3.org/2001/04/xmlenc#""> <EncryptionMethod Algorithm=""http://www.w3.org/2001/04/xmlenc#kw-aes256"" /> <KeyInfo xmlns=""http://www.w3.org/2000/09/xmldsig#""> <KeyName>myKey</KeyName> </KeyInfo> <CipherData> <CipherValue>b0dxJI/o00vZgTNOJ6wUt0/6wCKWlQANAYE8cBsEzok4LQma7ErEnA==</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>iXzecb+Cha80LLrl4zON3o7HfpRc0NxlJsnBb6zbKFa1HqtNhy2VrTnrEsZUViBWRkRbl+MCix7TiaIs4NtLijNU5Ob8Ez3vcD4T/QcmPywBYJDJhj1OUUeJSKH+icjg</CipherValue> </CipherData> </EncryptedData> <Provider>MySql</Provider> </Inventory> </Data.Setting> </settings>"; // Arrange var xmlConfigSrc = new XmlConfigurationProvider(ArbitraryFilePath); // Act & assert var ex = Assert.Throws <PlatformNotSupportedException>(() => xmlConfigSrc.Load(TestStreamHelpers.StringToStream(xml))); Assert.Equal(Resources.Error_EncryptedXmlNotSupported, ex.Message); }