//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.Mocks {
using System.Threading;
using System.Threading.Tasks;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Bindings;
using NUnit.Framework;
internal class MockReplayProtectionBindingElement : IChannelBindingElement {
private bool messageReceived;
#region IChannelBindingElement Members
MessageProtections IChannelBindingElement.Protection {
get { return MessageProtections.ReplayProtection; }
}
///
/// Gets or sets the channel that this binding element belongs to.
///
public Channel Channel { get; set; }
Task IChannelBindingElement.ProcessOutgoingMessageAsync(IProtocolMessage message, CancellationToken cancellationToken) {
var replayMessage = message as IReplayProtectedProtocolMessage;
if (replayMessage != null) {
replayMessage.Nonce = "someNonce";
return MessageProtectionTasks.ReplayProtection;
}
return MessageProtectionTasks.Null;
}
Task IChannelBindingElement.ProcessIncomingMessageAsync(IProtocolMessage message, CancellationToken cancellationToken) {
var replayMessage = message as IReplayProtectedProtocolMessage;
if (replayMessage != null) {
Assert.AreEqual("someNonce", replayMessage.Nonce, "The nonce didn't serialize correctly, or something");
// this mock implementation passes the first time and fails subsequent times.
if (this.messageReceived) {
throw new ReplayedMessageException(message);
}
this.messageReceived = true;
return MessageProtectionTasks.ReplayProtection;
}
return MessageProtectionTasks.Null;
}
#endregion
}
}