github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/sysfs/oflag_test.go (about)

     1  package sysfs
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/tetratelabs/wazero/experimental/sys"
     8  	"github.com/tetratelabs/wazero/internal/testing/require"
     9  )
    10  
    11  // Test_toOsOpenFlag doesn't use subtests to reduce volume of verbose output,
    12  // and in recognition we have tens of thousands of tests, which can hit IDE
    13  // limits.
    14  func Test_toOsOpenFlag(t *testing.T) {
    15  	tests := []struct {
    16  		name     string
    17  		flag     sys.Oflag
    18  		expected int
    19  	}{
    20  		{name: "O_RDONLY", flag: sys.O_RDONLY, expected: os.O_RDONLY},
    21  		{name: "O_RDWR", flag: sys.O_RDWR, expected: os.O_RDWR},
    22  		{name: "O_WRONLY", flag: sys.O_WRONLY, expected: os.O_WRONLY},
    23  		{name: "O_CREAT", flag: sys.O_CREAT, expected: os.O_RDONLY | os.O_CREATE},
    24  		{name: "O_APPEND", flag: sys.O_APPEND, expected: os.O_RDONLY | os.O_APPEND},
    25  		{
    26  			name:     "all portable",
    27  			flag:     sys.O_RDWR | sys.O_APPEND | sys.O_CREAT | sys.O_EXCL | sys.O_SYNC | sys.O_TRUNC,
    28  			expected: os.O_RDWR | os.O_APPEND | os.O_CREATE | os.O_EXCL | os.O_SYNC | os.O_TRUNC,
    29  		},
    30  		{name: "undefined", flag: 1 << 15, expected: os.O_RDONLY},
    31  	}
    32  
    33  	for _, tc := range tests {
    34  		require.Equal(t, tc.expected, toOsOpenFlag(tc.flag), tc.name)
    35  	}
    36  
    37  	// Tests any supported syscall flags
    38  	for n, f := range map[string]sys.Oflag{
    39  		"O_DIRECTORY": sys.O_DIRECTORY,
    40  		"O_DSYNC":     sys.O_DSYNC,
    41  		"O_NOFOLLOW":  sys.O_NOFOLLOW,
    42  		"O_NONBLOCK":  sys.O_NONBLOCK,
    43  		"O_RSYNC":     sys.O_RSYNC,
    44  	} {
    45  		if supportedSyscallOflag&f == 0 {
    46  			continue
    47  		}
    48  		require.NotEqual(t, 0, toOsOpenFlag(f), n)
    49  	}
    50  
    51  	// Example of a flag that can be or'd into O_RDONLY even if not
    52  	// currently supported in WASI.
    53  	const O_NOATIME = sys.Oflag(0x40000)
    54  	require.Zero(t, 0, toOsOpenFlag(O_NOATIME))
    55  }