[runtime] Remove all NACL support. It was unmaintained for a long time. (#4955)
[mono.git] / mono / tests / appdomain-thread-abort.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.Remoting;
4 using System.Reflection;
5
6 public class JustSomeClass {
7 }
8
9 public class Test2 : ContextBoundObject
10 {
11         public void Run () {
12                 Thread.CurrentThread.Abort ();
13         }
14 }
15
16 public class Test1 : MarshalByRefObject
17 {
18         public bool Run () {
19                 AppDomain d = AppDomain.CreateDomain ("foo2");
20
21                 var t2 = (Test2)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName,
22                                                                                                  "Test2");
23                 try {
24                         t2.Run ();
25                 } catch (ThreadAbortException ex) {
26                         Thread.ResetAbort ();
27                         return true;
28                 }
29
30                 return false;
31         }
32 }
33
34 public class Test : MarshalByRefObject {
35     ThreadAbortException exc;
36     public JustSomeClass other;
37
38     public void doThrow (int n, object state) {
39         if (n <= 0)
40             Thread.CurrentThread.Abort (state);
41         else
42             doThrow (n - 1, state);
43     }
44
45     public void abortProxy () {
46         doThrow (10, this);
47     }
48
49     public void abortOther () {
50         other = new JustSomeClass ();
51         doThrow (10, other);
52     }
53
54     public void abortString () {
55         try {
56             doThrow (10, "bla");
57         } catch (ThreadAbortException e) {
58             exc = e;
59         }
60     }
61
62     public void abortOtherIndirect (Test test) {
63         test.abortOther ();
64     }
65
66     public object getState () {
67         return exc.ExceptionState;
68     }
69
70     public int getInt () {
71             return 123;
72     }
73 }
74
75 public class main {
76     public static int Main (string [] args) {
77         AppDomain domain = AppDomain.CreateDomain ("newdomain");
78         Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
79         bool didAbort;
80         Test testHere = new Test ();
81
82         if (!RemotingServices.IsTransparentProxy (test)) {
83             Console.WriteLine ("test is no proxy");
84             return 5;
85         }
86
87         try {
88             test.abortOtherIndirect (testHere);
89         } catch (ThreadAbortException e) {
90             object state = e.ExceptionState;
91             Thread.ResetAbort ();
92             if ((JustSomeClass)state != testHere.other) {
93                 Console.WriteLine ("other class not preserved in state");
94                 return 16;
95             }
96         }
97
98         try {
99             didAbort = false;
100             test.abortString ();
101         } catch (ThreadAbortException e) {
102             object state;
103             state = e.ExceptionState;
104             Thread.ResetAbort ();
105             didAbort = true;
106             if (state == null) {
107                     Console.WriteLine ("state is null");
108                     return 13;
109             } else {
110                     if (RemotingServices.IsTransparentProxy (state)) {
111                             Console.WriteLine ("state is proxy");
112                             return 1;
113                     }
114                     if (!((string)state).Equals ("bla")) {
115                             Console.WriteLine ("state is wrong: " + (string)state);
116                             return 2;
117                     }
118             }
119             if (RemotingServices.IsTransparentProxy (e)) {
120                 Console.WriteLine ("exception is proxy");
121                 return 3;
122             }
123             if (test.getState () != null) {
124                 Console.WriteLine ("have state");
125                 return 12;
126             }
127         }
128         if (!didAbort) {
129             Console.WriteLine ("no abort");
130             return 4;
131         }
132
133         try {
134             didAbort = false;
135             test.abortProxy ();
136         } catch (ThreadAbortException e) {
137             object state;
138             state = e.ExceptionState;
139             Thread.ResetAbort ();
140             didAbort = true;
141             if (state == null) {
142                     Console.WriteLine ("state is null");
143                     return 14;
144             } else {
145                     if (!RemotingServices.IsTransparentProxy (state)) {
146                             Console.WriteLine ("state is not proxy");
147                             return 6;
148                     }
149                     if (((Test)state).getInt () != 123) {
150                             Console.WriteLine ("state doesn't work");
151                             return 15;
152                     }
153             }
154             if (RemotingServices.IsTransparentProxy (e)) {
155                     Console.WriteLine ("exception is proxy");
156                     return 7;
157             }
158         }
159         if (!didAbort) {
160             Console.WriteLine ("no abort");
161             return 8;
162         }
163
164         try {
165             didAbort = false;
166             test.abortOther ();
167         } catch (ThreadAbortException e) {
168             object state = null;
169             bool stateExc = false;
170
171             didAbort = true;
172
173             try {
174                 state = e.ExceptionState;
175                 Console.WriteLine ("have state");
176             } catch (Exception) {
177                 stateExc = true;
178                 /* FIXME: if we put this after the try/catch, mono
179                    quietly quits */
180                 Thread.ResetAbort ();
181             }
182             if (!stateExc) {
183                 Console.WriteLine ("no state exception");
184                 return 9;
185             }
186
187             if (RemotingServices.IsTransparentProxy (e)) {
188                 Console.WriteLine ("exception is proxy");
189                 return 10;
190             }
191         }
192         if (!didAbort) {
193             Console.WriteLine ("no abort");
194             return 11;
195         }
196
197         // #539394
198         // Calling Thread.Abort () from a remoting call throws a ThreadAbortException which
199         // cannot be caught because the exception handling code is confused by the domain
200         // transitions
201         bool res = false;
202
203         Thread thread = new Thread (delegate () {
204                         AppDomain d = AppDomain.CreateDomain ("foo");
205
206                         var t = (Test1)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName,
207                                                                                                                   "Test1");
208                         res = t.Run ();
209                 });
210
211         thread.Start ();
212         thread.Join ();
213
214         if (!res)
215                 return 12;
216
217         Console.WriteLine ("done");
218
219         return 0;
220     }
221 }