From 7e3bd4db0f6b9c70cb3f70471db3f6ff4cf20a9a Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Tue, 25 Oct 2022 11:33:21 -0700 Subject: [PATCH] Add rudimentary UDS test to lisafs PiperOrigin-RevId: 483732841 --- pkg/lisafs/testsuite/testsuite.go | 57 ++++++++++++++++++++++++++++++- runsc/fsgofer/BUILD | 1 + runsc/fsgofer/lisafs_test.go | 3 +- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/pkg/lisafs/testsuite/testsuite.go b/pkg/lisafs/testsuite/testsuite.go index bf46e9786..4193ad57c 100644 --- a/pkg/lisafs/testsuite/testsuite.go +++ b/pkg/lisafs/testsuite/testsuite.go @@ -77,6 +77,7 @@ var localFSTests = map[string]TestFunc{ "Walk": testWalk, "Rename": testRename, "Mknod": testMknod, + "UDS": testUDS, "Getdents": testGetdents, } @@ -200,6 +201,14 @@ func mknod(ctx context.Context, t *testing.T, dir lisafs.ClientFD, name string) return dir.Client().NewFD(nodeIno.ControlFD), nodeIno.Stat } +func bind(ctx context.Context, t *testing.T, dir lisafs.ClientFD, name string, sockType linux.SockType) (lisafs.ClientFD, *lisafs.ClientBoundSocketFD, linux.Statx) { + nodeIno, socket, err := dir.BindAt(ctx, sockType, name, 0777, lisafs.UID(unix.Getuid()), lisafs.GID(unix.Getgid())) + if err != nil { + t.Fatalf("bind failed: %v", err) + } + return dir.Client().NewFD(nodeIno.ControlFD), socket, nodeIno.Stat +} + func walk(ctx context.Context, t *testing.T, dir lisafs.ClientFD, names []string) []lisafs.Inode { _, inodes, err := dir.WalkMultiple(ctx, names) if err != nil { @@ -563,10 +572,22 @@ func testRename(ctx context.Context, t *testing.T, tester Tester, root lisafs.Cl } func testMknod(ctx context.Context, t *testing.T, tester Tester, root lisafs.ClientFD) { - name := "namedPipe" + name := "regular-file" pipeFile, pipeStat := mknod(ctx, t, root, name) defer closeFD(ctx, t, pipeFile) + if got := pipeStat.Mode & unix.S_IFMT; got != unix.S_IFREG { + t.Errorf("socket file mode is incorrect: want %#x, got %#x", unix.S_IFSOCK, got) + } + if tester.SetUserGroupIDSupported() { + if want := unix.Getuid(); int(pipeStat.UID) != want { + t.Errorf("socket file uid is incorrect: want %d, got %d", want, pipeStat.UID) + } + if want := unix.Getgid(); int(pipeStat.GID) != want { + t.Errorf("socket file gid is incorrect: want %d, got %d", want, pipeStat.GID) + } + } + var stat linux.Statx statTo(ctx, t, pipeFile, &stat) @@ -581,6 +602,40 @@ func testMknod(ctx context.Context, t *testing.T, tester Tester, root lisafs.Cli } } +func testUDS(ctx context.Context, t *testing.T, tester Tester, root lisafs.ClientFD) { + const name = "sock" + file, socket, stat := bind(ctx, t, root, name, unix.SOCK_STREAM) + defer closeFD(ctx, t, file) + defer socket.Close(ctx) + + if got := stat.Mode & unix.S_IFMT; got != unix.S_IFSOCK { + t.Errorf("socket file mode is incorrect: want %#x, got %#x", unix.S_IFSOCK, got) + } + if tester.SetUserGroupIDSupported() { + if want := unix.Getuid(); int(stat.UID) != want { + t.Errorf("socket file uid is incorrect: want %d, got %d", want, stat.UID) + } + if want := unix.Getgid(); int(stat.GID) != want { + t.Errorf("socket file gid is incorrect: want %d, got %d", want, stat.GID) + } + } + + var got linux.Statx + statTo(ctx, t, file, &got) + if stat.Mode != got.Mode { + t.Errorf("UDS mode is incorrect: want %d, got %d", stat.Mode, got.Mode) + } + if stat.UID != got.UID { + t.Errorf("mknod UID is incorrect: want %d, got %d", stat.UID, got.UID) + } + if stat.GID != got.GID { + t.Errorf("mknod GID is incorrect: want %d, got %d", stat.GID, got.GID) + } + + // TODO(b/194709873): Once listen and accept are implemented, test connecting + // and accepting a connection using sockF. +} + func testGetdents(ctx context.Context, t *testing.T, tester Tester, root lisafs.ClientFD) { tempDir, _ := mkdir(ctx, t, root, "tempDir") defer closeFD(ctx, t, tempDir) diff --git a/runsc/fsgofer/BUILD b/runsc/fsgofer/BUILD index a02c201b1..7322f8c93 100644 --- a/runsc/fsgofer/BUILD +++ b/runsc/fsgofer/BUILD @@ -53,5 +53,6 @@ go_test( "//pkg/lisafs", "//pkg/lisafs/testsuite", "//pkg/log", + "//runsc/config", ], ) diff --git a/runsc/fsgofer/lisafs_test.go b/runsc/fsgofer/lisafs_test.go index 1b0e3fa64..bfa2c1880 100644 --- a/runsc/fsgofer/lisafs_test.go +++ b/runsc/fsgofer/lisafs_test.go @@ -20,6 +20,7 @@ import ( "gvisor.dev/gvisor/pkg/lisafs" "gvisor.dev/gvisor/pkg/lisafs/testsuite" "gvisor.dev/gvisor/pkg/log" + "gvisor.dev/gvisor/runsc/config" "gvisor.dev/gvisor/runsc/fsgofer" ) @@ -38,7 +39,7 @@ type tester struct{} // NewServer implements testsuite.Tester.NewServer. func (tester) NewServer(t *testing.T) *lisafs.Server { - return &fsgofer.NewLisafsServer(fsgofer.Config{}).Server + return &fsgofer.NewLisafsServer(fsgofer.Config{HostUDS: config.HostUDSCreate}).Server } // LinkSupported implements testsuite.Tester.LinkSupported.