...

Source file src/github.com/Microsoft/hcsshim/internal/winapi/winapi_test.go

Documentation: github.com/Microsoft/hcsshim/internal/winapi

     1  //go:build windows
     2  
     3  package winapi
     4  
     5  import (
     6  	"strings"
     7  	"testing"
     8  	"unicode/utf16"
     9  )
    10  
    11  func wideStringsEqual(target, actual []uint16) bool {
    12  	if len(target) != len(actual) {
    13  		return false
    14  	}
    15  
    16  	for i := range target {
    17  		if target[i] != actual[i] {
    18  			return false
    19  		}
    20  	}
    21  	return true
    22  }
    23  
    24  func TestNewUnicodeString(t *testing.T) {
    25  	// include UTF8 chars which take more than 8 bits to encode
    26  	targetStrings := []string{"abcde", "abcd\n", "C:\\Test", "\\&_Test", "Äb", "\u8483\u119A2\u0041"}
    27  	for _, target := range targetStrings {
    28  		targetWideString := utf16.Encode(([]rune)(target))
    29  		targetLength := uint16(len(targetWideString) * 2)
    30  
    31  		uni, err := NewUnicodeString(target)
    32  		if err != nil {
    33  			t.Fatalf("failed to convert target string %s to Unicode String with %v", target, err)
    34  		}
    35  
    36  		if uni.Length != targetLength {
    37  			t.Fatalf("Expected new Unicode String length to be %d for target string %s, got %d instead", targetLength, target, uni.Length)
    38  		}
    39  		if uni.MaximumLength != targetLength {
    40  			t.Fatalf("Expected new Unicode String maximum length to be %d for target string %s, got %d instead", targetLength, target, uni.MaximumLength)
    41  		}
    42  
    43  		uniBufferStringAsSlice := Uint16BufferToSlice(uni.Buffer, int(targetLength/2))
    44  
    45  		if !wideStringsEqual(targetWideString, uniBufferStringAsSlice) {
    46  			t.Fatalf("Expected wide string %v, got %v instead", targetWideString, uniBufferStringAsSlice)
    47  		}
    48  	}
    49  }
    50  
    51  func TestUnicodeToString(t *testing.T) {
    52  	targetStrings := []string{"abcde", "abcd\n", "C:\\Test", "\\&_Test", "Äb", "\u8483\u119A2\u0041"}
    53  	for _, target := range targetStrings {
    54  		uni, err := NewUnicodeString(target)
    55  		if err != nil {
    56  			t.Fatalf("failed to convert target string %s to Unicode String with %v", target, err)
    57  		}
    58  
    59  		actualString := uni.String()
    60  		if actualString != target {
    61  			t.Fatalf("Expected unicode string function to return %s, instead got %s", target, actualString)
    62  		}
    63  	}
    64  }
    65  
    66  func TestUnicodeStringLimit(t *testing.T) {
    67  	var sb strings.Builder
    68  
    69  	// limit in bytes of how long the input string can be
    70  	// -1 to account for null character.
    71  	limit := NTSTRSAFE_UNICODE_STRING_MAX_CCH - 1
    72  
    73  	lengths := []int{limit - 1, limit, limit + 1}
    74  	testStrings := []string{}
    75  	for _, len := range lengths {
    76  		sb.Reset()
    77  		for i := 0; i < len; i++ {
    78  			// We are deliberately writing byte 41 here as it takes only 8
    79  			// bits in UTF-8 encoding.  If we use non-ASCII chars the limit
    80  			// calculations used above won't work.
    81  			if err := sb.WriteByte(41); err != nil {
    82  				t.Fatalf("string creation failed: %s", err)
    83  			}
    84  		}
    85  		testStrings = append(testStrings, sb.String())
    86  	}
    87  
    88  	for i, testStr := range testStrings {
    89  		_, err := NewUnicodeString(testStr)
    90  		if lengths[i] > limit && err == nil {
    91  			t.Fatalf("input string of length %d should throw ENAMETOOLONG error", lengths[i])
    92  		} else if lengths[i] <= limit && err != nil {
    93  			t.Fatalf("unexpected error for length %d: %s", lengths[i], err)
    94  		}
    95  	}
    96  }
    97  

View as plain text