Skip to content

Commit 986977d

Browse files
author
Elad Zelingher
committed
Some WampCra client/server integration tests
1 parent 80b0556 commit 986977d

File tree

6 files changed

+309
-0
lines changed

6 files changed

+309
-0
lines changed

src/mono/Tests/WampSharp.Tests.Wampv2/WampSharp.Tests.Wampv2.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
</Reference>
5151
<Reference Include="System" />
5252
<Reference Include="System.Core" />
53+
<Reference Include="System.Reactive.Core">
54+
<Private>True</Private>
55+
</Reference>
56+
<Reference Include="System.Reactive.Interfaces">
57+
<Private>True</Private>
58+
</Reference>
5359
<Reference Include="System.Runtime.Serialization" />
5460
<Reference Include="System.Xml.Linq" />
5561
<Reference Include="System.Data.DataSetExtensions" />
@@ -154,6 +160,9 @@
154160
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\Integration\SharedRpcTests.cs">
155161
<Link>Integration\SharedRpcTests.cs</Link>
156162
</Compile>
163+
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\Integration\WampCraAuthenticationTests.cs">
164+
<Link>Integration\WampCraAuthenticationTests.cs</Link>
165+
</Compile>
157166
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs">
158167
<Link>RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs</Link>
159168
</Compile>

