Replace SIZEOF_REGISTER with sizeof(mgreg_t) for consistency with sizeof(gpointer)
[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 #if NET_2_0
32
33 using System;
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Configuration;
37 using System.Net.Configuration;
38 using System.IO;
39 using NUnit.Framework;
40 using SysConfig = System.Configuration.Configuration;
41 using System.Runtime.InteropServices;
42
43 namespace MonoTests.System.Configuration {
44         [TestFixture]
45         public class ConfigurationManagerTest
46         {
47                 private string originalCurrentDir;
48                 private string tempFolder;
49
50                 [SetUp]
51                 public void SetUp ()
52                 {
53                         originalCurrentDir = Directory.GetCurrentDirectory ();
54                         tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
55                         if (!Directory.Exists (tempFolder))
56                                 Directory.CreateDirectory (tempFolder);
57                 }
58
59                 [TearDown]
60                 public void TearDown ()
61                 {
62                         Directory.SetCurrentDirectory (originalCurrentDir);
63                         if (Directory.Exists (tempFolder))
64                                 Directory.Delete (tempFolder, true);
65                 }
66
67                 [Test] // OpenExeConfiguration (ConfigurationUserLevel)
68                 [Category ("NotWorking")] // bug #323622
69                 public void OpenExeConfiguration1_Remote ()
70                 {
71                         AppDomain domain = null;
72                         string config_file;
73                         string config_xml = @"
74                                 <configuration>
75                                         <appSettings>
76                                                 <add key='anyKey' value='42' />
77                                         </appSettings>
78                                 </configuration>";
79
80                         config_file = Path.Combine (tempFolder, "otherConfig.noconfigext");
81                         File.WriteAllText (config_file, config_xml);
82
83                         try {
84                                 AppDomainSetup setup = new AppDomainSetup();
85                                 setup.ConfigurationFile = config_file;
86                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
87                                 domain = AppDomain.CreateDomain("foo", null, setup);
88
89                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
90
91                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
92                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
93                                 Assert.AreEqual ("42", config.GetSettingValue (userLevel, "anyKey"));
94                                 Assert.AreEqual ("42", config.GetSettingValue ("anyKey"));
95                         } finally {
96                                 if (domain != null)
97                                         AppDomain.Unload (domain);
98                                 File.Delete (config_file);
99                         }
100
101                         config_file = Path.Combine (tempFolder, "otherConfig.exe.config");
102                         File.WriteAllText (config_file, config_xml);
103
104                         try {
105                                 AppDomainSetup setup = new AppDomainSetup();
106                                 setup.ConfigurationFile = config_file;
107                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
108                                 domain = AppDomain.CreateDomain("foo", null, setup);
109
110                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
111
112                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
113                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
114                                 Assert.AreEqual ("42", config.GetSettingValue (userLevel, "anyKey"));
115                                 Assert.AreEqual ("42", config.GetSettingValue ("anyKey"));
116                         } finally {
117                                 if (domain != null)
118                                         AppDomain.Unload (domain);
119                                 File.Delete (config_file);
120                         }
121
122                         try {
123                                 AppDomainSetup setup = new AppDomainSetup();
124                                 setup.ConfigurationFile = config_file;
125                                 setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
126                                 domain = AppDomain.CreateDomain("foo", null, setup);
127
128                                 RemoteConfig config = RemoteConfig.GetInstance (domain);
129
130                                 ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
131                                 Assert.AreEqual (config_file, config.GetFilePath (userLevel));
132                                 Assert.IsNull (config.GetSettingValue (userLevel, "anyKey"));
133                                 Assert.IsNull (config.GetSettingValue ("anyKey"));
134                         } finally {
135                                 if (domain != null)
136                                         AppDomain.Unload (domain);
137                                 File.Delete (config_file);
138                         }
139                 }
140
141                 [Test] // OpenExeConfiguration (ConfigurationUserLevel)
142                 public void OpenExeConfiguration1_UserLevel_None ()
143                 {
144                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
145
146                         Console.WriteLine("application config path: {0}", config.FilePath);
147                         FileInfo fi = new FileInfo (config.FilePath);
148 #if TARGET_JVM
149                         Assert.AreEqual ("nunit-console.jar.config", fi.Name);
150 #else
151                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
152 #endif
153                 }
154
155                 [Test]
156                 public void OpenExeConfiguration1_UserLevel_PerUserRoaming ()
157                 {
158                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
159                         Console.WriteLine("roaming user config path: {0}", config.FilePath);
160
161                         FileInfo fi = new FileInfo (config.FilePath);
162                         Assert.AreEqual ("user.config", fi.Name);
163                 }
164
165                 [Test]
166                 public void OpenExeConfiguration1_UserLevel_PerUserRoamingAndLocal ()
167                 {
168                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
169                         Console.WriteLine("local user config path: {0}", config.FilePath);
170
171                         FileInfo fi = new FileInfo (config.FilePath);
172                         Assert.AreEqual ("user.config", fi.Name);
173                 }
174
175                 [Test] // OpenExeConfiguration (String)
176                 public void OpenExeConfiguration2 ()
177                 {
178                         String exePath;
179                         SysConfig config;
180
181                         exePath = Path.Combine (tempFolder, "DoesNotExist.whatever");
182                         File.Create (exePath).Close ();
183
184                         config = ConfigurationManager.OpenExeConfiguration (exePath);
185                         Assert.AreEqual (exePath + ".config", config.FilePath, "#1");
186
187                         exePath = Path.Combine (tempFolder, "SomeExecutable.exe");
188                         File.Create (exePath).Close ();
189
190                         config = ConfigurationManager.OpenExeConfiguration (exePath);
191                         Assert.AreEqual (exePath + ".config", config.FilePath, "#2");
192
193                         exePath = Path.Combine (tempFolder, "Foo.exe.config");
194                         File.Create (exePath).Close ();
195
196                         config = ConfigurationManager.OpenExeConfiguration (exePath);
197                         Assert.AreEqual (exePath + ".config", config.FilePath, "#3");
198
199                         Directory.SetCurrentDirectory (tempFolder);
200
201                         exePath = "relative.exe";
202                         File.Create (Path.Combine (tempFolder, exePath)).Close ();
203
204                         config = ConfigurationManager.OpenExeConfiguration (exePath);
205                         Assert.AreEqual (Path.Combine (tempFolder, exePath + ".config"), config.FilePath, "#4");
206                 }
207
208                 [Test] // OpenExeConfiguration (String)
209                 public void OpenExeConfiguration2_ExePath_Empty ()
210                 {
211                         AppDomain domain = AppDomain.CurrentDomain;
212
213                         SysConfig config = ConfigurationManager.OpenExeConfiguration (string.Empty);
214                         Assert.AreEqual (domain.SetupInformation.ConfigurationFile, config.FilePath);
215                 }
216
217                 [Test] // OpenExeConfiguration (String)
218                 public void OpenExeConfiguration2_ExePath_Null ()
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_DoesNotExist ()
228                 {
229                         String exePath = Path.Combine (tempFolder, "DoesNotExist.exe");
230
231                         try {
232                                 ConfigurationManager.OpenExeConfiguration (exePath);
233                                 Assert.Fail ("#1");
234                         } catch (ConfigurationErrorsException ex) {
235                                 // An error occurred loading a configuration file:
236                                 // The parameter 'exePath' is invalid
237                                 Assert.AreEqual (typeof (ConfigurationErrorsException), ex.GetType (), "#2");
238                                 Assert.IsNull (ex.Filename, "#3");
239                                 Assert.IsNotNull (ex.InnerException, "#4");
240                                 Assert.AreEqual (0, ex.Line, "#5");
241                                 Assert.IsNotNull (ex.Message, "#6");
242
243                                 // The parameter 'exePath' is invalid
244                                 ArgumentException inner = ex.InnerException as ArgumentException;
245                                 Assert.IsNotNull (inner, "#7");
246                                 Assert.AreEqual (typeof (ArgumentException), inner.GetType (), "#8");
247                                 Assert.IsNull (inner.InnerException, "#9");
248                                 Assert.IsNotNull (inner.Message, "#10");
249                                 Assert.AreEqual ("exePath", inner.ParamName, "#11");
250                         }
251                 }
252
253                 [Test]
254                 public void exePath_UserLevelNone ()
255                 {
256                         string basedir = AppDomain.CurrentDomain.BaseDirectory;
257                         SysConfig config = ConfigurationManager.OpenExeConfiguration("System.Configuration_test_net_2_0.dll.mdb");
258                         Assert.AreEqual (Path.Combine (basedir, "System.Configuration_test_net_2_0.dll.mdb.config"), config.FilePath);
259                 }
260
261                 [Test]
262                 public void exePath_UserLevelPerRoaming ()
263                 {
264                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
265                         string filePath = config.FilePath;
266                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
267                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
268                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
269                 }
270
271                 [Test]
272                 public void exePath_UserLevelPerRoamingAndLocal ()
273                 {
274                         SysConfig config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
275                         string filePath = config.FilePath;
276                         string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
277                         Assert.IsTrue (filePath.StartsWith (applicationData), "#1:" + filePath);
278                         Assert.AreEqual ("user.config", Path.GetFileName (filePath), "#2:" + filePath);
279                 }
280
281                 [Test]
282                 public void mapped_UserLevelNone ()
283                 {
284                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
285                         map.ExeConfigFilename = "execonfig";
286
287                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
288                         Console.WriteLine("mapped application config path: {0}", config.FilePath);      
289
290                         FileInfo fi = new FileInfo (config.FilePath);
291                         Assert.AreEqual ("execonfig", fi.Name);
292
293                 }
294
295                 [Test]
296                 public void mapped_UserLevelPerRoaming ()
297                 {
298                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
299                         map.ExeConfigFilename = "execonfig";
300                         map.RoamingUserConfigFilename = "roaminguser";
301
302                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);
303                         Console.WriteLine("mapped roaming user config path: {0}", config.FilePath);     
304
305                         FileInfo fi = new FileInfo (config.FilePath);
306                         Assert.AreEqual ("roaminguser", fi.Name);
307                 }
308
309                 [Test]
310                 [ExpectedException (typeof (ArgumentException))]
311                 [Category ("NotWorking")]
312                 public void mapped_UserLevelPerRoaming_no_execonfig ()
313                 {
314                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
315                         map.RoamingUserConfigFilename = "roaminguser";
316
317                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoaming);
318                 }
319
320                 [Test]
321                 public void mapped_UserLevelPerRoamingAndLocal ()
322                 {
323                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
324                         map.ExeConfigFilename = "execonfig";
325                         map.RoamingUserConfigFilename = "roaminguser";
326                         map.LocalUserConfigFilename = "localuser";
327
328                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoamingAndLocal);
329                         Console.WriteLine("mapped local user config path: {0}", config.FilePath);       
330
331                         FileInfo fi = new FileInfo (config.FilePath);
332                         Assert.AreEqual ("localuser", fi.Name);
333                 }
334
335                 [Test]
336                 [ExpectedException (typeof (ArgumentException))]
337                 [Category ("NotWorking")]
338                 public void mapped_UserLevelPerRoamingAndLocal_no_execonfig ()
339                 {
340                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
341                         map.RoamingUserConfigFilename = "roaminguser";
342                         map.LocalUserConfigFilename = "localuser";
343
344                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
345                 }
346
347                 [Test]
348                 [ExpectedException (typeof (ArgumentException))]
349                 [Category ("NotWorking")]
350                 public void mapped_UserLevelPerRoamingAndLocal_no_roaminguser ()
351                 {
352                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
353                         map.ExeConfigFilename = "execonfig";
354                         map.LocalUserConfigFilename = "localuser";
355
356                         ConfigurationManager.OpenMappedExeConfiguration (map, ConfigurationUserLevel.PerUserRoamingAndLocal);
357                 }
358
359                 [Test]
360                 public void MachineConfig ()
361                 {
362                         SysConfig config = ConfigurationManager.OpenMachineConfiguration ();
363                         Console.WriteLine("machine config path: {0}", config.FilePath);
364
365                         FileInfo fi = new FileInfo (config.FilePath);
366                         Assert.AreEqual ("machine.config", fi.Name);
367                 }
368
369                 [Test]
370                 public void mapped_MachineConfig ()
371                 {
372                         ConfigurationFileMap map = new ConfigurationFileMap ();
373                         map.MachineConfigFilename = "machineconfig";
374
375                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (map);
376                         Console.WriteLine("mapped machine config path: {0}", config.FilePath);
377
378                         FileInfo fi = new FileInfo (config.FilePath);
379                         Assert.AreEqual ("machineconfig", fi.Name);
380                 }
381
382                 [Test]
383                 public void exePath_UserLevelNone_null ()
384                 {
385                         SysConfig config = ConfigurationManager.OpenExeConfiguration (null);
386 #if false
387                         Console.WriteLine("null exe application config path: {0}", config.FilePath);    
388
389                         FileInfo fi = new FileInfo (config.FilePath);
390                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
391 #endif
392                 }
393
394                 [Test]
395                 [Category ("NotWorking")]
396                 public void mapped_ExeConfiguration_null ()
397                 {
398                         SysConfig config = ConfigurationManager.OpenMappedExeConfiguration(null, ConfigurationUserLevel.None);
399                         Console.WriteLine("null mapped application config path: {0}", config.FilePath); 
400
401                         FileInfo fi = new FileInfo (config.FilePath);
402 #if TARGET_JVM
403                         Assert.AreEqual("System.Configuration.Test20.jar.config", fi.Name);
404 #else
405                         Assert.AreEqual ("System.Configuration_test_net_2_0.dll.config", fi.Name);
406 #endif
407                 }
408
409                 [Test]
410                 [Category ("NotWorking")]
411                 public void mapped_MachineConfig_null ()
412                 {
413                         SysConfig config = ConfigurationManager.OpenMappedMachineConfiguration (null);
414                         Console.WriteLine("null mapped machine config path: {0}", config.FilePath);
415
416                         FileInfo fi = new FileInfo (config.FilePath);
417                         Assert.AreEqual ("machine.config", fi.Name);
418                 }
419
420                 [Test]
421                 public void GetSectionReturnsNativeObject ()
422                 {
423                         Assert.IsTrue (ConfigurationManager.GetSection ("appSettings") is NameValueCollection);
424                 }
425
426                 [Test] // test for bug #78372.
427                 public void OpenMachineConfiguration ()
428                 {
429                         SysConfig cfg = ConfigurationManager.OpenMachineConfiguration ();
430                         Assert.IsTrue (cfg.Sections.Count > 0, "#1");
431 #if !TARGET_JVM
432                         ConfigurationSection s = cfg.SectionGroups ["system.net"].Sections ["connectionManagement"];
433                         Assert.IsNotNull (s, "#2");
434                         Assert.IsTrue (s is ConnectionManagementSection, "#3");
435 #endif
436                 }
437
438                 [Test]
439                 public void SectionCollectionEnumerator ()
440                 {
441                         SysConfig c = ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None);
442                         ConfigurationSectionCollection col =
443                                 c.GetSectionGroup ("system.web").Sections;
444                         IEnumerator e = col.GetEnumerator ();
445                         e.MoveNext ();
446                         Assert.IsTrue (e.Current is ConfigurationSection);
447                 }
448
449                 class RemoteConfig : MarshalByRefObject
450                 {
451                         public static RemoteConfig GetInstance (AppDomain domain)
452                         {
453                                 RemoteConfig config = (RemoteConfig) domain.CreateInstanceAndUnwrap (
454                                         typeof (RemoteConfig).Assembly.FullName,
455                                         typeof (RemoteConfig).FullName, new object [0]);
456                                 return config;
457                         }
458
459                         public string GetFilePath (string exePath)
460                         {
461                                 global::System.Configuration.Configuration config =
462                                         ConfigurationManager.OpenExeConfiguration (exePath);
463                                 return config.FilePath;
464                         }
465
466                         public string GetFilePath (ConfigurationUserLevel userLevel)
467                         {
468                                 global::System.Configuration.Configuration config =
469                                         ConfigurationManager.OpenExeConfiguration (userLevel);
470                                 return config.FilePath;
471                         }
472
473                         public string GetSettingValue (string exePath, string key)
474                         {
475                                 global::System.Configuration.Configuration config =
476                                         ConfigurationManager.OpenExeConfiguration (exePath);
477                                 return config.AppSettings.Settings [key].Value;
478                         }
479
480                         public string GetSettingValue (ConfigurationUserLevel userLevel, string key)
481                         {
482                                 global::System.Configuration.Configuration config =
483                                         ConfigurationManager.OpenExeConfiguration (userLevel);
484                                 KeyValueConfigurationElement value = config.AppSettings.Settings [key];
485                                 return value != null ? value.Value : null;
486                         }
487
488                         public string GetSettingValue (string key)
489                         {
490                                 return ConfigurationManager.AppSettings [key];
491                         }
492                 }
493         }
494 }
495
496 #endif