Files
rvbox/cmd/rvc/main_test.go
T

89 lines
3.8 KiB
Go

package main
import (
"bytes"
"path/filepath"
"testing"
"time"
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestGlobalSocketAndCLIValueParsing_HP_CTL_11(t *testing.T) {
socket, args, err := globalSocket([]string{"--socket", filepath.Join(t.TempDir(), "rvbox.sock"), "run", "client", "echo"})
if err != nil || args[0] != "run" || socket == "" {
t.Fatalf("global socket = %q %#v, %v", socket, args, err)
}
if _, _, err := globalSocket([]string{"--socket", "relative.sock", "stat"}); err == nil {
t.Fatal("relative control socket accepted")
}
for input, want := range map[string]rvboxv1.ShellType{"sh": rvboxv1.ShellType_SHELL_SH, "bash": rvboxv1.ShellType_SHELL_BASH, "cmd": rvboxv1.ShellType_SHELL_CMD, "pwsh": rvboxv1.ShellType_SHELL_POWERSHELL, "": rvboxv1.ShellType_SHELL_TYPE_UNSPECIFIED} {
got, err := parseShell(input)
if err != nil || got != want {
t.Errorf("parseShell(%q) = %v, %v", input, got, err)
}
}
if _, err := parseProfile("unknown"); err == nil {
t.Fatal("unknown profile accepted")
}
if got := queueDuration(-time.Nanosecond); got != nil {
t.Fatal("negative queue duration was encoded")
}
if got := queueDuration(0); got == nil || got.AsDuration() != 0 {
t.Fatalf("zero queue duration = %v", got)
}
}
func TestGlobalRequestIDIsInjectedOnlyForMutations_HP_CTL_12(t *testing.T) {
t.Parallel()
id := "019c46f1-1d02-7000-8000-0000000000f1"
requestID, remaining, err := globalRequestID([]string{"--request-id", id, "run", "client", "echo"})
if err != nil || requestID != id || len(remaining) != 3 || remaining[0] != "run" {
t.Fatalf("global request ID = %q %#v %v", requestID, remaining, err)
}
if !readOnlyCommand([]string{"stat", "client"}) || !readOnlyCommand([]string{"storage", "incidents"}) || readOnlyCommand([]string{"run", "client", "echo"}) {
t.Fatal("read-only command classification is incorrect")
}
if _, _, err := globalRequestID([]string{"--request-id", id, "--request-id", id}); err == nil {
t.Fatal("duplicate global request IDs accepted")
}
}
func TestVersionDoesNotRequireControlSocket_HP_CTL_16(t *testing.T) {
t.Parallel()
var output, diagnostics bytes.Buffer
if err := run([]string{"--version"}, &output, &diagnostics); err != nil || output.String() != "dev\n" {
t.Fatalf("version = %q, %v", output.String(), err)
}
}
func TestRenderCommandStatShowsExpiryRetentionAndWindowsIdentity_HP_CTL_13(t *testing.T) {
t.Parallel()
expiry := time.Date(2026, 9, 6, 12, 0, 0, 0, time.UTC)
sessionID := uint32(7)
effective := rvboxv1.WindowsExecutionContext_WINDOWS_EXECUTION_CONTEXT_ACTIVE_SYSTEM
var output bytes.Buffer
renderCommandStat(&output, &rvboxv1.CommandRecord{
IssueUuid: "019c46f1-1d02-7000-8000-0000000000f2", TargetClientId: "win-a",
Lifecycle: rvboxv1.CommandLifecycle_COMMAND_SUCCEEDED, CommandRevision: 3,
LastEventSeq: 9, QueueExpiryTime: timestamppb.New(expiry), OutputIncomplete: true,
OutputTruncated: true, RetainedCompressedBytes: 1234, LateAfterExpiry: true,
WindowsExecutionIdentity: &rvboxv1.WindowsExecutionIdentity{
EffectiveContext: &effective, SessionId: &sessionID, EffectiveUserSid: "S-1-5-18",
SessionUserSid: "S-1-5-21-user", AttemptedContexts: []rvboxv1.WindowsExecutionContext{
rvboxv1.WindowsExecutionContext_WINDOWS_EXECUTION_CONTEXT_ACTIVE_USER_ELEVATED, effective,
}, SelectionDetail: "fallback\nused",
},
}, expiry.Add(time.Minute))
for _, want := range []string{
"queue_expired=true", "late_after_expiry=true", "output_truncated=true", "output_incomplete=true",
"retained_compressed_bytes=1234", "windows_attempted_contexts=active-user-elevated,active-system",
"windows_effective_context=active-system", "windows_session_id=7", "windows_selection_detail=fallback\\nused",
} {
if !bytes.Contains(output.Bytes(), []byte(want)) {
t.Errorf("stat output missing %q:\n%s", want, output.String())
}
}
}