src/net40/Tests/WampSharp.Tests.Wampv2/WampSharp.Tests.Wampv2.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
</Reference>
5151
<Reference Include="System" />
5252
<Reference Include="System.Core" />
53+
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54+
<SpecificVersion>False</SpecificVersion>
55+
<HintPath>..\..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
56+
</Reference>
57+
<Reference Include="System.Reactive.Interfaces, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58+
<SpecificVersion>False</SpecificVersion>
59+
<HintPath>..\..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
60+
</Reference>
5361
<Reference Include="System.Runtime.Serialization" />
5462
<Reference Include="System.Xml.Linq" />
5563
<Reference Include="System.Data.DataSetExtensions" />
@@ -154,6 +162,9 @@
154162
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\Integration\SharedRpcTests.cs">
155163
<Link>Integration\SharedRpcTests.cs</Link>
156164
</Compile>
165+
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\Integration\WampCraAuthenticationTests.cs">
166+
<Link>Integration\WampCraAuthenticationTests.cs</Link>
167+
</Compile>
157168
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs">
158169
<Link>RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs</Link>
159170
</Compile>
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
using WampSharp.Tests.Wampv2.TestHelpers.Integration;
6+
using WampSharp.V2;
7+
using WampSharp.V2.Authentication;
8+
using WampSharp.V2.Client;
9+
using WampSharp.V2.Core.Contracts;
10+
using WampSharp.V2.Realm;
11+
using WampSharp.V2.Rpc;
12+
13+
namespace WampSharp.Tests.Wampv2.Integration
14+
{
15+
[TestFixture]
16+
public class WampCraAuthenticationTests
17+
{
18+
[Test]
19+
public async Task AuthenticatedIntegrationTest()
20+
{
21+
WampAuthenticationPlayground playground =
22+
new WampAuthenticationPlayground
23+
(new WampCraUserDbAuthenticationFactory
24+
(new MyAuthenticationProvider(),
25+
new MyUserDb()));
26+
27+
SetupHost(playground);
28+
29+
IWampClientAuthenticator authenticator =
30+
new WampCraClientAuthenticator(authenticationId: "peter", secret: "secret1");
31+
32+
IWampChannel channel =
33+
playground.CreateNewChannel("realm1", authenticator);
34+
35+
IWampRealmProxy realmProxy = channel.RealmProxy;
36+
37+
await channel.Open().ConfigureAwait(false);
38+
39+
// call a procedure we are allowed to call (so this should succeed)
40+
//
41+
IAdd2AsyncService proxy = realmProxy.Services.GetCalleeProxy<IAdd2AsyncService>();
42+
43+
int five = await proxy.Add2(2, 3).ConfigureAwait(false);
44+
45+
Assert.That(five, Is.EqualTo(5));
46+
47+
// (try to) register a procedure where we are not allowed to (so this should fail)
48+
//
49+
Mul2Service service = new Mul2Service();
50+
51+
WampException registerException = null;
52+
53+
try
54+
{
55+
await realmProxy.Services.RegisterCallee(service)
56+
.ConfigureAwait(false);
57+
}
58+
catch (WampException ex)
59+
{
60+
registerException = ex;
61+
}
62+
63+
Assert.That(registerException, Is.Not.Null);
64+
65+
// (try to) publish to some topics
66+
//
67+
string[] topics =
68+
{
69+
"com.example.topic1",
70+
"com.example.topic2",
71+
"com.foobar.topic1",
72+
"com.foobar.topic2"
73+
};
74+
75+
List<string> successfulTopics = new List<string>();
76+
77+
foreach (string topic in topics)
78+
{
79+
IWampTopicProxy topicProxy = realmProxy.TopicContainer.GetTopicByUri(topic);
80+
81+
try
82+
{
83+
await topicProxy.Publish(new PublishOptions() { Acknowledge = true },
84+
new object[] { "hello" })
85+
.ConfigureAwait(false);
86+
87+
successfulTopics.Add(topic);
88+
}
89+
catch (WampException ex)
90+
{
91+
}
92+
}
93+
94+
Assert.That(successfulTopics, Is.EquivalentTo(new string[]
95+
{
96+
"com.foobar.topic1",
97+
"com.example.topic1"
98+
}));
99+
}
100+
101+
[Test]
102+
public async Task NotAuthenticatedIntegrationTest()
103+
{
104+
WampAuthenticationPlayground playground =
105+
new WampAuthenticationPlayground
106+
(new WampCraUserDbAuthenticationFactory
107+
(new MyAuthenticationProvider(),
108+
new MyUserDb()));
109+
110+
SetupHost(playground);
111+
112+
IWampClientAuthenticator authenticator =
113+
new WampCraClientAuthenticator(authenticationId: "peter", secret: "SECRET");
114+
115+
IWampChannel channel =
116+
playground.CreateNewChannel("realm1", authenticator);
117+
118+
IWampRealmProxy realmProxy = channel.RealmProxy;
119+
120+
WampConnectionBrokenException openException = null;
121+
122+
try
123+
{
124+
await channel.Open().ConfigureAwait(false);
125+
}
126+
catch (WampConnectionBrokenException ex)
127+
{
128+
openException = ex;
129+
}
130+
131+
Assert.That(openException, Is.Not.Null);
132+
Assert.That(openException.CloseType, Is.EqualTo(SessionCloseType.Abort));
133+
Assert.That(openException.Reason, Is.EqualTo(WampErrors.NotAuthorized));
134+
}
135+
136+
private static void SetupHost(WampAuthenticationPlayground playground)
137+
{
138+
IWampHost host = playground.Host;
139+
140+
IWampHostedRealm realm = host.RealmContainer.GetRealmByName("realm1");
141+
142+
string[] topics = new[]
143+
{
144+
"com.example.topic1",
145+
"com.example.topic2",
146+
"com.foobar.topic1",
147+
"com.foobar.topic2"
148+
};
149+
150+
foreach (string topic in topics)
151+
{
152+
realm.TopicContainer.CreateTopicByUri(topic, true);
153+
}
154+
155+
realm.Services.RegisterCallee(new Add2Service());
156+
157+
host.Open();
158+
}
159+
160+
public class Add2Service : IAdd2Service
161+
{
162+
public int Add2(int x, int y)
163+
{
164+
return (x + y);
165+
}
166+
}
167+
168+
public interface IAdd2AsyncService
169+
{
170+
[WampProcedure("com.example.add2")]
171+
Task<int> Add2(int a, int b);
172+
}
173+
174+
public interface IAdd2Service
175+
{
176+
[WampProcedure("com.example.add2")]
177+
int Add2(int a, int b);
178+
}
179+
180+
private class MyAuthenticationProvider : WampStaticAuthenticationProvider
181+
{
182+
public MyAuthenticationProvider() :
183+
base(new Dictionary<string, IDictionary<string, WampAuthenticationRole>>()
184+
{
185+
{
186+
"realm1", new Dictionary<string, WampAuthenticationRole>()
187+
{
188+
{
189+
"frontend",
190+
new WampAuthenticationRole
191+
{
192+
Authorizer = new WampStaticAuthorizer(new List<WampUriPermissions>
193+
{
194+
new WampUriPermissions
195+
{
196+
Uri = "com.example.add2",
197+
CanCall = true
198+
},
199+
new WampUriPermissions
200+
{
201+
Uri = "com.example.",
202+
Prefixed = true,
203+
CanPublish = true
204+
},
205+
new WampUriPermissions
206+
{
207+
Uri = "com.example.topic2",
208+
CanPublish = false
209+
},
210+
new WampUriPermissions
211+
{
212+
Uri = "com.foobar.topic1",
213+
CanPublish = true
214+
},
215+
})
216+
}
217+
}
218+
}
219+
}
220+
})
221+
{
222+
}
223+
}
224+
225+
private class MyUserDb : WampCraStaticUserDb
226+
{
227+
public MyUserDb() : base(new Dictionary<string, WampCraUser>()
228+
{
229+
{
230+
"joe", new WampCraUser()
231+
{
232+
Secret = "secret2",
233+
AuthenticationRole = "frontend"
234+
}
235+
},
236+
{
237+
"peter", new WampCraUser()
238+
{
239+
Secret = "prq7+YkJ1/KlW1X0YczMHw==",
240+
AuthenticationRole = "frontend",
241+
Salt = "salt123",
242+
Iterations = 100,
243+
KeyLength = 16
244+
}
245+
},
246+
})
247+
{
248+
}
249+
}
250+
251+
private class Mul2Service
252+
{
253+
[WampProcedure("com.example.mul2")]
254+
public int Multiply2(int x, int y)
255+
{
256+
return x * y;
257+
}
258+
}
259+
}
260+
}

