Skip to content

Commit 8ddbc86

Browse files
committed
Add users_test.TestSetUserID
1 parent 6752206 commit 8ddbc86

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package users_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math"
7+
"os/exec"
8+
"path/filepath"
9+
"strconv"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
"github.com/ubuntu/authd/internal/testutils"
14+
"github.com/ubuntu/authd/internal/users/db"
15+
"github.com/ubuntu/authd/log"
16+
)
17+
18+
func TestSetUserID(t *testing.T) {
19+
t.Parallel()
20+
21+
tests := map[string]struct {
22+
nonExistentUser bool
23+
emptyUsername bool
24+
uidAlreadySet bool
25+
uidAlreadyInUseByAuthdUser bool
26+
uidAlreadyInUseAsGIDofAuthdUser bool
27+
uidAlreadyInUseBySystemUser bool
28+
uidAlreadyInUseAsGIDofSystemUser bool
29+
uidTooHigh bool
30+
31+
wantErr bool
32+
wantErrType error
33+
}{
34+
"Successfully_set_UID": {},
35+
"Successfully_set_UID_if_ID_is_already_set": {uidAlreadySet: true},
36+
"Successfully_set_UID_if_ID_is_already_in_use_as_GID_of_system_user": {uidAlreadyInUseAsGIDofSystemUser: true},
37+
"Successfully_set_UID_if_ID_is_already_in_use_as_GID_of_authd_user": {uidAlreadyInUseAsGIDofAuthdUser: true},
38+
39+
"Error_if_username_is_empty": {emptyUsername: true, wantErr: true},
40+
"Error_if_user_does_not_exist": {nonExistentUser: true, wantErrType: db.NoDataFoundError{}},
41+
"Error_if_UID_is_already_in_use_by_authd": {uidAlreadyInUseByAuthdUser: true, wantErr: true},
42+
"Error_if_UID_is_already_in_use_by_system": {uidAlreadyInUseBySystemUser: true, wantErr: true},
43+
"Error_if_UID_is_too_high": {uidTooHigh: true, wantErr: true},
44+
}
45+
for name, tc := range tests {
46+
t.Run(name, func(t *testing.T) {
47+
t.Parallel()
48+
49+
if !testutils.RunningInBubblewrap() {
50+
testutils.RunTestInBubbleWrap(t)
51+
return
52+
}
53+
54+
dbDir := t.TempDir()
55+
err := db.Z_ForTests_CreateDBFromYAML(filepath.Join("testdata", "db", "multiple_users_and_groups.db.yaml"), dbDir)
56+
require.NoError(t, err, "Setup: could not create database from testdata")
57+
58+
m := newManagerForTests(t, dbDir)
59+
60+
username := "user1"
61+
if tc.nonExistentUser {
62+
username = "nonexistent"
63+
}
64+
if tc.emptyUsername {
65+
username = ""
66+
}
67+
68+
newUID := 123456
69+
if tc.uidAlreadySet {
70+
newUID = 1111
71+
}
72+
if tc.uidAlreadyInUseByAuthdUser {
73+
newUID = 2222
74+
}
75+
if tc.uidAlreadyInUseAsGIDofAuthdUser {
76+
newUID = 22222
77+
}
78+
if tc.uidTooHigh {
79+
newUID = math.MaxInt32 + 1
80+
}
81+
if tc.uidAlreadyInUseBySystemUser {
82+
err = addUserToSystem(newUID)
83+
require.NoError(t, err, "Setup: could not add user to system")
84+
}
85+
if tc.uidAlreadyInUseAsGIDofSystemUser {
86+
err = addGroupToSystem(newUID)
87+
require.NoError(t, err, "Setup: could not add group to system")
88+
}
89+
90+
//nolint:gosec // G115 we set the UID above to values that are valid uint32
91+
_, err = m.SetUserID(username, uint32(newUID))
92+
log.Infof(context.Background(), "SetUserID error: %v", err)
93+
94+
if tc.wantErrType != nil {
95+
require.ErrorIs(t, err, tc.wantErrType, "SetUserID should return expected error")
96+
return
97+
}
98+
if tc.wantErr {
99+
require.Error(t, err, "SetUserID should return an error but didn't")
100+
return
101+
}
102+
require.NoError(t, err, "SetUserID should not return an error on existing user")
103+
})
104+
}
105+
}
106+
107+
func addUserToSystem(uid int) error {
108+
//nolint:gosec // G204 we want to use exec.Command with variables here
109+
cmd := exec.Command(
110+
"useradd",
111+
"--uid", strconv.Itoa(uid),
112+
"--no-create-home",
113+
fmt.Sprintf("test-%d", uid),
114+
)
115+
if output, err := cmd.CombinedOutput(); err != nil {
116+
return fmt.Errorf("useradd failed: %w, output: %s", err, output)
117+
}
118+
return nil
119+
}
120+
121+
func addGroupToSystem(gid int) error {
122+
//nolint:gosec // G204 we want to use exec.Command with variables here
123+
cmd := exec.Command(
124+
"groupadd",
125+
"--gid", strconv.Itoa(gid),
126+
fmt.Sprintf("test-%d", gid),
127+
)
128+
if output, err := cmd.CombinedOutput(); err != nil {
129+
return fmt.Errorf("groupadd failed: %w, output: %s", err, output)
130+
}
131+
return nil
132+
}

internal/users/manager_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,11 @@ func newManagerForTests(t *testing.T, dbDir string, opts ...users.Option) *users
13171317
func TestMain(m *testing.M) {
13181318
log.SetLevel(log.DebugLevel)
13191319

1320+
if testutils.RunningInBubblewrap() {
1321+
m.Run()
1322+
return
1323+
}
1324+
13201325
userslocking.Z_ForTests_OverrideLocking()
13211326
defer userslocking.Z_ForTests_RestoreLocking()
13221327

0 commit comments

Comments
 (0)