]> Cypherpunks repositories - gostls13.git/commitdiff
os/user: fix tests to pass on non-english Windows
authorOlivier Mengué <olivier.mengue@gmail.com>
Fri, 18 Jul 2025 10:36:00 +0000 (12:36 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Mon, 28 Jul 2025 18:52:39 +0000 (11:52 -0700)
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 <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Freeman <mark@golang.org>
Reviewed-by: Quim Muntal <quimmuntal@gmail.com>
src/os/user/user_windows_test.go

index d9f2fe7c7469910c6277243dbdb15118fcc9459b..c95811594d1050cccbc323f3589dc2231797f126 100644 (file)
@@ -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)
+                       }
+               })
        }
 }