package tests import ( "concord.hectabit.org/Hectabit/burgerbackup/lib/common" "golang.org/x/crypto/argon2" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) func TestAES(t *testing.T) { t.Logf("[INFO] Running test: encryption and decryption\n") cryptoKeyHashed := argon2.IDKey([]byte("supersecretkey"), []byte("burgerbackup"), 1, 64*1024, 4, 32) encryptedContent, err := common.EncryptAES(cryptoKeyHashed, []byte("This is a test file for burgerbackup")) if err != nil { t.Fatalf("[FATAL] Error encrypting content: " + err.Error() + "\n") } decryptedContent, err := common.DecryptAES(cryptoKeyHashed, encryptedContent) if err != nil { t.Fatalf("[FATAL] Error decrypting content: " + err.Error() + "\n") } if string(decryptedContent) != "This is a test file for burgerbackup" { t.Logf(string(decryptedContent) + "\n") t.Fatalf("[FATAL] Decrypted content does not match original content\n") } t.Logf("[INFO] Test encryption and decryption successful: " + string(decryptedContent) + "\n") } func TestBld(t *testing.T) { t.Logf("[INFO] Running test: package build\n") goList := exec.Command("go", "list", "-m", "-f", "\"{{.Dir}}\"") directoryBytes, err := goList.Output() if err != nil { t.Fatalf("[FATAL] Error running go list: " + err.Error() + "\n") } directory := strings.TrimSuffix(strings.TrimPrefix(string(directoryBytes), "\""), "\"\n") t.Logf("[INFO] Located go.mod at: " + directory + "\n") err = os.RemoveAll("/tmp/burgerbackup") if err != nil { t.Fatalf("[FATAL] Error removing old build directory: " + err.Error() + "\n") } output, err := exec.Command("make", "-C", directory, "BUILDDIR=/tmp/burgerbackup").Output() if err != nil { t.Logf("[FATAL] Error building the package: " + err.Error() + "\n") t.Fatalf("[INFO] Make output: " + string(output) + "\n") } t.Logf("[INFO] Test package build successful\n") } func TestBbk(t *testing.T) { var processes []*exec.Cmd done := make(chan bool) t.Logf("[INFO] Starting requisites for test: server and client backup\n") serverIni := "[config]\nPORT = 8080\nHOST = 127.0.0.1\nBACKUP_FOLDER = /tmp/burgerbackup/\nBACKUP_KEY = supersecretkey\nCRYPTO_KEY = supersecretkey" err := os.WriteFile("/tmp/burgerbackup/server.ini", []byte(serverIni), 0644) if err != nil { t.Fatalf("[FATAL] Error writing server.ini: " + err.Error() + "\n") } clientIni := "[config]\nFILE_LOCATION = /tmp/burgerbackup/testfile.txt\nREMOTE_URL = http://127.0.0.1:8080/api/backup\nBACKUP_KEY = supersecretkey\nCRYPTO_KEY = supersecretkey\nBACKUP_INTERVAL = 1" err = os.WriteFile("/tmp/burgerbackup/client.ini", []byte(clientIni), 0644) if err != nil { t.Fatalf("[FATAL] Error writing client.ini: " + err.Error() + "\n") } testFile := "This is a test file for burgerbackup" err = os.WriteFile("/tmp/burgerbackup/testfile.txt", []byte(testFile), 0644) if err != nil { t.Fatalf("[FATAL] Error writing testfile: " + err.Error() + "\n") } t.Logf("[INFO] Requisites for test: server and client backup have finished\n") t.Logf("[INFO] Running test: server and client backup\n") go func() { cmd := exec.Command("/tmp/burgerbackup/bin/server", "/tmp/burgerbackup/server.ini") processes = append(processes, cmd) err := cmd.Start() if err != nil { t.Fatalf("[FATAL] Error running server: " + err.Error() + "\n") } }() go func() { cmd := exec.Command("/tmp/burgerbackup/bin/client", "/tmp/burgerbackup/server.ini") processes = append(processes, cmd) err := cmd.Start() if err != nil { t.Fatalf("[FATAL] Error running client: " + err.Error() + "\n") } }() go func() { time.Sleep(2 * 1000000000) for _, process := range processes { err := process.Process.Kill() if err != nil { t.Fatalf("[FATAL] Error killing process: " + err.Error() + "\n") } } t.Logf("[INFO] Server and client killed\n") matches, err := filepath.Glob("/tmp/burgerbackup/testfile_*.txt") if err != nil { t.Fatalf("[FATAL] Error globbing testfiles: " + err.Error() + "\n") } if len(matches) > 0 { matchesString := "" for _, match := range matches { matchesString += match + "," } t.Logf("[INFO] Testfiles found: " + matchesString + "\n") t.Logf("[INFO] Test server and client backup successful\n") done <- true } else { t.Fatalf("[FATAL] No testfiles found" + "\n") } }() finished := <-done if finished { return } else { t.Fatalf("[FATAL] Bit flip detected, please do not expose to cosmic radiation (impossible condition detected)\n") } } func Cleanup(t *testing.T) { t.Logf("[INFO] Tests completed successfully, cleaning up...\n") err := os.RemoveAll("/tmp/burgerbackup") if err != nil { t.Fatalf("[FATAL] Error cleaning up: " + err.Error() + "\n") } t.Logf("[INFO] Cleanup successful. Exiting...\n") }