Added tests for Task.WhenAll w/ empty list
[mono.git] / mcs / class / System.Configuration / System.Configuration / ProtectedConfigurationSection.cs
1 //
2 // System.Configuration.ProtectedConfigurationSection.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Chris Toshok (toshok@ximian.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
28 //
29
30 #if NET_2_0
31
32 using System.IO;
33 using System.Xml;
34
35 namespace System.Configuration
36 {
37         public sealed class ProtectedConfigurationSection: ConfigurationSection
38         {
39                 static ConfigurationProperty defaultProviderProp;
40                 static ConfigurationProperty providersProp;
41                 static ConfigurationPropertyCollection properties;
42
43                 ProtectedConfigurationProviderCollection providers;
44
45                 static ProtectedConfigurationSection ()
46                 {
47                         defaultProviderProp = new ConfigurationProperty ("defaultProvider", typeof (string), "RsaProtectedConfigurationProvider");
48                         providersProp = new ConfigurationProperty ("providers", typeof (ProviderSettingsCollection), null);
49
50                         properties = new ConfigurationPropertyCollection();
51                         properties.Add (defaultProviderProp);
52                         properties.Add (providersProp);
53                 }
54
55                 [ConfigurationProperty ("defaultProvider", DefaultValue="RsaProtectedConfigurationProvider")]
56                 public string DefaultProvider {
57                         get { return (string)base[defaultProviderProp]; }
58                         set { base[defaultProviderProp] = value; }
59                 }
60
61                 [ConfigurationProperty ("providers")] 
62                 public ProviderSettingsCollection Providers {
63                         get { return (ProviderSettingsCollection)base[providersProp]; }
64                 }
65
66                 protected internal override ConfigurationPropertyCollection Properties {
67                         get { return properties; }
68                 }
69
70                 internal string EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider)
71                 {
72                         XmlDocument doc = new ConfigurationXmlDocument ();
73                         doc.LoadXml (clearXml);
74
75                         XmlNode encryptedNode = protectionProvider.Encrypt (doc.DocumentElement);
76
77                         return encryptedNode.OuterXml;
78                 }
79
80                 internal string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider)
81                 {
82                         XmlDocument doc = new ConfigurationXmlDocument ();
83                         doc.InnerXml = encryptedXml;
84
85                         XmlNode decryptedNode = protectionProvider.Decrypt (doc.DocumentElement);
86
87                         return decryptedNode.OuterXml;
88                 }
89
90                 internal ProtectedConfigurationProviderCollection GetAllProviders ()
91                 {
92                         if (providers == null) {
93                                 providers = new ProtectedConfigurationProviderCollection ();
94
95                                 foreach (ProviderSettings ps in Providers) {
96                                         providers.Add (InstantiateProvider (ps));
97                                 }
98                         }
99
100                         return providers;
101                 }
102
103                 ProtectedConfigurationProvider InstantiateProvider (ProviderSettings ps)
104                 {
105                         Type t = Type.GetType (ps.Type, true);
106                         ProtectedConfigurationProvider prov = Activator.CreateInstance (t) as ProtectedConfigurationProvider;
107                         if (prov == null)
108                                 throw new Exception ("The type specified does not extend ProtectedConfigurationProvider class.");
109
110                         prov.Initialize (ps.Name, ps.Parameters);
111
112                         return prov;
113                 }
114         }
115 }
116 #endif