New test.
[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         class R1 : MarshalByRefObject 
104         {
105                 public R2 TestMBV() {
106                         return new R2();
107                 }
108         }
109
110         class Class1
111         {
112                 static int Main(string[] args)
113                 {
114                         Console.WriteLine("test " + AppDomain.CurrentDomain.FriendlyName);
115                         AppDomain app2 = AppDomain.CreateDomain("2");
116
117                         if (!RemotingServices.IsTransparentProxy(app2)) 
118                                 return 1;                               
119
120                         ObjectHandle o = AppDomain.CurrentDomain.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
121                         R1 myobj = (R1) o.Unwrap();
122                         
123                         // should not be a proxy in our domain..
124                         if (RemotingServices.IsTransparentProxy(myobj)) 
125                         {
126                                 Console.WriteLine("CreateInstance return TP for in our current domain");
127                                 return 2;                               
128                         }
129
130                         o = app2.CreateInstance(typeof(R1).Assembly.FullName, typeof(R1).FullName);
131
132                         Console.WriteLine("type: " + o.GetType().ToString());
133
134                         myobj = (R1) o.Unwrap();
135                         if (!RemotingServices.IsTransparentProxy(myobj))
136                                 return 3;
137
138                         Console.WriteLine("unwrapped type: " + myobj.GetType().ToString());
139
140                         R2 r2 = null;
141                         bool bSerExc = false;
142
143                         // this should crash
144                         try
145                         {
146                                 r2 = myobj.TestMBV();
147                         }               
148                         catch (SerializationException)
149                         {
150                                 bSerExc = true;
151                         }
152
153                         if (!bSerExc)
154                                 return 4;
155         
156                         Console.WriteLine("test-ok");
157                         return 0;
158                 }
159         }
160 }