1
2
3
4
5 package windows_test
6
7 import (
8 "internal/syscall/windows"
9 "math"
10 "strings"
11 "syscall"
12 "testing"
13 "unsafe"
14 )
15
16 func TestRoundtripNTUnicodeString(t *testing.T) {
17
18 maxString := strings.Repeat("*", (math.MaxUint16/2)-1)
19 for _, test := range []struct {
20 s string
21 wantErr bool
22 }{{
23 s: "",
24 }, {
25 s: "hello",
26 }, {
27 s: "Ƀ",
28 }, {
29 s: maxString,
30 }, {
31 s: maxString + "*",
32 wantErr: true,
33 }, {
34 s: "a\x00a",
35 wantErr: true,
36 }} {
37 ntus, err := windows.NewNTUnicodeString(test.s)
38 if (err != nil) != test.wantErr {
39 t.Errorf("NewNTUnicodeString(%q): %v, wantErr:%v", test.s, err, test.wantErr)
40 continue
41 }
42 if err != nil {
43 continue
44 }
45 u16 := unsafe.Slice(ntus.Buffer, ntus.MaximumLength/2)[:ntus.Length/2]
46 s2 := syscall.UTF16ToString(u16)
47 if test.s != s2 {
48 t.Errorf("round trip of %q = %q, wanted original", test.s, s2)
49 }
50 }
51 }
52
View as plain text