BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Configuration / Test / System.Configuration / ConfigurationManagerTest.cs
1 //
2 // System.Configuration.ConfigurationManagerTest.cs - Unit tests
3 // for System.Configuration.ConfigurationManager.
4 //
5 // Author:
6 //      Chris Toshok  <toshok@ximian.com>
7 //      Atsushi Enomoto  <atsushi@ximian.com>
8 //
9 // Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Configuration;
35 using System.Net.Configuration;
36 using System.IO;
37 using NUnit.Framework;
38 using SysConfig = System.Configuration.Configuration;
39 using System.Runtime.InteropServices;
40
41 namespace MonoTests.System.Configuration {
42         [TestFixture]
43         public class ConfigurationManagerTest
44         {
45                 private string originalCurrentDir;
46                 private string tempFolder;
47
48                 [SetUp]
49                 public void SetUp ()
50                 {
51                         originalCurrentDir = Directory.GetCurrentDirectory ();
52                         tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
53                         if (!Directory.Exists (tempFolder))
54                                 Directory.CreateDirectory (tempFolder);
55                 }
56
57                 [TearDown]
58                 public void TearDown ()
59                 {
60                         Directory.SetCurrentDirectory (originalCurrentDir);
61                         if (Directory.Exists (tempFolder))
62                                 Directory.Delete (tempFolder, true);
63                 }
64
65                 [Test] // OpenExeConfiguration (ConfigurationUserLevel)
66                 [Category ("NotWorking")] // bug #323622
67                 public void OpenExeConfiguration1_Remote ()
68                 {
69                         AppDomain domain = null;
70                         string config_file;
71                         string config_xml = @"
72                                 <configuration>
73                                         <appSettings>
74                                                 <add key='anyKey' value='42' />
75                                         </appSettings>
76                                 </configuration>";
77
78                         config_file = Path.Combine (tempFolder, "otherConfig.noconfigext");
79                         File.WriteAllText (config_file, config_xml);
80
81                         try {
82                                 AppDomainSetup setup = new AppDomainSetup();
83                                 setup.ConfigurationFile = config_file;
84                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
85                                 domain = AppDomain.CreateDomain("foo", null, setup);
86
87                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
88
89                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
90                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
91                                 Assert.AreEqual ("42", config.GetSettingValue (userLevel, "anyKey"));
92                                 Assert.AreEqual ("42", config.GetSettingValue ("anyKey"));
93                         } finally {
94                                 if (domain != null)
95                                         AppDomain.Unload (domain);
96                                 File.Delete (config_file);
97                         }
98
99                         config_file = Path.Combine (tempFolder, "otherConfig.exe.config");
100                         File.WriteAllText (config_file, config_xml);
101
102                         try {
103                                 AppDomainSetup setup = new AppDomainSetup();
104                                 setup.ConfigurationFile = config_file;
105                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
106                                 domain = AppDomain.CreateDomain("foo", null, setup);
107
108                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
109
110                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
111                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
112                                 Assert.AreEqual ("42", config.GetSettingValue (userLevel, "anyKey"));
113                                 Assert.AreEqual ("42", config.GetSettingValue ("anyKey"));
114                         } finally {
115                                 if (domain != null)
116                                         AppDomain.Unload (domain);
117                                 File.Delete (config_file);
118                         }
119
120                         try {
121                                 AppDomainSetup setup = new AppDomainSetup();
122                                 setup.ConfigurationFile = config_file;
123                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
124                                 domain = AppDomain.CreateDomain("foo", null, setup);
125
126                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
127
128                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
129                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
130                                 Assert.IsNull (config.GetSettingValue (userLevel, "anyKey"));
131                                 Assert.IsNull (config.GetSettingValue ("anyKey"));
132                         } finally {
133                                 if (domain != null)
134                                         AppDomain.Unload (domain);
135                                 File.Delete (config_file);
136                         }
137                 }
138
139                 [Test] // OpenExeConfiguration (ConfigurationUserLevel)
140                 public void OpenExeConfiguration1_UserLevel_None ()
141                 {
142                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
143
144                         Console.WriteLine("application config path: {0}", config.FilePath);
145                         FileInfo fi = new FileInfo (config.FilePath);
146 #if TARGET_JVM
147                         Assert.AreEqual ("nunit-console.jar.config", fi.Name);
148 #elif NET_4_5
149                         Assert.AreEqual ("System.Configuration_test_net_4_5.dll.config", fi.Name);
150 #elif NET_4_0
151                         Assert.AreEqual ("System.Configuration_test_net_4_0.dll.config", fi.Name);
152 #else
153                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
154 #endif
155                 }
156
157                 [Test]
158                 public void OpenExeConfiguration1_UserLevel_PerUserRoaming ()
159                 {
160                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
161                         Console.WriteLine("roaming user config path: {0}", config.FilePath);
162
163                         FileInfo fi = new FileInfo (config.FilePath);
164                         Assert.AreEqual ("user.config", fi.Name);
165                 }
166
167                 [Test]
168                 public void OpenExeConfiguration1_UserLevel_PerUserRoamingAndLocal ()
169                 {
170                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
171                         Console.WriteLine("local user config path: {0}", config.FilePath);
172
173                         FileInfo fi = new FileInfo (config.FilePath);
174                         Assert.AreEqual ("user.config", fi.Name);
175                 }
176
177                 [Test] // OpenExeConfiguration (String)
178                 public void OpenExeConfiguration2 ()
179                 {
180                         String exePath;
181                         SysConfig config;
182
183                         exePath = Path.Combine (tempFolder, "DoesNotExist.whatever");
184                         File.Create (exePath).Close ();
185
186                         config = ConfigurationManager.OpenExeConfiguration (exePath);
187                         Assert.AreEqual (exePath + ".config", config.FilePath, "#1");
188
189                         exePath = Path.Combine (tempFolder, "SomeExecutable.exe");
190                         File.Create (exePath).Close ();
191
192                         config = ConfigurationManager.OpenExeConfiguration (exePath);
193                         Assert.AreEqual (exePath + ".config", config.FilePath, "#2");
194
195                         exePath = Path.Combine (tempFolder, "Foo.exe.config");
196                         File.Create (exePath).Close ();
197
198                         config = ConfigurationManager.OpenExeConfiguration (exePath);
199                         Assert.AreEqual (exePath + ".config", config.FilePath, "#3");
200
201                         Directory.SetCurrentDirectory (tempFolder);
202
203                         exePath = "relative.exe";
204                         File.Create (Path.Combine (tempFolder, exePath)).Close ();
205
206                         config = ConfigurationManager.OpenExeConfiguration (exePath);
207                         Assert.AreEqual (Path.Combine (tempFolder, exePath + ".config"), config.FilePath, "#4");
208                 }
209
210                 [Test] // OpenExeConfiguration (String)
211                 public void OpenExeConfiguration2_ExePath_Empty ()
212                 {
213                         AppDomain domain = AppDomain.CurrentDomain;
214
215                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
216                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
217                 }
218
219                 [Test] // OpenExeConfiguration (String)
220                 public void OpenExeConfiguration2_ExePath_Null ()
221                 {
222                         AppDomain domain = AppDomain.CurrentDomain;
223
224                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
225                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
226                 }
227
228                 [Test] // OpenExeConfiguration (String)
229                 public void OpenExeConfiguration2_ExePath_DoesNotExist ()
230                 {
231                         String exePath = Path.Combine (tempFolder, "DoesNotExist.exe");
232
233                         try {
234                                 ConfigurationManager.OpenExeConfiguration (exePath);
235                                 Assert.Fail ("#1");
236                         } catch (ConfigurationErrorsException ex) {
237                                 // An error occurred loading a configuration file:
238                                 // The parameter 'exePath' is invalid
239                                 Assert.AreEqual (typeof (ConfigurationErrorsException), ex.GetType (), "#2");
240                                 Assert.IsNull (ex.Filename, "#3");
241                                 Assert.IsNotNull (ex.InnerException, "#4");
242                                 Assert.AreEqual (0, ex.Line, "#5");
243                                 Assert.IsNotNull (ex.Message, "#6");
244
245                                 // The parameter 'exePath' is invalid
246                                 ArgumentException inner = ex.InnerException as ArgumentException;
247                                 Assert.IsNotNull (inner, "#7");
248                                 Assert.AreEqual (typeof (ArgumentException), inner.GetType (), "#8");
249                                 Assert.IsNull (inner.InnerException, "#9");
250                                 Assert.IsNotNull (inner.Message, "#10");
251                                 Assert.AreEqual ("exePath", inner.ParamName, "#11");
252                         }
253                 }
254
255                 [Test]
256                 public void exePath_UserLevelNone ()
257                 {
258                         string basedir = AppDomain.CurrentDomain.BaseDirectory;
259                         SysConfig config = ConfigurationManager.OpenExeConfiguration("System.Configuration_test_net_2_0.dll.mdb");
260                         Assert.AreEqual (Path.Combine (basedir, "System.Configuration_test_net_2_0.dll.mdb.config"), config.FilePath);
261                 }
262
263                 [Test]
264                 public void exePath_UserLevelPerRoaming ()
265                 {
266                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
267                         string filePath = config.FilePath;
268                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
269                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
270                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
271                 }
272
273                 [Test]
274                 public void exePath_UserLevelPerRoamingAndLocal ()
275                 {
276                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
277                         string filePath = config.FilePath;
278                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
279                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
280                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
281                 }
282
283                 [Test]
284                 public void mapped_UserLevelNone ()
285                 {
286                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
287                         map.ExeConfigFilename = "execonfig";
288
289                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
290                         Console.WriteLine("mapped application config path: {0}", config.FilePath);      
291
292                         FileInfo fi = new FileInfo (config.FilePath);
293                         Assert.AreEqual ("execonfig", fi.Name);
294
295                 }
296
297                 [Test]
298                 public void mapped_UserLevelPerRoaming ()
299                 {
300                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
301                         map.ExeConfigFilename = "execonfig";
302                         map.RoamingUserConfigFilename = "roaminguser";
303
304                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
305                         Console.WriteLine("mapped roaming user config path: {0}", config.FilePath);     
306
307                         FileInfo fi = new FileInfo (config.FilePath);
308                         Assert.AreEqual ("roaminguser", fi.Name);
309                 }
310
311                 [Test]
312                 [ExpectedException (typeof (ArgumentException))]
313                 [Category ("NotWorking")]
314                 public void mapped_UserLevelPerRoaming_no_execonfig ()
315                 {
316                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
317                         map.RoamingUserConfigFilename = "roaminguser";
318
319                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoaming);
320                 }
321
322                 [Test]
323                 public void mapped_UserLevelPerRoamingAndLocal ()
324                 {
325                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
326                         map.ExeConfigFilename = "execonfig";
327                         map.RoamingUserConfigFilename = "roaminguser";
328                         map.LocalUserConfigFilename = "localuser";
329
330                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
331                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
332
333                         FileInfo fi = new FileInfo (config.FilePath);
334                         Assert.AreEqual ("localuser", fi.Name);
335                 }
336
337                 [Test]
338                 [ExpectedException (typeof (ArgumentException))]
339                 [Category ("NotWorking")]
340                 public void mapped_UserLevelPerRoamingAndLocal_no_execonfig ()
341                 {
342                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
343                         map.RoamingUserConfigFilename = "roaminguser";
344                         map.LocalUserConfigFilename = "localuser";
345
346                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
347                 }
348
349                 [Test]
350                 [ExpectedException (typeof (ArgumentException))]
351                 [Category ("NotWorking")]
352                 public void mapped_UserLevelPerRoamingAndLocal_no_roaminguser ()
353                 {
354                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
355                         map.ExeConfigFilename = "execonfig";
356                         map.LocalUserConfigFilename = "localuser";
357
358                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
359                 }
360
361                 [Test]
362                 public void MachineConfig ()
363                 {
364                         SysConfig config = ConfigurationManager.OpenMachineConfiguration ();
365                         Console.WriteLine("machine config path: {0}", config.FilePath);
366
367                         FileInfo fi = new FileInfo (config.FilePath);
368                         Assert.AreEqual ("machine.config", fi.Name);
369                 }
370
371                 [Test]
372                 public void mapped_MachineConfig ()
373                 {
374                         ConfigurationFileMap map = new ConfigurationFileMap ();
375                         map.MachineConfigFilename = "machineconfig";
376
377                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (map);
378                         Console.WriteLine("mapped machine config path: {0}", config.FilePath);
379
380                         FileInfo fi = new FileInfo (config.FilePath);
381                         Assert.AreEqual ("machineconfig", fi.Name);
382                 }
383
384                 [Test]
385                 public void exePath_UserLevelNone_null ()
386                 {
387                         SysConfig config = ConfigurationManager.OpenExeConfiguration (null);
388 #if false
389                         Console.WriteLine("null exe application config path: {0}", config.FilePath);    
390
391                         FileInfo fi = new FileInfo (config.FilePath);
392                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
393 #endif
394                 }
395
396                 [Test]
397                 [Category ("NotWorking")]
398                 public void mapped_ExeConfiguration_null ()
399                 {
400                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(null, ConfigurationUserLevel.None);
401                         Console.WriteLine("null mapped application config path: {0}", config.FilePath); 
402
403                         FileInfo fi = new FileInfo (config.FilePath);
404 #if TARGET_JVM
405                         Assert.AreEqual("System.Configuration.Test20.jar.config", fi.Name);
406 #else
407                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
408 #endif
409                 }
410
411                 [Test]
412                 [Category ("NotWorking")]
413                 public void mapped_MachineConfig_null ()
414                 {
415                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (null);
416                         Console.WriteLine("null mapped machine config path: {0}", config.FilePath);
417
418                         FileInfo fi = new FileInfo (config.FilePath);
419                         Assert.AreEqual ("machine.config", fi.Name);
420                 }
421
422                 [Test]
423                 public void GetSectionReturnsNativeObject ()
424                 {
425                         Assert.IsTrue (ConfigurationManager.GetSection ("appSettings") is NameValueCollection);
426                 }
427
428                 [Test] // test for bug #78372.
429                 public void OpenMachineConfiguration ()
430                 {
431                         SysConfig cfg = ConfigurationManager.OpenMachineConfiguration ();
432                         Assert.IsTrue (cfg.Sections.Count > 0, "#1");
433 #if !TARGET_JVM
434                         ConfigurationSection s = cfg.SectionGroups ["system.net"].Sections ["connectionManagement"];
435                         Assert.IsNotNull (s, "#2");
436                         Assert.IsTrue (s is ConnectionManagementSection, "#3");
437 #endif
438                 }
439
440                 [Test]
441                 public void SectionCollectionEnumerator ()
442                 {
443                         SysConfig c = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
444                         ConfigurationSectionCollection col =
445                                 c.GetSectionGroup ("system.web").Sections;
446                         IEnumerator e = col.GetEnumerator ();
447                         e.MoveNext ();
448                         Assert.IsTrue (e.Current is ConfigurationSection);
449                 }
450
451                 [Test]  // Test for bug #3412
452                 [Category("NotWorking")]
453                 public void TestAddRemoveSection()
454                 {
455                         const string name = "testsection";
456                         var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
457
458                         // ensure not present
459                         if (config.Sections.Get(name) != null)
460                         {
461                                 config.Sections.Remove(name);
462                         }
463
464                         // add
465                         config.Sections.Add(name, new TestSection());
466
467                         // remove
468                         var section = config.Sections.Get(name);
469                         Assert.IsNotNull(section);
470                         Assert.IsNotNull(section as TestSection);
471                         config.Sections.Remove(name);
472
473                         // add
474                         config.Sections.Add(name, new TestSection());
475
476                         // remove
477                         section = config.Sections.Get(name);
478                         Assert.IsNotNull(section);
479                         Assert.IsNotNull(section as TestSection);
480                         config.Sections.Remove(name);
481                 }
482                         
483                 class TestSection : ConfigurationSection  {}
484
485                 class RemoteConfig : MarshalByRefObject
486                 {
487                         public static RemoteConfig GetInstance (AppDomain domain)
488                         {
489                                 RemoteConfig config = (RemoteConfig) domain.CreateInstanceAndUnwrap (
490                                         typeof (RemoteConfig).Assembly.FullName,
491                                         typeof (RemoteConfig).FullName, new object [0]);
492                                 return config;
493                         }
494
495                         public string GetFilePath (string exePath)
496                         {
497                                 global::System.Configuration.Configuration config =
498                                         ConfigurationManager.OpenExeConfiguration (exePath);
499                                 return config.FilePath;
500                         }
501
502                         public string GetFilePath (ConfigurationUserLevel userLevel)
503                         {
504                                 global::System.Configuration.Configuration config =
505                                         ConfigurationManager.OpenExeConfiguration (userLevel);
506                                 return config.FilePath;
507                         }
508
509                         public string GetSettingValue (string exePath, string key)
510                         {
511                                 global::System.Configuration.Configuration config =
512                                         ConfigurationManager.OpenExeConfiguration (exePath);
513                                 return config.AppSettings.Settings [key].Value;
514                         }
515
516                         public string GetSettingValue (ConfigurationUserLevel userLevel, string key)
517                         {
518                                 global::System.Configuration.Configuration config =
519                                         ConfigurationManager.OpenExeConfiguration (userLevel);
520                                 KeyValueConfigurationElement value = config.AppSettings.Settings [key];
521                                 return value != null ? value.Value : null;
522                         }
523
524                         public string GetSettingValue (string key)
525                         {
526                                 return ConfigurationManager.AppSettings [key];
527                         }
528                 }
529         }
530 }