2022-01-09 20:00:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-04-07 07:44:10 +02:00
|
|
|
"fmt"
|
2022-01-09 23:34:59 +01:00
|
|
|
"io/ioutil"
|
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"
|
|
|
|
"strings"
|
2022-01-09 20:00:53 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ddworken/hishtory/shared"
|
|
|
|
)
|
|
|
|
|
2022-01-09 23:34:59 +01:00
|
|
|
func RunInteractiveBashCommands(t *testing.T, script string) string {
|
2022-04-07 03:18:46 +02:00
|
|
|
shared.Check(t, ioutil.WriteFile("/tmp/hishtory-test-in.sh", []byte("set -euo pipefail\n"+script), 0600))
|
2022-01-09 23:34:59 +01:00
|
|
|
cmd := exec.Command("bash", "-i")
|
|
|
|
cmd.Stdin = strings.NewReader(script)
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
var err bytes.Buffer
|
|
|
|
cmd.Stderr = &err
|
|
|
|
shared.CheckWithInfo(t, cmd.Run(), out.String()+err.String())
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
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-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) {
|
|
|
|
// 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-01-09 23:34:59 +01:00
|
|
|
out := RunInteractiveBashCommands(t, `
|
|
|
|
gvm use go1.17
|
2022-04-06 08:31:24 +02:00
|
|
|
cd /home/david/code/hishtory/
|
|
|
|
go build -o /tmp/client client/client.go
|
2022-04-07 07:44:10 +02:00
|
|
|
/tmp/client install `+userSecret)
|
2022-01-09 23:34:59 +01:00
|
|
|
match, err := regexp.MatchString(`Setting secret hishtory key to .*`, out)
|
|
|
|
shared.Check(t, err)
|
|
|
|
if !match {
|
2022-01-10 00:48:20 +01:00
|
|
|
t.Fatalf("unexpected output from install: %v", out)
|
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-06 08:31:24 +02: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)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
out = RunInteractiveBashCommands(t, `
|
|
|
|
gvm use go1.17
|
|
|
|
cd /home/david/code/hishtory/
|
|
|
|
go build -o /tmp/client client/client.go
|
|
|
|
/tmp/client install`)
|
|
|
|
match, err = regexp.MatchString(`Setting secret hishtory key to .*`, out)
|
|
|
|
shared.Check(t, err)
|
|
|
|
if !match {
|
|
|
|
t.Fatalf("unexpected output from install: %v", out)
|
|
|
|
}
|
|
|
|
|
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:44:10 +02:00
|
|
|
// Manually submit an event that is tied to the second user, and then we'll check if we see it for the third user
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testIntegration(t *testing.T) string {
|
|
|
|
// Test install
|
|
|
|
out := RunInteractiveBashCommands(t, `
|
|
|
|
gvm use go1.17
|
|
|
|
cd /home/david/code/hishtory
|
|
|
|
go build -o /tmp/client client/client.go
|
|
|
|
/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]
|
|
|
|
|
2022-04-07 07:43:07 +02:00
|
|
|
// Test the status subcommand
|
2022-04-07 02:47:21 +02:00
|
|
|
out = RunInteractiveBashCommands(t, `
|
|
|
|
hishtory status
|
|
|
|
`)
|
2022-04-07 07:43:07 +02:00
|
|
|
if out != fmt.Sprintf("Hishtory: e2e sync\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-01-10 00:48:20 +01:00
|
|
|
out = RunInteractiveBashCommands(t, `
|
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
|
|
|
|
`)
|
|
|
|
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-03-30 06:56:28 +02:00
|
|
|
|
|
|
|
// TODO(ddworken): Test export
|