Skip to content

Commit b6f3d32

Browse files
committed
pam_tests: Force doing a memory leak check at tests Cleanup
ASAN in go does not catch memory leaks properly at the end of the test program execution, so force this using a wrapper function that is called when each test is completed.
1 parent bf800c1 commit b6f3d32

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

pam/pam_test/module-transaction-dummy_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ func ptrValue[T any](value T) *T {
2626

2727
func TestSetGetItem(t *testing.T) {
2828
t.Parallel()
29+
t.Cleanup(MaybeDoLeakCheck)
30+
t.Cleanup(MaybeDoLeakCheck)
2931

3032
testCases := map[string]struct {
3133
item pam.Item
@@ -67,6 +69,7 @@ func TestSetGetItem(t *testing.T) {
6769
tc := tc
6870
t.Run(name, func(t *testing.T) {
6971
t.Parallel()
72+
t.Cleanup(MaybeDoLeakCheck)
7073

7174
require.True(t, tc.value != nil || tc.expectedValue != nil)
7275

@@ -88,6 +91,7 @@ func TestSetGetItem(t *testing.T) {
8891

8992
func TestSetPutEnv(t *testing.T) {
9093
t.Parallel()
94+
t.Cleanup(MaybeDoLeakCheck)
9195

9296
testCases := map[string]struct {
9397
env string
@@ -144,6 +148,7 @@ func TestSetPutEnv(t *testing.T) {
144148
tc := tc
145149
t.Run(name, func(t *testing.T) {
146150
t.Parallel()
151+
t.Cleanup(MaybeDoLeakCheck)
147152

148153
require.False(t, tc.skipPut && tc.expectedValue == nil)
149154

@@ -202,6 +207,7 @@ func TestSetPutEnv(t *testing.T) {
202207

203208
func TestSetGetData(t *testing.T) {
204209
t.Parallel()
210+
t.Cleanup(MaybeDoLeakCheck)
205211

206212
testCases := map[string]struct {
207213
key string
@@ -251,6 +257,7 @@ func TestSetGetData(t *testing.T) {
251257
tc := tc
252258
t.Run(name, func(t *testing.T) {
253259
t.Parallel()
260+
t.Cleanup(MaybeDoLeakCheck)
254261

255262
require.False(t, tc.skipGet && tc.skipSet)
256263

@@ -279,6 +286,7 @@ func TestSetGetData(t *testing.T) {
279286

280287
func TestGetUser(t *testing.T) {
281288
t.Parallel()
289+
t.Cleanup(MaybeDoLeakCheck)
282290

283291
testCases := map[string]struct {
284292
presetUser string
@@ -324,6 +332,7 @@ func TestGetUser(t *testing.T) {
324332
tc := tc
325333
t.Run(name, func(t *testing.T) {
326334
t.Parallel()
335+
t.Cleanup(MaybeDoLeakCheck)
327336

328337
tx := NewModuleTransactionDummy(tc.convHandler)
329338

@@ -342,6 +351,7 @@ func TestGetUser(t *testing.T) {
342351

343352
func TestStartStringConv(t *testing.T) {
344353
t.Parallel()
354+
t.Cleanup(MaybeDoLeakCheck)
345355

346356
testCases := map[string]struct {
347357
prompt string
@@ -392,6 +402,7 @@ func TestStartStringConv(t *testing.T) {
392402
tc := tc
393403
t.Run(name, func(t *testing.T) {
394404
t.Parallel()
405+
t.Cleanup(MaybeDoLeakCheck)
395406

396407
convFunCalled := false
397408
tx := NewModuleTransactionDummy(func() pam.ConversationFunc {
@@ -438,6 +449,7 @@ func TestStartStringConv(t *testing.T) {
438449

439450
func TestStartBinaryConv(t *testing.T) {
440451
t.Parallel()
452+
t.Cleanup(MaybeDoLeakCheck)
441453

442454
testCases := map[string]struct {
443455
request []byte
@@ -467,6 +479,7 @@ func TestStartBinaryConv(t *testing.T) {
467479
tc := tc
468480
t.Run(name, func(t *testing.T) {
469481
t.Parallel()
482+
t.Cleanup(MaybeDoLeakCheck)
470483

471484
convFunCalled := false
472485
var tx pam.ModuleTransaction

pam/pam_test/utils.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Package pam_test includes Test tools for the PAM module
2+
package pam_test
3+
4+
/*
5+
void __lsan_do_leak_check (void) __attribute__ ((weak));
6+
7+
static inline void
8+
maybe_do_leak_check (void)
9+
{
10+
if (__lsan_do_leak_check != NULL)
11+
__lsan_do_leak_check ();
12+
}
13+
*/
14+
import "C"
15+
16+
import (
17+
"os"
18+
"runtime"
19+
"time"
20+
)
21+
22+
// MaybeDoLeakCheck triggers the garbage collector and if the go program is
23+
// compiled with -asan flag, do a memory leak check.
24+
// This is meant to be used as a test Cleanup function, to force Go detecting
25+
// if allocated resources have been released, e.g. using
26+
// t.Cleanup(pam_test.MaybeDoLeakCheck)
27+
func MaybeDoLeakCheck() {
28+
if os.Getenv("AUTHD_PAM_SKIP_LEAK_CHECK") != "" {
29+
return
30+
}
31+
runtime.GC()
32+
time.Sleep(time.Millisecond * 10)
33+
C.maybe_do_leak_check()
34+
}

0 commit comments

Comments
 (0)