windows: add GUID handling functions

Virtually every project that works with x/sys/window's GUIDs winds up
rolling their own version of this in one way or another. So let's add
the correct win32 wrappers for it, so that these are always generated,
parsed, and converted in the uniform correct way.

Change-Id: I35f4b4ab5fc681d3e16fc5bbaf2cb20031eb3f12
Reviewed-on: https://go-review.googlesource.com/c/sys/+/180938
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Jason A. Donenfeld
2019-06-06 20:33:20 +00:00
committed by Jason Donenfeld
parent 5da285871e
commit 7fc4e5ec14
3 changed files with 92 additions and 0 deletions
+23
View File
@@ -5,6 +5,7 @@
package windows_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -169,3 +170,25 @@ func TestPseudoTokens(t *testing.T) {
t.Fatal("The real process token does not have the same as the pseudo thread token after impersonating self")
}
}
func TestGUID(t *testing.T) {
guid, err := windows.GenerateGUID()
if err != nil {
t.Fatal(err)
}
if guid.Data1 == 0 && guid.Data2 == 0 && guid.Data3 == 0 && guid.Data4 == [8]byte{} {
t.Fatal("Got an all zero GUID, which is overwhelmingly unlikely")
}
want := fmt.Sprintf("{%08X-%04X-%04X-%04X-%012X}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[:2], guid.Data4[2:])
got := guid.String()
if got != want {
t.Fatalf("String = %q; want %q", got, want)
}
guid2, err := windows.GUIDFromString(got)
if err != nil {
t.Fatal(err)
}
if guid2 != guid {
t.Fatalf("Did not parse string back to original GUID = %q; want %q", guid2, guid)
}
}