Source file src/internal/syscall/windows/string_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  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  	// NTUnicodeString maximum string length must fit in a uint16, less for terminal NUL.
    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