Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / remoting4.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.InteropServices;
4 using System.Runtime.Remoting;
5 using System.Runtime.Remoting.Messaging;
6 using System.Runtime.Remoting.Proxies;
7 using System.Runtime.Remoting.Channels;
8 using System.Runtime.Serialization;
9
10 namespace RemotingTest
11 {
12         class MyProxy : RealProxy 
13         {
14                 readonly MarshalByRefObject target;
15
16                 public MyProxy (MarshalByRefObject target) : base (target.GetType()) 
17                 {
18                         this.target = target;
19                 }
20
21                 public override IMessage Invoke (IMessage request) 
22                 {
23                         IMethodCallMessage call = (IMethodCallMessage)request;
24                         Console.WriteLine ("Invoke " + call.MethodName);
25
26                         Console.Write ("ARGS(");
27                         for (int i = 0; i < call.ArgCount; i++) 
28                         {
29                                 if (i != 0)
30                                         Console.Write (", ");
31                                 Console.Write (call.GetArgName (i) +  " " +
32                                         call.GetArg (i));
33                         }
34                         Console.WriteLine (")");
35                         Console.Write ("INARGS(");
36                         for (int i = 0; i < call.InArgCount; i++) 
37                         {
38                                 if (i != 0)
39                                         Console.Write (", ");
40                                 Console.Write (call.GetInArgName (i) +  " " +
41                                         call.GetInArg (i));
42                         }
43                         Console.WriteLine (")");
44
45                         IMethodReturnMessage res = RemotingServices.ExecuteMessage (target, call);
46
47                         Console.Write ("RESARGS(");
48                         for (int i = 0; i < res.ArgCount; i++) 
49                         {
50                                 if (i != 0)
51                                         Console.Write (", ");
52                                 Console.Write (res.GetArgName (i) +  " " +
53                                         res.GetArg (i));
54                         }
55                         Console.WriteLine (")");                
56                 
57                         Console.Write ("RESOUTARGS(");
58                         for (int i = 0; i < res.OutArgCount; i++) 
59                         {
60                                 if (i != 0)
61                                         Console.Write (", ");
62                                 Console.Write (res.GetOutArgName (i) +  " " +
63                                         res.GetOutArg (i));
64                         }
65                         Console.WriteLine (")");                
66                 
67                         return res;
68                 }
69         }
70
71         class R2 
72         {
73                 string sTest;
74                 public R2() 
75                 {
76                         sTest = "R2";
77                 }
78
79                 public void Print() 
80                 {
81                         Console.WriteLine(sTest);
82                 }
83         }
84
85         [Serializable]
86         class R2_MBV
87         {
88                 string sTest;
89                 public R2_MBV() 
90                 {
91                         sTest = "R2";
92                 }
93
94                 public string Data
95                 {
96                         get 
97                         {
98                                 return sTest;
99                         }
100                 }
101         }
102
103         interface GenericIFace {
104                 T Foo <T> ();
105         }
106
107         class R1 : MarshalByRefObject, GenericIFace
108         {
109                 public R2 TestMBV() {
110                         return new R2();
111                 }
112
113                 public T Foo <T> () {
114                         return default (T);
115                 }
116         }
117
118         class Class1
119         {
120                 static int Main(string[] args)
121                 {
122                         Console.WriteLine("test " + AppDomain.CurrentDomain.FriendlyName);
123                         AppDomain app2 = AppDomain.CreateDomain("2");
124
125                         if (!RemotingServices.IsTransparentProxy(app2)) 
126                                 return 1;                               
127
128                         ObjectHandle o = AppDomain.CurrentDomain.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
129                         R1 myobj = (R1) o.Unwrap();
130                         
131                         // should not be a proxy in our domain..
132                         if (RemotingServices.IsTransparentProxy(myobj)) 
133                         {
134                                 Console.WriteLine("CreateInstance return TP for in our current domain");
135                                 return 2;                               
136                         }
137
138                         o = app2.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
139
140                         Console.WriteLine("type: " + o.GetType().ToString());
141
142                         myobj = (R1) o.Unwrap();
143                         if (!RemotingServices.IsTransparentProxy(myobj))
144                                 return 3;
145
146                         Console.WriteLine("unwrapped type: " + myobj.GetType().ToString());
147
148                         R2 r2 = null;
149                         bool bSerExc = false;
150
151                         // this should crash
152                         try
153                         {
154                                 r2 = myobj.TestMBV();
155                         }               
156                         catch (SerializationException)
157                         {
158                                 bSerExc = true;
159                         }
160
161                         if (!bSerExc)
162                                 return 4;
163
164                         // Test generic virtual interface methods on proxies
165
166                         o = app2.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
167                         myobj = (R1) o.Unwrap();
168
169                         GenericIFace iface = (GenericIFace)myobj;
170                         if (iface.Foo <int> () != 0)
171                                 return 5;
172                         if (iface.Foo <string> () != null)
173                                 return 6;
174
175                         // Test type identity (#504886, comment #10 ff.)
176
177                         if (typeof (R1) != myobj.GetType ())
178                                 return 7;
179         
180                         AppDomain.Unload (app2);
181
182                         Console.WriteLine("test-ok");
183                         return 0;
184                 }
185         }
186 }