|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "net" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + pb "github.com/google/agent-shell-tools/exec_service/execservicepb" |
| 25 | + "github.com/google/agent-shell-tools/exec_service/server" |
| 26 | + "google.golang.org/grpc" |
| 27 | +) |
| 28 | + |
| 29 | +// startTestServer starts an ExecService gRPC server on a temporary Unix socket |
| 30 | +// and returns the socket path. The server is stopped on test cleanup. |
| 31 | +func startTestServer(t *testing.T) string { |
| 32 | + t.Helper() |
| 33 | + sock := filepath.Join(t.TempDir(), "test.sock") |
| 34 | + |
| 35 | + lis, err := net.Listen("unix", sock) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("listen: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + srv := grpc.NewServer() |
| 41 | + pb.RegisterExecServiceServer(srv, &server.ExecServer{}) |
| 42 | + go srv.Serve(lis) |
| 43 | + t.Cleanup(srv.GracefulStop) |
| 44 | + |
| 45 | + return sock |
| 46 | +} |
| 47 | + |
| 48 | +func TestBasicCommand(t *testing.T) { |
| 49 | + sock := startTestServer(t) |
| 50 | + |
| 51 | + var stdout, stderr bytes.Buffer |
| 52 | + code := run([]string{"-addr", sock, "echo", "hello"}, &stdout, &stderr) |
| 53 | + |
| 54 | + if code != 0 { |
| 55 | + t.Errorf("exit code = %d, want 0; stderr: %s", code, stderr.String()) |
| 56 | + } |
| 57 | + if got := strings.TrimSpace(stdout.String()); got != "hello" { |
| 58 | + t.Errorf("stdout = %q, want %q", got, "hello") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestExitCode(t *testing.T) { |
| 63 | + sock := startTestServer(t) |
| 64 | + |
| 65 | + var stdout, stderr bytes.Buffer |
| 66 | + code := run([]string{"-addr", sock, "exit 42"}, &stdout, &stderr) |
| 67 | + |
| 68 | + if code != 42 { |
| 69 | + t.Errorf("exit code = %d, want 42", code) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestWorkingDir(t *testing.T) { |
| 74 | + sock := startTestServer(t) |
| 75 | + dir := t.TempDir() |
| 76 | + |
| 77 | + var stdout, stderr bytes.Buffer |
| 78 | + code := run([]string{"-addr", sock, "-dir", dir, "pwd"}, &stdout, &stderr) |
| 79 | + |
| 80 | + if code != 0 { |
| 81 | + t.Errorf("exit code = %d, want 0; stderr: %s", code, stderr.String()) |
| 82 | + } |
| 83 | + if got := strings.TrimSpace(stdout.String()); got != dir { |
| 84 | + t.Errorf("pwd = %q, want %q", got, dir) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestMissingAddr(t *testing.T) { |
| 89 | + var stdout, stderr bytes.Buffer |
| 90 | + code := run([]string{"echo", "hello"}, &stdout, &stderr) |
| 91 | + |
| 92 | + if code != 2 { |
| 93 | + t.Errorf("exit code = %d, want 2", code) |
| 94 | + } |
| 95 | + if !strings.Contains(stderr.String(), "-addr is required") { |
| 96 | + t.Errorf("stderr = %q, want it to contain %q", stderr.String(), "-addr is required") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestMissingCommand(t *testing.T) { |
| 101 | + var stdout, stderr bytes.Buffer |
| 102 | + code := run([]string{"-addr", "/tmp/fake.sock"}, &stdout, &stderr) |
| 103 | + |
| 104 | + if code != 2 { |
| 105 | + t.Errorf("exit code = %d, want 2", code) |
| 106 | + } |
| 107 | + if !strings.Contains(stderr.String(), "command is required") { |
| 108 | + t.Errorf("stderr = %q, want it to contain %q", stderr.String(), "command is required") |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestStderrOutput(t *testing.T) { |
| 113 | + sock := startTestServer(t) |
| 114 | + |
| 115 | + var stdout, stderr bytes.Buffer |
| 116 | + code := run([]string{"-addr", sock, "echo error >&2"}, &stdout, &stderr) |
| 117 | + |
| 118 | + if code != 0 { |
| 119 | + t.Errorf("exit code = %d, want 0", code) |
| 120 | + } |
| 121 | + // Server merges stderr into stdout stream. |
| 122 | + if got := strings.TrimSpace(stdout.String()); got != "error" { |
| 123 | + t.Errorf("stdout = %q, want %q", got, "error") |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestArgQuoting(t *testing.T) { |
| 128 | + sock := startTestServer(t) |
| 129 | + dir := t.TempDir() |
| 130 | + |
| 131 | + // "touch" with a filename containing a space should create one file, not two. |
| 132 | + var stdout, stderr bytes.Buffer |
| 133 | + code := run([]string{"-addr", sock, "-dir", dir, "touch", "a b"}, &stdout, &stderr) |
| 134 | + if code != 0 { |
| 135 | + t.Fatalf("exit code = %d, want 0; stderr: %s", code, stderr.String()) |
| 136 | + } |
| 137 | + |
| 138 | + // Verify exactly one file named "a b" was created. |
| 139 | + var check bytes.Buffer |
| 140 | + code = run([]string{"-addr", sock, "-dir", dir, "ls"}, &check, &stderr) |
| 141 | + if code != 0 { |
| 142 | + t.Fatalf("ls exit code = %d; stderr: %s", code, stderr.String()) |
| 143 | + } |
| 144 | + if got := strings.TrimSpace(check.String()); got != "a b" { |
| 145 | + t.Errorf("ls = %q, want %q", got, "a b") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestCommandNotFound(t *testing.T) { |
| 150 | + sock := startTestServer(t) |
| 151 | + |
| 152 | + var stdout, stderr bytes.Buffer |
| 153 | + code := run([]string{"-addr", sock, "nonexistent_command_xyz"}, &stdout, &stderr) |
| 154 | + |
| 155 | + if code == 0 { |
| 156 | + t.Error("exit code = 0, want non-zero for command not found") |
| 157 | + } |
| 158 | +} |
0 commit comments