|
| 1 | +package zip |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "bytes" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestUnzipToDir(t *testing.T) { |
| 15 | + // Create a temporary directory for testing |
| 16 | + tempDir, err := os.MkdirTemp("", "zip-test-*") |
| 17 | + require.NoError(t, err) |
| 18 | + defer os.RemoveAll(tempDir) |
| 19 | + |
| 20 | + // Create a test zip file in memory |
| 21 | + buf := new(bytes.Buffer) |
| 22 | + zipWriter := zip.NewWriter(buf) |
| 23 | + |
| 24 | + // Add some test files to the zip |
| 25 | + files := []struct { |
| 26 | + name string |
| 27 | + content string |
| 28 | + }{ |
| 29 | + {"test1.txt", "Hello World"}, |
| 30 | + {"subdir/test2.txt", "Test Content"}, |
| 31 | + {"subdir/nested/test3.txt", "Nested Content"}, |
| 32 | + } |
| 33 | + |
| 34 | + for _, file := range files { |
| 35 | + writer, err := zipWriter.Create(file.name) |
| 36 | + require.NoError(t, err) |
| 37 | + _, err = writer.Write([]byte(file.content)) |
| 38 | + require.NoError(t, err) |
| 39 | + } |
| 40 | + |
| 41 | + require.NoError(t, zipWriter.Close()) |
| 42 | + |
| 43 | + // Test cases |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + filter func(string) bool |
| 47 | + expected map[string]string |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "no filter", |
| 51 | + filter: func(string) bool { return true }, |
| 52 | + expected: map[string]string{ |
| 53 | + "test1.txt": "Hello World", |
| 54 | + "subdir/test2.txt": "Test Content", |
| 55 | + "subdir/nested/test3.txt": "Nested Content", |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "filter txt files", |
| 60 | + filter: func(path string) bool { return filepath.Ext(path) == ".txt" }, |
| 61 | + expected: map[string]string{ |
| 62 | + "test1.txt": "Hello World", |
| 63 | + "subdir/test2.txt": "Test Content", |
| 64 | + "subdir/nested/test3.txt": "Nested Content", |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "filter specific file", |
| 69 | + filter: func(path string) bool { return path == "test1.txt" }, |
| 70 | + expected: map[string]string{ |
| 71 | + "test1.txt": "Hello World", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + // Create a new temp dir for each test case |
| 79 | + testDir, err := os.MkdirTemp(tempDir, "test-*") |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + // Unzip the test data |
| 83 | + err = UnzipToDir(buf.Bytes(), testDir, tt.filter) |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + // Verify the extracted files |
| 87 | + for path, expectedContent := range tt.expected { |
| 88 | + fullPath := filepath.Join(testDir, path) |
| 89 | + content, err := os.ReadFile(fullPath) |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, expectedContent, string(content)) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestSanitizeArchivePath(t *testing.T) { |
| 98 | + tests := []struct { |
| 99 | + name string |
| 100 | + dirPath string |
| 101 | + fileName string |
| 102 | + want string |
| 103 | + wantErr bool |
| 104 | + }{ |
| 105 | + { |
| 106 | + name: "valid path", |
| 107 | + dirPath: "/tmp/test", |
| 108 | + fileName: "file.txt", |
| 109 | + want: "/tmp/test/file.txt", |
| 110 | + wantErr: false, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "valid nested path", |
| 114 | + dirPath: "/tmp/test", |
| 115 | + fileName: "subdir/file.txt", |
| 116 | + want: "/tmp/test/subdir/file.txt", |
| 117 | + wantErr: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "path traversal attempt", |
| 121 | + dirPath: "/tmp/test", |
| 122 | + fileName: "../file.txt", |
| 123 | + wantErr: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "absolute path attempt", |
| 127 | + dirPath: "/tmp/test", |
| 128 | + fileName: "/etc/passwd", |
| 129 | + wantErr: true, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tt := range tests { |
| 134 | + t.Run(tt.name, func(t *testing.T) { |
| 135 | + got, err := sanitizeArchivePath(tt.dirPath, tt.fileName) |
| 136 | + if tt.wantErr { |
| 137 | + assert.Error(t, err) |
| 138 | + return |
| 139 | + } |
| 140 | + assert.NoError(t, err) |
| 141 | + assert.Equal(t, tt.want, got) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments