|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//go:build linux |
| 5 | + |
| 6 | +package harden |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestParseCapLastCap(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + content string |
| 21 | + want uintptr |
| 22 | + wantErr bool |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "typical value", |
| 26 | + content: "40\n", |
| 27 | + want: 40, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "higher kernel", |
| 31 | + content: "41\n", |
| 32 | + want: 41, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "no trailing newline", |
| 36 | + content: "40", |
| 37 | + want: 40, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "whitespace padding", |
| 41 | + content: " 40 \n", |
| 42 | + want: 40, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "non-numeric", |
| 46 | + content: "abc\n", |
| 47 | + wantErr: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "empty", |
| 51 | + content: "", |
| 52 | + wantErr: true, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + t.Parallel() |
| 59 | + got, err := ParseCapLastCapForTest(tt.content) |
| 60 | + if tt.wantErr { |
| 61 | + assert.Error(t, err) |
| 62 | + return |
| 63 | + } |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, tt.want, got) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestKeepSetContains(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + keep := []uintptr{CapSetUID, CapSetGID, CapNetBindService} |
| 74 | + |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + cap uintptr |
| 78 | + want bool |
| 79 | + }{ |
| 80 | + {name: "CAP_SETUID in set", cap: CapSetUID, want: true}, |
| 81 | + {name: "CAP_SETGID in set", cap: CapSetGID, want: true}, |
| 82 | + {name: "CAP_NET_BIND_SERVICE in set", cap: CapNetBindService, want: true}, |
| 83 | + {name: "CAP_CHOWN not in set", cap: CapChown, want: false}, |
| 84 | + {name: "CAP_KILL not in set", cap: CapKill, want: false}, |
| 85 | + {name: "arbitrary cap not in set", cap: 99, want: false}, |
| 86 | + } |
| 87 | + |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + assert.Equal(t, tt.want, KeepSetContainsForTest(keep, tt.cap)) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestKeepSetContains_EmptySet(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + // With an empty keep set, nothing should be kept. |
| 100 | + assert.False(t, KeepSetContainsForTest(nil, CapSetUID)) |
| 101 | + assert.False(t, KeepSetContainsForTest([]uintptr{}, CapSetGID)) |
| 102 | +} |
| 103 | + |
| 104 | +func TestCapConstants(t *testing.T) { |
| 105 | + t.Parallel() |
| 106 | + |
| 107 | + // Verify the capability constants match Linux kernel values. |
| 108 | + assert.Equal(t, uintptr(0), CapChown) |
| 109 | + assert.Equal(t, uintptr(5), CapKill) |
| 110 | + assert.Equal(t, uintptr(6), CapSetGID) |
| 111 | + assert.Equal(t, uintptr(7), CapSetUID) |
| 112 | + assert.Equal(t, uintptr(10), CapNetBindService) |
| 113 | +} |
| 114 | + |
| 115 | +func TestCapLastCap_ReadsProc(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + // capLastCap should return a reasonable value from /proc or the |
| 119 | + // fallback. On any Linux system the value should be >= 0. |
| 120 | + got := capLastCap() |
| 121 | + assert.GreaterOrEqual(t, got, uintptr(0)) |
| 122 | + // Modern kernels have at least 40 capabilities. |
| 123 | + assert.GreaterOrEqual(t, got, uintptr(36)) |
| 124 | +} |
0 commit comments