github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/windows/syscall_windows_test.go (about)

     1  // Copyright 2012 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  package windows_test
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"runtime"
    13  	"syscall"
    14  	"testing"
    15  
    16  	"github.com/Andyfoo/golang/x/sys/windows"
    17  )
    18  
    19  func TestWin32finddata(t *testing.T) {
    20  	dir, err := ioutil.TempDir("", "go-build")
    21  	if err != nil {
    22  		t.Fatalf("failed to create temp directory: %v", err)
    23  	}
    24  	defer os.RemoveAll(dir)
    25  
    26  	path := filepath.Join(dir, "long_name.and_extension")
    27  	f, err := os.Create(path)
    28  	if err != nil {
    29  		t.Fatalf("failed to create %v: %v", path, err)
    30  	}
    31  	f.Close()
    32  
    33  	type X struct {
    34  		fd  windows.Win32finddata
    35  		got byte
    36  		pad [10]byte // to protect ourselves
    37  
    38  	}
    39  	var want byte = 2 // it is unlikely to have this character in the filename
    40  	x := X{got: want}
    41  
    42  	pathp, _ := windows.UTF16PtrFromString(path)
    43  	h, err := windows.FindFirstFile(pathp, &(x.fd))
    44  	if err != nil {
    45  		t.Fatalf("FindFirstFile failed: %v", err)
    46  	}
    47  	err = windows.FindClose(h)
    48  	if err != nil {
    49  		t.Fatalf("FindClose failed: %v", err)
    50  	}
    51  
    52  	if x.got != want {
    53  		t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
    54  	}
    55  }
    56  
    57  func TestFormatMessage(t *testing.T) {
    58  	dll := windows.MustLoadDLL("netevent.dll")
    59  
    60  	const TITLE_SC_MESSAGE_BOX uint32 = 0xC0001B75
    61  	const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
    62  	buf := make([]uint16, 300)
    63  	_, err := windows.FormatMessage(flags, uintptr(dll.Handle), TITLE_SC_MESSAGE_BOX, 0, buf, nil)
    64  	if err != nil {
    65  		t.Fatalf("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, TITLE_SC_MESSAGE_BOX, err)
    66  	}
    67  }
    68  
    69  func abort(funcname string, err error) {
    70  	panic(funcname + " failed: " + err.Error())
    71  }
    72  
    73  func ExampleLoadLibrary() {
    74  	h, err := windows.LoadLibrary("kernel32.dll")
    75  	if err != nil {
    76  		abort("LoadLibrary", err)
    77  	}
    78  	defer windows.FreeLibrary(h)
    79  	proc, err := windows.GetProcAddress(h, "GetVersion")
    80  	if err != nil {
    81  		abort("GetProcAddress", err)
    82  	}
    83  	r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
    84  	major := byte(r)
    85  	minor := uint8(r >> 8)
    86  	build := uint16(r >> 16)
    87  	print("windows version ", major, ".", minor, " (Build ", build, ")\n")
    88  }
    89  
    90  func TestTOKEN_ALL_ACCESS(t *testing.T) {
    91  	if windows.TOKEN_ALL_ACCESS != 0xF01FF {
    92  		t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", windows.TOKEN_ALL_ACCESS)
    93  	}
    94  }
    95  
    96  func TestCreateWellKnownSid(t *testing.T) {
    97  	sid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
    98  	if err != nil {
    99  		t.Fatalf("Unable to create well known sid for administrators: %v", err)
   100  	}
   101  	sidStr, err := sid.String()
   102  	if err != nil {
   103  		t.Fatalf("Unable to convert sid into string: %v", err)
   104  	}
   105  	if sidStr != "S-1-5-32-544" {
   106  		t.Fatalf("Expecting administrators to be S-1-5-32-544, but found %s instead", sidStr)
   107  	}
   108  }
   109  
   110  func TestPseudoTokens(t *testing.T) {
   111  	version, err := windows.GetVersion()
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	if ((version&0xffff)>>8)|((version&0xff)<<8) < 0x0602 {
   116  		return
   117  	}
   118  
   119  	realProcessToken, err := windows.OpenCurrentProcessToken()
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	defer realProcessToken.Close()
   124  	realProcessUser, err := realProcessToken.GetTokenUser()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	pseudoProcessToken := windows.GetCurrentProcessToken()
   130  	pseudoProcessUser, err := pseudoProcessToken.GetTokenUser()
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	if !windows.EqualSid(realProcessUser.User.Sid, pseudoProcessUser.User.Sid) {
   135  		t.Fatal("The real process token does not have the same as the pseudo process token")
   136  	}
   137  
   138  	runtime.LockOSThread()
   139  	defer runtime.UnlockOSThread()
   140  
   141  	err = windows.RevertToSelf()
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	pseudoThreadToken := windows.GetCurrentThreadToken()
   147  	_, err = pseudoThreadToken.GetTokenUser()
   148  	if err != windows.ERROR_NO_TOKEN {
   149  		t.Fatal("Expected an empty thread token")
   150  	}
   151  	pseudoThreadEffectiveToken := windows.GetCurrentThreadEffectiveToken()
   152  	pseudoThreadEffectiveUser, err := pseudoThreadEffectiveToken.GetTokenUser()
   153  	if err != nil {
   154  		t.Fatal(nil)
   155  	}
   156  	if !windows.EqualSid(realProcessUser.User.Sid, pseudoThreadEffectiveUser.User.Sid) {
   157  		t.Fatal("The real process token does not have the same as the pseudo thread effective token, even though we aren't impersonating")
   158  	}
   159  
   160  	err = windows.ImpersonateSelf(windows.SecurityImpersonation)
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  	defer windows.RevertToSelf()
   165  	pseudoThreadUser, err := pseudoThreadToken.GetTokenUser()
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  	if !windows.EqualSid(realProcessUser.User.Sid, pseudoThreadUser.User.Sid) {
   170  		t.Fatal("The real process token does not have the same as the pseudo thread token after impersonating self")
   171  	}
   172  }
   173  
   174  func TestGUID(t *testing.T) {
   175  	guid, err := windows.GenerateGUID()
   176  	if err != nil {
   177  		t.Fatal(err)
   178  	}
   179  	if guid.Data1 == 0 && guid.Data2 == 0 && guid.Data3 == 0 && guid.Data4 == [8]byte{} {
   180  		t.Fatal("Got an all zero GUID, which is overwhelmingly unlikely")
   181  	}
   182  	want := fmt.Sprintf("{%08X-%04X-%04X-%04X-%012X}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[:2], guid.Data4[2:])
   183  	got := guid.String()
   184  	if got != want {
   185  		t.Fatalf("String = %q; want %q", got, want)
   186  	}
   187  	guid2, err := windows.GUIDFromString(got)
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  	if guid2 != guid {
   192  		t.Fatalf("Did not parse string back to original GUID = %q; want %q", guid2, guid)
   193  	}
   194  	_, err = windows.GUIDFromString("not-a-real-guid")
   195  	if err != syscall.Errno(windows.CO_E_CLASSSTRING) {
   196  		t.Fatalf("Bad GUID string error = %v; want CO_E_CLASSSTRING", err)
   197  	}
   198  }
   199  
   200  func TestKnownFolderPath(t *testing.T) {
   201  	token, err := windows.OpenCurrentProcessToken()
   202  	if err != nil {
   203  		t.Fatal(err)
   204  	}
   205  	defer token.Close()
   206  	profileDir, err := token.GetUserProfileDirectory()
   207  	if err != nil {
   208  		t.Fatal(err)
   209  	}
   210  	want := filepath.Join(profileDir, "Desktop")
   211  	got, err := windows.KnownFolderPath(windows.FOLDERID_Desktop, windows.KF_FLAG_DEFAULT)
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  	if want != got {
   216  		t.Fatalf("Path = %q; want %q", got, want)
   217  	}
   218  }
   219  
   220  func TestRtlGetVersion(t *testing.T) {
   221  	version := windows.RtlGetVersion()
   222  	major, minor, build := windows.RtlGetNtVersionNumbers()
   223  	// Go is not explictly added to the application compatibility database, so
   224  	// these two functions should return the same thing.
   225  	if version.MajorVersion != major || version.MinorVersion != minor || version.BuildNumber != build {
   226  		t.Fatalf("%d.%d.%d != %d.%d.%d", version.MajorVersion, version.MinorVersion, version.BuildNumber, major, minor, build)
   227  	}
   228  }