github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/fsconfig_test.go (about)

     1  package wazero
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/wasilibs/wazerox/experimental/sys"
     7  	"github.com/wasilibs/wazerox/internal/sysfs"
     8  	testfs "github.com/wasilibs/wazerox/internal/testing/fs"
     9  	"github.com/wasilibs/wazerox/internal/testing/require"
    10  )
    11  
    12  // TestFSConfig only tests the cases that change the inputs to sysfs.ValidatePreopens.
    13  func TestFSConfig(t *testing.T) {
    14  	base := NewFSConfig()
    15  
    16  	testFS := testfs.FS{}
    17  	testFS2 := testfs.FS{"/": &testfs.File{}}
    18  
    19  	tests := []struct {
    20  		name               string
    21  		input              FSConfig
    22  		expectedFS         []sys.FS
    23  		expectedGuestPaths []string
    24  	}{
    25  		{
    26  			name:  "empty",
    27  			input: base,
    28  		},
    29  		{
    30  			name:               "WithFSMount",
    31  			input:              base.WithFSMount(testFS, "/"),
    32  			expectedFS:         []sys.FS{&sysfs.AdaptFS{FS: testFS}},
    33  			expectedGuestPaths: []string{"/"},
    34  		},
    35  		{
    36  			name:               "WithFSMount overwrites",
    37  			input:              base.WithFSMount(testFS, "/").WithFSMount(testFS2, "/"),
    38  			expectedFS:         []sys.FS{&sysfs.AdaptFS{FS: testFS2}},
    39  			expectedGuestPaths: []string{"/"},
    40  		},
    41  		{
    42  			name:  "WithFsMount nil",
    43  			input: base.WithFSMount(nil, "/"),
    44  		},
    45  		{
    46  			name:               "WithDirMount overwrites",
    47  			input:              base.WithFSMount(testFS, "/").WithDirMount(".", "/"),
    48  			expectedFS:         []sys.FS{sysfs.DirFS(".")},
    49  			expectedGuestPaths: []string{"/"},
    50  		},
    51  		{
    52  			name:               "multiple",
    53  			input:              base.WithReadOnlyDirMount(".", "/").WithDirMount("/tmp", "/tmp"),
    54  			expectedFS:         []sys.FS{&sysfs.ReadFS{FS: sysfs.DirFS(".")}, sysfs.DirFS("/tmp")},
    55  			expectedGuestPaths: []string{"/", "/tmp"},
    56  		},
    57  	}
    58  
    59  	for _, tt := range tests {
    60  		tc := tt
    61  
    62  		t.Run(tc.name, func(t *testing.T) {
    63  			fs, guestPaths := tc.input.(*fsConfig).preopens()
    64  			require.Equal(t, tc.expectedFS, fs)
    65  			require.Equal(t, tc.expectedGuestPaths, guestPaths)
    66  		})
    67  	}
    68  }
    69  
    70  func TestFSConfig_clone(t *testing.T) {
    71  	fc := NewFSConfig().(*fsConfig)
    72  	fc.guestPathToFS["/"] = 0
    73  
    74  	cloned := fc.clone()
    75  
    76  	// Make post-clone changes
    77  	fc.guestPaths = []string{"/"}
    78  	fc.guestPathToFS["/"] = 1
    79  
    80  	// Ensure the guestPathToFS map is not shared
    81  	require.Equal(t, map[string]int{"/": 1}, fc.guestPathToFS)
    82  	require.Equal(t, map[string]int{"/": 0}, cloned.guestPathToFS)
    83  
    84  	// Ensure the guestPaths slice is not shared
    85  	require.Zero(t, len(cloned.guestPaths))
    86  }