2023-09-23 05:19:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-10-14 19:52:35 +02:00
|
|
|
"context"
|
2023-09-23 05:19:12 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-10-14 19:52:35 +02:00
|
|
|
"os"
|
2023-09-23 05:19:12 +02:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-09-30 06:38:50 +02:00
|
|
|
"syscall"
|
2023-09-23 05:19:12 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ddworken/hishtory/client/data"
|
|
|
|
"github.com/ddworken/hishtory/client/hctx"
|
|
|
|
"github.com/ddworken/hishtory/client/lib"
|
|
|
|
"github.com/ddworken/hishtory/shared"
|
|
|
|
"github.com/ddworken/hishtory/shared/testutils"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type shellTester interface {
|
|
|
|
RunInteractiveShell(t testing.TB, script string) string
|
|
|
|
RunInteractiveShellRelaxed(t testing.TB, script string) (string, error)
|
2023-09-30 06:38:50 +02:00
|
|
|
RunInteractiveShellBackground(t testing.TB, script string) error
|
2023-09-23 05:19:12 +02:00
|
|
|
ShellName() string
|
|
|
|
}
|
2023-11-12 09:42:39 +01:00
|
|
|
type bashTester struct{}
|
2023-09-23 05:19:12 +02:00
|
|
|
|
|
|
|
func (b bashTester) RunInteractiveShell(t testing.TB, script string) string {
|
|
|
|
out, err := b.RunInteractiveShellRelaxed(t, "set -emo pipefail\n"+script)
|
|
|
|
if err != nil {
|
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
2023-10-21 17:59:27 +02:00
|
|
|
require.NoError(t, err, fmt.Sprintf("error when running command at %s:%dv", filename, line))
|
2023-09-23 05:19:12 +02:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b bashTester) RunInteractiveShellRelaxed(t testing.TB, script string) (string, error) {
|
|
|
|
cmd := exec.Command("bash", "-i")
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unexpected error when running commands, out=%#v, err=%#v: %w", stdout.String(), stderr.String(), err)
|
|
|
|
}
|
|
|
|
outStr := stdout.String()
|
|
|
|
require.NotContains(t, outStr, "hishtory fatal error", "Ran command, but hishtory had a fatal error!")
|
|
|
|
return outStr, nil
|
|
|
|
}
|
|
|
|
|
2023-09-30 06:38:50 +02:00
|
|
|
func (b bashTester) RunInteractiveShellBackground(t testing.TB, script string) error {
|
|
|
|
cmd := exec.Command("bash", "-i")
|
|
|
|
// SetSid: true is required to prevent SIGTTIN signal killing the entire test
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
cmd.Stdout = nil
|
|
|
|
cmd.Stderr = nil
|
|
|
|
return cmd.Start()
|
|
|
|
}
|
|
|
|
|
2023-09-23 05:19:12 +02:00
|
|
|
func (b bashTester) ShellName() string {
|
|
|
|
return "bash"
|
|
|
|
}
|
|
|
|
|
2023-11-12 09:42:39 +01:00
|
|
|
type zshTester struct{}
|
2023-09-23 05:19:12 +02:00
|
|
|
|
|
|
|
func (z zshTester) RunInteractiveShell(t testing.TB, script string) string {
|
|
|
|
res, err := z.RunInteractiveShellRelaxed(t, "set -eo pipefail\n"+script)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z zshTester) RunInteractiveShellRelaxed(t testing.TB, script string) (string, error) {
|
|
|
|
cmd := exec.Command("zsh", "-is")
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return stdout.String(), fmt.Errorf("unexpected error when running command=%#v, out=%#v, err=%#v: %w", script, stdout.String(), stderr.String(), err)
|
|
|
|
}
|
|
|
|
outStr := stdout.String()
|
|
|
|
require.NotContains(t, outStr, "hishtory fatal error")
|
|
|
|
return outStr, nil
|
|
|
|
}
|
|
|
|
|
2023-09-30 06:38:50 +02:00
|
|
|
func (z zshTester) RunInteractiveShellBackground(t testing.TB, script string) error {
|
|
|
|
cmd := exec.Command("zsh", "-is")
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
cmd.Stdout = nil
|
|
|
|
cmd.Stderr = nil
|
|
|
|
return cmd.Start()
|
|
|
|
}
|
|
|
|
|
2023-09-23 05:19:12 +02:00
|
|
|
func (z zshTester) ShellName() string {
|
|
|
|
return "zsh"
|
|
|
|
}
|
|
|
|
|
|
|
|
type OnlineStatus int64
|
|
|
|
|
|
|
|
const (
|
|
|
|
Online OnlineStatus = iota
|
|
|
|
Offline
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertOnlineStatus(t testing.TB, onlineStatus OnlineStatus) {
|
|
|
|
config := hctx.GetConf(hctx.MakeContext())
|
|
|
|
if onlineStatus == Online && config.IsOffline {
|
|
|
|
t.Fatalf("We're supposed to be online, yet config.IsOffline=%#v (config=%#v)", config.IsOffline, config)
|
|
|
|
}
|
|
|
|
if onlineStatus == Offline && !config.IsOffline {
|
|
|
|
t.Fatalf("We're supposed to be offline, yet config.IsOffline=%#v (config=%#v)", config.IsOffline, config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hishtoryQuery(t testing.TB, tester shellTester, query string) string {
|
|
|
|
return tester.RunInteractiveShell(t, "hishtory query "+query)
|
|
|
|
}
|
|
|
|
|
|
|
|
func manuallySubmitHistoryEntry(t testing.TB, userSecret string, entry data.HistoryEntry) {
|
|
|
|
encEntry, err := data.EncryptHistoryEntry(userSecret, entry)
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-23 05:19:12 +02:00
|
|
|
if encEntry.Date != entry.EndTime {
|
|
|
|
t.Fatalf("encEntry.Date does not match the entry")
|
|
|
|
}
|
|
|
|
jsonValue, err := json.Marshal([]shared.EncHistoryEntry{encEntry})
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-23 05:19:12 +02:00
|
|
|
require.NotEqual(t, "", entry.DeviceId)
|
|
|
|
resp, err := http.Post("http://localhost:8080/api/v1/submit?source_device_id="+entry.DeviceId, "application/json", bytes.NewBuffer(jsonValue))
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-23 05:19:12 +02:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatalf("failed to submit result to backend, status_code=%d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read resp.Body: %v", err)
|
|
|
|
}
|
|
|
|
submitResp := shared.SubmitResponse{}
|
|
|
|
err = json.Unmarshal(respBody, &submitResp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to deserialize SubmitResponse: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func captureTerminalOutput(t testing.TB, tester shellTester, commands []string) string {
|
|
|
|
return captureTerminalOutputWithShellName(t, tester, tester.ShellName(), commands)
|
|
|
|
}
|
|
|
|
|
|
|
|
func captureTerminalOutputWithComplexCommands(t testing.TB, tester shellTester, commands []TmuxCommand) string {
|
|
|
|
return captureTerminalOutputWithShellNameAndDimensions(t, tester, tester.ShellName(), 200, 50, commands)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TmuxCommand struct {
|
|
|
|
Keys string
|
|
|
|
ResizeX int
|
|
|
|
ResizeY int
|
|
|
|
ExtraDelay float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func captureTerminalOutputWithShellName(t testing.TB, tester shellTester, overriddenShellName string, commands []string) string {
|
|
|
|
sCommands := make([]TmuxCommand, 0)
|
|
|
|
for _, command := range commands {
|
|
|
|
sCommands = append(sCommands, TmuxCommand{Keys: command})
|
|
|
|
}
|
|
|
|
return captureTerminalOutputWithShellNameAndDimensions(t, tester, overriddenShellName, 200, 50, sCommands)
|
|
|
|
}
|
|
|
|
|
|
|
|
func captureTerminalOutputWithShellNameAndDimensions(t testing.TB, tester shellTester, overriddenShellName string, width, height int, commands []TmuxCommand) string {
|
2023-09-30 03:21:23 +02:00
|
|
|
return captureTerminalOutputComplex(t,
|
|
|
|
TmuxCaptureConfig{
|
|
|
|
tester: tester,
|
|
|
|
overriddenShellName: overriddenShellName,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
complexCommands: commands,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type TmuxCaptureConfig struct {
|
|
|
|
tester shellTester
|
|
|
|
overriddenShellName string
|
|
|
|
commands []string
|
|
|
|
complexCommands []TmuxCommand
|
|
|
|
width, height int
|
|
|
|
includeEscapeSequences bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func captureTerminalOutputComplex(t testing.TB, captureConfig TmuxCaptureConfig) string {
|
|
|
|
require.NotNil(t, captureConfig.tester)
|
|
|
|
if captureConfig.overriddenShellName == "" {
|
|
|
|
captureConfig.overriddenShellName = captureConfig.tester.ShellName()
|
|
|
|
}
|
|
|
|
if captureConfig.width == 0 {
|
|
|
|
captureConfig.width = 200
|
|
|
|
}
|
|
|
|
if captureConfig.height == 0 {
|
|
|
|
captureConfig.height = 50
|
|
|
|
}
|
2023-09-23 05:19:12 +02:00
|
|
|
sleepAmount := "0.1"
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
sleepAmount = "0.2"
|
|
|
|
}
|
2023-09-30 03:21:23 +02:00
|
|
|
if captureConfig.overriddenShellName == "fish" {
|
2023-09-23 05:19:12 +02:00
|
|
|
// Fish is considerably slower so this is sadly necessary
|
|
|
|
sleepAmount = "0.5"
|
|
|
|
}
|
|
|
|
if testutils.IsGithubAction() {
|
|
|
|
sleepAmount = "0.5"
|
|
|
|
}
|
|
|
|
fullCommand := ""
|
|
|
|
fullCommand += " tmux kill-session -t foo || true\n"
|
2023-09-30 03:21:23 +02:00
|
|
|
fullCommand += fmt.Sprintf(" tmux -u new-session -d -x %d -y %d -s foo %s\n", captureConfig.width, captureConfig.height, captureConfig.overriddenShellName)
|
2023-09-23 05:19:12 +02:00
|
|
|
fullCommand += " sleep 1\n"
|
2023-09-30 03:21:23 +02:00
|
|
|
if captureConfig.overriddenShellName == "bash" {
|
2023-09-23 05:19:12 +02:00
|
|
|
fullCommand += " tmux send -t foo SPACE source SPACE ~/.bashrc ENTER\n"
|
|
|
|
}
|
|
|
|
fullCommand += " sleep " + sleepAmount + "\n"
|
2023-09-30 03:21:23 +02:00
|
|
|
if len(captureConfig.commands) > 0 {
|
|
|
|
require.Empty(t, captureConfig.complexCommands)
|
|
|
|
for _, command := range captureConfig.commands {
|
|
|
|
captureConfig.complexCommands = append(captureConfig.complexCommands, TmuxCommand{Keys: command})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NotEmpty(t, captureConfig.complexCommands)
|
|
|
|
for _, cmd := range captureConfig.complexCommands {
|
2023-09-23 05:19:12 +02:00
|
|
|
if cmd.Keys != "" {
|
2023-11-12 10:13:22 +01:00
|
|
|
fullCommand += " tmux send -t foo -- "
|
2023-09-23 05:19:12 +02:00
|
|
|
fullCommand += cmd.Keys
|
2023-11-12 10:13:22 +01:00
|
|
|
fullCommand += "\n"
|
2023-09-23 05:19:12 +02:00
|
|
|
}
|
|
|
|
if cmd.ResizeX != 0 && cmd.ResizeY != 0 {
|
|
|
|
fullCommand += fmt.Sprintf(" tmux resize-window -t foo -x %d -y %d\n", cmd.ResizeX, cmd.ResizeY)
|
|
|
|
}
|
|
|
|
if cmd.ExtraDelay != 0 {
|
|
|
|
fullCommand += fmt.Sprintf(" sleep %f\n", cmd.ExtraDelay)
|
|
|
|
}
|
|
|
|
fullCommand += " sleep " + sleepAmount + "\n"
|
|
|
|
}
|
|
|
|
fullCommand += " sleep 2.5\n"
|
|
|
|
if testutils.IsGithubAction() {
|
|
|
|
fullCommand += " sleep 2.5\n"
|
|
|
|
}
|
2023-09-30 03:21:23 +02:00
|
|
|
fullCommand += " tmux capture-pane -t foo -p"
|
|
|
|
if captureConfig.includeEscapeSequences {
|
|
|
|
fullCommand += "e"
|
|
|
|
}
|
|
|
|
fullCommand += "\n"
|
2023-09-23 05:19:12 +02:00
|
|
|
fullCommand += " tmux kill-session -t foo\n"
|
|
|
|
testutils.TestLog(t, "Running tmux command: "+fullCommand)
|
2023-09-30 03:21:23 +02:00
|
|
|
return strings.TrimSpace(captureConfig.tester.RunInteractiveShell(t, fullCommand))
|
2023-09-23 05:19:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertNoLeakedConnections(t testing.TB) {
|
2023-10-14 19:52:35 +02:00
|
|
|
resp, err := lib.ApiGet(makeTestOnlyContextWithFakeConfig(), "/api/v1/get-num-connections")
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-23 05:19:12 +02:00
|
|
|
numConnections, err := strconv.Atoi(string(resp))
|
2023-09-30 03:21:23 +02:00
|
|
|
require.NoError(t, err)
|
2023-09-23 05:19:12 +02:00
|
|
|
if numConnections > 1 {
|
|
|
|
t.Fatalf("DB has %d open connections, expected to have 1 or less", numConnections)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPidofCommand() string {
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// MacOS doesn't have pidof by default
|
|
|
|
return "pgrep"
|
|
|
|
}
|
|
|
|
return "pidof"
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:52:35 +02:00
|
|
|
func makeTestOnlyContextWithFakeConfig() context.Context {
|
|
|
|
fakeConfig := hctx.ClientConfig{
|
|
|
|
UserSecret: "FAKE_TEST_DEVICE",
|
|
|
|
DeviceId: "FAKE_TEST_DEVICE",
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, hctx.ConfigCtxKey, &fakeConfig)
|
|
|
|
// Note: We don't create a DB here
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("failed to get homedir: %w", err))
|
|
|
|
}
|
|
|
|
return context.WithValue(ctx, hctx.HomedirCtxKey, homedir)
|
|
|
|
}
|
|
|
|
|
2023-09-23 05:19:12 +02:00
|
|
|
type deviceSet struct {
|
|
|
|
deviceMap *map[device]deviceOp
|
|
|
|
currentDevice *device
|
|
|
|
}
|
|
|
|
|
|
|
|
type device struct {
|
|
|
|
key string
|
|
|
|
deviceId string
|
|
|
|
}
|
|
|
|
|
|
|
|
type deviceOp struct {
|
|
|
|
backup func()
|
|
|
|
restore func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDevice(t testing.TB, tester shellTester, devices *deviceSet, key, deviceId string) {
|
|
|
|
d := device{key, deviceId}
|
|
|
|
_, ok := (*devices.deviceMap)[d]
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("cannot create device twice for key=%s deviceId=%s", key, deviceId)
|
|
|
|
}
|
|
|
|
installHishtory(t, tester, key)
|
|
|
|
(*devices.deviceMap)[d] = deviceOp{
|
|
|
|
backup: func() { testutils.BackupAndRestoreWithId(t, key+deviceId) },
|
|
|
|
restore: testutils.BackupAndRestoreWithId(t, key+deviceId),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func switchToDevice(devices *deviceSet, d device) {
|
|
|
|
if devices.currentDevice != nil && d == *devices.currentDevice {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if devices.currentDevice != nil {
|
|
|
|
(*devices.deviceMap)[*devices.currentDevice].backup()
|
|
|
|
}
|
|
|
|
devices.currentDevice = &d
|
|
|
|
(*devices.deviceMap)[d].restore()
|
|
|
|
}
|
|
|
|
|
|
|
|
func installHishtory(t testing.TB, tester shellTester, userSecret string) string {
|
|
|
|
out := tester.RunInteractiveShell(t, ` /tmp/client install `+userSecret)
|
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
|
matches := r.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
t.Fatalf("Failed to extract userSecret from output=%#v: matches=%#v", out, matches)
|
|
|
|
}
|
|
|
|
return matches[1]
|
|
|
|
}
|
2023-10-23 21:24:43 +02:00
|
|
|
|
|
|
|
func stripShellPrefix(out string) string {
|
|
|
|
if strings.Contains(out, "\n\n\n") {
|
|
|
|
return strings.TrimSpace(strings.Split(out, "\n\n\n")[1])
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
2023-11-12 09:40:24 +01:00
|
|
|
|
2023-11-12 09:55:53 +01:00
|
|
|
func stripRequiredPrefix(t *testing.T, out, prefix string) string {
|
|
|
|
require.Contains(t, out, prefix)
|
|
|
|
return strings.TrimSpace(strings.Split(out, prefix)[1])
|
|
|
|
|
|
|
|
}
|
2023-11-12 09:40:24 +01:00
|
|
|
func stripTuiCommandPrefix(t *testing.T, out string) string {
|
2023-11-12 09:55:53 +01:00
|
|
|
return stripRequiredPrefix(t, out, "hishtory tquery")
|
2023-11-12 09:40:24 +01:00
|
|
|
}
|