|
| 1 | +package v5 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/zlib" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// TestDecompress_ValidData compresses data with zlib, then decompresses and verifies. |
| 10 | +func TestDecompress_ValidData(t *testing.T) { |
| 11 | + // Prepare test data |
| 12 | + original := []byte("Hello, this is test data for zlib compression in MAT-file v5 format!") |
| 13 | + |
| 14 | + // Compress with zlib |
| 15 | + var compressedBuf bytes.Buffer |
| 16 | + zlibWriter := zlib.NewWriter(&compressedBuf) |
| 17 | + if _, err := zlibWriter.Write(original); err != nil { |
| 18 | + t.Fatalf("zlib.Write() error: %v", err) |
| 19 | + } |
| 20 | + if err := zlibWriter.Close(); err != nil { |
| 21 | + t.Fatalf("zlib.Close() error: %v", err) |
| 22 | + } |
| 23 | + compressed := compressedBuf.Bytes() |
| 24 | + |
| 25 | + // Decompress using our function |
| 26 | + reader := bytes.NewReader(compressed) |
| 27 | + result, err := decompress(reader, uint32(len(compressed))) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("decompress() unexpected error: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + if !bytes.Equal(result, original) { |
| 33 | + t.Errorf("decompress() = %q, want %q", result, original) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// TestDecompress_InvalidZlib tests that garbage bytes produce an error. |
| 38 | +func TestDecompress_InvalidZlib(t *testing.T) { |
| 39 | + garbage := []byte{0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02, 0x03, 0x04} |
| 40 | + reader := bytes.NewReader(garbage) |
| 41 | + _, err := decompress(reader, uint32(len(garbage))) |
| 42 | + if err == nil { |
| 43 | + t.Error("decompress() expected error for invalid zlib data, got nil") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// TestDecompress_EmptyInput tests that an empty reader produces an error. |
| 48 | +func TestDecompress_EmptyInput(t *testing.T) { |
| 49 | + // compressedSize > 0 but empty reader => io.ReadFull should fail |
| 50 | + reader := bytes.NewReader([]byte{}) |
| 51 | + _, err := decompress(reader, 10) |
| 52 | + if err == nil { |
| 53 | + t.Error("decompress() expected error for empty input, got nil") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestDecompress_CorruptedData tests that a valid zlib header with corrupted body produces an error. |
| 58 | +func TestDecompress_CorruptedData(t *testing.T) { |
| 59 | + // First, create valid compressed data |
| 60 | + original := []byte("some data to compress") |
| 61 | + var compressedBuf bytes.Buffer |
| 62 | + zlibWriter := zlib.NewWriter(&compressedBuf) |
| 63 | + if _, err := zlibWriter.Write(original); err != nil { |
| 64 | + t.Fatalf("zlib.Write() error: %v", err) |
| 65 | + } |
| 66 | + if err := zlibWriter.Close(); err != nil { |
| 67 | + t.Fatalf("zlib.Close() error: %v", err) |
| 68 | + } |
| 69 | + compressed := compressedBuf.Bytes() |
| 70 | + |
| 71 | + // Corrupt the body (keep first 2 bytes which are the zlib header) |
| 72 | + if len(compressed) > 4 { |
| 73 | + for i := 2; i < len(compressed); i++ { |
| 74 | + compressed[i] = 0xFF |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + reader := bytes.NewReader(compressed) |
| 79 | + _, err := decompress(reader, uint32(len(compressed))) |
| 80 | + if err == nil { |
| 81 | + t.Error("decompress() expected error for corrupted zlib data, got nil") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// TestDecompress_TruncatedReader tests that a reader shorter than compressedSize produces an error. |
| 86 | +func TestDecompress_TruncatedReader(t *testing.T) { |
| 87 | + // Provide only 3 bytes but claim size is 100 |
| 88 | + reader := bytes.NewReader([]byte{0x78, 0x9C, 0x00}) |
| 89 | + _, err := decompress(reader, 100) |
| 90 | + if err == nil { |
| 91 | + t.Error("decompress() expected error for truncated reader, got nil") |
| 92 | + } |
| 93 | +} |
0 commit comments