58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package isphere
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadEncryptedPacketLogSourceFromFileReadsNonEmptyLines(t *testing.T) {
|
|
path := writePacketLogFileForTest(t, "\n encrypted-line-1 \r\n\r\n\tencrypted-line-2\n")
|
|
|
|
got, err := LoadEncryptedPacketLogSourceFromFile(path)
|
|
if err != nil {
|
|
t.Fatalf("LoadEncryptedPacketLogSourceFromFile returned error: %v", err)
|
|
}
|
|
want := EncryptedPacketLogSource{Lines: []string{"encrypted-line-1", "encrypted-line-2"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("source = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestLoadEncryptedPacketLogSourceFromDirectoryReadsSortedLogFiles(t *testing.T) {
|
|
root := t.TempDir()
|
|
writePacketLogFileAtPathForTest(t, root+"\\b.log", "\n line-b1 \n")
|
|
writePacketLogFileAtPathForTest(t, root+"\\a.log", "line-a1\n\nline-a2\n")
|
|
writePacketLogFileAtPathForTest(t, root+"\\nested\\c.txt", "line-c1\n")
|
|
writePacketLogFileAtPathForTest(t, root+"\\ignored.tmp", "ignored\n")
|
|
|
|
got, err := LoadEncryptedPacketLogSourceFromDirectory(root)
|
|
if err != nil {
|
|
t.Fatalf("LoadEncryptedPacketLogSourceFromDirectory returned error: %v", err)
|
|
}
|
|
want := EncryptedPacketLogSource{Lines: []string{"line-a1", "line-a2", "line-b1", "line-c1"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("source = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func writePacketLogFileForTest(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := t.TempDir() + "\\packet.log"
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write test packet log: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func writePacketLogFileAtPathForTest(t *testing.T, path string, content string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatalf("create parent for %s: %v", path, err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write test packet log %s: %v", path, err)
|
|
}
|
|
}
|