From f8eae7a3c33fcf9d31117b1a1a49a47b0343f811 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Olivier=20Mengu=C3=A9?= Date: Fri, 18 Jul 2025 12:36:00 +0200 Subject: [PATCH] os/user: fix tests to pass on non-english Windows MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 LUCI-TryBot-Result: Go LUCI Reviewed-by: Mark Freeman Reviewed-by: Quim Muntal --- src/os/user/user_windows_test.go | 66 +++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/src/os/user/user_windows_test.go b/src/os/user/user_windows_test.go index d9f2fe7c74..c95811594d 100644 --- a/src/os/user/user_windows_test.go +++ b/src/os/user/user_windows_test.go @@ -18,6 +18,7 @@ import ( "slices" "strconv" "strings" + "sync" "syscall" "testing" "unicode" @@ -261,9 +262,21 @@ func TestGroupIdsTestUser(t *testing.T) { } } +var isSystemDefaultLCIDEnglish = sync.OnceValue(func() bool { + // GetSystemDefaultLCID() + // https://learn.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getsystemdefaultlcid + r, _, _ := syscall.MustLoadDLL("kernel32.dll").MustFindProc("GetSystemDefaultLCID").Call() + lcid := uint32(r) + + lcidLow := lcid & 0xFF + // 0x0409 is en-US + // 0x1000 is "Locale without assigned LCID" + return lcidLow == 0x00 || lcidLow == 0x09 +}) + var serviceAccounts = []struct { sid string - name string + name string // name on english Windows }{ {"S-1-5-18", "NT AUTHORITY\\SYSTEM"}, {"S-1-5-19", "NT AUTHORITY\\LOCAL SERVICE"}, @@ -273,14 +286,21 @@ var serviceAccounts = []struct { func TestLookupServiceAccount(t *testing.T) { t.Parallel() for _, tt := range serviceAccounts { - u, err := Lookup(tt.name) - if err != nil { - t.Errorf("Lookup(%q): %v", tt.name, err) - continue - } - if u.Uid != tt.sid { - t.Errorf("unexpected uid for %q; got %q, want %q", u.Name, u.Uid, tt.sid) - } + t.Run(tt.name, func(t *testing.T) { + u, err := Lookup(tt.name) + if err != nil { + t.Logf("Lookup(%q): %v", tt.name, err) + if !isSystemDefaultLCIDEnglish() { + t.Skipf("test not supported on non-English Windows") + } + t.Fail() + return + } + if u.Uid != tt.sid { + t.Errorf("unexpected uid for %q; got %q, want %q", u.Name, u.Uid, tt.sid) + } + t.Logf("Lookup(%q): %q", tt.name, u.Username) + }) } } @@ -296,7 +316,11 @@ func TestLookupIdServiceAccount(t *testing.T) { t.Errorf("unexpected gid for %q; got %q, want %q", u.Name, u.Gid, tt.sid) } if u.Username != tt.name { - t.Errorf("unexpected user name for %q; got %q, want %q", u.Gid, u.Username, tt.name) + if isSystemDefaultLCIDEnglish() { + t.Errorf("unexpected user name for %q; got %q, want %q", u.Gid, u.Username, tt.name) + } else { + t.Logf("user name for %q: %q", u.Gid, u.Username) + } } } } @@ -304,14 +328,20 @@ func TestLookupIdServiceAccount(t *testing.T) { func TestLookupGroupServiceAccount(t *testing.T) { t.Parallel() for _, tt := range serviceAccounts { - u, err := LookupGroup(tt.name) - if err != nil { - t.Errorf("LookupGroup(%q): %v", tt.name, err) - continue - } - if u.Gid != tt.sid { - t.Errorf("unexpected gid for %q; got %q, want %q", u.Name, u.Gid, tt.sid) - } + t.Run(tt.name, func(t *testing.T) { + g, err := LookupGroup(tt.name) + if err != nil { + t.Logf("LookupGroup(%q): %v", tt.name, err) + if !isSystemDefaultLCIDEnglish() { + t.Skipf("test not supported on non-English Windows") + } + t.Fail() + return + } + if g.Gid != tt.sid { + t.Errorf("unexpected gid for %q; got %q, want %q", g.Name, g.Gid, tt.sid) + } + }) } } -- 2.51.0