|
| 1 | +//go:build linux || darwin || freebsd |
| 2 | + |
| 3 | +package rpc |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "tests/helpers" |
| 15 | + |
| 16 | + "github.com/roadrunner-server/config/v6" |
| 17 | + "github.com/roadrunner-server/logger/v6" |
| 18 | + rpcPlugin "github.com/roadrunner-server/rpc/v6" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +func TestUnixSocketInitErrors(t *testing.T) { |
| 23 | + cases := []struct { |
| 24 | + name string |
| 25 | + listen string |
| 26 | + options string |
| 27 | + wantErr string |
| 28 | + }{ |
| 29 | + {name: "TCP options", listen: "tcp://127.0.0.1:0", options: `{mode: "0600"}`, wantErr: "filesystem unix:// address"}, |
| 30 | + {name: "invalid mode", listen: "unix://rpc.sock", options: `{mode: "0780"}`, wantErr: "invalid unix socket mode"}, |
| 31 | + {name: "unquoted mode", listen: "unix://rpc.sock", options: "{mode: 0600}", wantErr: "invalid unix socket mode"}, |
| 32 | + {name: "empty socket address", listen: "unix://", options: `{mode: "0600"}`, wantErr: "filesystem unix:// address"}, |
| 33 | + {name: "negative UID", listen: "unix://rpc.sock", options: "{uid: -1}", wantErr: "invalid unix socket uid"}, |
| 34 | + {name: "negative GID", listen: "unix://rpc.sock", options: "{gid: -1}", wantErr: "invalid unix socket gid"}, |
| 35 | + {name: "reserved UID", listen: "unix://rpc.sock", options: "{uid: 4294967295}", wantErr: "invalid unix socket uid"}, |
| 36 | + {name: "reserved GID", listen: "unix://rpc.sock", options: "{gid: 4294967295}", wantErr: "invalid unix socket gid"}, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tc := range cases { |
| 40 | + t.Run(tc.name, func(t *testing.T) { |
| 41 | + cfg := &config.Plugin{Path: unixSocketConfig(t, tc.listen, tc.options)} |
| 42 | + require.NoError(t, cfg.Init()) |
| 43 | + log := logger.NewLogger(logger.ChannelConfig{}, slog.New(slog.DiscardHandler)) |
| 44 | + p := &rpcPlugin.Plugin{} |
| 45 | + |
| 46 | + require.ErrorContains(t, p.Init(cfg, log), tc.wantErr) |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestUnixSocketMode(t *testing.T) { |
| 52 | + cases := []struct { |
| 53 | + name string |
| 54 | + flags []string |
| 55 | + mode os.FileMode |
| 56 | + }{ |
| 57 | + {name: "configured mode", mode: 0o600}, |
| 58 | + {name: "mode override", flags: []string{"rpc.unix_socket.mode=0640"}, mode: 0o640}, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range cases { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + t.Chdir(t.TempDir()) |
| 64 | + path := unixSocketConfig(t, "unix://rpc.sock", `{mode: "0600"}`) |
| 65 | + helpers.Start(t, path, rpcPlugins(), helpers.WithConfigFlags(tc.flags...)) |
| 66 | + |
| 67 | + info, err := os.Stat("rpc.sock") |
| 68 | + require.NoError(t, err) |
| 69 | + require.Equal(t, tc.mode, info.Mode().Perm()) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestServesUnixSocket(t *testing.T) { |
| 75 | + t.Chdir(t.TempDir()) |
| 76 | + path := unixSocketConfig(t, "unix://rpc.sock", `{mode: "0600"}`) |
| 77 | + helpers.Start(t, path, rpcPlugins()) |
| 78 | + client := helpers.NewRPCClient(t, "unix", "rpc.sock") |
| 79 | + |
| 80 | + var got string |
| 81 | + require.NoError(t, client.Call("rpc_test.plugin1.Hello", "Valery", &got)) |
| 82 | + require.Equal(t, "Hello, username: Valery", got) |
| 83 | +} |
| 84 | + |
| 85 | +func TestUnixSocketStopRemovesListener(t *testing.T) { |
| 86 | + t.Chdir(t.TempDir()) |
| 87 | + path := unixSocketConfig(t, "unix://rpc.sock", `{mode: "0600"}`) |
| 88 | + stop := helpers.Start(t, path, rpcPlugins()) |
| 89 | + require.FileExists(t, "rpc.sock") |
| 90 | + |
| 91 | + stop() |
| 92 | + |
| 93 | + require.NoFileExists(t, "rpc.sock") |
| 94 | +} |
| 95 | + |
| 96 | +func TestUnixSocketOwnershipErrorRemovesListener(t *testing.T) { |
| 97 | + if os.Geteuid() == 0 { |
| 98 | + t.Skip("Requires an unprivileged process.") |
| 99 | + } |
| 100 | + t.Chdir(t.TempDir()) |
| 101 | + cfg := &config.Plugin{Path: unixSocketConfig(t, "unix://rpc.sock", "{uid: 0}")} |
| 102 | + require.NoError(t, cfg.Init()) |
| 103 | + log := logger.NewLogger(logger.ChannelConfig{}, slog.New(slog.DiscardHandler)) |
| 104 | + p := &rpcPlugin.Plugin{} |
| 105 | + require.NoError(t, p.Init(cfg, log)) |
| 106 | + t.Cleanup(func() { require.NoError(t, p.Stop(context.Background())) }) |
| 107 | + |
| 108 | + select { |
| 109 | + case err := <-p.Serve(): |
| 110 | + require.ErrorContains(t, err, "chown unix socket") |
| 111 | + case <-time.After(5 * time.Second): |
| 112 | + t.Fatal("expected an ownership error") |
| 113 | + } |
| 114 | + require.NoFileExists(t, "rpc.sock") |
| 115 | +} |
| 116 | + |
| 117 | +func unixSocketConfig(t *testing.T, listen, options string) string { |
| 118 | + t.Helper() |
| 119 | + |
| 120 | + contents := fmt.Sprintf(`version: "3" |
| 121 | +rpc: |
| 122 | + listen: %q |
| 123 | + unix_socket: %s |
| 124 | +`, listen, options) |
| 125 | + path := filepath.Join(t.TempDir(), ".rr.yaml") |
| 126 | + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) |
| 127 | + return path |
| 128 | +} |
0 commit comments