Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / unhandled-exception-test-case.2.cs
1 using System;
2 using System.Reflection;
3 using System.Threading;
4
5 namespace UnhandledExceptionTest
6 {
7         class TestConfiguration {
8                 private bool useDifferentThread = false;
9                 public bool DT {
10                         get {
11                                 return useDifferentThread;
12                         }
13                 }
14                 private bool useDifferentApplicationDomain = false;
15                 public bool DA {
16                         get {
17                                 return useDifferentApplicationDomain;
18                         }
19                 }
20                 private bool differentConfigurationIsLegacy;
21                 public bool DCIL {
22                         get {
23                                 return differentConfigurationIsLegacy;
24                         }
25                 }
26                 private bool useDifferentThreadInDifferentApplicationDomain = false;
27                 public bool DTDA {
28                         get {
29                                 return useDifferentThreadInDifferentApplicationDomain;
30                         }
31                 }
32                 private bool addHandlerToRootApplicationDomain = false;
33                 public bool HRA {
34                         get {
35                                 return addHandlerToRootApplicationDomain;
36                         }
37                 }
38                 private bool addHandlerToDifferentApplicationDomain = false;
39                 public bool HDA {
40                         get {
41                                 return addHandlerToDifferentApplicationDomain;
42                         }
43                 }
44                 
45                 private static bool ParseArgumentValue (string value) {
46                         if ((value.Length == 1)) {
47                                 switch (value [0]) {
48                                 case 'T':
49                                         return true;
50                                 case 'F':
51                                         return false;
52                                 default:
53                                         Console.WriteLine ("Invalid argument value {0}", value);
54                                         throw new ApplicationException ("Invalid argument value " + value);
55                                 }
56                         } else {
57                                 Console.WriteLine ("Invalid argument value {0}", value);
58                                 throw new ApplicationException ("Invalid argument value " + value);
59                         }
60                 }
61                 
62                 public TestConfiguration (string configuration) {
63                         string [] arguments = configuration.Split (',');
64                         foreach (string argument in arguments) {
65                                 string [] components = argument.Split (':');
66                                 if (components.Length == 2) {
67                                         switch (components [0]) {
68                                         case "DT":
69                                                 useDifferentThread = ParseArgumentValue (components [1]);
70                                                 break;
71                                         case "DA":
72                                                 useDifferentApplicationDomain = ParseArgumentValue (components [1]);
73                                                 break;
74                                         case "DCIL":
75                                                 differentConfigurationIsLegacy = ParseArgumentValue (components [1]);
76                                                 break;
77                                         case "DTDA":
78                                                 useDifferentThreadInDifferentApplicationDomain = ParseArgumentValue (components [1]);
79                                                 break;
80                                         case "HRA":
81                                                 addHandlerToRootApplicationDomain = ParseArgumentValue (components [1]);
82                                                 break;
83                                         case "HDA":
84                                                 addHandlerToDifferentApplicationDomain = ParseArgumentValue (components [1]);
85                                                 break;
86                                         default:
87                                                 Console.WriteLine ("Invalid argument {0}", argument);
88                                                 throw new ApplicationException ("Invalid argument " + argument);
89                                         }
90                                 } else {
91                                         Console.WriteLine ("Invalid argument {0}", argument);
92                                         throw new ApplicationException ("Invalid argument " + argument);
93                                 }
94                         }
95                 }
96                 private static string BoolToString (bool value) {
97                         return value ? "T" : "F";
98                 }
99                 public void Print () {
100                         Console.WriteLine ("Configuration: DT={0},DA={1},DCIL={2},DTDA={3},HRA={4},HDA={5}",
101                                         BoolToString (useDifferentThread),
102                                         BoolToString (useDifferentApplicationDomain),
103                                         BoolToString (differentConfigurationIsLegacy),
104                                         BoolToString (useDifferentThreadInDifferentApplicationDomain),
105                                         BoolToString (addHandlerToRootApplicationDomain),
106                                         BoolToString (addHandlerToDifferentApplicationDomain));
107                 }
108         }
109         
110         class Test {
111                 private string configurationDescription;
112                 private TestConfiguration configuration;
113                 public Test (string configurationDescription) {
114                         this.configurationDescription = configurationDescription;
115                         this.configuration = new TestConfiguration (configurationDescription);
116                 }
117                 
118                 private AppDomain CreateDiffrentAppDomain () {
119                         AppDomainSetup ads = new AppDomainSetup();
120                         ads.ApplicationBase = System.Environment.CurrentDirectory;
121                         ads.DisallowBindingRedirects = false;
122                         ads.DisallowCodeDownload = true;
123                         ads.ConfigurationFile = System.Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar +
124                                         (configuration.DCIL ? "unhandled-exception-legacy-configuration.config" : "unhandled-exception-base-configuration.config");
125                         return AppDomain.CreateDomain("DifferentAppDomain", null, ads);
126                 }
127                 
128                 public void RunTest () {
129                         if (configuration.DA) {
130                                 AppDomain differentAppDomain = CreateDiffrentAppDomain ();
131                                 if (configuration.HDA) {
132                                         differentAppDomain.UnhandledException += new UnhandledExceptionEventHandler (DifferentDomainUnhandledExceptionHandler);
133                                 }
134                                 DifferentDomainActor dda = (DifferentDomainActor) differentAppDomain.CreateInstanceAndUnwrap (
135                                                 Assembly.GetEntryAssembly().FullName, typeof (DifferentDomainActor).FullName);
136                                 dda.Act (configurationDescription);
137                         } else {
138                                 if (configuration.DT) {
139                                         Console.WriteLine ("Throwing ApplicationException in different thread");
140                                 } else {
141                                         Console.WriteLine ("Throwing ApplicationException in main thread");
142                                 }
143                                 throw new ApplicationException ("This exception is unhandled");
144                         }
145                         if (configuration.DT) {
146                                 Console.WriteLine ("Continuing in different thread after the exception was thrown");
147                         }
148                 }
149                 
150                 static void Main (string [] args) {
151                         if (args.Length != 1) {
152                                 Console.WriteLine ("Invalid arguments (number of) {0}", args.Length);
153                                 throw new ApplicationException ("Invalid arguments (number of) " + args.Length);
154                         }
155                         Test test = new Test (args [0]);
156                         test.Act ();
157                 }
158                 public void Act () {
159                         configuration.Print ();
160                         Console.WriteLine ("Running under version {0}", Environment.Version);
161                         
162                         if (configuration.HRA) {
163                                 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (RootDomainUnhandledExceptionHandler);
164                         }
165                         
166                         if (configuration.DT) {
167                                 Thread thread = new Thread (new ThreadStart (this.RunTest));
168                                 thread.Start ();
169                                 thread.Join ();
170                         } else {
171                                 RunTest ();
172                         }
173                         
174                         Console.WriteLine ("Continuing in main thread after the exception was thrown");
175                         Console.WriteLine ("Continuing in root AppDomain after the exception was thrown");
176                         Console.WriteLine ("MARKER-CONT");
177                 }
178
179                 public static void PrintUnhandledException (string caller, object sender, UnhandledExceptionEventArgs e) {
180                         Exception ex = (Exception)e.ExceptionObject;
181
182                         Console.WriteLine ("Running {0}", caller);
183                         Console.WriteLine ("Handling exception type {0}", ex.GetType().Name);
184                         Console.WriteLine ("Message is {0}", ex.Message);
185                         Console.WriteLine ("IsTerminating is set to {0}", e.IsTerminating);
186                 }
187                 public static void RootDomainUnhandledExceptionHandler (object sender, UnhandledExceptionEventArgs e) {
188                         Console.WriteLine ("MARKER-RDUE");
189                         PrintUnhandledException ("RootDomainUnhandledExceptionHandler", sender, e);
190                 }
191                 public static void DifferentDomainUnhandledExceptionHandler (object sender, UnhandledExceptionEventArgs e) {
192                         Console.WriteLine ("MARKER-DDUE");
193                         PrintUnhandledException ("DifferentDomainUnhandledExceptionHandler", sender, e);
194                 }
195         }
196         
197         public class DifferentDomainActor : MarshalByRefObject {
198                 //private string configurationDescription = null;
199                 private TestConfiguration configuration = null;
200                 
201                 public void RunTest () {
202                         if (configuration.DTDA) {
203                                 Console.WriteLine ("Throwing ApplicationException in new thread (different appdomain)");
204                         } else if (configuration.DT) {
205                                 Console.WriteLine ("Throwing ApplicationException in different thread (different appdomain)");
206                         } else {
207                                 Console.WriteLine ("Throwing ApplicationException in main thread (different appdomain)");
208                         }
209                         throw new ApplicationException ("This exception is unhandled");
210                 }
211                 
212                 //  Call this method via a proxy.
213                 public void Act (string configurationDescription) {
214                         //this.configurationDescription = configurationDescription;
215                         this.configuration = new TestConfiguration (configurationDescription);
216                         
217                         if (configuration.DTDA) {
218                                 Thread thread = new Thread (new ThreadStart (this.RunTest));
219                                 thread.Start ();
220                                 thread.Join ();
221                         } else {
222                                 RunTest ();
223                         }
224                 }
225         }
226 }