2010-02-19 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / cross-domain.cs
blob0db2f5bb3cd31e819816596436d0066631195fa0
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;
8 public class Test: MarshalByRefObject
9 {
10 public static int Main (string[] args)
12 AppDomain domain = AppDomain.CreateDomain ("testdomain1");
13 Test server = (Test) domain.CreateInstanceAndUnwrap(typeof(Test).Assembly.FullName, "Test");
14 return server.RunTest ();
17 public int RunTest ()
19 try
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);
28 Dada d = new Dada ();
29 d.p = 22;
31 server.Run ();
32 server.Run2 (88, "hola");
33 server.Run3 (99, d, "adeu");
35 string r = server.Run4 (200, d, "que");
36 CheckValue (r, "vist", 140);
38 try {
39 server.Run5 (200, d, "que");
40 throw new TestException ("Exception expected", 150);
42 catch (Exception ex) {
43 CheckValue (ex.Message, "peta", 151);
46 Dada d2;
47 d = server.Run6 (99, out d2, "adeu");
48 CheckValue (d.p, 987, 161);
49 CheckValue (d2.p, 987, 162);
51 d.p = 22;
52 d2 = server.Run7 (d);
53 CheckValue (d.p, 22, 170);
54 CheckValue (d2.p, 33, 170);
56 byte[] ba = new byte[5];
57 for (byte n=0; n<ba.Length; n++)
58 ba [n] = n;
60 server.Run8 (ba);
62 for (int n=0; n<ba.Length; n++)
63 CheckValue (ba[n], (byte) (ba.Length - n), 180);
65 StringBuilder sb = new StringBuilder ("un");
66 server.Run9 (sb);
67 Test.CheckValue (sb, new StringBuilder ("un"), 190);
70 catch (TestException ex)
72 Console.WriteLine ("TEST ERROR ({0}): {1}", ex.Code, ex);
73 return ex.Code;
75 catch (Exception ex)
77 Console.WriteLine ("TEST ERROR: " + ex);
78 return -1;
80 return 0;
83 public static void CheckDomain (object ob, Type t, int ec)
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);
94 public static void CheckValue (object ob1, object ob2, int ec)
96 if ((ob1 == null || ob2 == null) && ob1 != ob2)
97 throw new TestException ("Null objects are not equal", ec);
99 if (ob2.GetType () != ob1.GetType ())
100 throw new TestException ("Wrong type (maybe wrong domain?)", ec);
102 if (ob1 is StringBuilder) {
103 if (Object.ReferenceEquals (ob1, ob2))
104 throw new TestException ("StringBuilders are ReferenceEquals", ec);
106 StringBuilder sb1 = (StringBuilder) ob1;
107 StringBuilder sb2 = (StringBuilder) ob2;
109 if (sb1.ToString () != sb2.ToString ())
110 throw new TestException ("Strings are not equal", ec);
112 else if (!ob1.Equals (ob2))
113 throw new TestException ("Objects are not equal", ec);
117 public class Remo: MarshalByRefObject
119 int domid;
121 public Remo ()
123 domid = System.Threading.Thread.GetDomainID ();
126 public int GetDomainId ()
128 return domid;
131 public void CheckThisDomain (int ec)
133 if (domid != System.Threading.Thread.GetDomainID ())
134 throw new TestException ("Wrong domain", ec);
137 public void Run ()
139 CheckThisDomain (10);
142 public void Run2 (int a, string b)
144 CheckThisDomain (20);
145 Test.CheckValue (a, 88, 21);
146 Test.CheckValue (b, "hola", 22);
149 public void Run3 (int a, Dada d, string b)
151 CheckThisDomain (30);
152 Test.CheckValue (a, 99, 31);
153 Test.CheckValue (b, "adeu", 32);
154 Test.CheckValue (d.p, 22, 33);
157 public string Run4 (int a, Dada d, string b)
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";
166 public Dada Run5 (int a, Dada d, string b)
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;
176 public Dada Run6 (int a, out Dada d, string b)
178 CheckThisDomain (60);
179 Test.CheckValue (a, 99, 61);
180 Test.CheckValue (b, "adeu", 62);
182 d = new Dada ();
183 d.p = 987;
184 return d;
187 public Dada Run7 (Dada d)
189 CheckThisDomain (70);
190 Test.CheckValue (d.p, 22, 71);
191 d.p = 33;
192 return d;
195 public void Run8 ([In,Out] byte[] bytes)
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);
205 public void Run9 ([In,Out] StringBuilder sb)
207 CheckThisDomain (90);
208 Test.CheckValue (sb, new StringBuilder ("un"), 91);
209 sb.Append ("-dos");
212 public void Peta ()
214 throw new Exception ("peta");
218 [Serializable]
219 public class Dada
221 public int p;
224 [Serializable]
225 public class MyException: Exception
227 public MyException (string s): base (s) {}
230 [Serializable]
231 public class TestException: Exception
233 public int Code = -1;
235 public TestException (SerializationInfo i, StreamingContext ctx): base (i, ctx) {
236 Code = i.GetInt32 ("Code");
239 public TestException (string txt, int code): base (txt + " (code: " + code + ")")
241 Code = code;
244 public override void GetObjectData (SerializationInfo info, StreamingContext context)
246 base.GetObjectData (info, context);
247 info.AddValue ("Code", Code);