[interp] disable some tests that fail on CI only
[mono.git] / mono / tests / cross-domain.cs
1 using System;
2 using System.Net;
3 using System.Runtime.Remoting;
4 using System.Runtime.InteropServices;
5 using System.Text;
6 using System.Runtime.Serialization;
7
8 public class Test: MarshalByRefObject
9 {       
10         public static int Main (string[] args)
11         {
12                 AppDomain domain = AppDomain.CreateDomain ("testdomain1");
13                 Test server = (Test) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, "Test");
14                 return server.RunTest ();
15         }
16
17         public int RunTest ()
18         {
19                 try
20                 {
21                         object t = null;
22                         string s = (string)t;
23                         AppDomain domain = AppDomain.CreateDomain ("testdomain");
24                         Remo server = (Remo) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName,"Remo");
25                         if (System.Threading.Thread.GetDomainID () == server.GetDomainId ())
26                                 throw new TestException ("Object not created in new domain", 1);
27         
28                         Dada d = new Dada ();
29                         d.p = 22;
30                         
31                         server.Run ();
32                         server.Run2 (88, "hola");
33                         server.Run3 (99, d, "adeu");
34                         
35                         string r = server.Run4 (200, d, "que");
36                         CheckValue (r, "vist", 140);
37                         
38                         try {
39                                 server.Run5 (200, d, "que");
40                                 throw new TestException ("Exception expected", 150);
41                         }
42                         catch (Exception ex) {
43                                 CheckValue (ex.Message, "peta", 151);
44                         }
45                         
46                         Dada d2;
47                         d = server.Run6 (99, out d2, "adeu");
48                         CheckValue (d.p, 987, 161);
49                         CheckValue (d2.p, 987, 162);
50                                 
51                         d.p = 22;
52                         d2 = server.Run7 (d);
53                         CheckValue (d.p, 22, 170);
54                         CheckValue (d2.p, 33, 170);
55                         
56                         byte[] ba = new byte[5];
57                         for (byte n=0; n<ba.Length; n++)
58                                 ba [n] = n;
59                                 
60                         server.Run8 (ba);
61
62                         for (int n=0; n<ba.Length; n++)
63                                 CheckValue (ba[n], (byte) (ba.Length - n), 180);
64                         
65                         StringBuilder sb = new StringBuilder ("un");
66                         server.Run9_1 (sb);
67                         Test.CheckValue (sb, new StringBuilder ("un"), 190);
68
69                         StringBuilder sb2 = new StringBuilder ("prefix");
70                         StringBuilder sb3 = server.Run9_2 (sb2);
71                         Test.CheckValue (sb2, new StringBuilder ("prefix"), 192);
72                         Test.CheckValue (sb3, new StringBuilder ("prefix-middle"), 192);
73                         StringBuilder sb4 = server.Run9_3 (sb3);
74                         Test.CheckValue (sb3, new StringBuilder ("prefix-middle"), 193);
75                         Test.CheckValue (sb4, new StringBuilder ("prefix-middle-end"), 193);
76
77                         
78                 }
79                 catch (TestException ex)
80                 {
81                         Console.WriteLine ("TEST ERROR ({0}): {1}", ex.Code, ex);
82                         return ex.Code;
83                 }
84                 catch (Exception ex)
85                 {
86                         Console.WriteLine ("TEST ERROR: " + ex);
87                         return -1;
88                 }
89                 return 0;
90         }
91         
92         public static void CheckDomain (object ob, Type t, int ec)
93         {
94                 if (ob == null) return;
95                 if (ob.GetType () != t) {
96                         if (t.ToString() == ob.GetType().ToString())
97                                 throw new TestException ("Parameter not properly marshalled", ec);
98                         else
99                                 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
100                 }
101         }
102         
103         public static void CheckValue (object ob1, object ob2, int ec)
104         {
105                 if ((ob1 == null || ob2 == null) && ob1 != ob2)
106                         throw new TestException ("Null objects are not equal", ec);
107                 
108                 if (ob2.GetType () != ob1.GetType ())
109                         throw new TestException ("Wrong type (maybe wrong domain?)", ec);
110                 
111                 if (ob1 is StringBuilder) {
112                         if (Object.ReferenceEquals (ob1, ob2))
113                                 throw new TestException ("StringBuilders are ReferenceEquals", ec);
114
115                         StringBuilder sb1 = (StringBuilder) ob1;
116                         StringBuilder sb2 = (StringBuilder) ob2;
117
118                         if (sb1.ToString () != sb2.ToString ())
119                                 throw new TestException ("String in StringBuilders are not equal", ec);
120
121                         if (sb1.Length != sb2.Length)
122                                 throw new TestException ("Lengths in StringBuilders are not equal", ec);
123                 }
124                 else if (!ob1.Equals (ob2))
125                         throw new TestException ("Objects are not equal", ec);
126         }
127 }
128
129 public class Remo: MarshalByRefObject
130 {
131         int domid;
132         
133         public Remo ()
134         {
135                 domid = System.Threading.Thread.GetDomainID ();
136         }
137         
138         public int GetDomainId ()
139         {
140                 return domid;
141         }
142         
143         public void CheckThisDomain (int ec)
144         {
145                 if (domid != System.Threading.Thread.GetDomainID ())
146                         throw new TestException ("Wrong domain", ec);
147         }
148         
149         public void Run ()
150         {
151                 CheckThisDomain (10);
152         }
153         
154         public void Run2 (int a, string b)
155         {
156                 CheckThisDomain (20);
157                 Test.CheckValue (a, 88, 21);
158                 Test.CheckValue (b, "hola", 22);
159         }
160         
161         public void Run3 (int a, Dada d, string b)
162         {
163                 CheckThisDomain (30);
164                 Test.CheckValue (a, 99, 31);
165                 Test.CheckValue (b, "adeu", 32);
166                 Test.CheckValue (d.p, 22, 33);
167         }
168         
169         public string Run4 (int a, Dada d, string b)
170         {
171                 CheckThisDomain (40);
172                 Test.CheckValue (a, 200, 41);
173                 Test.CheckValue (b, "que", 42);
174                 Test.CheckValue (d.p, 22, 43);
175                 return "vist";
176         }
177         
178         public Dada Run5 (int a, Dada d, string b)
179         {
180                 CheckThisDomain (50);
181                 Test.CheckValue (a, 200, 51);
182                 Test.CheckValue (b, "que", 52);
183                 Test.CheckValue (d.p, 22, 53);
184                 Peta ();
185                 return d;
186         }
187         
188         public Dada Run6 (int a, out Dada d, string b)
189         {
190                 CheckThisDomain (60);
191                 Test.CheckValue (a, 99, 61);
192                 Test.CheckValue (b, "adeu", 62);
193                 
194                 d = new Dada ();
195                 d.p = 987;
196                 return d;
197         }
198         
199         public Dada Run7 (Dada d)
200         {
201                 CheckThisDomain (70);
202                 Test.CheckValue (d.p, 22, 71);
203                 d.p = 33;
204                 return d;
205         }
206
207         public void Run8 ([In,Out] byte[] bytes)
208         {
209                 CheckThisDomain (80);
210                 Test.CheckDomain (bytes, typeof(byte[]), 81);
211                 for (int n=0; n < bytes.Length; n++) {
212                         Test.CheckValue (bytes[n], (byte)n, 82);
213                         bytes[n] = (byte) (bytes.Length - n);
214                 }
215         }
216
217         public void Run9_1 ([In,Out] StringBuilder sb)
218         {
219                 CheckThisDomain (90);
220                 Test.CheckValue (sb, new StringBuilder ("un"), 91);
221                 sb.Append ("-dos");
222         }
223
224         public StringBuilder Run9_2 ([In] StringBuilder sb)
225         {
226                 CheckThisDomain (91);
227                 Test.CheckValue (sb, new StringBuilder ("prefix"), 92);
228                 sb.Append ("-middle");
229                 return sb;
230         }
231
232         public StringBuilder Run9_3 ([In,Out] StringBuilder sb)
233         {
234                 CheckThisDomain (92);
235                 Test.CheckValue (sb, new StringBuilder ("prefix-middle"), 93);
236                 sb.Append ("-end");
237
238                 return sb;
239         }
240
241         public void Peta ()
242         {
243                 throw new Exception ("peta");
244         }
245 }
246
247 [Serializable]
248 public class Dada
249 {
250         public int p;
251 }
252
253 [Serializable]
254 public class MyException: Exception
255 {
256         public MyException (string s): base (s) {}
257 }
258
259 [Serializable]
260 public class TestException: Exception
261 {
262         public int Code = -1;
263         
264         public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
265                 Code = i.GetInt32  ("Code");
266         }
267         
268         public TestException (string txt, int code): base (txt + " (code: " + code + ")")
269         {
270                 Code = code;
271         }
272         
273         public override void GetObjectData (SerializationInfo info, StreamingContext context)
274         {
275                 base.GetObjectData (info, context);
276                 info.AddValue ("Code", Code);
277         }
278 }