Skip to content

Commit

Permalink
feat: make IPolicy synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Aug 20, 2019
1 parent 117fddf commit a493add
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 178 deletions.
10 changes: 2 additions & 8 deletions src/BlackList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ public class BlackList<T> : ConcurrentBag<T>, IPolicy<T>
where T : IEquatable<T>
{
/// <inheritdoc />
public Task<bool> IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
public bool IsAllowed(T target)
{
return Task.FromResult(!this.Contains(target));
return !this.Contains(target);
}

/// <inheritdoc />
public async Task<bool> IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
{
var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false);
return !q;
}
}
}
24 changes: 3 additions & 21 deletions src/IPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,10 @@ interface IPolicy<T>
/// <param name="target">
/// An object to test against the rule.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <returns>
/// A task that represents the asynchronous operation. The task's result is
/// <b>true</b> if the <paramref name="target"/> passes the rule.
/// </returns>
Task<bool> IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken));

/// <summary>
/// Determines if the target fails the rule.
/// </summary>
/// <param name="target">
/// An object to test against the rule.
/// </param>
/// <param name="cancel">
/// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised.
/// </param>
/// <returns>
/// A task that represents the asynchronous operation. The task's result is
/// <b>true</b> if the <paramref name="target"/> fails the rule.
/// <b>true</b> if the <paramref name="target"/> passes the rule;
/// otherwise <b>false</b>.
/// </returns>
Task<bool> IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken));
bool IsAllowed(T target);
}
}
9 changes: 2 additions & 7 deletions src/MultiAddressBlackList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace PeerTalk
public class MultiAddressBlackList : List<MultiAddress>, IPolicy<MultiAddress>
{
/// <inheritdoc />
public Task<bool> IsAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken))
public bool IsAllowed(MultiAddress target)
{
return Task.FromResult(!this.Any(filter => Matches(filter, target)));
return !this.Any(filter => Matches(filter, target));
}

bool Matches(MultiAddress filter, MultiAddress target)
Expand All @@ -30,10 +30,5 @@ bool Matches(MultiAddress filter, MultiAddress target)
.All(fp => target.Protocols.Any(tp => tp.Code == fp.Code && tp.Value == fp.Value));
}

/// <inheritdoc />
public async Task<bool> IsNotAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken))
{
return !await IsAllowedAsync(target, cancel).ConfigureAwait(false);
}
}
}
11 changes: 3 additions & 8 deletions src/MultiAddressWhiteList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ namespace PeerTalk
public class MultiAddressWhiteList : ConcurrentBag<MultiAddress>, IPolicy<MultiAddress>
{
/// <inheritdoc />
public Task<bool> IsAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken))
public bool IsAllowed(MultiAddress target)
{
if (IsEmpty)
return Task.FromResult(true);
return true;

return Task.FromResult(this.Any(filter => Matches(filter, target)));
return this.Any(filter => Matches(filter, target));
}

bool Matches(MultiAddress filter, MultiAddress target)
Expand All @@ -34,10 +34,5 @@ bool Matches(MultiAddress filter, MultiAddress target)
.All(fp => target.Protocols.Any(tp => tp.Code == fp.Code && tp.Value == fp.Value));
}

/// <inheritdoc />
public async Task<bool> IsNotAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken))
{
return !await IsAllowedAsync(target, cancel).ConfigureAwait(false);
}
}
}
15 changes: 7 additions & 8 deletions src/Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ namespace PeerTalk
public abstract class Policy<T> : IPolicy<T>
{
/// <inheritdoc />
public abstract Task<bool> IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken));
public abstract bool IsAllowed(T target);

/// <inheritdoc />
public async Task<bool> IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
public bool IsNotAllowed(T target)
{
var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false);
return !q;
return !IsAllowed(target);
}
}

