(Chore): Added unit tests detection and rar.go

This commit is contained in:
2025-11-22 08:28:33 +00:00
parent 9424c8aa50
commit e6f1382691
2 changed files with 173 additions and 0 deletions

View File

@@ -25,8 +25,10 @@ func TestDetectArchiveTypeByExtension(t *testing.T) {
{"targz_extension", "test.tar.gz", models.TARGZ},
{"tgz_extension", "test.tgz", models.TARGZ},
{"gz_extension", "test.gz", models.GZIP},
{"rar_extension", "test.rar", models.RAR},
{"uppercase_zip", "test.ZIP", models.ZIP},
{"uppercase_tar", "test.TAR", models.TAR},
{"uppercase_rar", "test.RAR", models.RAR},
}
for _, tc := range testCases {
@@ -222,6 +224,37 @@ func TestDetectGzipMagicBytes(t *testing.T) {
}
}
func TestDetectRarMagicBytes(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "zipprine-rar-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
// Create a file with RAR magic bytes (no .rar extension)
testFile := filepath.Join(tmpDir, "test.bin")
file, err := os.Create(testFile)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Write RAR magic bytes: "Rar!" (0x52 0x61 0x72 0x21)
rarMagic := []byte{0x52, 0x61, 0x72, 0x21, 0x1A, 0x07, 0x00}
if _, err := file.Write(rarMagic); err != nil {
t.Fatalf("Failed to write RAR magic bytes: %v", err)
}
file.Close()
detectedType, err := DetectArchiveType(testFile)
if err != nil {
t.Fatalf("DetectArchiveType failed: %v", err)
}
if detectedType != models.RAR {
t.Errorf("Expected RAR, got %s", detectedType)
}
}
func TestDetectNonExistentFile(t *testing.T) {
// DetectArchiveType returns type based on extension without checking file existence
// This is by design - it detects type, not validates existence