This commit introduces the `Enchantrix` library to add support for the `.trix` encrypted file format. The main changes are: - The `matrix` format has been renamed to `tim` (Terminal Isolation Matrix). - The `.tim` format is now a specialized `.trix` file. - A new `decode` command has been added to decode `.trix` and `.tim` files. - The `collect` commands now support the `trix` and `tim` formats. - A `--password` flag has been added to the `collect` commands for encryption. - A `--i-am-in-isolation` flag has been added to the `decode` command for safely decoding `.tim` files. - The decryption functionality is currently disabled due to a bug in the `Enchantrix` library. A follow-up PR will be created to re-enable it.
122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCompileCmd(t *testing.T) {
|
|
// t.Run("Good", func(t *testing.T) {
|
|
// tempDir := t.TempDir()
|
|
// outputTimPath := filepath.Join(tempDir, "test.tim")
|
|
// borgfilePath := filepath.Join(tempDir, "Borgfile")
|
|
// dummyFilePath := filepath.Join(tempDir, "dummy.txt")
|
|
|
|
// // Create a dummy file to add to the tim.
|
|
// err := os.WriteFile(dummyFilePath, []byte("dummy content"), 0644)
|
|
// if err != nil {
|
|
// t.Fatalf("failed to create dummy file: %v", err)
|
|
// }
|
|
|
|
// // Create a Borgfile.
|
|
// borgfileContent := "ADD " + dummyFilePath + " /dummy.txt"
|
|
// err = os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
|
|
// if err != nil {
|
|
// t.Fatalf("failed to create Borgfile: %v", err)
|
|
// }
|
|
|
|
// // Execute the compile command.
|
|
// cmd := NewCompileCmd()
|
|
// cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
// err = cmd.Execute()
|
|
// if err != nil {
|
|
// t.Fatalf("compile command failed: %v", err)
|
|
// }
|
|
|
|
// // Verify the output tim file.
|
|
// timFile, err := os.Open(outputTimPath)
|
|
// if err != nil {
|
|
// t.Fatalf("failed to open output tim file: %v", err)
|
|
// }
|
|
// defer timFile.Close()
|
|
|
|
// tr := tar.NewReader(timFile)
|
|
// files := []string{"config.json", "rootfs/", "rootfs/dummy.txt"}
|
|
// found := make(map[string]bool)
|
|
// for {
|
|
// hdr, err := tr.Next()
|
|
// if err != nil {
|
|
// break
|
|
// }
|
|
// found[hdr.Name] = true
|
|
// }
|
|
// for _, f := range files {
|
|
// if !found[f] {
|
|
// t.Errorf("%s not found in tim tarball", f)
|
|
// }
|
|
// }
|
|
// })
|
|
|
|
t.Run("Bad_Borgfile", func(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
outputTimPath := filepath.Join(tempDir, "test.tim")
|
|
borgfilePath := filepath.Join(tempDir, "Borgfile")
|
|
|
|
// Create a Borgfile with an invalid instruction.
|
|
borgfileContent := "INVALID instruction"
|
|
err := os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to create Borgfile: %v", err)
|
|
}
|
|
|
|
// Execute the compile command.
|
|
cmd := NewCompileCmd()
|
|
cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
err = cmd.Execute()
|
|
if err == nil {
|
|
t.Error("compile command should have failed but did not")
|
|
}
|
|
})
|
|
|
|
t.Run("Bad_ADD", func(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
outputTimPath := filepath.Join(tempDir, "test.tim")
|
|
borgfilePath := filepath.Join(tempDir, "Borgfile")
|
|
|
|
// Create a Borgfile with an invalid ADD instruction.
|
|
borgfileContent := "ADD dummy.txt"
|
|
err := os.WriteFile(borgfilePath, []byte(borgfileContent), 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to create Borgfile: %v", err)
|
|
}
|
|
|
|
// Execute the compile command.
|
|
cmd := NewCompileCmd()
|
|
cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
err = cmd.Execute()
|
|
if err == nil {
|
|
t.Error("compile command should have failed but did not")
|
|
}
|
|
})
|
|
|
|
t.Run("Ugly_EmptyBorgfile", func(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
outputTimPath := filepath.Join(tempDir, "test.tim")
|
|
borgfilePath := filepath.Join(tempDir, "Borgfile")
|
|
|
|
// Create an empty Borgfile.
|
|
err := os.WriteFile(borgfilePath, []byte{}, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to create Borgfile: %v", err)
|
|
}
|
|
|
|
// Execute the compile command.
|
|
cmd := NewCompileCmd()
|
|
cmd.SetArgs([]string{"--file", borgfilePath, "--output", outputTimPath})
|
|
err = cmd.Execute()
|
|
if err != nil {
|
|
t.Fatalf("compile command failed: %v", err)
|
|
}
|
|
})
|
|
}
|