src/net45/Tests/WampSharp.Tests.Wampv2/TestHelpers/Integration/WampPlayground.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public IWampChannel CreateNewChannel(string realm)
8080
Binding);
8181
}
8282

83+
public IWampChannel CreateNewChannel(string realm,
84+
IWampClientAuthenticator authenticator)
85+
{
86+
return mChannelFactory.CreateChannel(realm,
87+
CreateClientConnection(),
88+
Binding,
89+
authenticator);
90+
}
91+
8392
public IWampServerProxy CreateRawConnection(IWampClient<TMessage> client)
8493
{
8594
IControlledWampConnection<TMessage> connection = CreateClientConnection();

src/net45/Tests/WampSharp.Tests.Wampv2/WampSharp.Tests.Wampv2.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
</Reference>
5151
<Reference Include="System" />
5252
<Reference Include="System.Core" />
53+
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54+
<SpecificVersion>False</SpecificVersion>
55+
<HintPath>..\..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
56+
</Reference>
57+
<Reference Include="System.Reactive.Interfaces, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58+
<SpecificVersion>False</SpecificVersion>
59+
<HintPath>..\..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
60+
</Reference>
5361
<Reference Include="System.Runtime.Serialization" />
5462
<Reference Include="System.Xml.Linq" />
5563
<Reference Include="System.Data.DataSetExtensions" />
@@ -90,6 +98,7 @@
9098
<Compile Include="Integration\RpcServices\TimeService.cs" />
9199
<Compile Include="Integration\SerializedValue.cs" />
92100
<Compile Include="Integration\SharedRpcTests.cs" />
101+
<Compile Include="Integration\WampCraAuthenticationTests.cs" />
93102
<Compile Include="RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs" />
94103
<Compile Include="RequestMapper.cs" />
95104
<Compile Include="TestHelpers\Integration\MockTransport.cs" />

src/pcl/Tests/WampSharp.Tests.Wampv2/WampSharp.Tests.Wampv2.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
</Reference>
5151
<Reference Include="System" />
5252
<Reference Include="System.Core" />
53+
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54+
<SpecificVersion>False</SpecificVersion>
55+
<HintPath>..\..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
56+
</Reference>
57+
<Reference Include="System.Reactive.Interfaces, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
58+
<SpecificVersion>False</SpecificVersion>
59+
<HintPath>..\..\packages\Rx-Interfaces.2.2.5\lib\net45\System.Reactive.Interfaces.dll</HintPath>
60+
</Reference>
5361
<Reference Include="System.Runtime.Serialization" />
5462
<Reference Include="System.Xml.Linq" />
5563
<Reference Include="System.Data.DataSetExtensions" />
@@ -154,6 +162,9 @@
154162
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\Integration\SharedRpcTests.cs">
155163
<Link>Integration\SharedRpcTests.cs</Link>
156164
</Compile>
165+
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\Integration\WampCraAuthenticationTests.cs">
166+
<Link>Integration\WampCraAuthenticationTests.cs</Link>
167+
</Compile>
157168
<Compile Include="..\..\..\net45\Tests\WampSharp.Tests.Wampv2\RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs">
158169
<Link>RecordedTests\MockBuilder\WelcomeDetailsInterceptor.cs</Link>
159170
</Compile>

0 commit comments

Comments
 (0)