Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Tidying up
Browse files Browse the repository at this point in the history
Remove vestiges of the event log use in the service.
Add trivial unit tests to check that when a service principal is fed in,
appropriately shaped ACLs come out.
  • Loading branch information
SteveGilham committed May 3, 2014
1 parent 1216ee4 commit 6d3866d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion OpenCover.Test.Service/OpenCover.Test.Service/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override void OnStop()
/// </summary>
protected override void OnShutdown()
{
eventLog.WriteEntry("Shutting down service");
Debug.WriteLine("Shutting down service");
waiter.Set();
}
}
Expand Down
3 changes: 0 additions & 3 deletions OpenCover.Test.Service/OpenCover.Test.Service/Service.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="eventLog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayLargeIcon" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
Expand Down
21 changes: 21 additions & 0 deletions main/OpenCover.Framework/Manager/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ internal class ManagedMemoryBlock : ManagedBlock, IManagedMemoryBlock
public MemoryMappedViewStream StreamAccessorResults { get; private set; }
public int BufferSize { get; private set; }

/// <summary>
/// Gets an ACL for unit test purposes
/// </summary>
internal MemoryMappedFileSecurity MemoryAcl
{
get {
return _mmfResults.GetAccessControl();
}
}

internal ManagedMemoryBlock(string @namespace, string key, int bufferSize, int bufferId, IEnumerable<string> servicePrincpal)
{
Namespace = @namespace;
Expand Down Expand Up @@ -114,6 +124,17 @@ internal class ManagedCommunicationBlock : ManagedBlock, IManagedCommunicationBl
public byte[] DataCommunication { get; private set; }
public GCHandle PinnedDataCommunication { get; private set; }

/// <summary>
/// Gets an ACL for unit test purposes
/// </summary>
internal MemoryMappedFileSecurity MemoryAcl
{
get
{
return _memoryMappedFile.GetAccessControl();
}
}

internal ManagedCommunicationBlock(string @namespace, string key, int bufferSize, int bufferId, IEnumerable<string> servicePrincpal)
{
Namespace = @namespace;
Expand Down
77 changes: 77 additions & 0 deletions main/OpenCover.Test/Framework/Manager/ProfilerManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -314,6 +316,81 @@ public void Manager_SendsResults_ForProcessing()
Container.GetMock<IPersistance>().Verify(x => x.SaveVisitData(It.IsAny<byte[]>()), Times.Once());
}

[Test]
public void Manager_Sets_Service_ACLs_On_Events()
{
// arrange
var networkServiceSid = new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null);
var networkServiceAccount = networkServiceSid.Translate(typeof(NTAccount));
var servicePrincipal = new[] { networkServiceAccount.ToString() };
var self = WindowsIdentity.GetCurrent().User;

// act
using (var mcb = new MemoryManager.ManagedCommunicationBlock("Local", _key, 100, 1, servicePrincipal))
using (var mmb = new MemoryManager.ManagedMemoryBlock("Local", _key, 100, 1, servicePrincipal))
{
var phrRules = mmb.ProfilerHasResults.GetAccessControl().GetAccessRules(true, false, typeof(SecurityIdentifier));
var rhbrRules = mmb.ResultsHaveBeenReceived.GetAccessControl().GetAccessRules(true, false, typeof(SecurityIdentifier));
var priRules = mcb.ProfilerRequestsInformation.GetAccessControl().GetAccessRules(true, false, typeof(SecurityIdentifier));
var irfpRules = mcb.InformationReadyForProfiler.GetAccessControl().GetAccessRules(true, false, typeof(SecurityIdentifier));
var irbpRules = mcb.InformationReadByProfiler.GetAccessControl().GetAccessRules(true, false, typeof(SecurityIdentifier));

var rules = new[] { phrRules, rhbrRules, priRules, irfpRules, irbpRules };

// assert
foreach (var ruleset in rules)
{
Assert.That(ruleset.Count, Is.EqualTo(2));

Assert.That(ruleset.Cast<AccessRule>().Any(r => r.IdentityReference == networkServiceSid));
Assert.That(ruleset.Cast<AccessRule>()
.Where(r => r.IdentityReference == networkServiceSid)
.Any(r => r.AccessControlType == AccessControlType.Allow));

Assert.That(ruleset.Cast<AccessRule>().Any(r => r.IdentityReference == self));
Assert.That(ruleset.Cast<AccessRule>()
.Where(r => r.IdentityReference == self)
.Any(r => r.AccessControlType == AccessControlType.Allow));
}
}
}

[Test]
public void Manager_Sets_Service_ACLs_On_Memory()
{
// arrange
var networkServiceSid = new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null);
var networkServiceAccount = networkServiceSid.Translate(typeof(NTAccount));
var servicePrincipal = new[] { networkServiceAccount.ToString() };
var self = WindowsIdentity.GetCurrent().User;

// act
using (var mcb = new MemoryManager.ManagedCommunicationBlock("Local", _key, 100, 1, servicePrincipal))
using (var mmb = new MemoryManager.ManagedMemoryBlock("Local", _key, 100, 1, servicePrincipal))
{
var mcbRules = mcb.MemoryAcl.GetAccessRules(true, false, typeof(SecurityIdentifier));
var mmbRules = mmb.MemoryAcl.GetAccessRules(true, false, typeof(SecurityIdentifier));

var rules = new[] { mcbRules, mmbRules };

// assert
foreach (var ruleset in rules)
{
Assert.That(ruleset.Count, Is.EqualTo(2));

Assert.That(ruleset.Cast<AccessRule>().Any(r => r.IdentityReference == networkServiceSid));
Assert.That(ruleset.Cast<AccessRule>()
.Where(r => r.IdentityReference == networkServiceSid)
.Any(r => r.AccessControlType == AccessControlType.Allow));

Assert.That(ruleset.Cast<AccessRule>().Any(r => r.IdentityReference == self));
Assert.That(ruleset.Cast<AccessRule>()
.Where(r => r.IdentityReference == self)
.Any(r => r.AccessControlType == AccessControlType.Allow));
}
}
}

private void RunSimpleProcess(StringDictionary dict)
{
RunProcess(dict, standardMessageDataReady => { }, () => { });
Expand Down

0 comments on commit 6d3866d

Please sign in to comment.