-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathPosixSignalRegistrationTests.Unix.cs
233 lines (192 loc) · 8.43 KB
/
PosixSignalRegistrationTests.Unix.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Xunit;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Microsoft.DotNet.RemoteExecutor;
using System.Collections.Generic;
namespace System.Tests
{
public partial class PosixSignalRegistrationTests
{
public static IEnumerable<object[]> UninstallableSignals()
{
yield return new object[] { (PosixSignal)9 };
}
public static IEnumerable<object[]> SupportedSignals()
{
foreach (PosixSignal value in Enum.GetValues(typeof(PosixSignal)))
yield return new object[] { value };
}
public static IEnumerable<object[]> UnsupportedSignals()
{
if (PlatformDetection.IsMobile)
{
foreach (PosixSignal value in Enum.GetValues(typeof(PosixSignal)))
yield return new object[] { value };
}
yield return new object[] { 0 };
yield return new object[] { -1000 };
yield return new object[] { 1000 };
}
public static bool NotMobileAndRemoteExecutable => PlatformDetection.IsNotMobile && RemoteExecutor.IsSupported;
[ConditionalTheory(nameof(NotMobileAndRemoteExecutable))]
[MemberData(nameof(SupportedSignals))]
public void SignalHandlerCalledForKnownSignals(PosixSignal s)
{
RemoteExecutor.Invoke(signalStr =>
{
PosixSignal signal = Enum.Parse<PosixSignal>(signalStr);
using SemaphoreSlim semaphore = new(0);
using var _ = PosixSignalRegistration.Create(signal, ctx =>
{
Assert.Equal(signal, ctx.Signal);
// Ensure signal doesn't cause the process to terminate.
ctx.Cancel = true;
semaphore.Release();
});
// Use 'kill' command with signal name to validate the signal pal mapping.
string sigArg = signalStr.StartsWith("SIG") ? signalStr.Substring(3) : signalStr;
using var process = Process.Start(new ProcessStartInfo
{
FileName = "/bin/sh", // Use a shell because not all platforms include a 'kill' executable.
ArgumentList = { "-c", $"kill -s {sigArg} {Environment.ProcessId.ToString()}" }
});
process.WaitForExit();
Assert.Equal(0, process.ExitCode);
bool entered = semaphore.Wait(SuccessTimeout);
Assert.True(entered);
}, s.ToString()).Dispose();
}
[ConditionalTheory(nameof(NotMobileAndRemoteExecutable))]
[MemberData(nameof(PosixSignalAsRawValues))]
public void SignalHandlerCalledForRawSignals(PosixSignal s)
{
RemoteExecutor.Invoke((signalStr) =>
{
PosixSignal signal = Enum.Parse<PosixSignal>(signalStr);
using SemaphoreSlim semaphore = new(0);
using var _ = PosixSignalRegistration.Create(signal, ctx =>
{
Assert.Equal(signal, ctx.Signal);
// Ensure signal doesn't cause the process to terminate.
ctx.Cancel = true;
semaphore.Release();
});
kill(signal);
bool entered = semaphore.Wait(SuccessTimeout);
Assert.True(entered);
}, s.ToString()).Dispose();
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))]
public void SignalHandlerWorksForSecondRegistration()
{
PosixSignal signal = PosixSignal.SIGCONT;
for (int i = 0; i < 2; i++)
{
using SemaphoreSlim semaphore = new(0);
using var _ = PosixSignalRegistration.Create(signal, ctx =>
{
Assert.Equal(signal, ctx.Signal);
// Ensure signal doesn't cause the process to terminate.
ctx.Cancel = true;
semaphore.Release();
});
kill(signal);
bool entered = semaphore.Wait(SuccessTimeout);
Assert.True(entered);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))]
public void SignalHandlerNotCalledWhenDisposed()
{
PosixSignal signal = PosixSignal.SIGCONT;
PosixSignalRegistration.Create(signal, ctx =>
{
Assert.False(true, "Signal handler was called.");
}).Dispose();
kill(signal);
Thread.Sleep(100);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotMobile))]
public void SignalHandlerNotCalledWhenFinalized()
{
PosixSignal signal = PosixSignal.SIGCONT;
CreateDanglingRegistration();
GC.Collect();
GC.WaitForPendingFinalizers();
kill(signal);
Thread.Sleep(100);
[MethodImpl(MethodImplOptions.NoInlining)]
void CreateDanglingRegistration()
{
PosixSignalRegistration.Create(signal, ctx =>
{
Assert.False(true, "Signal handler was called.");
});
}
}
[ConditionalTheory(nameof(NotMobileAndRemoteExecutable))]
[InlineData(PosixSignal.SIGINT, true, 0)]
[InlineData(PosixSignal.SIGINT, false, 130)]
[InlineData(PosixSignal.SIGTERM, true, 0)]
[InlineData(PosixSignal.SIGTERM, false, 143)]
[InlineData(PosixSignal.SIGQUIT, true, 0)]
[InlineData(PosixSignal.SIGQUIT, false, 131)]
public void SignalCanCancelTermination(PosixSignal signal, bool cancel, int expectedExitCode)
{
// Mono doesn't restore and call SIG_DFL on SIGQUIT.
if (PlatformDetection.IsMonoRuntime && signal == PosixSignal.SIGQUIT && cancel == false)
{
expectedExitCode = 0;
}
RemoteExecutor.Invoke((signalStr, cancelStr, expectedStr) =>
{
PosixSignal signalArg = Enum.Parse<PosixSignal>(signalStr);
bool cancelArg = bool.Parse(cancelStr);
int expected = int.Parse(expectedStr);
using SemaphoreSlim semaphore = new(0);
using var _ = PosixSignalRegistration.Create(signalArg, ctx =>
{
ctx.Cancel = cancelArg;
semaphore.Release();
});
kill(signalArg);
bool entered = semaphore.Wait(SuccessTimeout);
Assert.True(entered);
// Give the default signal handler a chance to run.
Thread.Sleep(expected == 0 ? TimeSpan.FromSeconds(2) : TimeSpan.FromMinutes(10));
return 0;
}, signal.ToString(), cancel.ToString(), expectedExitCode.ToString(),
new RemoteInvokeOptions() { ExpectedExitCode = expectedExitCode, TimeOut = 10 * 60 * 1000 }).Dispose();
}
public static TheoryData<PosixSignal> PosixSignalAsRawValues
{
get
{
var data = new TheoryData<PosixSignal>();
foreach (var value in Enum.GetValues(typeof(PosixSignal)))
{
int signo = GetPlatformSignalNumber((PosixSignal)value);
Assert.True(signo > 0, "Expected raw signal number to be greater than 0.");
data.Add((PosixSignal)signo);
}
return data;
}
}
[DllImport("libc", SetLastError = true)]
private static extern int kill(int pid, int sig);
private static void kill(PosixSignal sig)
{
int signo = GetPlatformSignalNumber(sig);
Assert.NotEqual(0, signo);
int rv = kill(Environment.ProcessId, signo);
Assert.Equal(0, rv);
}
[DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetPlatformSignalNumber")]
private static extern int GetPlatformSignalNumber(PosixSignal signal);
}
}