File: process_linux_test.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (91 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2020, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package capabilities

import (
	"runtime"
	"testing"

	"github.com/sylabs/singularity/v4/internal/pkg/test"
)

func TestGetProcess(t *testing.T) {
        t.Skip("Skipping test as this needs privilege containers")
	test.EnsurePrivilege(t)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	tests := []struct {
		name string
		fn   func() (uint64, error)
		cap  string
	}{
		{
			name: "effective",
			fn:   GetProcessEffective,
			cap:  "CAP_SYS_ADMIN",
		},
		{
			name: "permitted",
			fn:   GetProcessPermitted,
		},
		{
			name: "inheritable",
			fn:   GetProcessInheritable,
		},
	}

	for _, tt := range tests {
		caps, err := tt.fn()
		if err != nil {
			t.Fatalf("unexpected error while getting process %s capabilities: %s", tt.name, err)
		}
		c := Map[tt.cap]
		if tt.cap != "" && caps&uint64(1<<c.Value) == 0 {
			t.Fatalf("%s capability %s missing", tt.name, tt.cap)
		}
	}
}

func TestSetProcessEffective(t *testing.T) {
        t.Skip("Skipping test as this needs privilege containers")
	test.EnsurePrivilege(t)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	data, err := getProcessCapabilities()
	if err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		name         string
		cap          uint64
		oldEffective uint64
	}{
		{
			name:         "set cap_sys_admin only",
			cap:          uint64(1 << Map["CAP_SYS_ADMIN"].Value),
			oldEffective: uint64(data[0].Effective) | uint64(data[1].Effective)<<32,
		},
		{
			name:         "restore capabilities",
			cap:          uint64(data[0].Effective) | uint64(data[1].Effective)<<32,
			oldEffective: uint64(1 << Map["CAP_SYS_ADMIN"].Value),
		},
	}

	for _, tt := range tests {
		old, err := SetProcessEffective(tt.cap)
		if err != nil {
			t.Fatalf("unexpected error for %s: %s", tt.name, err)
		} else if old != tt.oldEffective {
			t.Fatalf("unexpected old effective set for %s", tt.name)
		}
	}
}