do not check order sequence if option /order was not used
[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                         //
207                         // The temp directory as computed by the runtime is slightly different, as
208                         // it will contain the full path after following links, while the test
209                         // below is not comprehensive enough to follow links if there are any
210                         // present in tempFolder
211                         //
212                         
213                         //config = ConfigurationManager.OpenExeConfiguration (exePath);
214                         //Assert.AreEqual (Path.Combine (tempFolder, exePath + ".config"), config.FilePath, "#4");
215                 }
216
217                 [Test] // OpenExeConfiguration (String)
218                 public void OpenExeConfiguration2_ExePath_Empty ()
219                 {
220                         AppDomain domain = AppDomain.CurrentDomain;
221
222                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
223                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
224                 }
225
226                 [Test] // OpenExeConfiguration (String)
227                 public void OpenExeConfiguration2_ExePath_Null ()
228                 {
229                         AppDomain domain = AppDomain.CurrentDomain;
230
231                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
232                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
233                 }
234
235                 [Test] // OpenExeConfiguration (String)
236                 public void OpenExeConfiguration2_ExePath_DoesNotExist ()
237                 {
238                         String exePath = Path.Combine (tempFolder, "DoesNotExist.exe");
239
240                         try {
241                                 ConfigurationManager.OpenExeConfiguration (exePath);
242                                 Assert.Fail ("#1");
243                         } catch (ConfigurationErrorsException ex) {
244                                 // An error occurred loading a configuration file:
245                                 // The parameter 'exePath' is invalid
246                                 Assert.AreEqual (typeof (ConfigurationErrorsException), ex.GetType (), "#2");
247                                 Assert.IsNull (ex.Filename, "#3");
248                                 Assert.IsNotNull (ex.InnerException, "#4");
249                                 Assert.AreEqual (0, ex.Line, "#5");
250                                 Assert.IsNotNull (ex.Message, "#6");
251
252                                 // The parameter 'exePath' is invalid
253                                 ArgumentException inner = ex.InnerException as ArgumentException;
254                                 Assert.IsNotNull (inner, "#7");
255                                 Assert.AreEqual (typeof (ArgumentException), inner.GetType (), "#8");
256                                 Assert.IsNull (inner.InnerException, "#9");
257                                 Assert.IsNotNull (inner.Message, "#10");
258                                 Assert.AreEqual ("exePath", inner.ParamName, "#11");
259                         }
260                 }
261
262                 [Test]
263                 public void exePath_UserLevelNone ()
264                 {
265                         string basedir = AppDomain.CurrentDomain.BaseDirectory;
266                         SysConfig config = ConfigurationManager.OpenExeConfiguration("System.Configuration_test_net_2_0.dll.mdb");
267                         Assert.AreEqual (Path.Combine (basedir, "System.Configuration_test_net_2_0.dll.mdb.config"), config.FilePath);
268                 }
269
270                 [Test]
271                 public void exePath_UserLevelPerRoaming ()
272                 {
273                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
274                         string filePath = config.FilePath;
275                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
276                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
277                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
278                 }
279
280                 [Test]
281                 public void exePath_UserLevelPerRoamingAndLocal ()
282                 {
283                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
284                         string filePath = config.FilePath;
285                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
286                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
287                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
288                 }
289
290                 [Test]
291                 public void mapped_UserLevelNone ()
292                 {
293                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
294                         map.ExeConfigFilename = "execonfig";
295
296                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
297                         Console.WriteLine("mapped application config path: {0}", config.FilePath);      
298
299                         FileInfo fi = new FileInfo (config.FilePath);
300                         Assert.AreEqual ("execonfig", fi.Name);
301
302                 }
303
304                 [Test]
305                 public void mapped_UserLevelPerRoaming ()
306                 {
307                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
308                         map.ExeConfigFilename = "execonfig";
309                         map.RoamingUserConfigFilename = "roaminguser";
310
311                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
312                         Console.WriteLine("mapped roaming user config path: {0}", config.FilePath);     
313
314                         FileInfo fi = new FileInfo (config.FilePath);
315                         Assert.AreEqual ("roaminguser", fi.Name);
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentException))]
320                 [Category ("NotWorking")]
321                 public void mapped_UserLevelPerRoaming_no_execonfig ()
322                 {
323                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
324                         map.RoamingUserConfigFilename = "roaminguser";
325
326                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoaming);
327                 }
328
329                 [Test]
330                 public void mapped_UserLevelPerRoamingAndLocal ()
331                 {
332                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
333                         map.ExeConfigFilename = "execonfig";
334                         map.RoamingUserConfigFilename = "roaminguser";
335                         map.LocalUserConfigFilename = "localuser";
336
337                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
338                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
339
340                         FileInfo fi = new FileInfo (config.FilePath);
341                         Assert.AreEqual ("localuser", fi.Name);
342                 }
343
344                 [Test]
345                 [ExpectedException (typeof (ArgumentException))]
346                 [Category ("NotWorking")]
347                 public void mapped_UserLevelPerRoamingAndLocal_no_execonfig ()
348                 {
349                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
350                         map.RoamingUserConfigFilename = "roaminguser";
351                         map.LocalUserConfigFilename = "localuser";
352
353                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
354                 }
355
356                 [Test]
357                 [ExpectedException (typeof (ArgumentException))]
358                 [Category ("NotWorking")]
359                 public void mapped_UserLevelPerRoamingAndLocal_no_roaminguser ()
360                 {
361                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
362                         map.ExeConfigFilename = "execonfig";
363                         map.LocalUserConfigFilename = "localuser";
364
365                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
366                 }
367
368                 [Test]
369                 public void MachineConfig ()
370                 {
371                         SysConfig config = ConfigurationManager.OpenMachineConfiguration ();
372                         Console.WriteLine("machine config path: {0}", config.FilePath);
373
374                         FileInfo fi = new FileInfo (config.FilePath);
375                         Assert.AreEqual ("machine.config", fi.Name);
376                 }
377
378                 [Test]
379                 public void mapped_MachineConfig ()
380                 {
381                         ConfigurationFileMap map = new ConfigurationFileMap ();
382                         map.MachineConfigFilename = "machineconfig";
383
384                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (map);
385                         Console.WriteLine("mapped machine config path: {0}", config.FilePath);
386
387                         FileInfo fi = new FileInfo (config.FilePath);
388                         Assert.AreEqual ("machineconfig", fi.Name);
389                 }
390
391                 [Test]
392                 public void exePath_UserLevelNone_null ()
393                 {
394                         SysConfig config = ConfigurationManager.OpenExeConfiguration (null);
395 #if false
396                         Console.WriteLine("null exe application config path: {0}", config.FilePath);    
397
398                         FileInfo fi = new FileInfo (config.FilePath);
399                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
400 #endif
401                 }
402
403                 [Test]
404                 [Category ("NotWorking")]
405                 public void mapped_ExeConfiguration_null ()
406                 {
407                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(null, ConfigurationUserLevel.None);
408                         Console.WriteLine("null mapped application config path: {0}", config.FilePath); 
409
410                         FileInfo fi = new FileInfo (config.FilePath);
411 #if TARGET_JVM
412                         Assert.AreEqual("System.Configuration.Test20.jar.config", fi.Name);
413 #else
414                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
415 #endif
416                 }
417
418                 [Test]
419                 [Category ("NotWorking")]
420                 public void mapped_MachineConfig_null ()
421                 {
422                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (null);
423                         Console.WriteLine("null mapped machine config path: {0}", config.FilePath);
424
425                         FileInfo fi = new FileInfo (config.FilePath);
426                         Assert.AreEqual ("machine.config", fi.Name);
427                 }
428
429                 [Test]
430                 public void GetSectionReturnsNativeObject ()
431                 {
432                         Assert.IsTrue (ConfigurationManager.GetSection ("appSettings") is NameValueCollection);
433                 }
434
435                 [Test] // test for bug #78372.
436                 public void OpenMachineConfiguration ()
437                 {
438                         SysConfig cfg = ConfigurationManager.OpenMachineConfiguration ();
439                         Assert.IsTrue (cfg.Sections.Count > 0, "#1");
440 #if !TARGET_JVM
441                         ConfigurationSection s = cfg.SectionGroups ["system.net"].Sections ["connectionManagement"];
442                         Assert.IsNotNull (s, "#2");
443                         Assert.IsTrue (s is ConnectionManagementSection, "#3");
444 #endif
445                 }
446
447                 [Test]
448                 public void SectionCollectionEnumerator ()
449                 {
450                         SysConfig c = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
451                         ConfigurationSectionCollection col =
452                                 c.GetSectionGroup ("system.web").Sections;
453                         IEnumerator e = col.GetEnumerator ();
454                         e.MoveNext ();
455                         Assert.IsTrue (e.Current is ConfigurationSection);
456                 }
457
458                 [Test]  // Test for bug #3412
459                 [Category("NotWorking")]
460                 public void TestAddRemoveSection()
461                 {
462                         const string name = "testsection";
463                         var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
464
465                         // ensure not present
466                         if (config.Sections.Get(name) != null)
467                         {
468                                 config.Sections.Remove(name);
469                         }
470
471                         // add
472                         config.Sections.Add(name, new TestSection());
473
474                         // remove
475                         var section = config.Sections.Get(name);
476                         Assert.IsNotNull(section);
477                         Assert.IsNotNull(section as TestSection);
478                         config.Sections.Remove(name);
479
480                         // add
481                         config.Sections.Add(name, new TestSection());
482
483                         // remove
484                         section = config.Sections.Get(name);
485                         Assert.IsNotNull(section);
486                         Assert.IsNotNull(section as TestSection);
487                         config.Sections.Remove(name);
488                 }
489                         
490                 class TestSection : ConfigurationSection  {}
491
492                 class RemoteConfig : MarshalByRefObject
493                 {
494                         public static RemoteConfig GetInstance (AppDomain domain)
495                         {
496                                 RemoteConfig config = (RemoteConfig) domain.CreateInstanceAndUnwrap (
497                                         typeof (RemoteConfig).Assembly.FullName,
498                                         typeof (RemoteConfig).FullName, new object [0]);
499                                 return config;
500                         }
501
502                         public string GetFilePath (string exePath)
503                         {
504                                 global::System.Configuration.Configuration config =
505                                         ConfigurationManager.OpenExeConfiguration (exePath);
506                                 return config.FilePath;
507                         }
508
509                         public string GetFilePath (ConfigurationUserLevel userLevel)
510                         {
511                                 global::System.Configuration.Configuration config =
512                                         ConfigurationManager.OpenExeConfiguration (userLevel);
513                                 return config.FilePath;
514                         }
515
516                         public string GetSettingValue (string exePath, string key)
517                         {
518                                 global::System.Configuration.Configuration config =
519                                         ConfigurationManager.OpenExeConfiguration (exePath);
520                                 return config.AppSettings.Settings [key].Value;
521                         }
522
523                         public string GetSettingValue (ConfigurationUserLevel userLevel, string key)
524                         {
525                                 global::System.Configuration.Configuration config =
526                                         ConfigurationManager.OpenExeConfiguration (userLevel);
527                                 KeyValueConfigurationElement value = config.AppSettings.Settings [key];
528                                 return value != null ? value.Value : null;
529                         }
530
531                         public string GetSettingValue (string key)
532                         {
533                                 return ConfigurationManager.AppSettings [key];
534                         }
535                 }
536         }
537 }