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-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-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-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-07 08:05:30 +02:00
|
|
|
if strings.Contains(outStr, "hishtory fatal error:") {
|
|
|
|
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) {
|
2022-04-09 21:31:31 +02:00
|
|
|
if os.Getenv("GITHUB_ACTIONS") != "" {
|
2022-04-09 21:27:24 +02:00
|
|
|
// TODO: debug why these tests fail on github actions, the error message is:
|
|
|
|
// `bash: cannot set terminal process group (683): Inappropriate ioctl for device\nbash: no job control in this shell`
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:00:53 +01:00
|
|
|
// Set up
|
2022-03-30 06:56:28 +02:00
|
|
|
defer shared.BackupAndRestore(t)()
|
2022-04-06 08:31:24 +02:00
|
|
|
defer shared.RunTestServer(t)()
|
2022-03-30 06:56:28 +02:00
|
|
|
|
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) {
|
2022-04-09 21:31:31 +02:00
|
|
|
if os.Getenv("GITHUB_ACTIONS") != "" {
|
2022-04-09 21:27:24 +02:00
|
|
|
// TODO: debug why these tests fail on github actions, the error message is:
|
|
|
|
// `bash: cannot set terminal process group (683): Inappropriate ioctl for device\nbash: no job control in this shell`
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.RunTestServer(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-08 07:53:39 +02:00
|
|
|
out := RunInteractiveBashCommands(t, "hishtory query")
|
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")
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory query")
|
|
|
|
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`)
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory query")
|
|
|
|
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-07 02:47:21 +02:00
|
|
|
out = RunInteractiveBashCommands(t, "hishtory query")
|
|
|
|
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")
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory query")
|
|
|
|
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-07 07:49:45 +02:00
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query`)
|
|
|
|
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-08 08:25:13 +02:00
|
|
|
if out != fmt.Sprintf(
|
2022-04-09 21:19:01 +02:00
|
|
|
"/tmp/client install\nset -emo pipefail\nset -emo pipefail\nhishtory status\nset -emo pipefail\nhishtory query\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\n/tmp/client install %s\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", userSecret, userSecret) {
|
2022-04-07 08:05:30 +02:00
|
|
|
t.Fatalf("hishtory export had unexpected output! out=%#v", out)
|
|
|
|
}
|
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-06 08:31:24 +02:00
|
|
|
out := RunInteractiveBashCommands(t, `
|
2022-04-09 06:17:11 +02:00
|
|
|
go build -o /tmp/client
|
2022-04-08 07:53:39 +02:00
|
|
|
/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-08 07:53:39 +02:00
|
|
|
out := RunInteractiveBashCommands(t, `
|
2022-04-07 02:47:21 +02:00
|
|
|
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-07 07:44:10 +02:00
|
|
|
// Test the banner
|
2022-04-07 07:43:07 +02:00
|
|
|
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query`)
|
|
|
|
if !strings.Contains(out, "HELLO_FROM_SERVER") {
|
|
|
|
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-08 08:25:13 +02:00
|
|
|
out, err := RunInteractiveBashCommandsWithoutStrictMode(t, `
|
2022-04-09 21:19:01 +02:00
|
|
|
set -m
|
2022-01-09 23:34:59 +01:00
|
|
|
ls /a
|
|
|
|
ls /bar
|
|
|
|
ls /foo
|
|
|
|
echo foo
|
|
|
|
echo bar
|
2022-01-10 00:48:20 +01:00
|
|
|
hishtory disable
|
2022-01-09 23:34:59 +01:00
|
|
|
echo thisisnotrecorded
|
2022-01-10 00:48:20 +01:00
|
|
|
hishtory enable
|
2022-01-09 23:34:59 +01:00
|
|
|
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-01-10 00:48:20 +01:00
|
|
|
out = RunInteractiveBashCommands(t, "hishtory query")
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, "hishtory query foo")
|
|
|
|
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
|
|
|
|
|
|
|
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)()
|
|
|
|
defer shared.RunTestServer(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-08 08:25:13 +02:00
|
|
|
_, _ = RunInteractiveBashCommandsWithoutStrictMode(t, `
|
2022-04-09 21:19:01 +02:00
|
|
|
set -m
|
2022-04-08 07:53:39 +02:00
|
|
|
echo nevershouldappear
|
2022-04-08 08:25:13 +02:00
|
|
|
notacommand
|
2022-04-08 07:53:39 +02:00
|
|
|
cd /tmp/
|
|
|
|
echo querybydir
|
2022-04-12 07:36:52 +02:00
|
|
|
hishtory disable
|
2022-04-08 07:53:39 +02:00
|
|
|
`)
|
|
|
|
|
|
|
|
// Query based on cwd
|
|
|
|
out := RunInteractiveBashCommands(t, `hishtory query cwd:/tmp`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query cwd:tmp`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query cwd:/tmp querybydir`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query exit_code:127`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query exit_code:127 foo`)
|
|
|
|
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:
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query before:2025-07-02 cwd:/tmp`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query before:2025-07-02 cwd:tmp`)
|
|
|
|
if strings.Count(out, "\n") != 3 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query before:2025-07-02 cwd:mp`)
|
|
|
|
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:
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query after:2020-07-02 cwd:/tmp`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query after:2120-07-02 cwd:/tmp`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query user:otheruser`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query user:noexist`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query hostname:otherhostname`)
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query`)
|
|
|
|
if !strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query doesn't contain expected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query -cmd_with_diff_hostname_and_username`)
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query -echo`)
|
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 7 {
|
|
|
|
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test filtering out with an atom
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query -hostname:otherhostname`)
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query -user:otheruser`)
|
|
|
|
if strings.Contains(out, "cmd_with_diff_hostname_and_username") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query -exit_code:0`)
|
|
|
|
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)
|
|
|
|
out = RunInteractiveBashCommands(t, `hishtory query -echo`)
|
|
|
|
if strings.Contains(out, "echo") {
|
|
|
|
t.Fatalf("hishtory query contains unexpected result, out=%#v", out)
|
|
|
|
}
|
|
|
|
if strings.Count(out, "\n") != 7 {
|
|
|
|
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) {
|
2022-04-09 21:53:34 +02:00
|
|
|
if os.Getenv("GITHUB_ACTIONS") != "" {
|
|
|
|
// TODO: debug why these tests fail on github actions, the error message is:
|
|
|
|
// `bash: cannot set terminal process group (683): Inappropriate ioctl for device\nbash: no job control in this shell`
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
2022-04-09 21:50:01 +02:00
|
|
|
// Set up
|
|
|
|
defer shared.BackupAndRestore(t)()
|
|
|
|
defer shared.RunTestServer(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-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
|
|
|
}
|
|
|
|
}
|