Source file src/cmd/go/internal/work/exportpipe_windows_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !cmd_go_bootstrap && windows
     6  
     7  package work
     8  
     9  import (
    10  	"path/filepath"
    11  	"runtime"
    12  	"testing"
    13  	"unsafe"
    14  
    15  	"golang.org/x/sys/windows"
    16  )
    17  
    18  func TestExportPipeSecurityDescriptor(t *testing.T) {
    19  	objdir := t.TempDir() + string(filepath.Separator)
    20  	ep, err := newExportPipe(objdir)
    21  	if err != nil {
    22  		t.Fatalf("newExportPipe: %v", err)
    23  	}
    24  	defer ep.close()
    25  
    26  	user, err := windows.GetCurrentProcessToken().GetTokenUser()
    27  	if err != nil {
    28  		t.Fatalf("GetTokenUser: %v", err)
    29  	}
    30  
    31  	sd, err := windows.GetSecurityInfo(ep.h, windows.SE_KERNEL_OBJECT, windows.DACL_SECURITY_INFORMATION)
    32  	if err != nil {
    33  		t.Fatalf("GetSecurityInfo: %v", err)
    34  	}
    35  	defer runtime.KeepAlive(sd)
    36  
    37  	control, _, err := sd.Control()
    38  	if err != nil {
    39  		t.Fatalf("Control: %v", err)
    40  	}
    41  	if control&windows.SE_DACL_PROTECTED == 0 {
    42  		t.Errorf("security descriptor DACL is not protected: control=%#x, sddl=%q", control, sd)
    43  	}
    44  	dacl, defaulted, err := sd.DACL()
    45  	if err != nil {
    46  		t.Fatalf("DACL: %v", err)
    47  	}
    48  	if defaulted {
    49  		t.Errorf("security descriptor DACL is defaulted: sddl=%q", sd)
    50  	}
    51  	if dacl == nil {
    52  		t.Fatalf("DACL is nil (sddl=%q)", sd)
    53  	}
    54  	if dacl.AceCount != 1 {
    55  		t.Fatalf("DACL AceCount = %d, want 1 (sddl=%q)", dacl.AceCount, sd)
    56  	}
    57  	var ace *windows.ACCESS_ALLOWED_ACE
    58  	if err := windows.GetAce(dacl, 0, &ace); err != nil {
    59  		t.Fatalf("GetAce: %v", err)
    60  	}
    61  	if ace.Header.AceType != windows.ACCESS_ALLOWED_ACE_TYPE {
    62  		t.Errorf("ACE type = %d, want ACCESS_ALLOWED_ACE_TYPE (%d)", ace.Header.AceType, windows.ACCESS_ALLOWED_ACE_TYPE)
    63  	}
    64  	sid := (*windows.SID)(unsafe.Pointer(&ace.SidStart))
    65  	if !sid.Equals(user.User.Sid) {
    66  		t.Errorf("ACE SID = %s, want current user SID %s", sid, user.User.Sid)
    67  	}
    68  }
    69  

View as plain text