Skip to content

Commit f8eae7a

Browse files
dolmenqmuntal
authored andcommitted
os/user: fix tests to pass on non-english Windows
Tests on Windows are dependent on the english names of system accounts and groups. But on a french install of Windows the system accounts are: - AUTORITE NT\Système - AUTORITE NT\SERVICE LOCAL - AUTORITE NT\SERVICE RÉSEAU To allow the tests to pass on non-english Windows we only log differences in user/group names if GetSystemDefaultLCID() reports a non-english LCID, instead of failing. Change-Id: Ib81acc2896c45675fa3faf5dc390b57ec5159689 Reviewed-on: https://go-review.googlesource.com/c/go/+/688715 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Quim Muntal <[email protected]>
1 parent 0984264 commit f8eae7a

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

src/os/user/user_windows_test.go

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"slices"
1919
"strconv"
2020
"strings"
21+
"sync"
2122
"syscall"
2223
"testing"
2324
"unicode"
@@ -261,9 +262,21 @@ func TestGroupIdsTestUser(t *testing.T) {
261262
}
262263
}
263264

265+
var isSystemDefaultLCIDEnglish = sync.OnceValue(func() bool {
266+
// GetSystemDefaultLCID()
267+
// https://learn.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getsystemdefaultlcid
268+
r, _, _ := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetSystemDefaultLCID").Call()
269+
lcid := uint32(r)
270+
271+
lcidLow := lcid & 0xFF
272+
// 0x0409 is en-US
273+
// 0x1000 is "Locale without assigned LCID"
274+
return lcidLow == 0x00 || lcidLow == 0x09
275+
})
276+
264277
var serviceAccounts = []struct {
265278
sid string
266-
name string
279+
name string // name on english Windows
267280
}{
268281
{"S-1-5-18", "NT AUTHORITY\\SYSTEM"},
269282
{"S-1-5-19", "NT AUTHORITY\\LOCAL SERVICE"},
@@ -273,14 +286,21 @@ var serviceAccounts = []struct {
273286
func TestLookupServiceAccount(t *testing.T) {
274287
t.Parallel()
275288
for _, tt := range serviceAccounts {
276-
u, err := Lookup(tt.name)
277-
if err != nil {
278-
t.Errorf("Lookup(%q): %v", tt.name, err)
279-
continue
280-
}
281-
if u.Uid != tt.sid {
282-
t.Errorf("unexpected uid for %q; got %q, want %q", u.Name, u.Uid, tt.sid)
283-
}
289+
t.Run(tt.name, func(t *testing.T) {
290+
u, err := Lookup(tt.name)
291+
if err != nil {
292+
t.Logf("Lookup(%q): %v", tt.name, err)
293+
if !isSystemDefaultLCIDEnglish() {
294+
t.Skipf("test not supported on non-English Windows")
295+
}
296+
t.Fail()
297+
return
298+
}
299+
if u.Uid != tt.sid {
300+
t.Errorf("unexpected uid for %q; got %q, want %q", u.Name, u.Uid, tt.sid)
301+
}
302+
t.Logf("Lookup(%q): %q", tt.name, u.Username)
303+
})
284304
}
285305
}
286306

@@ -296,22 +316,32 @@ func TestLookupIdServiceAccount(t *testing.T) {
296316
t.Errorf("unexpected gid for %q; got %q, want %q", u.Name, u.Gid, tt.sid)
297317
}
298318
if u.Username != tt.name {
299-
t.Errorf("unexpected user name for %q; got %q, want %q", u.Gid, u.Username, tt.name)
319+
if isSystemDefaultLCIDEnglish() {
320+
t.Errorf("unexpected user name for %q; got %q, want %q", u.Gid, u.Username, tt.name)
321+
} else {
322+
t.Logf("user name for %q: %q", u.Gid, u.Username)
323+
}
300324
}
301325
}
302326
}
303327

304328
func TestLookupGroupServiceAccount(t *testing.T) {
305329
t.Parallel()
306330
for _, tt := range serviceAccounts {
307-
u, err := LookupGroup(tt.name)
308-
if err != nil {
309-
t.Errorf("LookupGroup(%q): %v", tt.name, err)
310-
continue
311-
}
312-
if u.Gid != tt.sid {
313-
t.Errorf("unexpected gid for %q; got %q, want %q", u.Name, u.Gid, tt.sid)
314-
}
331+
t.Run(tt.name, func(t *testing.T) {
332+
g, err := LookupGroup(tt.name)
333+
if err != nil {
334+
t.Logf("LookupGroup(%q): %v", tt.name, err)
335+
if !isSystemDefaultLCIDEnglish() {
336+
t.Skipf("test not supported on non-English Windows")
337+
}
338+
t.Fail()
339+
return
340+
}
341+
if g.Gid != tt.sid {
342+
t.Errorf("unexpected gid for %q; got %q, want %q", g.Name, g.Gid, tt.sid)
343+
}
344+
})
315345
}
316346
}
317347

0 commit comments

Comments
 (0)