This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
group_test.go
194 lines (156 loc) · 4.9 KB
/
group_test.go
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
package main
import (
"os/exec"
"testing"
"github.com/stretchr/testify/require"
"github.com/ubuntu/aad-auth/internal/testutils"
)
// TODO: process coverage once https://github.com/golang/go/issues/51430 is implemented in Go.
//nolint:dupl // TestNssGetGroupByName and TestGetPasswdByName have similar code that triggers dupl, despite being different.
func TestNssGetGroupByName(t *testing.T) {
t.Parallel()
uid, gid := testutils.GetCurrentUIDGID(t)
noShadow := 0
tests := map[string]struct {
name string
cacheDB string
rootUID int
shadowMode *int
wantErr bool
}{
"list existing group": {},
"access to shadow is not needed to list existing group": {shadowMode: &noShadow},
"no cache no error on existing local group": {name: "root", cacheDB: "-"},
// error cases
"group does not exists": {name: "doesnotexist@domain.com", wantErr: true},
"no cache can't get group": {cacheDB: "-", wantErr: true},
"invalid permissions on cache can't get group": {rootUID: 4242, wantErr: true},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
cacheDir := t.TempDir()
if tc.name == "" {
tc.name = "myuser@domain.com"
}
if tc.cacheDB == "" {
tc.cacheDB = "users_in_db"
}
if tc.cacheDB != "-" {
testutils.PrepareDBsForTests(t, cacheDir, tc.cacheDB)
}
if tc.rootUID == 0 {
tc.rootUID = uid
}
shadowMode := -1
if tc.shadowMode != nil {
shadowMode = *tc.shadowMode
}
got, err := outNSSCommandForLib(t, tc.rootUID, gid, shadowMode, cacheDir, nil, "getent", "group", tc.name)
if tc.wantErr {
require.Error(t, err, "getent should have errored out but didn't")
return
}
require.NoError(t, err, "getent should succeed")
want := testutils.LoadAndUpdateFromGolden(t, got)
require.Equal(t, want, got, "Should get expected aad group")
})
}
}
//nolint:dupl // TestNssGetGroupByGID and TestGetPasswdByUID have similar code that triggers dupl, despite being different.
func TestNssGetGroupByGID(t *testing.T) {
t.Parallel()
uid, gid := testutils.GetCurrentUIDGID(t)
noShadow := 0
tests := map[string]struct {
gid string
cacheDB string
rootUID int
shadowMode *int
wantErr bool
}{
"list existing group": {},
"access to shadow is not needed to list existing group": {shadowMode: &noShadow},
"no cache no error on existing local group": {gid: "0", cacheDB: "-"},
// error cases
"group does not exists": {gid: "4242", wantErr: true},
"no cache can't get group": {cacheDB: "-", wantErr: true},
"invalid permissions on cache can't get group": {rootUID: 4242, wantErr: true},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
cacheDir := t.TempDir()
if tc.gid == "" {
tc.gid = "1929326240"
}
if tc.cacheDB == "" {
tc.cacheDB = "users_in_db"
}
if tc.cacheDB != "-" {
testutils.PrepareDBsForTests(t, cacheDir, tc.cacheDB)
}
if tc.rootUID == 0 {
tc.rootUID = uid
}
shadowMode := -1
if tc.shadowMode != nil {
shadowMode = *tc.shadowMode
}
got, err := outNSSCommandForLib(t, tc.rootUID, gid, shadowMode, cacheDir, nil, "getent", "group", tc.gid)
if tc.wantErr {
require.Error(t, err, "getent should have errored out but didn't")
return
}
require.NoError(t, err, "getent should succeed")
want := testutils.LoadAndUpdateFromGolden(t, got)
require.Equal(t, want, got, "Should get expected aad group")
})
}
}
func TestNssGetGroup(t *testing.T) {
t.Parallel()
originOut, err := exec.Command("getent", "group").CombinedOutput()
require.NoError(t, err, "Setup: can't run getent to get original output from system")
uid, gid := testutils.GetCurrentUIDGID(t)
noShadow := 0
tests := map[string]struct {
cacheDB string
rootUID int
shadowGid int
shadowMode *int
}{
"list all groups": {},
"access to shadow is not needed to list groups": {shadowMode: &noShadow},
// special cases
"no cache lists no groups": {cacheDB: "-"},
"invalid permissions on cache lists no groups": {rootUID: 4242},
"old groups are cleaned up": {cacheDB: "db_with_old_users"},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
cacheDir := t.TempDir()
if tc.cacheDB == "" {
tc.cacheDB = "users_in_db"
}
if tc.cacheDB != "-" {
testutils.PrepareDBsForTests(t, cacheDir, tc.cacheDB)
}
if tc.rootUID == 0 {
tc.rootUID = uid
}
shadowMode := -1
if tc.shadowMode != nil {
shadowMode = *tc.shadowMode
}
got, err := outNSSCommandForLib(t, tc.rootUID, gid, shadowMode, cacheDir, originOut, "getent", "group")
require.NoError(t, err, "getent should succeed")
want := testutils.LoadAndUpdateFromGolden(t, got)
require.Equal(t, want, got, "Should get expected aad groups listed")
})
}
}