|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | +using AustinHarris.JsonRpc; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace AustinHarris.JsonRpcTestN |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Reserved method names (<c>rpc.</c>-prefixed and <c>$/cancelRequest</c>) are refused by |
| 12 | + /// <see cref="SMDServiceCollection"/> itself, so every registration path refuses them. |
| 13 | + /// </summary> |
| 14 | + [TestFixture] |
| 15 | + public sealed class ReservedNameTests |
| 16 | + { |
| 17 | + private const string Session = "reserved-names"; |
| 18 | + private static readonly string[] Reserved = { "rpc.x", "$/cancelRequest" }; |
| 19 | + private static readonly string[] Allowed = { "$/progress", "rpcx", "Rpc.x", "x.rpc.y", "$/cancelrequest" }; |
| 20 | + private static SMDServiceCollection Services => Handler.GetSessionHandler(Session).MetaData.Services; |
| 21 | + |
| 22 | + [TearDown] |
| 23 | + public void Clean() => Handler.DestroySession(Session); |
| 24 | + |
| 25 | + private static SMDService NewService(int result) |
| 26 | + { |
| 27 | + return new SMDService("POST", "JSON-RPC-2.0", new Dictionary<string, Type> { ["returns"] = typeof(int) }, new Dictionary<string, object>(), new Func<int>(() => result)); |
| 28 | + } |
| 29 | + |
| 30 | + private static SMDService Find(string name) => Services.Find(Encoding.UTF8.GetBytes(name)); |
| 31 | + |
| 32 | + private static void AssertReserved(string name, TestDelegate register) |
| 33 | + { |
| 34 | + var ex = Assert.Throws<ArgumentException>(register, name); |
| 35 | + StringAssert.StartsWith("'" + name + "' is a reserved JSON-RPC method name.", ex.Message); |
| 36 | + Assert.IsFalse(Services.ContainsKey(name), name); |
| 37 | + Assert.IsNull(Find(name), name); |
| 38 | + } |
| 39 | + |
| 40 | + private static void AssertRegistered(string name) |
| 41 | + { |
| 42 | + Assert.IsTrue(Services.ContainsKey(name), name); |
| 43 | + Assert.IsNotNull(Find(name), name); |
| 44 | + } |
| 45 | + |
| 46 | + private interface IPair |
| 47 | + { |
| 48 | + int First(); |
| 49 | + int Second(); |
| 50 | + } |
| 51 | + |
| 52 | + private sealed class Pair : IPair |
| 53 | + { |
| 54 | + public int First() => 1; |
| 55 | + public int Second() => 2; |
| 56 | + } |
| 57 | + |
| 58 | + private sealed class ReservedAlias |
| 59 | + { |
| 60 | + [JsonRpcMethod("rpc.x")] |
| 61 | + public int M() => 1; |
| 62 | + } |
| 63 | + |
| 64 | + private sealed class ReservedCancelAlias |
| 65 | + { |
| 66 | + [JsonRpcMethod("$/cancelRequest")] |
| 67 | + public int M() => 1; |
| 68 | + } |
| 69 | + |
| 70 | + private sealed class AllowedAliases |
| 71 | + { |
| 72 | + [JsonRpcMethod("$/progress")] |
| 73 | + [JsonRpcMethod("rpcx")] |
| 74 | + [JsonRpcMethod("Rpc.x")] |
| 75 | + [JsonRpcMethod("x.rpc.y")] |
| 76 | + [JsonRpcMethod("$/cancelrequest")] |
| 77 | + public int M() => 1; |
| 78 | + } |
| 79 | + |
| 80 | + private sealed class NullKeyEntries : IReadOnlyDictionary<string, SMDService> |
| 81 | + { |
| 82 | + public int Count => 1; |
| 83 | + public IEnumerable<string> Keys => new string[] { null }; |
| 84 | + public IEnumerable<SMDService> Values => new SMDService[] { null }; |
| 85 | + public SMDService this[string key] => throw new NotImplementedException(); |
| 86 | + public bool ContainsKey(string key) => false; |
| 87 | + public bool TryGetValue(string key, out SMDService value) { value = null; return false; } |
| 88 | + public IEnumerator<KeyValuePair<string, SMDService>> GetEnumerator() |
| 89 | + { |
| 90 | + yield return new KeyValuePair<string, SMDService>(null, null); |
| 91 | + } |
| 92 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void BindMethod_RefusesReserved_AcceptsTheRest() |
| 97 | + { |
| 98 | + foreach (var name in Reserved) |
| 99 | + AssertReserved(name, () => ServiceBinder.BindMethod(Session, name, () => 1)); |
| 100 | + foreach (var name in Allowed) |
| 101 | + { |
| 102 | + ServiceBinder.BindMethod(Session, name, () => 7); |
| 103 | + Assert.AreEqual("{\"jsonrpc\":\"2.0\",\"result\":7,\"id\":1}", |
| 104 | + JsonRpcProcessor.ProcessSync(Session, "{\"method\":\"" + name + "\",\"id\":1}", null), name); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public void BindInterface_RefusesReserved_AcceptsTheRest() |
| 110 | + { |
| 111 | + foreach (var name in Reserved) |
| 112 | + { |
| 113 | + AssertReserved(name, () => ServiceBinder.BindInterface<IPair>(Session, new Pair(), |
| 114 | + new RpcInterfaceBindingOptions { NameRule = m => m.Leaf == "First" ? name : "second" })); |
| 115 | + Assert.AreEqual(0, Services.Count); |
| 116 | + } |
| 117 | + foreach (var name in Allowed) |
| 118 | + { |
| 119 | + ServiceBinder.BindInterface<IPair>(Session, new Pair(), |
| 120 | + new RpcInterfaceBindingOptions { NameRule = m => m.Leaf == "First" ? name : "second." + name }); |
| 121 | + AssertRegistered(name); |
| 122 | + AssertRegistered("second." + name); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public void AttributeBinder_RefusesReservedAliases_AcceptsTheRest() |
| 128 | + { |
| 129 | + AssertReserved("rpc.x", () => ServiceBinder.BindService(Session, new ReservedAlias())); |
| 130 | + AssertReserved("$/cancelRequest", () => ServiceBinder.BindService(Session, new ReservedCancelAlias())); |
| 131 | + Assert.AreEqual(0, Services.Count); |
| 132 | + ServiceBinder.BindService(Session, new AllowedAliases()); |
| 133 | + foreach (var name in Allowed) AssertRegistered(name); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + public void RegisterFuction_RefusesReserved_AcceptsTheRest() |
| 138 | + { |
| 139 | + var handler = Handler.GetSessionHandler(Session); |
| 140 | +#pragma warning disable CS0618 |
| 141 | + foreach (var name in Reserved) |
| 142 | + AssertReserved(name, () => handler.RegisterFuction(name, new Dictionary<string, Type> { ["returns"] = typeof(int) }, null, new Func<int>(() => 1))); |
| 143 | + foreach (var name in Allowed) |
| 144 | + { |
| 145 | + handler.RegisterFuction(name, new Dictionary<string, Type> { ["returns"] = typeof(int) }, null, new Func<int>(() => 1)); |
| 146 | + AssertRegistered(name); |
| 147 | + } |
| 148 | +#pragma warning restore CS0618 |
| 149 | + } |
| 150 | + |
| 151 | + [Test] |
| 152 | + public void DirectCollectionAdds_RefuseReserved_AcceptTheRest() |
| 153 | + { |
| 154 | + var services = Services; |
| 155 | + foreach (var name in Reserved) |
| 156 | + { |
| 157 | + AssertReserved(name, () => services.Add(name, NewService(1))); |
| 158 | + AssertReserved(name, () => services.Add(new KeyValuePair<string, SMDService>(name, NewService(1)))); |
| 159 | + AssertReserved(name, () => services[name] = NewService(1)); |
| 160 | + Assert.AreEqual("key", Assert.Throws<ArgumentException>(() => services.Add(name, NewService(1))).ParamName); |
| 161 | + Assert.AreEqual("key", Assert.Throws<ArgumentException>(() => services[name] = NewService(1)).ParamName); |
| 162 | + } |
| 163 | + Assert.AreEqual(0, services.Count); |
| 164 | + |
| 165 | + foreach (var name in Allowed) |
| 166 | + { |
| 167 | + services.Add(name, NewService(1)); |
| 168 | + AssertRegistered(name); |
| 169 | + Assert.IsTrue(services.Remove(name)); |
| 170 | + services.Add(new KeyValuePair<string, SMDService>(name, NewService(2))); |
| 171 | + AssertRegistered(name); |
| 172 | + var replacement = NewService(3); |
| 173 | + services[name] = replacement; |
| 174 | + Assert.AreSame(replacement, Find(name)); |
| 175 | + } |
| 176 | + |
| 177 | + Assert.Throws<ArgumentNullException>(() => services.Add(null, NewService(1))); |
| 178 | + Assert.Throws<ArgumentNullException>(() => services[null] = NewService(1)); |
| 179 | + } |
| 180 | + |
| 181 | + [Test] |
| 182 | + public void Names_AreComparedAsGiven() |
| 183 | + { |
| 184 | + // no trimming and no case folding: these are ordinary names |
| 185 | + foreach (var name in new[] { " rpc.x", "RPC.x", "$/CancelRequest", "$/cancelRequest ", "rpc" }) |
| 186 | + { |
| 187 | + Services.Add(name, NewService(1)); |
| 188 | + AssertRegistered(name); |
| 189 | + } |
| 190 | + AssertReserved("rpc.", () => Services.Add("rpc.", NewService(1))); |
| 191 | + } |
| 192 | + |
| 193 | + [Test] |
| 194 | + public void AddBatch_WithOneReservedEntry_LeavesTheCollectionUnchanged() |
| 195 | + { |
| 196 | + ServiceBinder.BindMethod(Session, "before", () => 1); |
| 197 | + var before = Find("before"); |
| 198 | + int count = Services.Count; |
| 199 | + |
| 200 | + var ex = Assert.Throws<ArgumentException>(() => ServiceBinder.BindInterface<IPair>(Session, new Pair(), |
| 201 | + new RpcInterfaceBindingOptions { NameRule = m => m.Leaf == "First" ? "ok" : "$/cancelRequest" })); |
| 202 | + Assert.AreEqual("entries", ex.ParamName); |
| 203 | + |
| 204 | + Assert.AreEqual(count, Services.Count); |
| 205 | + Assert.AreSame(before, Find("before")); |
| 206 | + Assert.IsNull(Find("ok")); |
| 207 | + Assert.IsNull(Find("$/cancelRequest")); |
| 208 | + Assert.IsFalse(Services.ContainsKey("ok")); |
| 209 | + CollectionAssert.AreEqual(new[] { "before" }, Services.Keys); |
| 210 | + } |
| 211 | + |
| 212 | + [Test] |
| 213 | + public void AddBatch_NullName_RetainsArgumentNullException() |
| 214 | + { |
| 215 | + var ex = Assert.Throws<ArgumentNullException>(() => Services.AddBatch(new NullKeyEntries())); |
| 216 | + Assert.AreEqual(0, Services.Count); |
| 217 | + } |
| 218 | + |
| 219 | + [Test] |
| 220 | + public void AddReserved_AcceptsReservedOnce_AndKeepsTheDuplicateRule() |
| 221 | + { |
| 222 | + var first = NewService(1); |
| 223 | + Services.AddReserved("rpc.discover", first); |
| 224 | + Assert.AreSame(first, Find("rpc.discover")); |
| 225 | + Assert.AreSame(first, Services["rpc.discover"]); |
| 226 | + |
| 227 | + Assert.Throws<ArgumentException>(() => Services.AddReserved("rpc.discover", NewService(2))); |
| 228 | + Assert.AreSame(first, Find("rpc.discover"), "the first registration stands"); |
| 229 | + Assert.AreEqual(1, Services.Count); |
| 230 | + |
| 231 | + Assert.Throws<ArgumentNullException>(() => Services.AddReserved(null, NewService(1))); |
| 232 | + Assert.Throws<ArgumentNullException>(() => Services.AddReserved("rpc.other", null)); |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments