1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
//-----------------------------------------------------------------------
// <copyright file="Coordinator.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOAuth.Test.Scenarios {
using System;
using System.Threading;
using DotNetOAuth.OAuth;
using DotNetOAuth.Test.Mocks;
using DotNetOAuth.Test.OAuth;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Runs a Consumer and Service Provider simultaneously so they can interact in a full simulation.
/// </summary>
internal class Coordinator {
private ConsumerDescription consumerDescription;
private ServiceProviderDescription serviceDescription;
private Action<WebConsumer> consumerAction;
private Action<ServiceProvider> serviceProviderAction;
/// <summary>Initializes a new instance of the <see cref="Coordinator"/> class.</summary>
/// <param name="consumerDescription">The description of the consumer.</param>
/// <param name="serviceDescription">The service description that will be used to construct the Consumer and ServiceProvider objects.</param>
/// <param name="consumerAction">The code path of the Consumer.</param>
/// <param name="serviceProviderAction">The code path of the Service Provider.</param>
internal Coordinator(ConsumerDescription consumerDescription, ServiceProviderDescription serviceDescription, Action<WebConsumer> consumerAction, Action<ServiceProvider> serviceProviderAction) {
if (consumerDescription == null) {
throw new ArgumentNullException("consumerDescription");
}
if (serviceDescription == null) {
throw new ArgumentNullException("serviceDescription");
}
if (consumerAction == null) {
throw new ArgumentNullException("consumerAction");
}
if (serviceProviderAction == null) {
throw new ArgumentNullException("serviceProviderAction");
}
this.consumerDescription = consumerDescription;
this.serviceDescription = serviceDescription;
this.consumerAction = consumerAction;
this.serviceProviderAction = serviceProviderAction;
}
/// <summary>
/// Starts the simulation.
/// </summary>
internal void Run() {
// Clone the template signing binding element.
var signingElement = this.serviceDescription.CreateTamperProtectionElement();
var consumerSigningElement = signingElement.Clone();
var spSigningElement = signingElement.Clone();
// Prepare token managers
InMemoryTokenManager consumerTokenManager = new InMemoryTokenManager();
InMemoryTokenManager serviceTokenManager = new InMemoryTokenManager();
consumerTokenManager.AddConsumer(this.consumerDescription);
serviceTokenManager.AddConsumer(this.consumerDescription);
// Prepare channels that will pass messages directly back and forth.
CoordinatingOAuthChannel consumerChannel = new CoordinatingOAuthChannel(consumerSigningElement, true, consumerTokenManager);
CoordinatingOAuthChannel serviceProviderChannel = new CoordinatingOAuthChannel(spSigningElement, false, serviceTokenManager);
consumerChannel.RemoteChannel = serviceProviderChannel;
serviceProviderChannel.RemoteChannel = consumerChannel;
// Prepare the Consumer and Service Provider objects
WebConsumer consumer = new WebConsumer(this.serviceDescription, consumerTokenManager) {
OAuthChannel = consumerChannel,
ConsumerKey = this.consumerDescription.ConsumerKey,
};
ServiceProvider serviceProvider = new ServiceProvider(this.serviceDescription, serviceTokenManager) {
OAuthChannel = serviceProviderChannel,
};
Thread consumerThread = null, serviceProviderThread = null;
Exception failingException = null;
// Each thread we create needs a surrounding exception catcher so that we can
// terminate the other thread and inform the test host that the test failed.
Action<Action> safeWrapper = (action) => {
try {
action();
} catch (Exception ex) {
// We may be the second thread in an ThreadAbortException, so check the "flag"
if (failingException == null) {
failingException = ex;
if (Thread.CurrentThread == consumerThread) {
serviceProviderThread.Abort();
} else {
consumerThread.Abort();
}
}
}
};
// Run the threads, and wait for them to complete.
// If this main thread is aborted (test run aborted), go ahead and abort the other two threads.
consumerThread = new Thread(() => { safeWrapper(() => { consumerAction(consumer); }); });
serviceProviderThread = new Thread(() => { safeWrapper(() => { serviceProviderAction(serviceProvider); }); });
try {
consumerThread.Start();
serviceProviderThread.Start();
consumerThread.Join();
serviceProviderThread.Join();
} catch (ThreadAbortException) {
consumerThread.Abort();
serviceProviderThread.Abort();
throw;
}
// Use the failing reason of a failing sub-thread as our reason, if anything failed.
if (failingException != null) {
throw new AssertFailedException("Coordinator thread threw unhandled exception: " + failingException, failingException);
}
}
}
}
|