1 #region License, Terms and Conditions
2 //
3 // Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
4 // Written by Atif Aziz (www.raboof.com)
5 // Copyright (c) Atif Aziz. All rights reserved.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 // details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this library; if not, write to the Free Software Foundation, Inc.,
19 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 #endregion
22 
23 namespace Jayrock.Services
24 {
25     #region Imports
26 
27     using System;
28     using System.Security;
29     using System.Security.Permissions;
30     using Jayrock.Json;
31     using Jayrock.JsonRpc;
32     using NUnit.Framework;
33 
34     #endregion
35 
36     [ TestFixture ]
37     public class TestMethod
38     {
39         [ Test ]
FailedMethodYieldsInvocationException()40         public void FailedMethodYieldsInvocationException()
41         {
42             try
43             {
44                 TestService service = new TestService();
45                 service.GetClass().GetMethodByName("BadMethod").Invoke(service, null, null);
46                 Assert.Fail("Expecting an exception.");
47             }
48             catch (TargetMethodException e)
49             {
50                 Assert.IsTrue(e.InnerException.GetType() == typeof(ApplicationException), "Unexpected inner exception ({0}).", e.InnerException.GetType().FullName);
51             }
52         }
53 
54         [ Test ]
VariableArguments()55         public void VariableArguments()
56         {
57             TestService service = new TestService();
58             Method method = service.GetClass().FindMethodByName("VarMethod");
59             object[] args = new object[] { 1, 2, 3, 4 };
60             object[] invokeArgs = method.TransposeVariableArguments(args);
61             Assert.AreEqual(1, invokeArgs.Length);
62             Assert.AreEqual(args, invokeArgs[0]);
63         }
64 
65         [ Test ]
FixedAndVariableArguments()66         public void FixedAndVariableArguments()
67         {
68             TestService service = new TestService();
69             Method method = service.GetClass().FindMethodByName("FixedVarMethod");
70             object[] args = new object[] { 1, 2, 3, 4 };
71             args = method.TransposeVariableArguments(args);
72             Assert.AreEqual(3, args.Length);
73             Assert.AreEqual(1, args[0]);
74             Assert.AreEqual(2, args[1]);
75             Assert.AreEqual(new object[] { 3, 4 }, args[2]);
76         }
77 
78         [ Test ]
RetransposingYieldsTheSame()79         public void RetransposingYieldsTheSame()
80         {
81             TestService service = new TestService();
82             Method method = service.GetClass().FindMethodByName("FixedVarMethod");
83             object[] args = new object[] { 1, 2, 3, 4 };
84             args = method.TransposeVariableArguments(args);
85             args = method.TransposeVariableArguments(args);
86             Assert.AreEqual(3, args.Length);
87             Assert.AreEqual(1, args[0]);
88             Assert.AreEqual(2, args[1]);
89             Assert.AreEqual(new object[] { 3, 4 }, args[2]);
90         }
91 
92         [ Test ]
Bug8131()93         public void Bug8131()
94         {
95             //
96             // Bug #8131: Varargs transposition when last arg is collection
97             // http://developer.berlios.de/bugs/?func=detailbug&bug_id=8131&group_id=4638
98             //
99 
100             TestService service = new TestService();
101             Method method = service.GetClass().FindMethodByName("VarMethod");
102             object[] args = new object[] { 1, 2, new int[] { 3, 4 } };
103             args = method.TransposeVariableArguments(args);
104             Assert.AreEqual(1, args.Length);
105             object[] varargs = (object[]) args[0];
106             Assert.AreEqual(3, varargs.Length);
107             Assert.AreEqual(1, varargs[0]);
108             Assert.AreEqual(2, varargs[1]);
109             Assert.AreEqual(new int[] { 3, 4 }, varargs[2]);
110         }
111 
112         [ Test ]
Bug8148()113         public void Bug8148()
114         {
115             //
116             // Bug #8148: Varargs transposition drops args
117             // http://developer.berlios.de/bugs/?func=detailbug&bug_id=8148&group_id=4638
118             //
119 
120             TestService service = new TestService();
121             Method method = service.GetClass().FindMethodByName("FixedVarMethod");
122             object[] args = new object[] { 1, 2,
123                 new int[] { 3, 4 },
124                 new JsonObject(new string[] { "five", "six" }, new object[] { 5, 6 }) };
125             args = method.TransposeVariableArguments(args);
126             Assert.AreEqual(3, args.Length);
127             Assert.AreEqual(1, args[0]);
128             Assert.AreEqual(2, args[1]);
129             object[] varargs = (object[]) args[2];
130             Assert.AreEqual(2, varargs.Length);
131             Assert.AreEqual(new int[] { 3, 4 }, varargs[0]);
132             JsonObject o = (JsonObject) varargs[1];
133             Assert.AreEqual(5, o["five"]);
134             Assert.AreEqual(6, o["six"]);
135         }
136 
137         [Test, ExpectedException(typeof(SecurityException))]
138         #if MONO
139         [Ignore("See http://code.google.com/p/jayrock/issues/detail?id=23")]
140         #endif
Issue12()141         public void Issue12()
142         {
143             // Issue #12: PrincipalPermissionAttribute causes InvalidCastException when applied to service methods
144             // http://code.google.com/p/jayrock/issues/detail?id=12
145 
146             SecuredService service = new SecuredService();
147             Method method = service.GetClass().GetMethodByName("SecuredMethod");
148             try
149             {
150                 method.Invoke(service, null, null);
151             }
152             catch (TargetMethodException e)
153             {
154                 Assert.IsNotNull(e.InnerException);
155                 throw e.InnerException;
156             }
157         }
158 
159         private sealed class TestService : JsonRpcService
160         {
161             [ JsonRpcMethod ]
BadMethod()162             public void BadMethod()
163             {
164                 throw new ApplicationException();
165             }
166 
167             [ JsonRpcMethod ]
VarMethod(params object[] args)168             public void VarMethod(params object[] args)
169             {
170             }
171 
172             [ JsonRpcMethod ]
FixedVarMethod(int a, int b, params object[] args)173             public void FixedVarMethod(int a, int b, params object[] args)
174             {
175             }
176         }
177 
178         private sealed class SecuredService : JsonRpcService
179         {
180             [JsonRpcMethod, PrincipalPermission(SecurityAction.Demand)]
SecuredMethod()181             public void SecuredMethod() { }
182         }
183     }
184 }
185 
186