Expand All @@ -35,9 +34,9 @@ public async Task<bool> IsNotAllowedAsync(T target, CancellationToken cancel = d
public class PolicyAlways<T> : Policy<T>
{
/// <inheritdoc />
public override Task<bool> IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
public override bool IsAllowed(T target)
{
return Task.FromResult(true);
return true;
}
}

Expand All @@ -50,9 +49,9 @@ public override Task<bool> IsAllowedAsync(T target, CancellationToken cancel = d
public class PolicyNever<T> : Policy<T>
{
/// <inheritdoc />
public override Task<bool> IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
public override bool IsAllowed(T target)
{
return Task.FromResult(false);
return false;
}
}
}
18 changes: 6 additions & 12 deletions src/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ public IEnumerable<Peer> KnownPeers
/// added to the <see cref="KnownPeerAddresses"/>.
/// </remarks>
/// <seealso cref="RegisterPeer(Peer)"/>
public async Task<Peer> RegisterPeerAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken))
public Task<Peer> RegisterPeerAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken))
{
var peerId = address.PeerId;
if (peerId == LocalPeer.Id)
{
throw new Exception("Cannot register to self.");
}

if (!await IsAllowedAsync(address, cancel).ConfigureAwait(false))
if (!IsAllowed(address))
{
throw new Exception($"Communication with '{address}' is not allowed.");
}
Expand All @@ -250,7 +250,7 @@ public async Task<Peer> RegisterPeerAsync(MultiAddress address, CancellationToke
Addresses = new List<MultiAddress> { address }
};

return RegisterPeer(peer);
return Task.FromResult(RegisterPeer(peer));
}

/// <summary>
Expand Down Expand Up @@ -1047,17 +1047,11 @@ public async Task StopListeningAsync(MultiAddress address)
}

/// <inheritdoc />
public async Task<bool> IsAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken))
public bool IsAllowed(MultiAddress target)
{
return await BlackList.IsAllowedAsync(target, cancel).ConfigureAwait(false)
&& await WhiteList.IsAllowedAsync(target, cancel).ConfigureAwait(false);
return BlackList.IsAllowed(target)
&& WhiteList.IsAllowed(target);
}

/// <inheritdoc />
public async Task<bool> IsNotAllowedAsync(MultiAddress target, CancellationToken cancel = default(CancellationToken))
{
var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false);
return !q;
}
}
}
11 changes: 2 additions & 9 deletions src/WhiteList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ public class WhiteList<T> : ConcurrentBag<T>, IPolicy<T>
where T : IEquatable<T>
{
/// <inheritdoc />
public Task<bool> IsAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
public bool IsAllowed(T target)
{
return Task.FromResult(this.IsEmpty || this.Contains(target));
}

/// <inheritdoc />
public async Task<bool> IsNotAllowedAsync(T target, CancellationToken cancel = default(CancellationToken))
{
var q = await IsAllowedAsync(target, cancel).ConfigureAwait(false);
return !q;
return this.IsEmpty || this.Contains(target);
}
}
}
27 changes: 7 additions & 20 deletions test/BlackListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,22 @@ namespace PeerTalk
public class BlackListTest
{
[TestMethod]
public async Task Allowed()
public void Allowed()
{
var policy = new BlackList<string>();
policy.Add("c");
policy.Add("d");
Assert.IsTrue(await policy.IsAllowedAsync("a"));
Assert.IsTrue(await policy.IsAllowedAsync("b"));
Assert.IsFalse(await policy.IsAllowedAsync("c"));
Assert.IsFalse(await policy.IsAllowedAsync("d"));
Assert.IsTrue(policy.IsAllowed("a"));
Assert.IsTrue(policy.IsAllowed("b"));
Assert.IsFalse(policy.IsAllowed("c"));
Assert.IsFalse(policy.IsAllowed("d"));
}

[TestMethod]
public async Task NotAllowed()
public void Empty()
{
var policy = new BlackList<string>();
policy.Add("c");
policy.Add("d");
Assert.IsFalse(await policy.IsNotAllowedAsync("a"));
Assert.IsFalse(await policy.IsNotAllowedAsync("b"));
Assert.IsTrue(await policy.IsNotAllowedAsync("c"));
Assert.IsTrue(await policy.IsNotAllowedAsync("d"));
}

[TestMethod]
public async Task Empty()
{
var policy = new BlackList<string>();
Assert.IsTrue(await policy.IsAllowedAsync("a"));
Assert.IsFalse(await policy.IsNotAllowedAsync("a"));
Assert.IsTrue(policy.IsAllowed("a"));
}
}
}
42 changes: 14 additions & 28 deletions test/MultiAdressBlackListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,35 @@ public class MultiAddressBlackListTest
MultiAddress d = "/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64";

[TestMethod]
public async Task Allowed()
public void Allowed()
{
var policy = new MultiAddressBlackList();
policy.Add(a);
policy.Add(b);
Assert.IsFalse(await policy.IsAllowedAsync(a));
Assert.IsFalse(await policy.IsAllowedAsync(a1));
Assert.IsFalse(await policy.IsAllowedAsync(b));
Assert.IsTrue(await policy.IsAllowedAsync(c));
Assert.IsTrue(await policy.IsAllowedAsync(d));
Assert.IsFalse(policy.IsAllowed(a));
Assert.IsFalse(policy.IsAllowed(a1));
Assert.IsFalse(policy.IsAllowed(b));
Assert.IsTrue(policy.IsAllowed(c));
Assert.IsTrue(policy.IsAllowed(d));
}

