Merge pull request #1595 from directhex/randomize-tcp-ports-in-tests
[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 (sb);
67                         Test.CheckValue (sb, new StringBuilder ("un"), 190);
68                         
69                 }
70                 catch (TestException ex)
71                 {
72                         Console.WriteLine ("TEST ERROR ({0}): {1}", ex.Code, ex);
73                         return ex.Code;
74                 }
75                 catch (Exception ex)
76                 {
77                         Console.WriteLine ("TEST ERROR: " + ex);
78                         return -1;
79                 }
80                 return 0;
81         }
82         
83         public static void CheckDomain (object ob, Type t, int ec)
84         {
85                 if (ob == null) return;
86                 if (ob.GetType () != t) {
87                         if (t.ToString() == ob.GetType().ToString())
88                                 throw new TestException ("Parameter not properly marshalled", ec);
89                         else
90                                 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
91                 }
92         }
93         
94         public static void CheckValue (object ob1, object ob2, int ec)
95         {
96                 if ((ob1 == null || ob2 == null) && ob1 != ob2)
97                         throw new TestException ("Null objects are not equal", ec);
98                 
99                 if (ob2.GetType () != ob1.GetType ())
100                         throw new TestException ("Wrong type (maybe wrong domain?)", ec);
101                 
102                 if (ob1 is StringBuilder) {
103                         if (Object.ReferenceEquals (ob1, ob2))
104                                 throw new TestException ("StringBuilders are ReferenceEquals", ec);
105
106                         StringBuilder sb1 = (StringBuilder) ob1;
107                         StringBuilder sb2 = (StringBuilder) ob2;
108
109                         if (sb1.ToString () != sb2.ToString ())
110                                 throw new TestException ("Strings are not equal", ec);
111                 }
112                 else if (!ob1.Equals (ob2))
113                         throw new TestException ("Objects are not equal", ec);
114         }
115 }
116
117 public class Remo: MarshalByRefObject
118 {
119         int domid;
120         
121         public Remo ()
122         {
123                 domid = System.Threading.Thread.GetDomainID ();
124         }
125         
126         public int GetDomainId ()
127         {
128                 return domid;
129         }
130         
131         public void CheckThisDomain (int ec)
132         {
133                 if (domid != System.Threading.Thread.GetDomainID ())
134                         throw new TestException ("Wrong domain", ec);
135         }
136         
137         public void Run ()
138         {
139                 CheckThisDomain (10);
140         }
141         
142         public void Run2 (int a, string b)
143         {
144                 CheckThisDomain (20);
145                 Test.CheckValue (a, 88, 21);
146                 Test.CheckValue (b, "hola", 22);
147         }
148         
149         public void Run3 (int a, Dada d, string b)
150         {
151                 CheckThisDomain (30);
152                 Test.CheckValue (a, 99, 31);
153                 Test.CheckValue (b, "adeu", 32);
154                 Test.CheckValue (d.p, 22, 33);
155         }
156         
157         public string Run4 (int a, Dada d, string b)
158         {
159                 CheckThisDomain (40);
160                 Test.CheckValue (a, 200, 41);
161                 Test.CheckValue (b, "que", 42);
162                 Test.CheckValue (d.p, 22, 43);
163                 return "vist";
164         }
165         
166         public Dada Run5 (int a, Dada d, string b)
167         {
168                 CheckThisDomain (50);
169                 Test.CheckValue (a, 200, 51);
170                 Test.CheckValue (b, "que", 52);
171                 Test.CheckValue (d.p, 22, 53);
172                 Peta ();
173                 return d;
174         }
175         
176         public Dada Run6 (int a, out Dada d, string b)
177         {
178                 CheckThisDomain (60);
179                 Test.CheckValue (a, 99, 61);
180                 Test.CheckValue (b, "adeu", 62);
181                 
182                 d = new Dada ();
183                 d.p = 987;
184                 return d;
185         }
186         
187         public Dada Run7 (Dada d)
188         {
189                 CheckThisDomain (70);
190                 Test.CheckValue (d.p, 22, 71);
191                 d.p = 33;
192                 return d;
193         }
194
195         public void Run8 ([In,Out] byte[] bytes)
196         {
197                 CheckThisDomain (80);
198                 Test.CheckDomain (bytes, typeof(byte[]), 81);
199                 for (int n=0; n < bytes.Length; n++) {
200                         Test.CheckValue (bytes[n], (byte)n, 82);
201                         bytes[n] = (byte) (bytes.Length - n);
202                 }
203         }
204         
205         public void Run9 ([In,Out] StringBuilder sb)
206         {
207                 CheckThisDomain (90);
208                 Test.CheckValue (sb, new StringBuilder ("un"), 91);
209                 sb.Append ("-dos");
210         }
211         
212         public void Peta ()
213         {
214                 throw new Exception ("peta");
215         }
216 }
217
218 [Serializable]
219 public class Dada
220 {
221         public int p;
222 }
223
224 [Serializable]
225 public class MyException: Exception
226 {
227         public MyException (string s): base (s) {}
228 }
229
230 [Serializable]
231 public class TestException: Exception
232 {
233         public int Code = -1;
234         
235         public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
236                 Code = i.GetInt32  ("Code");
237         }
238         
239         public TestException (string txt, int code): base (txt + " (code: " + code + ")")
240         {
241                 Code = code;
242         }
243         
244         public override void GetObjectData (SerializationInfo info, StreamingContext context)
245         {
246                 base.GetObjectData (info, context);
247                 info.AddValue ("Code", Code);
248         }
249 }