Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 960aae8

Browse files
authored
Fixed autolog tests (#28939)
1 parent 7979727 commit 960aae8

File tree

5 files changed

+36
-53
lines changed

5 files changed

+36
-53
lines changed

src/System.ServiceProcess.ServiceController/tests/ServiceBaseTests.cs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -164,52 +164,33 @@ public void TestOnContinueBeforePause()
164164
[ConditionalFact(nameof(IsElevatedAndSupportsEventLogs))]
165165
public void LogWritten()
166166
{
167-
using (EventLog eventLog = new EventLog("Application"))
168-
{
169-
ServiceBase sb = new ServiceBase() { ServiceName = nameof(LogWritten) + Guid.NewGuid().ToString() };
170-
Assert.False(EventLog.SourceExists(sb.ServiceName));
171-
try
172-
{
173-
ServiceBase.Run(sb);
174-
eventLog.Source = sb.ServiceName;
175-
Assert.True(EventLog.SourceExists(sb.ServiceName));
176-
}
177-
finally
178-
{
179-
sb.Stop();
180-
EventLog.DeleteEventSource(sb.ServiceName);
181-
}
182-
}
167+
string serviceName = Guid.NewGuid().ToString();
168+
// The default username for installing the service is NT AUTHORITY\\LocalService which does not have access to EventLog.
169+
// If the username is null, then the service is created under LocalSystem Account which have access to EventLog.
170+
var testService = new TestServiceProvider(serviceName, userName: null);
171+
Assert.True(EventLog.SourceExists(serviceName));
172+
testService.DeleteTestServices();
183173
}
184174

185175
[ConditionalFact(nameof(IsElevatedAndSupportsEventLogs))]
186176
public void LogWritten_AutoLog_False()
187177
{
188-
using (EventLog eventLog = new EventLog("Application"))
189-
{
190-
ServiceBase sb = new ServiceBase() { ServiceName = nameof(LogWritten) + Guid.NewGuid().ToString(), AutoLog = false };
191-
Assert.False(EventLog.SourceExists(sb.ServiceName));
192-
try
193-
{
194-
ServiceBase.Run(sb);
195-
Assert.False(EventLog.SourceExists(sb.ServiceName));
196-
}
197-
finally
198-
{
199-
sb.Stop();
200-
}
201-
}
178+
string serviceName = nameof(LogWritten_AutoLog_False) + Guid.NewGuid().ToString();
179+
var testService = new TestServiceProvider(serviceName);
180+
Assert.False(EventLog.SourceExists(serviceName));
181+
testService.DeleteTestServices();
202182
}
203-
183+
204184
[ConditionalFact(nameof(IsProcessElevated))]
185+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Full Framework receives the Connected Byte Code after the Exception Thrown Byte Code")]
205186
public void PropagateExceptionFromOnStart()
206187
{
207188
string serviceName = nameof(PropagateExceptionFromOnStart) + Guid.NewGuid().ToString();
208-
TestServiceProvider _testService = new TestServiceProvider(serviceName);
209-
_testService.Client.Connect(connectionTimeout);
210-
Assert.Equal((int)PipeMessageByteCode.Connected, _testService.GetByte());
211-
Assert.Equal((int)PipeMessageByteCode.ExceptionThrown, _testService.GetByte());
212-
_testService.DeleteTestServices();
189+
var testService = new TestServiceProvider(serviceName);
190+
testService.Client.Connect(connectionTimeout);
191+
Assert.Equal((int)PipeMessageByteCode.Connected, testService.GetByte());
192+
Assert.Equal((int)PipeMessageByteCode.ExceptionThrown, testService.GetByte());
193+
testService.DeleteTestServices();
213194
}
214195

215196
private ServiceController ConnectToServer()

src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/Helpers.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
namespace System.ServiceProcess.Tests
66
{
7-
[Flags]
87
public enum PipeMessageByteCode
98
{
10-
Start = 0,
11-
Continue = 1,
12-
Pause = 2,
13-
Stop = 4,
14-
OnCustomCommand = 8,
15-
ExceptionThrown = 16,
16-
Connected = 32
9+
Start,
10+
Continue,
11+
Pause,
12+
Stop,
13+
OnCustomCommand,
14+
ExceptionThrown,
15+
Connected
1716
};
1817
}

src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ static int Main(string[] args)
3131
}
3232
}
3333
}
34+
else if (args[0].StartsWith("LogWritten"))
35+
{
36+
testService = new TestService(args[0], throwException: null);
37+
testService.AutoLog = false;
38+
ServiceBase.Run(testService);
39+
}
3440
else
3541
{
3642
testService = new TestService(args[0]);

src/System.ServiceProcess.ServiceController/tests/System.ServiceProcess.ServiceController.TestService/TestServiceInstaller.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace System.ServiceProcess.Tests
1111
{
1212
public class TestServiceInstaller
1313
{
14-
public const string LocalServiceName = "NT AUTHORITY\\LocalService";
15-
1614
public TestServiceInstaller()
1715
{
1816
}
@@ -38,11 +36,6 @@ public unsafe void Install()
3836
string username = Username;
3937
string password = Password;
4038

41-
if (string.IsNullOrEmpty(username))
42-
{
43-
username = LocalServiceName;
44-
}
45-
4639
if (ServiceCommandLine == null)
4740
{
4841
string processName = Process.GetCurrentProcess().MainModule.FileName;
@@ -202,4 +195,4 @@ private void DeleteService()
202195
}
203196
}
204197
}
205-
}
198+
}

src/System.ServiceProcess.ServiceController/tests/TestServiceProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace System.ServiceProcess.Tests
1212
internal sealed class TestServiceProvider
1313
{
1414
private const int readTimeout = 60000;
15+
public const string LocalServiceName = "NT AUTHORITY\\LocalService";
1516

1617
private static readonly Lazy<bool> s_runningWithElevatedPrivileges = new Lazy<bool>(
1718
() => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator));
@@ -47,6 +48,7 @@ public NamedPipeClientStream Client
4748
public readonly string TestMachineName;
4849
public readonly TimeSpan ControlTimeout;
4950
public readonly string TestServiceName;
51+
public readonly string Username;
5052
public readonly string TestServiceDisplayName;
5153

5254
private readonly TestServiceProvider _dependentServices;
@@ -63,12 +65,13 @@ public TestServiceProvider()
6365
CreateTestServices();
6466
}
6567

66-
public TestServiceProvider(string serviceName)
68+
public TestServiceProvider(string serviceName, string userName = LocalServiceName)
6769
{
6870
TestMachineName = ".";
6971
ControlTimeout = TimeSpan.FromSeconds(120);
7072
TestServiceName = serviceName;
7173
TestServiceDisplayName = "Test Service " + TestServiceName;
74+
Username = userName;
7275

7376
// Create the service
7477
CreateTestServices();
@@ -92,6 +95,7 @@ private void CreateTestServices()
9295
testServiceInstaller.ServiceName = TestServiceName;
9396
testServiceInstaller.DisplayName = TestServiceDisplayName;
9497
testServiceInstaller.Description = "__Dummy Test Service__";
98+
testServiceInstaller.Username = Username;
9599

96100
if (_dependentServices != null)
97101
{

0 commit comments

Comments
 (0)