| 1 |
//go:build !windows |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright The ORAS Authors. |
| 5 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
you may not use this file except in compliance with the License. |
| 7 |
You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software |
| 12 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
See the License for the specific language governing permissions and |
| 15 |
limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package file |
| 19 |
|
| 20 |
import ( |
| 21 |
"bytes" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"testing" |
| 25 |
) |
| 26 |
|
| 27 |
func Test_extractTarDirectory_PreservePermissions(t *testing.T) { |
| 28 |
fileContent := "hello world" |
| 29 |
fileMode := os.FileMode(0771) |
| 30 |
tarData := createTar(t, []tarEntry{ |
| 31 |
{name: "base/", mode: os.ModeDir | 0777}, |
| 32 |
{name: "base/test.txt", content: fileContent, mode: fileMode}, |
| 33 |
}) |
| 34 |
|
| 35 |
tempDir := t.TempDir() |
| 36 |
dirName := "base" |
| 37 |
dirPath := filepath.Join(tempDir, dirName) |
| 38 |
buf := make([]byte, 1024) |
| 39 |
|
| 40 |
if err := extractTarDirectory(dirPath, dirName, bytes.NewReader(tarData), buf, true); err != nil { |
| 41 |
t.Fatalf("extractTarDirectory() error = %v", err) |
| 42 |
} |
| 43 |
|
| 44 |
filePath := filepath.Join(dirPath, "test.txt") |
| 45 |
fi, err := os.Lstat(filePath) |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("failed to stat file %s: %v", filePath, err) |
| 48 |
} |
| 49 |
|
| 50 |
gotContent, err := os.ReadFile(filePath) |
| 51 |
if err != nil { |
| 52 |
t.Fatalf("failed to read file %s: %v", filePath, err) |
| 53 |
} |
| 54 |
if string(gotContent) != fileContent { |
| 55 |
t.Errorf("file content = %s, want %s", gotContent, fileContent) |
| 56 |
} |
| 57 |
|
| 58 |
if fi.Mode() != fileMode { |
| 59 |
t.Errorf("file %q mode = %s, want %s", fi.Name(), fi.Mode(), fileMode) |
| 60 |
} |
| 61 |
} |