2022-01-09 20:00:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-04-07 08:17:58 +02:00
|
|
|
"encoding/json"
|
2022-04-07 07:44:10 +02:00
|
|
|
"fmt"
|
2022-04-07 08:17:58 +02:00
|
|
|
"net/http"
|
2022-04-07 07:43:07 +02:00
|
|
|
"os"
|
2022-04-07 07:44:10 +02:00
|
|
|
"os/exec"
|
2022-04-16 09:56:08 +02:00
|
|
|
"path"
|
2022-01-09 23:34:59 +01:00
|
|
|
"regexp"
|
2022-04-12 07:36:52 +02:00
|
|
|
"runtime"
|
2022-01-09 23:34:59 +01:00
|
|
|
"strings"
|
2022-01-09 20:00:53 +01:00
|
|
|
"testing"
|
2022-04-15 05:18:49 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-01-09 20:00:53 +01:00
|
|
|
|
2022-04-08 05:59:40 +02:00
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2022-01-09 20:00:53 +01:00
|
|
|
"github.com/ddworken/hishtory/shared"
|
|
|
|
)
|
|
|
|
|
2022-04-15 20:20:23 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
defer shared.RunTestServer()()
|
2022-04-16 03:12:26 +02:00
|
|
|
cmd := exec.Command("go", "build", "-o", "/tmp/client")
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to build client: %v", err))
|
|
|
|
}
|
2022-04-15 20:20:23 +02:00
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
|
2022-01-09 23:34:59 +01:00
|
|
|
func RunInteractiveBashCommands(t *testing.T, script string) string {
|
2022-04-09 21:04:13 +02:00
|
|
|
out, err := RunInteractiveBashCommandsWithoutStrictMode(t, "set -emo pipefail\n"+script)
|
2022-04-08 08:25:13 +02:00
|
|
|
if err != nil {
|
2022-04-12 07:36:52 +02:00
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("error when running command at %s:%d: %v", filename, line, err)
|
2022-04-08 08:25:13 +02:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunInteractiveBashCommandsWithoutStrictMode(t *testing.T, script string) (string, error) {
|
2022-01-09 23:34:59 +01:00
|
|
|
cmd := exec.Command("bash", "-i")
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
2022-04-08 08:25:13 +02:00
|
|
|
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: %v", stdout.String(), stderr.String(), err)
|
|
|
|
}
|
|
|
|
outStr := stdout.String()
|
2022-04-15 05:18:49 +02:00
|
|
|
if strings.Contains(outStr, "hishtory fatal error") {
|
2022-04-07 08:05:30 +02:00
|
|
|
t.Fatalf("Ran command, but hishtory had a fatal error! out=%#v", outStr)
|
|
|
|
}
|
2022-04-08 08:25:13 +02:00
|
|
|
return outStr, nil
|
2022-01-09 23:34:59 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 20:00:53 +01:00
|
|
|
func TestIntegration(t *testing.T) {
|
|
|
|
// Set up
|
2022-03-30 06:56:28 +02:00
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
// Run the test
|
|
|
|
testIntegration(t)
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
2022-01-09 20:00:53 +01:00
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
func TestIntegrationWithNewDevice(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
// Run the test
|
|
|
|
userSecret := testIntegration(t)
|
2022-04-06 08:31:24 +02:00
|
|
|
|
|
|
|
// Clear all local state
|
|
|
|
shared.ResetLocalState(t)
|
|
|
|
|
|
|
|
// Install it again
|
2022-04-08 07:53:39 +02:00
|
|
|
installHishtory(t, userSecret)
|
2022-01-09 23:34:59 +01:00
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
// Querying should show the history from the previous run
|
2022-04-15 05:18:49 +02:00
|
|
|
out := hishtoryQuery(t, "")
|
2022-04-06 08:31:24 +02:00
|
|
|
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
t.Fatalf("output is missing expected item %#v: %#v", item, out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, item) != 1 {
|
|
|
|
t.Fatalf("output has %#v in it multiple times! out=%#v", item, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, "echo mynewcommand")
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-06 08:31:24 +02:00
|
|
|
if !strings.Contains(out, "echo mynewcommand") {
|
|
|
|
t.Fatalf("output is missing `echo mynewcommand`")
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo mynewcommand") != 1 {
|
|
|
|
t.Fatalf("output has `echo mynewcommand` the wrong number of times")
|
|
|
|
}
|
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
// Clear local state again
|
2022-04-07 02:47:21 +02:00
|
|
|
shared.ResetLocalState(t)
|
|
|
|
|
|
|
|
// Install it a 3rd time
|
2022-04-08 07:53:39 +02:00
|
|
|
installHishtory(t, "adifferentsecret")
|
2022-04-07 02:47:21 +02:00
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
// Run a command that shouldn't be in the hishtory later on
|
|
|
|
RunInteractiveBashCommands(t, `echo notinthehistory`)
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-07 07:43:07 +02:00
|
|
|
if !strings.Contains(out, "echo notinthehistory") {
|
|
|
|
t.Fatalf("output is missing `echo notinthehistory`")
|
|
|
|
}
|
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
// Set the secret key to the previous secret key
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory init `+userSecret)
|
|
|
|
if !strings.Contains(out, "Setting secret hishtory key to "+userSecret) {
|
2022-04-07 02:47:21 +02:00
|
|
|
t.Fatalf("Failed to re-init with the user secret: %v", out)
|
|
|
|
}
|
|
|
|
|
2022-04-07 03:18:46 +02:00
|
|
|
// Querying should show the history from the previous run
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-07 02:47:21 +02:00
|
|
|
expected = []string{"echo thisisrecorded", "echo mynewcommand", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
t.Fatalf("output is missing expected item %#v: %#v", item, out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, item) != 1 {
|
|
|
|
t.Fatalf("output has %#v in it multiple times! out=%#v", item, out)
|
|
|
|
}
|
|
|
|
}
|
2022-04-07 07:43:07 +02:00
|
|
|
// But not from the previous account
|
|
|
|
if strings.Contains(out, "notinthehistory") {
|
|
|
|
t.Fatalf("output contains the unexpected item: notinthehistory")
|
|
|
|
}
|
2022-04-07 02:47:21 +02:00
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, "echo mynewercommand")
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-07 02:47:21 +02:00
|
|
|
if !strings.Contains(out, "echo mynewercommand") {
|
|
|
|
t.Fatalf("output is missing `echo mynewercommand`")
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo mynewercommand") != 1 {
|
|
|
|
t.Fatalf("output has `echo mynewercommand` the wrong number of times")
|
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
|
2022-04-07 07:49:45 +02:00
|
|
|
// Manually submit an event that isn't in the local DB, and then we'll
|
2022-04-07 08:17:58 +02:00
|
|
|
// check if we see it when we do a query without ever having done an init
|
2022-04-08 05:59:40 +02:00
|
|
|
newEntry := data.MakeFakeHistoryEntry("othercomputer")
|
2022-04-11 02:38:20 +02:00
|
|
|
manuallySubmitHistoryEntry(t, userSecret, newEntry)
|
|
|
|
|
2022-04-07 08:17:58 +02:00
|
|
|
// Now check if that is in there when we do hishtory query
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-07 07:49:45 +02:00
|
|
|
if !strings.Contains(out, "othercomputer") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain cmd run on another machine! out=%#v", out)
|
|
|
|
}
|
2022-04-07 08:05:30 +02:00
|
|
|
|
|
|
|
// Finally, test the export command
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory export`)
|
2022-04-15 05:18:49 +02:00
|
|
|
if strings.Contains(out, "thisisnotrecorded") {
|
|
|
|
t.Fatalf("hishtory export contains a command that should not have been recorded, out=%#v", out)
|
|
|
|
}
|
2022-04-16 07:54:38 +02:00
|
|
|
expectedOutputWithoutKey := "set -emo pipefail\nhishtory status\nset -emo pipefail\nhishtory query\nset -m\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nset -emo pipefail\nhishtory query\nset -emo pipefail\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nset -emo pipefail\nhishtory query complex\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mynewcommand\nset -emo pipefail\nhishtory query\nhishtory init %s\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mynewercommand\nset -emo pipefail\nhishtory query\nothercomputer\nset -emo pipefail\nhishtory query\nset -emo pipefail\n"
|
2022-04-15 05:18:49 +02:00
|
|
|
expectedOutput := fmt.Sprintf(expectedOutputWithoutKey, userSecret)
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
2022-04-16 07:54:38 +02:00
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
2022-04-07 08:05:30 +02:00
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 07:53:39 +02:00
|
|
|
func installHishtory(t *testing.T, userSecret string) string {
|
2022-04-16 03:12:26 +02:00
|
|
|
out := RunInteractiveBashCommands(t, `/tmp/client install `+userSecret)
|
2022-04-06 08:31:24 +02:00
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
|
matches := r.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
t.Fatalf("Failed to extract userSecret from output: matches=%#v", matches)
|
|
|
|
}
|
2022-04-08 07:53:39 +02:00
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIntegration(t *testing.T) string {
|
|
|
|
// Test install
|
|
|
|
userSecret := installHishtory(t, "")
|
2022-04-06 08:31:24 +02:00
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
// Test the status subcommand
|
2022-04-14 06:48:52 +02:00
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
2022-04-09 23:37:21 +02:00
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
2022-04-07 02:47:21 +02:00
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
|
2022-04-16 09:56:08 +02:00
|
|
|
// Assert that hishtory is correctly using the dev config.sh
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get homedir: %v", err)
|
|
|
|
}
|
|
|
|
dat, err := os.ReadFile(path.Join(homedir, shared.HISHTORY_PATH, "config.sh"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read config.sh: %v", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(dat), "except it doesn't run the save process in the background") {
|
|
|
|
t.Fatalf("config.sh is the prod version when it shouldn't be, config.sh=%#v", dat)
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:44:10 +02:00
|
|
|
// Test the banner
|
2022-04-07 07:43:07 +02:00
|
|
|
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-16 09:44:47 +02:00
|
|
|
if !strings.Contains(out, "HELLO_FROM_SERVER\nHostname") {
|
2022-04-07 07:43:07 +02:00
|
|
|
t.Fatalf("hishtory query didn't show the banner message! out=%#v", out)
|
|
|
|
}
|
|
|
|
os.Setenv("FORCED_BANNER", "")
|
|
|
|
|
2022-01-09 23:34:59 +01:00
|
|
|
// Test recording commands
|
2022-04-16 09:56:08 +02:00
|
|
|
out, err = RunInteractiveBashCommandsWithoutStrictMode(t, `set -m
|
2022-04-14 06:48:52 +02:00
|
|
|
ls /a
|
|
|
|
ls /bar
|
|
|
|
ls /foo
|
|
|
|
echo foo
|
|
|
|
echo bar
|
|
|
|
hishtory disable
|
|
|
|
echo thisisnotrecorded
|
2022-04-15 05:18:49 +02:00
|
|
|
sleep 0.5
|
2022-04-14 06:48:52 +02:00
|
|
|
hishtory enable
|
|
|
|
echo thisisrecorded`)
|
2022-04-08 08:25:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-09 23:34:59 +01:00
|
|
|
if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\n" {
|
|
|
|
t.Fatalf("unexpected output from running commands: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test querying for all commands
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-01-10 00:48:20 +01:00
|
|
|
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
t.Fatalf("output is missing expected item %#v: %#v", item, out)
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 03:18:09 +01:00
|
|
|
// match, err = regexp.MatchString(`.*~/.*\s+[a-zA-Z]{3} \d+ 2022 \d\d:\d\d:\d\d PST\s+\d{1,2}ms\s+0\s+echo thisisrecorded.*`, out)
|
|
|
|
// shared.Check(t, err)
|
|
|
|
// if !match {
|
|
|
|
// t.Fatalf("output is missing the row for `echo thisisrecorded`: %v", out)
|
|
|
|
// }
|
2022-01-10 00:48:20 +01:00
|
|
|
|
|
|
|
// Test querying for a specific command
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "foo")
|
2022-01-10 00:48:20 +01:00
|
|
|
expected = []string{"echo foo", "ls /foo"}
|
|
|
|
unexpected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "ls /bar", "ls /a"}
|
2022-01-09 23:34:59 +01:00
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
t.Fatalf("output is missing expected item %#v: %#v", item, out)
|
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
if strings.Count(out, item) != 1 {
|
|
|
|
t.Fatalf("output has %#v in it multiple times! out=%#v", item, out)
|
|
|
|
}
|
2022-01-09 20:00:53 +01:00
|
|
|
}
|
2022-01-10 00:48:20 +01:00
|
|
|
for _, item := range unexpected {
|
|
|
|
if strings.Contains(out, item) {
|
|
|
|
t.Fatalf("output is containing unexpected item %#v: %#v", item, out)
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
|
2022-04-16 03:24:58 +02:00
|
|
|
// Add a complex command
|
2022-04-16 07:54:38 +02:00
|
|
|
complexCommand := "echo hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\""
|
2022-04-16 03:24:58 +02:00
|
|
|
_, _ = RunInteractiveBashCommandsWithoutStrictMode(t, complexCommand)
|
|
|
|
|
|
|
|
// Query for it
|
|
|
|
out = hishtoryQuery(t, "complex")
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, complexCommand) {
|
|
|
|
t.Fatalf("hishtory query doesn't contain the expected complex command, out=%#v", out)
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
return userSecret
|
2022-01-09 20:00:53 +01:00
|
|
|
}
|
2022-04-08 07:53:39 +02:00
|
|
|
|
|
|
|
func TestAdvancedQuery(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
// Install hishtory
|
2022-04-11 02:38:20 +02:00
|
|
|
userSecret := installHishtory(t, "")
|
2022-04-08 07:53:39 +02:00
|
|
|
|
|
|
|
// Run some commands we can query for
|
2022-04-15 05:18:49 +02:00
|
|
|
_, err := RunInteractiveBashCommandsWithoutStrictMode(t, `set -m
|
|
|
|
echo nevershouldappear
|
|
|
|
notacommand
|
|
|
|
cd /tmp/
|
|
|
|
echo querybydir
|
|
|
|
hishtory disable`)
|
2022-04-12 08:48:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-04-08 07:53:39 +02:00
|
|
|
|
2022-04-13 06:24:25 +02:00
|
|
|
// A super basic query just to ensure the basics are working
|
2022-04-15 05:18:49 +02:00
|
|
|
out := hishtoryQuery(t, `echo`)
|
2022-04-13 06:24:25 +02:00
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
2022-04-15 05:18:49 +02:00
|
|
|
t.Fatalf("hishtory query doesn't contain result matching echo querybydir, out=%#v", out)
|
2022-04-13 06:24:25 +02:00
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-08 07:53:39 +02:00
|
|
|
// Query based on cwd
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `cwd:/tmp`)
|
2022-04-08 07:53:39 +02:00
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "nevershouldappear") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected entry, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-09 03:23:17 +02:00
|
|
|
// Query based on cwd without the slash
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `cwd:tmp`)
|
2022-04-09 03:23:17 +02:00
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:tmp, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-08 07:53:39 +02:00
|
|
|
// Query based on cwd and another term
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `cwd:/tmp querybydir`)
|
2022-04-08 07:53:39 +02:00
|
|
|
if !strings.Contains(out, "echo querybydir") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "nevershouldappear") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected entry, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-08 08:25:13 +02:00
|
|
|
// Query based on exit_code
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `exit_code:127`)
|
2022-04-08 08:25:13 +02:00
|
|
|
if !strings.Contains(out, "notacommand") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-08 08:30:31 +02:00
|
|
|
// Query based on exit_code and something else that matches nothing
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `exit_code:127 foo`)
|
2022-04-08 08:30:31 +02:00
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-09 03:23:17 +02:00
|
|
|
// Query based on before: and cwd:
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `before:2025-07-02 cwd:/tmp`)
|
2022-04-09 03:23:17 +02:00
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `before:2025-07-02 cwd:tmp`)
|
2022-04-09 03:23:17 +02:00
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `before:2025-07-02 cwd:mp`)
|
2022-04-09 03:23:17 +02:00
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query based on after: and cwd:
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `after:2020-07-02 cwd:/tmp`)
|
2022-04-09 03:23:17 +02:00
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query based on after: that returns no results
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `after:2120-07-02 cwd:/tmp`)
|
2022-04-09 03:23:17 +02:00
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
2022-04-11 02:38:20 +02:00
|
|
|
// Manually submit an entry with a different hostname and username so we can test those atoms
|
|
|
|
entry := data.MakeFakeHistoryEntry("cmd_with_diff_hostname_and_username")
|
|
|
|
entry.LocalUsername = "otheruser"
|
|
|
|
entry.Hostname = "otherhostname"
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry)
|
|
|
|
|
|
|
|
// Query based on the username that exists
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `user:otheruser`)
|
2022-04-11 02:38:20 +02:00
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query based on the username that doesn't exist
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `user:noexist`)
|
2022-04-11 02:38:20 +02:00
|
|
|
if strings.Count(out, "\n") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query based on the hostname
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `hostname:otherhostname`)
|
2022-04-11 02:38:20 +02:00
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
2022-04-12 07:36:52 +02:00
|
|
|
|
|
|
|
// Test filtering out a search item
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
2022-04-12 07:36:52 +02:00
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `-cmd_with_diff_hostname_and_username`)
|
2022-04-12 07:36:52 +02:00
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `-echo`)
|
2022-04-12 07:36:52 +02:00
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
if strings.Count(out, "\n") != 5 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
2022-04-12 07:36:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test filtering out with an atom
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `-hostname:otherhostname`)
|
2022-04-12 07:36:52 +02:00
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `-user:otheruser`)
|
2022-04-12 07:36:52 +02:00
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `-exit_code:0`)
|
2022-04-12 07:36:52 +02:00
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test filtering out a search item that also looks like it could be a search for a flag
|
|
|
|
entry = data.MakeFakeHistoryEntry("foo -echo")
|
|
|
|
manuallySubmitHistoryEntry(t, userSecret, entry)
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, `-echo -install`)
|
2022-04-12 07:36:52 +02:00
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
2022-04-14 06:30:27 +02:00
|
|
|
if strings.Count(out, "\n") != 5 {
|
2022-04-12 07:36:52 +02:00
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
2022-04-08 07:53:39 +02:00
|
|
|
}
|
2022-04-09 08:47:13 +02:00
|
|
|
|
2022-04-09 21:50:01 +02:00
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
|
|
|
|
// Check the status command
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
2022-04-09 23:37:21 +02:00
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
2022-04-09 21:50:01 +02:00
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update
|
|
|
|
RunInteractiveBashCommands(t, `hishtory update`)
|
|
|
|
|
|
|
|
// Then check the status command again to confirm the update worked
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory status`)
|
2022-04-09 23:48:17 +02:00
|
|
|
if !strings.Contains(out, fmt.Sprintf("\nEnabled: true\nSecret Key: %s\nCommit Hash: ", userSecret)) {
|
2022-04-09 21:50:01 +02:00
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "\nCommit Hash: Unknown\n") {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 05:18:49 +02:00
|
|
|
func TestRepeatedCommandThenQuery(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
|
|
|
|
// Check the status command
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a command many times
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
RunInteractiveBashCommands(t, fmt.Sprintf("echo mycommand-%d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that it shows up correctly
|
|
|
|
out = hishtoryQuery(t, `mycommand`)
|
|
|
|
if strings.Count(out, "\n") != 26 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo mycommand") != 25 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, `echo mycommand-30
|
|
|
|
echo mycommand-31
|
|
|
|
echo mycommand-3`)
|
|
|
|
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory export")
|
|
|
|
expectedOutput := "set -emo pipefail\nhishtory status\nset -emo pipefail\necho mycommand-0\nset -emo pipefail\necho mycommand-1\nset -emo pipefail\necho mycommand-2\nset -emo pipefail\necho mycommand-3\nset -emo pipefail\necho mycommand-4\nset -emo pipefail\necho mycommand-5\nset -emo pipefail\necho mycommand-6\nset -emo pipefail\necho mycommand-7\nset -emo pipefail\necho mycommand-8\nset -emo pipefail\necho mycommand-9\nset -emo pipefail\necho mycommand-10\nset -emo pipefail\necho mycommand-11\nset -emo pipefail\necho mycommand-12\nset -emo pipefail\necho mycommand-13\nset -emo pipefail\necho mycommand-14\nset -emo pipefail\necho mycommand-15\nset -emo pipefail\necho mycommand-16\nset -emo pipefail\necho mycommand-17\nset -emo pipefail\necho mycommand-18\nset -emo pipefail\necho mycommand-19\nset -emo pipefail\necho mycommand-20\nset -emo pipefail\necho mycommand-21\nset -emo pipefail\necho mycommand-22\nset -emo pipefail\necho mycommand-23\nset -emo pipefail\necho mycommand-24\nset -emo pipefail\nhishtory query mycommand\nset -emo pipefail\necho mycommand-30\necho mycommand-31\necho mycommand-3\nset -emo pipefail\n"
|
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedCommandAndQuery(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
userSecret := installHishtory(t, "")
|
|
|
|
|
|
|
|
// Check the status command
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run a command many times
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
RunInteractiveBashCommands(t, fmt.Sprintf("echo mycommand-%d", i))
|
|
|
|
out = hishtoryQuery(t, fmt.Sprintf("mycommand-%d", i))
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of lines=%d, out=%#v", i, strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo mycommand") != 1 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of commands=%d, out=%#v", i, strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepeatedEnableDisable(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
installHishtory(t, "")
|
|
|
|
|
|
|
|
// Run a command many times
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
RunInteractiveBashCommands(t, fmt.Sprintf(`echo mycommand-%d
|
|
|
|
hishtory disable
|
|
|
|
echo shouldnotshowup
|
|
|
|
sleep 0.5
|
|
|
|
hishtory enable`, i))
|
|
|
|
out := hishtoryQuery(t, fmt.Sprintf("mycommand-%d", i))
|
|
|
|
if strings.Count(out, "\n") != 2 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of lines=%d, out=%#v", i, strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo mycommand") != 1 {
|
|
|
|
t.Fatalf("hishtory query #%d has the wrong number of commands=%d, out=%#v", i, strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
if strings.Contains(out, "shouldnotshowup") {
|
|
|
|
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out := RunInteractiveBashCommands(t, "hishtory export")
|
|
|
|
expectedOutput := "set -emo pipefail\necho mycommand-0\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-0\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-1\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-1\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-2\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-2\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-3\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-3\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-4\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-4\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-5\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-5\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-6\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-6\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-7\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-7\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-8\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-8\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-9\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-9\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-10\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-10\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-11\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-11\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-12\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-12\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-13\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-13\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-14\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-14\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-15\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-15\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-16\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-16\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-17\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-17\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-18\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-18\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-19\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-19\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-20\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-20\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-21\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-21\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-22\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-22\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-23\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-23\nset -emo pipefail\nhishtory query\nset -emo pipefail\necho mycommand-24\nhishtory enable\nset -emo pipefail\nhishtory query mycommand-24\nset -emo pipefail\nhishtory query\nset -emo pipefail\n"
|
2022-04-16 09:44:47 +02:00
|
|
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
|
|
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
2022-04-15 05:18:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:04:49 +02:00
|
|
|
func TestExcludeHiddenCommand(t *testing.T) {
|
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
installHishtory(t, "")
|
|
|
|
|
|
|
|
RunInteractiveBashCommands(t, `echo hello1
|
|
|
|
echo hidden
|
|
|
|
echo hello2
|
|
|
|
echo hidden`)
|
|
|
|
RunInteractiveBashCommands(t, " echo hidden")
|
|
|
|
out := hishtoryQuery(t, "")
|
2022-04-15 09:17:02 +02:00
|
|
|
if strings.Count(out, "\n") != 5 && strings.Count(out, "\n") != 6 {
|
2022-04-15 09:04:49 +02:00
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo hello") != 2 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo hello1") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "echo hello2") != 1 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of commands=%d, out=%#v", strings.Count(out, "echo mycommand"), out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "hidden") {
|
|
|
|
t.Fatalf("hishtory query contains a result that should not have been recorded, out=%#v", out)
|
|
|
|
}
|
|
|
|
|
2022-04-16 03:06:48 +02:00
|
|
|
out = RunInteractiveBashCommands(t, "hishtory export | grep -v pipefail")
|
|
|
|
expectedOutput := "echo hello1\necho hello2\nhishtory query\n"
|
2022-04-15 09:04:49 +02:00
|
|
|
if out != expectedOutput {
|
|
|
|
t.Fatalf("hishtory export has unexpected output=%#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 05:18:49 +02:00
|
|
|
func waitForBackgroundSavesToComplete(t *testing.T) {
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
cmd := exec.Command("pidof", "hishtory")
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil && err.Error() != "exit status 1" {
|
|
|
|
t.Fatalf("failed to check if hishtory was running: %v, stdout=%#v, stderr=%#v", err, stdout.String(), stderr.String())
|
|
|
|
}
|
|
|
|
if !strings.Contains(stdout.String(), "\n") {
|
|
|
|
// pidof had no output, so hishtory isn't running and we're done waitng
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Fatalf("failed to wait until hishtory wasn't running")
|
|
|
|
}
|
|
|
|
|
|
|
|
func hishtoryQuery(t *testing.T, query string) string {
|
|
|
|
return RunInteractiveBashCommands(t, "hishtory query "+query)
|
|
|
|
}
|
|
|
|
|
2022-04-11 02:38:20 +02:00
|
|
|
// TODO: Maybe a dedicated unit test for retrieveAdditionalEntriesFromRemote
|
2022-04-09 08:47:13 +02:00
|
|
|
|
2022-04-11 02:38:20 +02:00
|
|
|
func manuallySubmitHistoryEntry(t *testing.T, userSecret string, entry data.HistoryEntry) {
|
|
|
|
encEntry, err := data.EncryptHistoryEntry(userSecret, entry)
|
2022-04-09 08:47:13 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-11 02:38:20 +02:00
|
|
|
jsonValue, err := json.Marshal([]shared.EncHistoryEntry{encEntry})
|
2022-04-09 08:47:13 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-11 02:38:20 +02:00
|
|
|
resp, err := http.Post("http://localhost:8080/api/v1/esubmit", "application/json", bytes.NewBuffer(jsonValue))
|
2022-04-09 08:47:13 +02:00
|
|
|
shared.Check(t, err)
|
2022-04-11 02:38:20 +02:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatalf("failed to submit result to backend, status_code=%d", resp.StatusCode)
|
2022-04-09 08:47:13 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
|
|
|
|
func TestHishtoryBackgroundSaving(t *testing.T) {
|
|
|
|
// Setup
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
|
|
|
|
// Test install with an unset HISHTORY_TEST var so that we save in the background (this is likely to be flakey!)
|
|
|
|
out := RunInteractiveBashCommands(t, `unset HISHTORY_TEST
|
|
|
|
go build -o /tmp/client
|
|
|
|
/tmp/client install`)
|
|
|
|
r := regexp.MustCompile(`Setting secret hishtory key to (.*)`)
|
|
|
|
matches := r.FindStringSubmatch(out)
|
|
|
|
if len(matches) != 2 {
|
|
|
|
t.Fatalf("Failed to extract userSecret from output: matches=%#v", matches)
|
|
|
|
}
|
|
|
|
userSecret := matches[1]
|
|
|
|
|
|
|
|
// Assert that config.sh isn't the dev version
|
2022-04-16 09:56:08 +02:00
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get homedir: %v", err)
|
|
|
|
}
|
|
|
|
dat, err := os.ReadFile(path.Join(homedir, shared.HISHTORY_PATH, "config.sh"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read config.sh: %v", err)
|
|
|
|
}
|
|
|
|
if strings.Contains(string(dat), "except it doesn't run the save process in the background") {
|
|
|
|
t.Fatalf("config.sh is the testing version when it shouldn't be, config.sh=%#v", dat)
|
|
|
|
}
|
2022-04-15 05:18:49 +02:00
|
|
|
|
|
|
|
// Test the status subcommand
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory status`)
|
|
|
|
if out != fmt.Sprintf("Hishtory: v0.Unknown\nEnabled: true\nSecret Key: %s\nCommit Hash: Unknown\n", userSecret) {
|
|
|
|
t.Fatalf("status command has unexpected output: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test recording commands
|
2022-04-16 09:56:08 +02:00
|
|
|
out, err = RunInteractiveBashCommandsWithoutStrictMode(t, `set -m
|
2022-04-15 05:18:49 +02:00
|
|
|
ls /a
|
|
|
|
echo foo`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if out != "foo\n" {
|
|
|
|
t.Fatalf("unexpected output from running commands: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test querying for all commands
|
2022-04-15 20:20:23 +02:00
|
|
|
waitForBackgroundSavesToComplete(t)
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "")
|
|
|
|
expected := []string{"echo foo", "ls /a"}
|
|
|
|
for _, item := range expected {
|
|
|
|
if !strings.Contains(out, item) {
|
|
|
|
t.Fatalf("output is missing expected item %#v: %#v", item, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test querying for a specific command
|
2022-04-15 20:20:23 +02:00
|
|
|
waitForBackgroundSavesToComplete(t)
|
2022-04-15 05:18:49 +02:00
|
|
|
out = hishtoryQuery(t, "foo")
|
|
|
|
if !strings.Contains(out, "echo foo") {
|
|
|
|
t.Fatalf("output doesn't contain the expected item, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "ls /a") {
|
|
|
|
t.Fatalf("output contains unexpected item, out=%#v", out)
|
|
|
|
}
|
|
|
|
}
|