[TestMethod]
public async Task Allowed_Alias()
public void Allowed_Alias()
{
var policy = new MultiAddressBlackList();
policy.Add(a);
Assert.IsFalse(await policy.IsAllowedAsync(a));
Assert.IsFalse(await policy.IsAllowedAsync(a1));
Assert.IsFalse(await policy.IsAllowedAsync(b));
Assert.IsTrue(await policy.IsAllowedAsync(c));
Assert.IsTrue(await policy.IsAllowedAsync(d));
Assert.IsFalse(policy.IsAllowed(a));
Assert.IsFalse(policy.IsAllowed(a1));
Assert.IsFalse(policy.IsAllowed(b));
Assert.IsTrue(policy.IsAllowed(c));
Assert.IsTrue(policy.IsAllowed(d));
}

[TestMethod]
public async Task NotAllowed()
public void Empty()
{
var policy = new MultiAddressBlackList();
policy.Add(a);
policy.Add(b);
Assert.IsTrue(await policy.IsNotAllowedAsync(a));
Assert.IsTrue(await policy.IsNotAllowedAsync(a1));
Assert.IsTrue(await policy.IsNotAllowedAsync(b));
Assert.IsFalse(await policy.IsNotAllowedAsync(c));
Assert.IsFalse(await policy.IsNotAllowedAsync(d));
}

[TestMethod]
public async Task Empty()
{
var policy = new MultiAddressBlackList();
Assert.IsTrue(await policy.IsAllowedAsync(a));
Assert.IsFalse(await policy.IsNotAllowedAsync(a));
Assert.IsTrue( policy.IsAllowed(a));
}
}
}
42 changes: 14 additions & 28 deletions test/MultiAdressWhiteListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,35 @@ public class MultiAddressWhiteListTest
MultiAddress d = "/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64";

[TestMethod]
public async Task Allowed()
public void Allowed()
{
var policy = new MultiAddressWhiteList();
policy.Add(a);
policy.Add(b);
Assert.IsTrue(await policy.IsAllowedAsync(a));
Assert.IsTrue(await policy.IsAllowedAsync(a1));
Assert.IsTrue(await policy.IsAllowedAsync(b));
Assert.IsFalse(await policy.IsAllowedAsync(c));
Assert.IsFalse(await policy.IsAllowedAsync(d));
Assert.IsTrue(policy.IsAllowed(a));
Assert.IsTrue(policy.IsAllowed(a1));
Assert.IsTrue(policy.IsAllowed(b));
Assert.IsFalse(policy.IsAllowed(c));
Assert.IsFalse(policy.IsAllowed(d));
}

[TestMethod]
public async Task Allowed_Alias()
public void Allowed_Alias()
{
var policy = new MultiAddressWhiteList();
policy.Add(a);
Assert.IsTrue(await policy.IsAllowedAsync(a));
Assert.IsTrue(await policy.IsAllowedAsync(a1));
Assert.IsTrue(await policy.IsAllowedAsync(b));
Assert.IsFalse(await policy.IsAllowedAsync(c));
Assert.IsFalse(await policy.IsAllowedAsync(d));
Assert.IsTrue(policy.IsAllowed(a));
Assert.IsTrue(policy.IsAllowed(a1));
Assert.IsTrue(policy.IsAllowed(b));
Assert.IsFalse(policy.IsAllowed(c));
Assert.IsFalse(policy.IsAllowed(d));
}

[TestMethod]
public async Task NotAllowed()
public void Empty()
{
var policy = new MultiAddressWhiteList();
policy.Add(a);
policy.Add(b);
Assert.IsFalse(await policy.IsNotAllowedAsync(a));
Assert.IsFalse(await policy.IsNotAllowedAsync(a1));
Assert.IsFalse(await policy.IsNotAllowedAsync(b));
Assert.IsTrue(await policy.IsNotAllowedAsync(c));
Assert.IsTrue(await policy.IsNotAllowedAsync(d));
}

[TestMethod]
public async Task Empty()
{
var policy = new MultiAddressWhiteList();
Assert.IsTrue(await policy.IsAllowedAsync(a));
Assert.IsFalse(await policy.IsNotAllowedAsync(a));
Assert.IsTrue(policy.IsAllowed(a));
}
}
}
Loading

0 comments on commit a493add

Please sign in to comment.