Source file src/syscall/fs_wasip1_test.go

     1  // Copyright 2023 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  //go:build wasip1
     6  
     7  package syscall_test
     8  
     9  import (
    10  	"syscall"
    11  	"testing"
    12  	"unsafe"
    13  )
    14  
    15  var joinPathTests = [...]struct {
    16  	dir, file, path string
    17  }{
    18  	0:  {".", ".", "."},
    19  	1:  {"./", "./", "./"},
    20  	2:  {"././././", ".", "."},
    21  	3:  {".", "./././", "./"},
    22  	4:  {".", "a", "a"},
    23  	5:  {".", "a/b", "a/b"},
    24  	6:  {".", "..", ".."},
    25  	7:  {".", "../", "../"},
    26  	8:  {".", "../../", "../../"},
    27  	9:  {".", "../..", "../.."},
    28  	10: {".", "../..//..///", "../../../"},
    29  	11: {"/", "/", "/"},
    30  	12: {"/", "a", "/a"},
    31  	13: {"/", "a/b", "/a/b"},
    32  	14: {"/a", "b", "/a/b"},
    33  	15: {"/", ".", "/"},
    34  	16: {"/", "..", "/"},
    35  	17: {"/", "../../", "/"},
    36  	18: {"/", "/../a/b/c", "/a/b/c"},
    37  	19: {"/", "/../a/b/c", "/a/b/c"},
    38  	20: {"/", "./hello/world", "/hello/world"},
    39  	21: {"/a", "../", "/"},
    40  	22: {"/a/b/c", "..", "/a/b"},
    41  	23: {"/a/b/c", "..///..///", "/a/"},
    42  	24: {"/a/b/c", "..///..///..", "/"},
    43  	25: {"/a/b/c", "..///..///..///..", "/"},
    44  	26: {"/a/b/c", "..///..///..///..///..", "/"},
    45  	27: {"/a/b/c/", "/d/e/f/", "/a/b/c/d/e/f/"},
    46  	28: {"a/b/c/", ".", "a/b/c"},
    47  	29: {"a/b/c/", "./d", "a/b/c/d"},
    48  	30: {"a/b/c/", "./d/", "a/b/c/d/"},
    49  	31: {"a/b/", "./c/d/", "a/b/c/d/"},
    50  	32: {"../", "..", "../.."},
    51  	33: {"a/b/c/d", "e/../..", "a/b/c"},
    52  	34: {"a/b/c/d", "./e/../..", "a/b/c"},
    53  	35: {"a/b/c/d", "./e/..//../../f/g//", "a/b/f/g/"},
    54  	36: {"../../../", "a/../../b/c", "../../b/c"},
    55  	37: {"/a/b/c", "/.././/hey!", "/a/b/hey!"},
    56  }
    57  
    58  func TestJoinPath(t *testing.T) {
    59  	for _, test := range joinPathTests {
    60  		t.Run("", func(t *testing.T) {
    61  			path := syscall.JoinPath(test.dir, test.file)
    62  			if path != test.path {
    63  				t.Errorf("join(%q,%q): want=%q got=%q", test.dir, test.file, test.path, path)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func BenchmarkJoinPath(b *testing.B) {
    70  	for _, test := range joinPathTests {
    71  		b.Run("", func(b *testing.B) {
    72  			for i := 0; i < b.N; i++ {
    73  				syscall.JoinPath(test.dir, test.file)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestRandomGetOverflow(t *testing.T) {
    80  	// Use unsafe.Slice to avoid actually allocating ~4GB.
    81  	var dummy byte
    82  	b := unsafe.Slice(&dummy, int(1<<32))
    83  	if err := syscall.RandomGet(b); err != syscall.EINVAL {
    84  		t.Errorf("syscall.RandomGet succeeds given a slice that is larger than what random_get supports, want %v", syscall.EINVAL)
    85  	}
    86  }
    87  

View as plain text