d2b3a8f86367bbe87ced448abacbea7a8c5c03b2
[mono.git] / mcs / class / System.Web / Test / System.Web.Hosting / ApplicationHostTest.cs
1 //
2 // System.Web.Hosting.ApplicationHost.cs 
3 // 
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com)
6 //
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Runtime.Serialization;
31 using System.IO;
32 using System.Web.Hosting;
33 using NUnit.Framework;
34 using System.Web;
35
36 namespace MonoTests.System.Web.Hosting {
37
38         public class MBR : MarshalByRefObject {
39
40                 public string GetDomainName ()
41                 {
42                         return AppDomain.CurrentDomain.FriendlyName;
43                 }
44
45                 public AppDomain GetDomain ()
46                 {
47                         return AppDomain.CurrentDomain;
48                 }
49         }
50
51         [Serializable]
52         public class MySerializable {
53                 public string GetDomainName ()
54                 {
55                         return AppDomain.CurrentDomain.FriendlyName;
56                 }
57         }
58         
59         [TestFixture]
60         public class ApplicationHostTest {
61
62                 [SetUp] 
63                 public void Setup ()
64                 {
65                         try {
66                                 //
67                                 // Only needed in Windows, where we need to have a bin/ASSEMBLY.DLL
68                                 //
69                                 string p = typeof (ApplicationHostTest).Assembly.Location;
70                                 try {
71                                         Directory.Delete ("bin", true);
72                                 } catch {}
73
74                                 try {
75                                         Directory.CreateDirectory ("bin");
76                                 } catch {}
77
78                                 string fn = Path.GetFileName (p);
79                                 File.Copy (p, Path.Combine ("bin", fn));
80                         } catch {}
81                 }
82
83                 [TearDown] public void Shutdown ()
84                 {
85                         try {
86                                 Directory.Delete ("bin", true);
87                         } catch {}
88                 }
89                 
90                 [Test][ExpectedException (typeof (NullReferenceException))]
91                 public void ConstructorTestNull ()
92                 {
93                         ApplicationHost.CreateApplicationHost (null, null, null);
94                 }
95
96                 [Test]
97         [ExpectedException (typeof (ArgumentException))]
98                 public void ConstructorTestNull2 ()
99                 {
100                         ApplicationHost.CreateApplicationHost (null, "/app", ".");
101                 }
102
103                 [Test]
104         [ExpectedException (typeof (ArgumentNullException))]
105                 public void ConstructorTestNull3 ()
106                 {
107                         ApplicationHost.CreateApplicationHost (typeof (MBR), null, ".");
108                 }
109
110                 [Test][ExpectedException (typeof (NullReferenceException))]
111                 public void ConstructorTestNull4 ()
112                 {
113                         ApplicationHost.CreateApplicationHost (typeof (MBR), "/app", null);
114                 }
115
116                 [Test]
117                 [ExpectedException(typeof(SerializationException))]
118 #if TARGET_JVM //System.Security.Policy.Evidence not implemented
119                 [Category ("NotWorking")]
120 #endif
121                 public void Constructor_PlainType ()
122                 {
123                         ApplicationHost.CreateApplicationHost (typeof (ApplicationHostTest), "/app", Environment.CurrentDirectory);
124                 }
125
126                 [Test]
127                 public void Constructor_SerializableType ()
128                 {
129                         object o = ApplicationHost.CreateApplicationHost (typeof (MySerializable), "/app", Environment.CurrentDirectory);
130                         Assert.AreEqual (typeof (MySerializable), o.GetType (), "C2");
131                         MySerializable m = (MySerializable) o;
132
133                         Assert.AreEqual (m.GetDomainName (), AppDomain.CurrentDomain.FriendlyName, "C4");
134                 }
135
136                 static void p (string s, object x)
137                 {
138                         Console.WriteLine ("{0} {1}", s, x);
139                 }
140                 
141                 [Test]
142                 [Category ("NotDotNet")] // D2 and D3 asserts will fail in windows on because file system environment 
143                 public void ConstructorTest ()
144                 {
145                         object o = ApplicationHost.CreateApplicationHost (typeof (MBR), "/app", Environment.CurrentDirectory);
146                         Assert.AreEqual (typeof (MBR), o.GetType (), "C5");
147                         MBR m = (MBR) o;
148
149                         Assert.AreEqual (true, m.GetDomainName () != AppDomain.CurrentDomain.FriendlyName);
150
151                         AppDomain other = m.GetDomain ();
152                         AppDomainSetup setup = other.SetupInformation;
153
154                         string tb = Environment.CurrentDirectory;
155                         if (tb[tb.Length - 1] == Path.DirectorySeparatorChar)
156                                 tb = tb.Substring (0, tb.Length - 1);
157
158                         // Need to fix an issue in AppDomainSetup
159                         // Assert.AreEqual (tb + "/", setup.ApplicationBase, "D1");
160                         
161                         p ("AppDomain's Applicationname is: ", setup.ApplicationName);
162                         // IGNORE this test. We are setting CachePath to DynamicBase by now.
163                         // Assert.AreEqual (null, setup.CachePath, "D2");
164
165                         Assert.AreEqual (0, String.Compare (tb + Path.DirectorySeparatorChar + "Web.Config", setup.ConfigurationFile, StringComparison.OrdinalIgnoreCase), "D3");
166                         Assert.AreEqual (false, setup.DisallowBindingRedirects, "D4");
167                         Assert.AreEqual (true, setup.DisallowCodeDownload, "D5");
168                         Assert.AreEqual (false, setup.DisallowPublisherPolicy, "D6");
169                         // Disabling D7 test, as we set it there to avoid locking in sys.web.compilation
170                         // Assert.AreEqual (null, setup.DynamicBase, "D7");
171                         Assert.AreEqual (null, setup.LicenseFile, "D8");
172                         //Assert.AreEqual (LoaderOptimization.NotSpecified, setup.LoaderOptimization);
173                         p ("LoaderOptimization is: ", setup.LoaderOptimization);
174                         Assert.AreEqual (0, string.Compare (
175 #if NET_2_0
176                                 String.Format ("{0}{1}bin", tb, Path.DirectorySeparatorChar),
177 #else
178                                 "bin",
179 #endif
180                                 setup.PrivateBinPath, true), "D9"
181                         );
182                         Assert.AreEqual (setup.PrivateBinPathProbe, "*", "D10");
183                         p ("ShadowCopyDirs: ", setup.ShadowCopyDirectories);
184                         Assert.AreEqual (true, setup.ShadowCopyDirectories.EndsWith ("bin") || setup.ShadowCopyDirectories.EndsWith ("Bin"), "D11");
185                         Assert.AreEqual (false, setup.ShadowCopyDirectories.StartsWith ("file:"), "D12");
186                         Assert.AreEqual ("true", setup.ShadowCopyFiles, "D13");
187
188                         p ("ApsInstal", HttpRuntime.AspInstallDirectory);
189                 }
190         }
191         
192 }