2022-04-05 07:07:01 +02:00
|
|
|
package lib
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-01-10 01:39:13 +01:00
|
|
|
"time"
|
2022-04-05 07:07:01 +02:00
|
|
|
|
|
|
|
"github.com/ddworken/hishtory/shared"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
2022-04-05 07:07:01 +02:00
|
|
|
defer shared.BackupAndRestore(t)()
|
2022-04-06 08:31:24 +02:00
|
|
|
defer shared.RunTestServer(t)()
|
2022-01-09 06:59:28 +01:00
|
|
|
homedir, err := os.UserHomeDir()
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, err)
|
|
|
|
if _, err := os.Stat(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH)); err == nil {
|
2022-01-09 06:59:28 +01:00
|
|
|
t.Fatalf("hishtory secret file already exists!")
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, Setup([]string{}))
|
|
|
|
if _, err := os.Stat(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH)); err != nil {
|
2022-01-09 06:59:28 +01:00
|
|
|
t.Fatalf("hishtory secret file does not exist after Setup()!")
|
|
|
|
}
|
2022-04-05 07:07:01 +02:00
|
|
|
data, err := os.ReadFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH))
|
|
|
|
shared.Check(t, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
if len(data) < 10 {
|
|
|
|
t.Fatalf("hishtory secret has unexpected length: %d", len(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildHistoryEntry(t *testing.T) {
|
2022-04-05 07:07:01 +02:00
|
|
|
defer shared.BackupAndRestore(t)()
|
2022-04-06 08:31:24 +02:00
|
|
|
defer shared.RunTestServer(t)()
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, Setup([]string{}))
|
2022-01-10 01:39:13 +01:00
|
|
|
entry, err := BuildHistoryEntry([]string{"unused", "saveHistoryEntry", "120", " 123 ls / ", "1641774958326745663"})
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
if entry.ExitCode != 120 {
|
|
|
|
t.Fatalf("history entry has unexpected exit code: %v", entry.ExitCode)
|
|
|
|
}
|
|
|
|
if entry.LocalUsername != "david" {
|
|
|
|
t.Fatalf("history entry has unexpected user name: %v", entry.LocalUsername)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
|
|
|
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
|
|
|
}
|
|
|
|
if entry.Command != "ls /" {
|
|
|
|
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
|
|
|
}
|
2022-01-10 01:39:13 +01:00
|
|
|
if entry.StartTime.Format(time.RFC3339) != "2022-01-09T16:35:58-08:00" {
|
|
|
|
t.Fatalf("history entry has incorrect start time: %v", entry.StartTime.Format(time.RFC3339))
|
|
|
|
}
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUserSecret(t *testing.T) {
|
2022-04-05 07:07:01 +02:00
|
|
|
defer shared.BackupAndRestore(t)()
|
2022-04-06 08:31:24 +02:00
|
|
|
defer shared.RunTestServer(t)()
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, Setup([]string{}))
|
2022-01-09 06:59:28 +01:00
|
|
|
secret1, err := GetUserSecret()
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
if len(secret1) < 10 || strings.Contains(secret1, " ") || strings.Contains(secret1, "\n") {
|
|
|
|
t.Fatalf("unexpected secret: %v", secret1)
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, Setup([]string{}))
|
2022-01-09 06:59:28 +01:00
|
|
|
secret2, err := GetUserSecret()
|
2022-04-05 07:07:01 +02:00
|
|
|
shared.Check(t, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
if secret1 == secret2 {
|
|
|
|
t.Fatalf("GetUserSecret() returned the same values for different setups! val=%v", secret1)
|
|
|
|
}
|
|
|
|
}
|